OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CORE_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CORE_H_ |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CORE_H_ | 6 #define MOJO_EDK_SYSTEM_CORE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "mojo/edk/system/handle_table.h" |
| 15 #include "mojo/edk/system/mapping_table.h" |
| 16 #include "mojo/edk/system/system_impl_export.h" |
13 #include "mojo/public/c/system/buffer.h" | 17 #include "mojo/public/c/system/buffer.h" |
14 #include "mojo/public/c/system/data_pipe.h" | 18 #include "mojo/public/c/system/data_pipe.h" |
15 #include "mojo/public/c/system/message_pipe.h" | 19 #include "mojo/public/c/system/message_pipe.h" |
16 #include "mojo/public/c/system/types.h" | 20 #include "mojo/public/c/system/types.h" |
17 #include "mojo/public/cpp/system/macros.h" | 21 #include "mojo/public/cpp/system/macros.h" |
18 #include "third_party/mojo/src/mojo/edk/system/handle_table.h" | |
19 #include "third_party/mojo/src/mojo/edk/system/mapping_table.h" | |
20 #include "third_party/mojo/src/mojo/edk/system/memory.h" | |
21 #include "third_party/mojo/src/mojo/edk/system/mutex.h" | |
22 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h" | |
23 | 22 |
24 namespace mojo { | 23 namespace mojo { |
25 | 24 |
26 namespace embedder { | 25 namespace edk { |
27 class PlatformSupport; | |
28 } | |
29 | |
30 namespace system { | |
31 | 26 |
32 class Dispatcher; | 27 class Dispatcher; |
| 28 class PlatformSupport; |
33 struct HandleSignalsState; | 29 struct HandleSignalsState; |
34 | 30 |
35 // |Core| is an object that implements the Mojo system calls. All public methods | 31 // |Core| is an object that implements the Mojo system calls. All public methods |
36 // are thread-safe. | 32 // are thread-safe. |
37 class MOJO_SYSTEM_IMPL_EXPORT Core { | 33 class MOJO_SYSTEM_IMPL_EXPORT Core { |
38 public: | 34 public: |
39 // --------------------------------------------------------------------------- | 35 // --------------------------------------------------------------------------- |
40 | 36 |
41 // These methods are only to be used by via the embedder API (and internally): | 37 // These methods are only to be used by via the embedder API (and internally): |
42 | 38 |
43 // |*platform_support| must outlive this object. | 39 // |*platform_support| must outlive this object. |
44 explicit Core(embedder::PlatformSupport* platform_support); | 40 explicit Core(PlatformSupport* platform_support); |
45 virtual ~Core(); | 41 virtual ~Core(); |
46 | 42 |
47 // Adds |dispatcher| to the handle table, returning the handle for it. Returns | 43 // Adds |dispatcher| to the handle table, returning the handle for it. Returns |
48 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full. | 44 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full. |
49 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); | 45 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); |
50 | 46 |
51 // Looks up the dispatcher for the given handle. Returns null if the handle is | 47 // Looks up the dispatcher for the given handle. Returns null if the handle is |
52 // invalid. | 48 // invalid. |
53 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); | 49 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); |
54 | 50 |
55 // Like |GetDispatcher()|, but also removes the handle from the handle table. | 51 // Like |GetDispatcher()|, but also removes the handle from the handle table. |
56 // On success, gets the dispatcher for a given handle (which should not be | 52 // On success, gets the dispatcher for a given handle (which should not be |
57 // |MOJO_HANDLE_INVALID|) and removes it. (On failure, returns an appropriate | 53 // |MOJO_HANDLE_INVALID|) and removes it. (On failure, returns an appropriate |
58 // result (and leaves |dispatcher| alone), namely | 54 // result (and leaves |dispatcher| alone), namely |
59 // |MOJO_RESULT_INVALID_ARGUMENT| if there's no dispatcher for the given | 55 // |MOJO_RESULT_INVALID_ARGUMENT| if there's no dispatcher for the given |
60 // handle or |MOJO_RESULT_BUSY| if the handle is marked as busy.) | 56 // handle or |MOJO_RESULT_BUSY| if the handle is marked as busy.) |
61 MojoResult GetAndRemoveDispatcher(MojoHandle handle, | 57 MojoResult GetAndRemoveDispatcher(MojoHandle handle, |
62 scoped_refptr<Dispatcher>* dispatcher); | 58 scoped_refptr<Dispatcher>* dispatcher); |
63 | 59 |
64 // Watches on the given handle for the given signals, calling |callback| when | 60 // Watches on the given handle for the given signals, calling |callback| when |
65 // a signal is satisfied or when all signals become unsatisfiable. |callback| | 61 // a signal is satisfied or when all signals become unsatisfiable. |callback| |
66 // must satisfy stringent requirements -- see |Awakable::Awake()| in | 62 // must satisfy stringent requirements -- see |Awakable::Awake()| in |
67 // awakable.h. In particular, it must not call any Mojo system functions. | 63 // awakable.h. In particular, it must not call any Mojo system functions. |
68 MojoResult AsyncWait(MojoHandle handle, | 64 MojoResult AsyncWait(MojoHandle handle, |
69 MojoHandleSignals signals, | 65 MojoHandleSignals signals, |
70 const base::Callback<void(MojoResult)>& callback); | 66 const base::Callback<void(MojoResult)>& callback); |
71 | 67 |
72 embedder::PlatformSupport* platform_support() const { | 68 PlatformSupport* platform_support() const { |
73 return platform_support_; | 69 return platform_support_; |
74 } | 70 } |
75 | 71 |
76 // --------------------------------------------------------------------------- | 72 // --------------------------------------------------------------------------- |
77 | 73 |
78 // The following methods are essentially implementations of the Mojo Core | 74 // The following methods are essentially implementations of the Mojo Core |
79 // functions of the Mojo API, with the C interface translated to C++ by | 75 // functions of the Mojo API, with the C interface translated to C++ by |
80 // "mojo/edk/embedder/entrypoints.cc". The best way to understand the contract | 76 // "mojo/edk/embedder/entrypoints.cc". The best way to understand the contract |
81 // of these methods is to look at the header files defining the corresponding | 77 // of these methods is to look at the header files defining the corresponding |
82 // API functions, referenced below. | 78 // API functions, referenced below. |
83 | 79 |
84 // These methods correspond to the API functions defined in | 80 // These methods correspond to the API functions defined in |
85 // "mojo/public/c/system/functions.h": | 81 // "mojo/public/c/system/functions.h": |
86 MojoTimeTicks GetTimeTicksNow(); | 82 MojoTimeTicks GetTimeTicksNow(); |
87 MojoResult Close(MojoHandle handle); | 83 MojoResult Close(MojoHandle handle); |
88 MojoResult Wait(MojoHandle handle, | 84 MojoResult Wait(MojoHandle handle, |
89 MojoHandleSignals signals, | 85 MojoHandleSignals signals, |
90 MojoDeadline deadline, | 86 MojoDeadline deadline, |
91 UserPointer<MojoHandleSignalsState> signals_state); | 87 MojoHandleSignalsState* signals_state); |
92 MojoResult WaitMany(UserPointer<const MojoHandle> handles, | 88 MojoResult WaitMany(const MojoHandle* handles, |
93 UserPointer<const MojoHandleSignals> signals, | 89 const MojoHandleSignals* signals, |
94 uint32_t num_handles, | 90 uint32_t num_handles, |
95 MojoDeadline deadline, | 91 MojoDeadline deadline, |
96 UserPointer<uint32_t> result_index, | 92 uint32_t* result_index, |
97 UserPointer<MojoHandleSignalsState> signals_states); | 93 MojoHandleSignalsState* signals_states); |
98 | 94 |
99 // These methods correspond to the API functions defined in | 95 // These methods correspond to the API functions defined in |
100 // "mojo/public/c/system/message_pipe.h": | 96 // "mojo/public/c/system/message_pipe.h": |
101 MojoResult CreateMessagePipe( | 97 MojoResult CreateMessagePipe( |
102 UserPointer<const MojoCreateMessagePipeOptions> options, | 98 const MojoCreateMessagePipeOptions* options, |
103 UserPointer<MojoHandle> message_pipe_handle0, | 99 MojoHandle* message_pipe_handle0, |
104 UserPointer<MojoHandle> message_pipe_handle1); | 100 MojoHandle* message_pipe_handle1); |
105 MojoResult WriteMessage(MojoHandle message_pipe_handle, | 101 MojoResult WriteMessage(MojoHandle message_pipe_handle, |
106 UserPointer<const void> bytes, | 102 const void* bytes, |
107 uint32_t num_bytes, | 103 uint32_t num_bytes, |
108 UserPointer<const MojoHandle> handles, | 104 const MojoHandle* handles, |
109 uint32_t num_handles, | 105 uint32_t num_handles, |
110 MojoWriteMessageFlags flags); | 106 MojoWriteMessageFlags flags); |
111 MojoResult ReadMessage(MojoHandle message_pipe_handle, | 107 MojoResult ReadMessage(MojoHandle message_pipe_handle, |
112 UserPointer<void> bytes, | 108 void* bytes, |
113 UserPointer<uint32_t> num_bytes, | 109 uint32_t* num_bytes, |
114 UserPointer<MojoHandle> handles, | 110 MojoHandle* handles, |
115 UserPointer<uint32_t> num_handles, | 111 uint32_t* num_handles, |
116 MojoReadMessageFlags flags); | 112 MojoReadMessageFlags flags); |
117 | 113 |
118 // These methods correspond to the API functions defined in | 114 // These methods correspond to the API functions defined in |
119 // "mojo/public/c/system/data_pipe.h": | 115 // "mojo/public/c/system/data_pipe.h": |
120 MojoResult CreateDataPipe( | 116 MojoResult CreateDataPipe( |
121 UserPointer<const MojoCreateDataPipeOptions> options, | 117 const MojoCreateDataPipeOptions* options, |
122 UserPointer<MojoHandle> data_pipe_producer_handle, | 118 MojoHandle* data_pipe_producer_handle, |
123 UserPointer<MojoHandle> data_pipe_consumer_handle); | 119 MojoHandle* data_pipe_consumer_handle); |
124 MojoResult WriteData(MojoHandle data_pipe_producer_handle, | 120 MojoResult WriteData(MojoHandle data_pipe_producer_handle, |
125 UserPointer<const void> elements, | 121 const void* elements, |
126 UserPointer<uint32_t> num_bytes, | 122 uint32_t* num_bytes, |
127 MojoWriteDataFlags flags); | 123 MojoWriteDataFlags flags); |
128 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, | 124 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, |
129 UserPointer<void*> buffer, | 125 void** buffer, |
130 UserPointer<uint32_t> buffer_num_bytes, | 126 uint32_t* buffer_num_bytes, |
131 MojoWriteDataFlags flags); | 127 MojoWriteDataFlags flags); |
132 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, | 128 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, |
133 uint32_t num_bytes_written); | 129 uint32_t num_bytes_written); |
134 MojoResult ReadData(MojoHandle data_pipe_consumer_handle, | 130 MojoResult ReadData(MojoHandle data_pipe_consumer_handle, |
135 UserPointer<void> elements, | 131 void* elements, |
136 UserPointer<uint32_t> num_bytes, | 132 uint32_t* num_bytes, |
137 MojoReadDataFlags flags); | 133 MojoReadDataFlags flags); |
138 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, | 134 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, |
139 UserPointer<const void*> buffer, | 135 const void** buffer, |
140 UserPointer<uint32_t> buffer_num_bytes, | 136 uint32_t* buffer_num_bytes, |
141 MojoReadDataFlags flags); | 137 MojoReadDataFlags flags); |
142 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, | 138 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, |
143 uint32_t num_bytes_read); | 139 uint32_t num_bytes_read); |
144 | 140 |
145 // These methods correspond to the API functions defined in | 141 // These methods correspond to the API functions defined in |
146 // "mojo/public/c/system/buffer.h": | 142 // "mojo/public/c/system/buffer.h": |
147 MojoResult CreateSharedBuffer( | 143 MojoResult CreateSharedBuffer( |
148 UserPointer<const MojoCreateSharedBufferOptions> options, | 144 const MojoCreateSharedBufferOptions* options, |
149 uint64_t num_bytes, | 145 uint64_t num_bytes, |
150 UserPointer<MojoHandle> shared_buffer_handle); | 146 MojoHandle* shared_buffer_handle); |
151 MojoResult DuplicateBufferHandle( | 147 MojoResult DuplicateBufferHandle( |
152 MojoHandle buffer_handle, | 148 MojoHandle buffer_handle, |
153 UserPointer<const MojoDuplicateBufferHandleOptions> options, | 149 const MojoDuplicateBufferHandleOptions* options, |
154 UserPointer<MojoHandle> new_buffer_handle); | 150 MojoHandle* new_buffer_handle); |
155 MojoResult MapBuffer(MojoHandle buffer_handle, | 151 MojoResult MapBuffer(MojoHandle buffer_handle, |
156 uint64_t offset, | 152 uint64_t offset, |
157 uint64_t num_bytes, | 153 uint64_t num_bytes, |
158 UserPointer<void*> buffer, | 154 void** buffer, |
159 MojoMapBufferFlags flags); | 155 MojoMapBufferFlags flags); |
160 MojoResult UnmapBuffer(UserPointer<void> buffer); | 156 MojoResult UnmapBuffer(void* buffer); |
161 | 157 |
162 private: | 158 private: |
163 friend bool internal::ShutdownCheckNoLeaks(Core*); | 159 friend bool internal::ShutdownCheckNoLeaks(Core*); |
164 | 160 |
165 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic | 161 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic |
166 // validation of arguments. |*result_index| is only set if the result (whether | 162 // validation of arguments. |*result_index| is only set if the result (whether |
167 // success or failure) applies to a specific handle, so its value should be | 163 // success or failure) applies to a specific handle, so its value should be |
168 // preinitialized to |static_cast<uint32_t>(-1)|. | 164 // preinitialized to |static_cast<uint32_t>(-1)|. |
169 MojoResult WaitManyInternal(const MojoHandle* handles, | 165 MojoResult WaitManyInternal(const MojoHandle* handles, |
170 const MojoHandleSignals* signals, | 166 const MojoHandleSignals* signals, |
171 uint32_t num_handles, | 167 uint32_t num_handles, |
172 MojoDeadline deadline, | 168 MojoDeadline deadline, |
173 uint32_t* result_index, | 169 uint32_t* result_index, |
174 HandleSignalsState* signals_states); | 170 HandleSignalsState* signals_states); |
175 | 171 |
176 embedder::PlatformSupport* const platform_support_; | 172 PlatformSupport* const platform_support_; |
177 | 173 |
178 // TODO(vtl): |handle_table_mutex_| should be a reader-writer lock (if only we | 174 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we |
179 // had them). | 175 // had them). |
180 Mutex handle_table_mutex_; | 176 base::Lock handle_table_lock_; // Protects |handle_table_|. |
181 HandleTable handle_table_ MOJO_GUARDED_BY(handle_table_mutex_); | 177 HandleTable handle_table_; |
182 | 178 |
183 Mutex mapping_table_mutex_; | 179 base::Lock mapping_table_lock_; // Protects |mapping_table_|. |
184 MappingTable mapping_table_ MOJO_GUARDED_BY(mapping_table_mutex_); | 180 MappingTable mapping_table_; |
185 | 181 |
186 MOJO_DISALLOW_COPY_AND_ASSIGN(Core); | 182 MOJO_DISALLOW_COPY_AND_ASSIGN(Core); |
187 }; | 183 }; |
188 | 184 |
189 } // namespace system | 185 } // namespace edk |
190 } // namespace mojo | 186 } // namespace mojo |
191 | 187 |
192 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_CORE_H_ | 188 #endif // MOJO_EDK_SYSTEM_CORE_H_ |
OLD | NEW |