added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / impl / apple / external_commit_helper.hpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 Realm Inc.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include <future>
20
21 namespace realm {
22 class Realm;
23
24 namespace _impl {
25 class RealmCoordinator;
26
27 class ExternalCommitHelper {
28 public:
29     ExternalCommitHelper(RealmCoordinator& parent);
30     ~ExternalCommitHelper();
31
32     void notify_others();
33
34 private:
35     // A RAII holder for a file descriptor which automatically closes the wrapped
36     // fd when it's deallocated
37     class FdHolder {
38     public:
39         FdHolder() = default;
40         ~FdHolder() { close(); }
41         operator int() const { return m_fd; }
42
43         FdHolder& operator=(int newFd) {
44             close();
45             m_fd = newFd;
46             return *this;
47         }
48
49     private:
50         int m_fd = -1;
51         void close();
52
53         FdHolder& operator=(FdHolder const&) = delete;
54         FdHolder(FdHolder const&) = delete;
55     };
56
57     void listen();
58
59     RealmCoordinator& m_parent;
60
61     // The listener thread
62     std::future<void> m_thread;
63
64     // Pipe which is waited on for changes and written to when there is a new
65     // commit to notify others of. When using a named pipe m_notify_fd is
66     // read-write and m_notify_fd_write is unused; when using an anonymous pipe
67     // (on tvOS) m_notify_fd is read-only and m_notify_fd_write is write-only.
68     FdHolder m_notify_fd;
69     FdHolder m_notify_fd_write;
70
71     // File descriptor for the kqueue
72     FdHolder m_kq;
73
74     // The two ends of an anonymous pipe used to notify the kqueue() thread that
75     // it should be shut down.
76     FdHolder m_shutdown_read_fd;
77     FdHolder m_shutdown_write_fd;
78 };
79 } // namespace _impl
80 } // namespace realm