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