blob: c8a47e0f8af3af3c6d0f52f194eeab83afc3d837 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Morrita7ab59802014-12-12 15:30:26 -080010#include "base/callback.h"
James Robinson646469d2014-10-03 15:33:28 -070011#include "mojo/edk/system/handle_table.h"
12#include "mojo/edk/system/mapping_table.h"
13#include "mojo/edk/system/memory.h"
Viet-Trung Luud42e48a2015-11-03 14:54:55 -080014#include "mojo/edk/util/mutex.h"
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070015#include "mojo/edk/util/ref_ptr.h"
Viet-Trung Luud42e48a2015-11-03 14:54:55 -080016#include "mojo/edk/util/thread_annotations.h"
James Robinson646469d2014-10-03 15:33:28 -070017#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 Luu7d5ced22015-06-24 15:21:39 -070021#include "mojo/public/cpp/system/macros.h"
James Robinson646469d2014-10-03 15:33:28 -070022
23namespace mojo {
24
25namespace embedder {
26class PlatformSupport;
27}
28
29namespace system {
30
31class Dispatcher;
32struct HandleSignalsState;
33
34// |Core| is an object that implements the Mojo system calls. All public methods
35// are thread-safe.
Viet-Trung Luu535e5d92015-09-18 11:12:49 -070036class Core {
James Robinson646469d2014-10-03 15:33:28 -070037 public:
38 // ---------------------------------------------------------------------------
39
40 // These methods are only to be used by via the embedder API (and internally):
Viet-Trung Luud14187f2015-02-02 16:52:16 -080041
42 // |*platform_support| must outlive this object.
43 explicit Core(embedder::PlatformSupport* platform_support);
James Robinson646469d2014-10-03 15:33:28 -070044 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 Luu1a0ba962015-10-19 17:00:28 -070048 MojoHandle AddDispatcher(Dispatcher* dispatcher);
James Robinson646469d2014-10-03 15:33:28 -070049
50 // Looks up the dispatcher for the given handle. Returns null if the handle is
51 // invalid.
Viet-Trung Luu6a1f1a82015-10-30 15:18:01 -070052 util::RefPtr<Dispatcher> GetDispatcher(MojoHandle handle);
James Robinson646469d2014-10-03 15:33:28 -070053
Nick Brayda897d62015-04-08 16:28:55 -070054 // 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 Luu6a1f1a82015-10-30 15:18:01 -070061 util::RefPtr<Dispatcher>* dispatcher);
Nick Brayda897d62015-04-08 16:28:55 -070062
Hajime Morrita7ab59802014-12-12 15:30:26 -080063 // 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 Luu868714b2015-02-11 15:36:50 -080069 const base::Callback<void(MojoResult)>& callback);
Hajime Morrita7ab59802014-12-12 15:30:26 -080070
James Robinson646469d2014-10-03 15:33:28 -070071 embedder::PlatformSupport* platform_support() const {
Viet-Trung Luud14187f2015-02-02 16:52:16 -080072 return platform_support_;
James Robinson646469d2014-10-03 15:33:28 -070073 }
74
75 // ---------------------------------------------------------------------------
76
Geoffrey Gowan90561de2015-01-30 16:59:23 -080077 // 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 Robinson646469d2014-10-03 15:33:28 -070085 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 Gowan90561de2015-01-30 16:59:23 -080097
98 // These methods correspond to the API functions defined in
99 // "mojo/public/c/system/message_pipe.h":
James Robinson646469d2014-10-03 15:33:28 -0700100 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 Gowan90561de2015-01-30 16:59:23 -0800116
117 // These methods correspond to the API functions defined in
118 // "mojo/public/c/system/data_pipe.h":
James Robinson646469d2014-10-03 15:33:28 -0700119 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 Gowan90561de2015-01-30 16:59:23 -0800143
144 // These methods correspond to the API functions defined in
145 // "mojo/public/c/system/buffer.h":
James Robinson646469d2014-10-03 15:33:28 -0700146 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 Luud14187f2015-02-02 16:52:16 -0800175 embedder::PlatformSupport* const platform_support_;
James Robinson646469d2014-10-03 15:33:28 -0700176
Viet-Trung Luubc3a37c2015-07-07 13:07:09 -0700177 // TODO(vtl): |handle_table_mutex_| should be a reader-writer lock (if only we
James Robinson646469d2014-10-03 15:33:28 -0700178 // had them).
Viet-Trung Luud42e48a2015-11-03 14:54:55 -0800179 util::Mutex handle_table_mutex_;
Viet-Trung Luubc3a37c2015-07-07 13:07:09 -0700180 HandleTable handle_table_ MOJO_GUARDED_BY(handle_table_mutex_);
James Robinson646469d2014-10-03 15:33:28 -0700181
Viet-Trung Luud42e48a2015-11-03 14:54:55 -0800182 util::Mutex mapping_table_mutex_;
Viet-Trung Luubc3a37c2015-07-07 13:07:09 -0700183 MappingTable mapping_table_ MOJO_GUARDED_BY(mapping_table_mutex_);
James Robinson646469d2014-10-03 15:33:28 -0700184
Viet-Trung Luu7d5ced22015-06-24 15:21:39 -0700185 MOJO_DISALLOW_COPY_AND_ASSIGN(Core);
James Robinson646469d2014-10-03 15:33:28 -0700186};
187
188} // namespace system
189} // namespace mojo
190
191#endif // MOJO_EDK_SYSTEM_CORE_H_