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.
5 #ifndef MOJO_SYSTEM_CORE_H_
6 #define MOJO_SYSTEM_CORE_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
12 #include "mojo/public/c/system/buffer.h"
13 #include "mojo/public/c/system/data_pipe.h"
14 #include "mojo/public/c/system/message_pipe.h"
15 #include "mojo/public/c/system/types.h"
16 #include "mojo/system/handle_table.h"
17 #include "mojo/system/mapping_table.h"
18 #include "mojo/system/system_impl_export.h"
25 // |Core| is an object that implements the Mojo system calls. All public methods
27 class MOJO_SYSTEM_IMPL_EXPORT Core
{
29 // These methods are only to be used by via the embedder API (and internally).
33 // Adds |dispatcher| to the handle table, returning the handle for it. Returns
34 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full.
35 MojoHandle
AddDispatcher(const scoped_refptr
<Dispatcher
>& dispatcher
);
37 // Looks up the dispatcher for the given handle. Returns null if the handle is
39 scoped_refptr
<Dispatcher
> GetDispatcher(MojoHandle handle
);
41 // System calls implementation.
42 MojoTimeTicks
GetTimeTicksNow();
43 MojoResult
Close(MojoHandle handle
);
44 MojoResult
Wait(MojoHandle handle
,
46 MojoDeadline deadline
);
47 MojoResult
WaitMany(const MojoHandle
* handles
,
48 const MojoWaitFlags
* flags
,
50 MojoDeadline deadline
);
51 MojoResult
CreateMessagePipe(MojoHandle
* message_pipe_handle0
,
52 MojoHandle
* message_pipe_handle1
);
53 MojoResult
WriteMessage(MojoHandle message_pipe_handle
,
56 const MojoHandle
* handles
,
58 MojoWriteMessageFlags flags
);
59 MojoResult
ReadMessage(MojoHandle message_pipe_handle
,
63 uint32_t* num_handles
,
64 MojoReadMessageFlags flags
);
65 MojoResult
CreateDataPipe(const MojoCreateDataPipeOptions
* options
,
66 MojoHandle
* data_pipe_producer_handle
,
67 MojoHandle
* data_pipe_consumer_handle
);
68 MojoResult
WriteData(MojoHandle data_pipe_producer_handle
,
71 MojoWriteDataFlags flags
);
72 MojoResult
BeginWriteData(MojoHandle data_pipe_producer_handle
,
74 uint32_t* buffer_num_bytes
,
75 MojoWriteDataFlags flags
);
76 MojoResult
EndWriteData(MojoHandle data_pipe_producer_handle
,
77 uint32_t num_bytes_written
);
78 MojoResult
ReadData(MojoHandle data_pipe_consumer_handle
,
81 MojoReadDataFlags flags
);
82 MojoResult
BeginReadData(MojoHandle data_pipe_consumer_handle
,
84 uint32_t* buffer_num_bytes
,
85 MojoReadDataFlags flags
);
86 MojoResult
EndReadData(MojoHandle data_pipe_consumer_handle
,
87 uint32_t num_bytes_read
);
88 MojoResult
CreateSharedBuffer(const MojoCreateSharedBufferOptions
* options
,
90 MojoHandle
* shared_buffer_handle
);
91 MojoResult
DuplicateBufferHandle(
92 MojoHandle buffer_handle
,
93 const MojoDuplicateBufferHandleOptions
* options
,
94 MojoHandle
* new_buffer_handle
);
95 MojoResult
MapBuffer(MojoHandle buffer_handle
,
99 MojoMapBufferFlags flags
);
100 MojoResult
UnmapBuffer(void* buffer
);
103 friend bool internal::ShutdownCheckNoLeaks(Core
*);
105 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic
106 // validation of arguments.
107 MojoResult
WaitManyInternal(const MojoHandle
* handles
,
108 const MojoWaitFlags
* flags
,
109 uint32_t num_handles
,
110 MojoDeadline deadline
);
112 // ---------------------------------------------------------------------------
114 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we
116 base::Lock handle_table_lock_
; // Protects |handle_table_|.
117 HandleTable handle_table_
;
119 base::Lock mapping_table_lock_
; // Protects |mapping_table_|.
120 MappingTable mapping_table_
;
122 // ---------------------------------------------------------------------------
124 DISALLOW_COPY_AND_ASSIGN(Core
);
127 } // namespace system
130 #endif // MOJO_SYSTEM_CORE_H_