1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2015 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
19 #include "impl/external_commit_helper.hpp"
21 #include "impl/realm_coordinator.hpp"
27 #include <sys/event.h>
30 #include <system_error>
33 using namespace realm;
34 using namespace realm::_impl;
37 // Write a byte to a pipe to notify anyone waiting for data on the pipe
38 void notify_fd(int fd, int read_fd)
42 ssize_t ret = write(fd, &c, 1);
47 // If the pipe's buffer is full, we need to read some of the old data in
48 // it to make space. We don't just read in the code waiting for
49 // notifications so that we can notify multiple waiters with a single
51 assert(ret == -1 && errno == EAGAIN);
53 read(read_fd, buff, sizeof buff);
56 } // anonymous namespace
58 void ExternalCommitHelper::FdHolder::close()
66 // Inter-thread and inter-process notifications of changes are done using a
67 // named pipe in the filesystem next to the Realm file. Everyone who wants to be
68 // notified of commits waits for data to become available on the pipe, and anyone
69 // who commits a write transaction writes data to the pipe after releasing the
70 // write lock. Note that no one ever actually *reads* from the pipe: the data
71 // actually written is meaningless, and trying to read from a pipe from multiple
72 // processes at once is fraught with race conditions.
74 // When a RLMRealm instance is created, we add a CFRunLoopSource to the current
75 // thread's runloop. On each cycle of the run loop, the run loop checks each of
76 // its sources for work to do, which in the case of CFRunLoopSource is just
77 // checking if CFRunLoopSourceSignal has been called since the last time it ran,
78 // and if so invokes the function pointer supplied when the source is created,
79 // which in our case just invokes `[realm handleExternalChange]`.
81 // Listening for external changes is done using kqueue() on a background thread.
82 // kqueue() lets us efficiently wait until the amount of data which can be read
83 // from one or more file descriptors has changed, and tells us which of the file
84 // descriptors it was that changed. We use this to wait on both the shared named
85 // pipe, and a local anonymous pipe. When data is written to the named pipe, we
86 // signal the runloop source and wake up the target runloop, and when data is
87 // written to the anonymous pipe the background thread removes the runloop
88 // source from the runloop and and shuts down.
89 ExternalCommitHelper::ExternalCommitHelper(RealmCoordinator& parent)
94 throw std::system_error(errno, std::system_category());
98 auto path = parent.get_path() + ".note";
100 // Create and open the named pipe
101 int ret = mkfifo(path.c_str(), 0600);
104 if (err == ENOTSUP) {
105 // Filesystem doesn't support named pipes, so try putting it in tmp instead
106 // Hash collisions are okay here because they just result in doing
107 // extra work, as opposed to correctness problems
108 std::ostringstream ss;
109 ss << getenv("TMPDIR");
110 ss << "realm_" << std::hash<std::string>()(path) << ".note";
112 ret = mkfifo(path.c_str(), 0600);
115 // the fifo already existing isn't an error
116 if (ret == -1 && err != EEXIST) {
117 throw std::system_error(err, std::system_category());
121 m_notify_fd = open(path.c_str(), O_RDWR);
122 if (m_notify_fd == -1) {
123 throw std::system_error(errno, std::system_category());
126 // Make writing to the pipe return -1 when the pipe's buffer is full
127 // rather than blocking until there's space available
128 ret = fcntl(m_notify_fd, F_SETFL, O_NONBLOCK);
130 throw std::system_error(errno, std::system_category());
133 #else // !TARGET_OS_TV
135 // tvOS does not support named pipes, so use an anonymous pipe instead
136 int notification_pipe[2];
137 int ret = pipe(notification_pipe);
139 throw std::system_error(errno, std::system_category());
142 m_notify_fd = notification_pipe[0];
143 m_notify_fd_write = notification_pipe[1];
145 #endif // TARGET_OS_TV
147 // Create the anonymous pipe for shutdown notifications
148 int shutdown_pipe[2];
149 ret = pipe(shutdown_pipe);
151 throw std::system_error(errno, std::system_category());
154 m_shutdown_read_fd = shutdown_pipe[0];
155 m_shutdown_write_fd = shutdown_pipe[1];
157 m_thread = std::async(std::launch::async, [=] {
161 #pragma clang diagnostic push
162 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
163 catch (std::exception const& e) {
164 fprintf(stderr, "uncaught exception in notifier thread: %s: %s\n", typeid(e).name(), e.what());
165 asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "uncaught exception in notifier thread: %s: %s", typeid(e).name(), e.what());
169 fprintf(stderr, "uncaught exception in notifier thread\n");
170 asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "uncaught exception in notifier thread");
173 #pragma clang diagnostic pop
177 ExternalCommitHelper::~ExternalCommitHelper()
179 notify_fd(m_shutdown_write_fd, m_shutdown_read_fd);
180 m_thread.wait(); // Wait for the thread to exit
183 void ExternalCommitHelper::listen()
185 pthread_setname_np("RLMRealm notification listener");
188 // EVFILT_READ indicates that we care about data being available to read
189 // on the given file descriptor.
190 // EV_CLEAR makes it wait for the amount of data available to be read to
191 // change rather than just returning when there is any data to read.
193 EV_SET(&ke[0], m_notify_fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);
194 EV_SET(&ke[1], m_shutdown_read_fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0);
195 int ret = kevent(m_kq, ke, 2, nullptr, 0, nullptr);
200 // Wait for data to become on either fd
201 // Return code is number of bytes available or -1 on error
202 ret = kevent(m_kq, nullptr, 0, &event, 1, nullptr);
205 // Spurious wakeup; just wait again
209 // Check which file descriptor had activity: if it's the shutdown
210 // pipe, then someone called -stop; otherwise it's the named pipe
211 // and someone committed a write transaction
212 if (event.ident == (uint32_t)m_shutdown_read_fd) {
215 assert(event.ident == (uint32_t)m_notify_fd);
217 m_parent.on_change();
221 void ExternalCommitHelper::notify_others()
223 if (m_notify_fd_write != -1) {
224 notify_fd(m_notify_fd_write, m_notify_fd);
227 notify_fd(m_notify_fd, m_notify_fd);