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 #include "mojo/public/c/system/thunks.h"
6
7 #include <assert.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 extern "C" {
12
13 static MojoSystemThunks g_thunks = {0};
14
MojoGetTimeTicksNow()15 MojoTimeTicks MojoGetTimeTicksNow() {
16 assert(g_thunks.GetTimeTicksNow);
17 return g_thunks.GetTimeTicksNow();
18 }
19
MojoClose(MojoHandle handle)20 MojoResult MojoClose(MojoHandle handle) {
21 assert(g_thunks.Close);
22 return g_thunks.Close(handle);
23 }
24
MojoWait(MojoHandle handle,MojoHandleSignals signals,MojoDeadline deadline,struct MojoHandleSignalsState * signals_state)25 MojoResult MojoWait(MojoHandle handle,
26 MojoHandleSignals signals,
27 MojoDeadline deadline,
28 struct MojoHandleSignalsState* signals_state) {
29 assert(g_thunks.Wait);
30 return g_thunks.Wait(handle, signals, deadline, signals_state);
31 }
32
MojoWaitMany(const MojoHandle * handles,const MojoHandleSignals * signals,uint32_t num_handles,MojoDeadline deadline,uint32_t * result_index,struct MojoHandleSignalsState * signals_states)33 MojoResult MojoWaitMany(const MojoHandle* handles,
34 const MojoHandleSignals* signals,
35 uint32_t num_handles,
36 MojoDeadline deadline,
37 uint32_t* result_index,
38 struct MojoHandleSignalsState* signals_states) {
39 assert(g_thunks.WaitMany);
40 return g_thunks.WaitMany(handles, signals, num_handles, deadline,
41 result_index, signals_states);
42 }
43
MojoCreateMessagePipe(const MojoCreateMessagePipeOptions * options,MojoHandle * message_pipe_handle0,MojoHandle * message_pipe_handle1)44 MojoResult MojoCreateMessagePipe(const MojoCreateMessagePipeOptions* options,
45 MojoHandle* message_pipe_handle0,
46 MojoHandle* message_pipe_handle1) {
47 assert(g_thunks.CreateMessagePipe);
48 return g_thunks.CreateMessagePipe(options, message_pipe_handle0,
49 message_pipe_handle1);
50 }
51
MojoWriteMessage(MojoHandle message_pipe_handle,const void * bytes,uint32_t num_bytes,const MojoHandle * handles,uint32_t num_handles,MojoWriteMessageFlags flags)52 MojoResult MojoWriteMessage(MojoHandle message_pipe_handle,
53 const void* bytes,
54 uint32_t num_bytes,
55 const MojoHandle* handles,
56 uint32_t num_handles,
57 MojoWriteMessageFlags flags) {
58 assert(g_thunks.WriteMessage);
59 return g_thunks.WriteMessage(message_pipe_handle, bytes, num_bytes, handles,
60 num_handles, flags);
61 }
62
MojoReadMessage(MojoHandle message_pipe_handle,void * bytes,uint32_t * num_bytes,MojoHandle * handles,uint32_t * num_handles,MojoReadMessageFlags flags)63 MojoResult MojoReadMessage(MojoHandle message_pipe_handle,
64 void* bytes,
65 uint32_t* num_bytes,
66 MojoHandle* handles,
67 uint32_t* num_handles,
68 MojoReadMessageFlags flags) {
69 assert(g_thunks.ReadMessage);
70 return g_thunks.ReadMessage(message_pipe_handle, bytes, num_bytes, handles,
71 num_handles, flags);
72 }
73
MojoCreateDataPipe(const MojoCreateDataPipeOptions * options,MojoHandle * data_pipe_producer_handle,MojoHandle * data_pipe_consumer_handle)74 MojoResult MojoCreateDataPipe(const MojoCreateDataPipeOptions* options,
75 MojoHandle* data_pipe_producer_handle,
76 MojoHandle* data_pipe_consumer_handle) {
77 assert(g_thunks.CreateDataPipe);
78 return g_thunks.CreateDataPipe(options, data_pipe_producer_handle,
79 data_pipe_consumer_handle);
80 }
81
MojoWriteData(MojoHandle data_pipe_producer_handle,const void * elements,uint32_t * num_elements,MojoWriteDataFlags flags)82 MojoResult MojoWriteData(MojoHandle data_pipe_producer_handle,
83 const void* elements,
84 uint32_t* num_elements,
85 MojoWriteDataFlags flags) {
86 assert(g_thunks.WriteData);
87 return g_thunks.WriteData(data_pipe_producer_handle, elements, num_elements,
88 flags);
89 }
90
MojoBeginWriteData(MojoHandle data_pipe_producer_handle,void ** buffer,uint32_t * buffer_num_elements,MojoWriteDataFlags flags)91 MojoResult MojoBeginWriteData(MojoHandle data_pipe_producer_handle,
92 void** buffer,
93 uint32_t* buffer_num_elements,
94 MojoWriteDataFlags flags) {
95 assert(g_thunks.BeginWriteData);
96 return g_thunks.BeginWriteData(data_pipe_producer_handle, buffer,
97 buffer_num_elements, flags);
98 }
99
MojoEndWriteData(MojoHandle data_pipe_producer_handle,uint32_t num_elements_written)100 MojoResult MojoEndWriteData(MojoHandle data_pipe_producer_handle,
101 uint32_t num_elements_written) {
102 assert(g_thunks.EndWriteData);
103 return g_thunks.EndWriteData(data_pipe_producer_handle, num_elements_written);
104 }
105
MojoReadData(MojoHandle data_pipe_consumer_handle,void * elements,uint32_t * num_elements,MojoReadDataFlags flags)106 MojoResult MojoReadData(MojoHandle data_pipe_consumer_handle,
107 void* elements,
108 uint32_t* num_elements,
109 MojoReadDataFlags flags) {
110 assert(g_thunks.ReadData);
111 return g_thunks.ReadData(data_pipe_consumer_handle, elements, num_elements,
112 flags);
113 }
114
MojoBeginReadData(MojoHandle data_pipe_consumer_handle,const void ** buffer,uint32_t * buffer_num_elements,MojoReadDataFlags flags)115 MojoResult MojoBeginReadData(MojoHandle data_pipe_consumer_handle,
116 const void** buffer,
117 uint32_t* buffer_num_elements,
118 MojoReadDataFlags flags) {
119 assert(g_thunks.BeginReadData);
120 return g_thunks.BeginReadData(data_pipe_consumer_handle, buffer,
121 buffer_num_elements, flags);
122 }
123
MojoEndReadData(MojoHandle data_pipe_consumer_handle,uint32_t num_elements_read)124 MojoResult MojoEndReadData(MojoHandle data_pipe_consumer_handle,
125 uint32_t num_elements_read) {
126 assert(g_thunks.EndReadData);
127 return g_thunks.EndReadData(data_pipe_consumer_handle, num_elements_read);
128 }
129
MojoCreateSharedBuffer(const struct MojoCreateSharedBufferOptions * options,uint64_t num_bytes,MojoHandle * shared_buffer_handle)130 MojoResult MojoCreateSharedBuffer(
131 const struct MojoCreateSharedBufferOptions* options,
132 uint64_t num_bytes,
133 MojoHandle* shared_buffer_handle) {
134 assert(g_thunks.CreateSharedBuffer);
135 return g_thunks.CreateSharedBuffer(options, num_bytes, shared_buffer_handle);
136 }
137
MojoDuplicateBufferHandle(MojoHandle buffer_handle,const struct MojoDuplicateBufferHandleOptions * options,MojoHandle * new_buffer_handle)138 MojoResult MojoDuplicateBufferHandle(
139 MojoHandle buffer_handle,
140 const struct MojoDuplicateBufferHandleOptions* options,
141 MojoHandle* new_buffer_handle) {
142 assert(g_thunks.DuplicateBufferHandle);
143 return g_thunks.DuplicateBufferHandle(buffer_handle, options,
144 new_buffer_handle);
145 }
146
MojoMapBuffer(MojoHandle buffer_handle,uint64_t offset,uint64_t num_bytes,void ** buffer,MojoMapBufferFlags flags)147 MojoResult MojoMapBuffer(MojoHandle buffer_handle,
148 uint64_t offset,
149 uint64_t num_bytes,
150 void** buffer,
151 MojoMapBufferFlags flags) {
152 assert(g_thunks.MapBuffer);
153 return g_thunks.MapBuffer(buffer_handle, offset, num_bytes, buffer, flags);
154 }
155
MojoUnmapBuffer(void * buffer)156 MojoResult MojoUnmapBuffer(void* buffer) {
157 assert(g_thunks.UnmapBuffer);
158 return g_thunks.UnmapBuffer(buffer);
159 }
160
MojoCreateWaitSet(MojoHandle * wait_set)161 MojoResult MojoCreateWaitSet(MojoHandle* wait_set) {
162 assert(g_thunks.CreateWaitSet);
163 return g_thunks.CreateWaitSet(wait_set);
164 }
165
MojoAddHandle(MojoHandle wait_set,MojoHandle handle,MojoHandleSignals signals)166 MojoResult MojoAddHandle(MojoHandle wait_set,
167 MojoHandle handle,
168 MojoHandleSignals signals) {
169 assert(g_thunks.AddHandle);
170 return g_thunks.AddHandle(wait_set, handle, signals);
171 }
172
MojoRemoveHandle(MojoHandle wait_set,MojoHandle handle)173 MojoResult MojoRemoveHandle(MojoHandle wait_set, MojoHandle handle) {
174 assert(g_thunks.RemoveHandle);
175 return g_thunks.RemoveHandle(wait_set, handle);
176 }
177
MojoGetReadyHandles(MojoHandle wait_set,uint32_t * count,MojoHandle * handles,MojoResult * results,struct MojoHandleSignalsState * signals_states)178 MojoResult MojoGetReadyHandles(MojoHandle wait_set,
179 uint32_t* count,
180 MojoHandle* handles,
181 MojoResult* results,
182 struct MojoHandleSignalsState* signals_states) {
183 assert(g_thunks.GetReadyHandles);
184 return g_thunks.GetReadyHandles(wait_set, count, handles, results,
185 signals_states);
186 }
187
MojoWatch(MojoHandle handle,MojoHandleSignals signals,MojoWatchCallback callback,uintptr_t context)188 MojoResult MojoWatch(MojoHandle handle,
189 MojoHandleSignals signals,
190 MojoWatchCallback callback,
191 uintptr_t context) {
192 assert(g_thunks.Watch);
193 return g_thunks.Watch(handle, signals, callback, context);
194 }
195
MojoCancelWatch(MojoHandle handle,uintptr_t context)196 MojoResult MojoCancelWatch(MojoHandle handle, uintptr_t context) {
197 assert(g_thunks.CancelWatch);
198 return g_thunks.CancelWatch(handle, context);
199 }
200
MojoFuseMessagePipes(MojoHandle handle0,MojoHandle handle1)201 MojoResult MojoFuseMessagePipes(MojoHandle handle0, MojoHandle handle1) {
202 assert(g_thunks.FuseMessagePipes);
203 return g_thunks.FuseMessagePipes(handle0, handle1);
204 }
205
MojoWriteMessageNew(MojoHandle message_pipe_handle,MojoMessageHandle message,MojoWriteMessageFlags flags)206 MojoResult MojoWriteMessageNew(MojoHandle message_pipe_handle,
207 MojoMessageHandle message,
208 MojoWriteMessageFlags flags) {
209 assert(g_thunks.WriteMessageNew);
210 return g_thunks.WriteMessageNew(message_pipe_handle, message, flags);
211 }
212
MojoReadMessageNew(MojoHandle message_pipe_handle,MojoMessageHandle * message,uint32_t * num_bytes,MojoHandle * handles,uint32_t * num_handles,MojoReadMessageFlags flags)213 MojoResult MojoReadMessageNew(MojoHandle message_pipe_handle,
214 MojoMessageHandle* message,
215 uint32_t* num_bytes,
216 MojoHandle* handles,
217 uint32_t* num_handles,
218 MojoReadMessageFlags flags) {
219 assert(g_thunks.ReadMessageNew);
220 return g_thunks.ReadMessageNew(message_pipe_handle, message, num_bytes,
221 handles, num_handles, flags);
222 }
223
MojoAllocMessage(uint32_t num_bytes,const MojoHandle * handles,uint32_t num_handles,MojoAllocMessageFlags flags,MojoMessageHandle * message)224 MojoResult MojoAllocMessage(uint32_t num_bytes,
225 const MojoHandle* handles,
226 uint32_t num_handles,
227 MojoAllocMessageFlags flags,
228 MojoMessageHandle* message) {
229 assert(g_thunks.AllocMessage);
230 return g_thunks.AllocMessage(
231 num_bytes, handles, num_handles, flags, message);
232 }
233
MojoFreeMessage(MojoMessageHandle message)234 MojoResult MojoFreeMessage(MojoMessageHandle message) {
235 assert(g_thunks.FreeMessage);
236 return g_thunks.FreeMessage(message);
237 }
238
MojoGetMessageBuffer(MojoMessageHandle message,void ** buffer)239 MojoResult MojoGetMessageBuffer(MojoMessageHandle message, void** buffer) {
240 assert(g_thunks.GetMessageBuffer);
241 return g_thunks.GetMessageBuffer(message, buffer);
242 }
243
MojoWrapPlatformHandle(const struct MojoPlatformHandle * platform_handle,MojoHandle * mojo_handle)244 MojoResult MojoWrapPlatformHandle(
245 const struct MojoPlatformHandle* platform_handle,
246 MojoHandle* mojo_handle) {
247 assert(g_thunks.WrapPlatformHandle);
248 return g_thunks.WrapPlatformHandle(platform_handle, mojo_handle);
249 }
250
MojoUnwrapPlatformHandle(MojoHandle mojo_handle,struct MojoPlatformHandle * platform_handle)251 MojoResult MojoUnwrapPlatformHandle(
252 MojoHandle mojo_handle,
253 struct MojoPlatformHandle* platform_handle) {
254 assert(g_thunks.UnwrapPlatformHandle);
255 return g_thunks.UnwrapPlatformHandle(mojo_handle, platform_handle);
256 }
257
MojoWrapPlatformSharedBufferHandle(const struct MojoPlatformHandle * platform_handle,size_t num_bytes,MojoPlatformSharedBufferHandleFlags flags,MojoHandle * mojo_handle)258 MojoResult MojoWrapPlatformSharedBufferHandle(
259 const struct MojoPlatformHandle* platform_handle,
260 size_t num_bytes,
261 MojoPlatformSharedBufferHandleFlags flags,
262 MojoHandle* mojo_handle) {
263 assert(g_thunks.WrapPlatformSharedBufferHandle);
264 return g_thunks.WrapPlatformSharedBufferHandle(platform_handle, num_bytes,
265 flags, mojo_handle);
266 }
267
MojoUnwrapPlatformSharedBufferHandle(MojoHandle mojo_handle,struct MojoPlatformHandle * platform_handle,size_t * num_bytes,MojoPlatformSharedBufferHandleFlags * flags)268 MojoResult MojoUnwrapPlatformSharedBufferHandle(
269 MojoHandle mojo_handle,
270 struct MojoPlatformHandle* platform_handle,
271 size_t* num_bytes,
272 MojoPlatformSharedBufferHandleFlags* flags) {
273 assert(g_thunks.UnwrapPlatformSharedBufferHandle);
274 return g_thunks.UnwrapPlatformSharedBufferHandle(mojo_handle, platform_handle,
275 num_bytes, flags);
276 }
277
MojoNotifyBadMessage(MojoMessageHandle message,const char * error,size_t error_num_bytes)278 MojoResult MojoNotifyBadMessage(MojoMessageHandle message,
279 const char* error,
280 size_t error_num_bytes) {
281 assert(g_thunks.NotifyBadMessage);
282 return g_thunks.NotifyBadMessage(message, error, error_num_bytes);
283 }
284
MojoGetProperty(MojoPropertyType type,void * value)285 MojoResult MojoGetProperty(MojoPropertyType type, void* value) {
286 assert(g_thunks.GetProperty);
287 return g_thunks.GetProperty(type, value);
288 }
289
290 } // extern "C"
291
MojoEmbedderSetSystemThunks(const MojoSystemThunks * system_thunks)292 size_t MojoEmbedderSetSystemThunks(const MojoSystemThunks* system_thunks) {
293 if (system_thunks->size >= sizeof(g_thunks))
294 g_thunks = *system_thunks;
295 return sizeof(g_thunks);
296 }
297