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