{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/pulls/comments/26090145","id":26090145,"diff_hunk":"@@ -0,0 +1,271 @@\n+////////////////////////////////////////////////////////////////////////////\n+//\n+// Copyright 2014 Realm Inc.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+// http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+//\n+////////////////////////////////////////////////////////////////////////////\n+\n+#import \"RLMRealmUtil.h\"\n+\n+#import \"RLMRealm_Private.hpp\"\n+\n+#import <sys/event.h>\n+#import <sys/stat.h>\n+#import <sys/time.h>\n+#import <unistd.h>\n+\n+// Global realm state\n+static NSMutableDictionary *s_realmsPerPath = [NSMutableDictionary new];\n+\n+void RLMCacheRealm(RLMRealm *realm) {\n+    @synchronized(s_realmsPerPath) {\n+        if (!s_realmsPerPath[realm.path]) {\n+            s_realmsPerPath[realm.path] = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsObjectPersonality\n+                                                                valueOptions:NSPointerFunctionsWeakMemory];\n+        }\n+        [s_realmsPerPath[realm.path] setObject:realm forKey:@(realm->_threadID)];\n+    }\n+}\n+\n+RLMRealm *RLMGetAnyCachedRealmForPath(NSString *path) {\n+    @synchronized(s_realmsPerPath) {\n+        return [s_realmsPerPath[path] objectEnumerator].nextObject;\n+    }\n+}\n+\n+RLMRealm *RLMGetThreadLocalCachedRealmForPath(NSString *path) {\n+    mach_port_t threadID = pthread_mach_thread_np(pthread_self());\n+    @synchronized(s_realmsPerPath) {\n+        return [s_realmsPerPath[path] objectForKey:@(threadID)];\n+    }\n+}\n+\n+void RLMClearRealmCache() {\n+    @synchronized(s_realmsPerPath) {\n+        [s_realmsPerPath removeAllObjects];\n+    }\n+}\n+\n+// Convert an error code to either an NSError or an exception\n+static id handleError(int err, NSError **error) {\n+    if (!error) {\n+        @throw RLMException(@(strerror(err)));\n+    }\n+    *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:err userInfo:nil];\n+    return nil;\n+}\n+\n+// Write a byte to a pipe to notify anyone waiting for data on the pipe\n+static void notifyFd(int fd) {\n+    while (true) {\n+        char c = 0;\n+        ssize_t ret = write(fd, &c, 1);\n+        if (ret == 1) {\n+            break;\n+        }\n+\n+        // If the pipe's buffer is full, we need to read some of the old data in\n+        // it to make space. We don't just read in the code waiting for\n+        // notifications so that we can notify multiple waiters with a single\n+        // write.\n+        assert(ret == -1 && errno == EAGAIN);\n+        char buff[1024];\n+        read(fd, buff, sizeof buff);\n+    }\n+}\n+\n+namespace {\n+// A RAII holder for a file descriptor which automatically closes the wrapped fd\n+// when it's deallocated\n+class FdHolder {\n+    int fd = -1;\n+    void close() {\n+        if (fd != -1) {\n+            ::close(fd);\n+        }\n+        fd = -1;\n+    }\n+\n+public:\n+    ~FdHolder() { close(); }\n+    operator int() const { return fd; }\n+    FdHolder& operator=(int newFd) {\n+        close();\n+        fd = newFd;\n+        return *this;\n+    }\n+};\n+}\n+\n+// Inter-thread and inter-process notifications of changes are done using a\n+// named pipe in the filesystem next to the Realm file. Everyone who wants to be\n+// notified of commits waits for data to become available on the pipe, and anyone\n+// who commits a write transaction writes data to the pipe after releasing the\n+// write lock. Note that no one ever actually *reads* from the pipe: the data\n+// actually written is meaningless, and trying to read from a pipe from multiple\n+// processes at once is fraught with race conditions.\n+\n+// When a RLMRealm instance is created, we add a CFRunLoopSource to the current\n+// thread's runloop. On each cycle of the run loop, the run loop checks each of\n+// its sources for work to do, which in the case of CFRunLoopSource is just\n+// checking if CFRunLoopSourceSignal has been called since the last time it ran,\n+// and if so invokes the function pointer supplied when the source is created,\n+// which in our case just invokes `[realm handleExternalChange]`.\n+\n+// Listening for external changes is done using kqueue() on a background thread.\n+// kqueue() lets us efficiently wait until the amount of data which can be read\n+// from one or more file descriptors has changed, and tells us which of the file\n+// descriptors it was that changed. We use this to wait on both the shared named\n+// pipe, and a local anonymous pipe. When data is written to the named pipe, we\n+// signal the runloop source and wake up the target runloop, and when data is\n+// written to the anonymous pipe the background thread removes the runloop\n+// source from the runloop and and shuts down.\n+\n+@implementation RLMNotifier {\n+    // Realm to notify of changes\n+    __weak RLMRealm *_realm;\n+    // Runloop which notifications are delivered on\n+    CFRunLoopRef _runLoop;\n+\n+    // Read-write file descriptor for the named pipe which is waited on for\n+    // changes and written to when a commit is made\n+    FdHolder _notifyFd;\n+    // File descriptor for the kqueue\n+    FdHolder _kq;\n+    // The two ends of an anonymous pipe used to notify the kqueue() thread that\n+    // it should be shut down.\n+    FdHolder _shutdownReadFd;\n+    FdHolder _shutdownWriteFd;\n+}\n+\n+- (instancetype)initWithRealm:(RLMRealm *)realm error:(NSError **)error {\n+    self = [super init];\n+    if (self) {\n+        _realm = realm;\n+        _runLoop = CFRunLoopGetCurrent();","path":"Realm/RLMRealmUtil.mm","position":156,"original_position":156,"commit_id":"c0d241078c894cff4f2de719270d1ee1669d2796","original_commit_id":"c0d241078c894cff4f2de719270d1ee1669d2796","user":{"login":"alazier","id":250144,"avatar_url":"https://avatars.githubusercontent.com/u/250144?v=3","gravatar_id":"","url":"https://api.github.com/users/alazier","html_url":"https://github.com/alazier","followers_url":"https://api.github.com/users/alazier/followers","following_url":"https://api.github.com/users/alazier/following{/other_user}","gists_url":"https://api.github.com/users/alazier/gists{/gist_id}","starred_url":"https://api.github.com/users/alazier/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alazier/subscriptions","organizations_url":"https://api.github.com/users/alazier/orgs","repos_url":"https://api.github.com/users/alazier/repos","events_url":"https://api.github.com/users/alazier/events{/privacy}","received_events_url":"https://api.github.com/users/alazier/received_events","type":"User","site_admin":false},"body":"we should retain this.","created_at":"2015-03-10T00:26:10Z","updated_at":"2015-03-10T00:26:10Z","html_url":"https://github.com/realm/realm-cocoa/pull/1381#discussion_r26090145","pull_request_url":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/comments/26090145"},"html":{"href":"https://github.com/realm/realm-cocoa/pull/1381#discussion_r26090145"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381","id":27798258,"html_url":"https://github.com/realm/realm-cocoa/pull/1381","diff_url":"https://github.com/realm/realm-cocoa/pull/1381.diff","patch_url":"https://github.com/realm/realm-cocoa/pull/1381.patch","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1381","number":1381,"state":"open","locked":false,"title":"Add support for interprocess change notifications","user":{"login":"tgoyne","id":408944,"avatar_url":"https://avatars.githubusercontent.com/u/408944?v=3","gravatar_id":"","url":"https://api.github.com/users/tgoyne","html_url":"https://github.com/tgoyne","followers_url":"https://api.github.com/users/tgoyne/followers","following_url":"https://api.github.com/users/tgoyne/following{/other_user}","gists_url":"https://api.github.com/users/tgoyne/gists{/gist_id}","starred_url":"https://api.github.com/users/tgoyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tgoyne/subscriptions","organizations_url":"https://api.github.com/users/tgoyne/orgs","repos_url":"https://api.github.com/users/tgoyne/repos","events_url":"https://api.github.com/users/tgoyne/events{/privacy}","received_events_url":"https://api.github.com/users/tgoyne/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-21T21:52:11Z","updated_at":"2015-03-10T00:26:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aa542be3ac222948000e7f69dfd60313fe74a2b4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381/commits","review_comments_url":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381/comments","review_comment_url":"https://api.github.com/repos/realm/realm-cocoa/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/1381/comments","statuses_url":"https://api.github.com/repos/realm/realm-cocoa/statuses/c0d241078c894cff4f2de719270d1ee1669d2796","head":{"label":"realm:tg-interprocess","ref":"tg-interprocess","sha":"c0d241078c894cff4f2de719270d1ee1669d2796","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4044891,"name":"realm-cocoa","full_name":"realm/realm-cocoa","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-cocoa","description":"Realm is a mobile database: a replacement for Core Data & SQLite","fork":false,"url":"https://api.github.com/repos/realm/realm-cocoa","forks_url":"https://api.github.com/repos/realm/realm-cocoa/forks","keys_url":"https://api.github.com/repos/realm/realm-cocoa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-cocoa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-cocoa/teams","hooks_url":"https://api.github.com/repos/realm/realm-cocoa/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-cocoa/events","assignees_url":"https://api.github.com/repos/realm/realm-cocoa/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-cocoa/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-cocoa/tags","blobs_url":"https://api.github.com/repos/realm/realm-cocoa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-cocoa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-cocoa/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-cocoa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-cocoa/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-cocoa/languages","stargazers_url":"https://api.github.com/repos/realm/realm-cocoa/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-cocoa/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-cocoa/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-cocoa/subscription","commits_url":"https://api.github.com/repos/realm/realm-cocoa/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-cocoa/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-cocoa/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-cocoa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-cocoa/merges","archive_url":"https://api.github.com/repos/realm/realm-cocoa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-cocoa/downloads","issues_url":"https://api.github.com/repos/realm/realm-cocoa/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-cocoa/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-cocoa/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-cocoa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-cocoa/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-cocoa/releases{/id}","created_at":"2012-04-16T20:06:56Z","updated_at":"2015-03-09T21:32:50Z","pushed_at":"2015-03-10T00:17:43Z","git_url":"git://github.com/realm/realm-cocoa.git","ssh_url":"git@github.com:realm/realm-cocoa.git","clone_url":"https://github.com/realm/realm-cocoa.git","svn_url":"https://github.com/realm/realm-cocoa","homepage":"http://realm.io","size":110700,"stargazers_count":3148,"watchers_count":3148,"language":"Objective-C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":233,"mirror_url":null,"open_issues_count":113,"forks":233,"open_issues":113,"watchers":3148,"default_branch":"master"}},"base":{"label":"realm:master","ref":"master","sha":"df4b19b376af5ee6066f11ee1168dc961829a055","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4044891,"name":"realm-cocoa","full_name":"realm/realm-cocoa","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-cocoa","description":"Realm is a mobile database: a replacement for Core Data & SQLite","fork":false,"url":"https://api.github.com/repos/realm/realm-cocoa","forks_url":"https://api.github.com/repos/realm/realm-cocoa/forks","keys_url":"https://api.github.com/repos/realm/realm-cocoa/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-cocoa/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-cocoa/teams","hooks_url":"https://api.github.com/repos/realm/realm-cocoa/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-cocoa/events","assignees_url":"https://api.github.com/repos/realm/realm-cocoa/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-cocoa/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-cocoa/tags","blobs_url":"https://api.github.com/repos/realm/realm-cocoa/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-cocoa/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-cocoa/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-cocoa/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-cocoa/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-cocoa/languages","stargazers_url":"https://api.github.com/repos/realm/realm-cocoa/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-cocoa/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-cocoa/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-cocoa/subscription","commits_url":"https://api.github.com/repos/realm/realm-cocoa/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-cocoa/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-cocoa/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-cocoa/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-cocoa/merges","archive_url":"https://api.github.com/repos/realm/realm-cocoa/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-cocoa/downloads","issues_url":"https://api.github.com/repos/realm/realm-cocoa/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-cocoa/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-cocoa/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-cocoa/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-cocoa/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-cocoa/releases{/id}","created_at":"2012-04-16T20:06:56Z","updated_at":"2015-03-09T21:32:50Z","pushed_at":"2015-03-10T00:17:43Z","git_url":"git://github.com/realm/realm-cocoa.git","ssh_url":"git@github.com:realm/realm-cocoa.git","clone_url":"https://github.com/realm/realm-cocoa.git","svn_url":"https://github.com/realm/realm-cocoa","homepage":"http://realm.io","size":110700,"stargazers_count":3148,"watchers_count":3148,"language":"Objective-C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":233,"mirror_url":null,"open_issues_count":113,"forks":233,"open_issues":113,"watchers":3148,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381"},"html":{"href":"https://github.com/realm/realm-cocoa/pull/1381"},"issue":{"href":"https://api.github.com/repos/realm/realm-cocoa/issues/1381"},"comments":{"href":"https://api.github.com/repos/realm/realm-cocoa/issues/1381/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-cocoa/pulls/1381/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-cocoa/statuses/c0d241078c894cff4f2de719270d1ee1669d2796"}}}}