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/system/handle_table.h"
13 #include "mojo/system/mapping_table.h"
14 #include "mojo/system/system_impl_export.h"
21 // |Core| is an object that implements the Mojo system calls. All public methods
23 class MOJO_SYSTEM_IMPL_EXPORT Core {
25 // These methods are only to be used by via the embedder API.
28 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher);
30 // System calls implementation.
31 MojoTimeTicks GetTimeTicksNow();
32 MojoResult Close(MojoHandle handle);
33 MojoResult Wait(MojoHandle handle,
35 MojoDeadline deadline);
36 MojoResult WaitMany(const MojoHandle* handles,
37 const MojoWaitFlags* flags,
39 MojoDeadline deadline);
40 MojoResult CreateMessagePipe(
41 MojoHandle* message_pipe_handle0,
42 MojoHandle* message_pipe_handle1);
43 MojoResult WriteMessage(MojoHandle message_pipe_handle,
46 const MojoHandle* handles,
48 MojoWriteMessageFlags flags);
49 MojoResult ReadMessage(MojoHandle message_pipe_handle,
53 uint32_t* num_handles,
54 MojoReadMessageFlags flags);
55 MojoResult CreateDataPipe(
56 const MojoCreateDataPipeOptions* options,
57 MojoHandle* data_pipe_producer_handle,
58 MojoHandle* data_pipe_consumer_handle);
59 MojoResult WriteData(MojoHandle data_pipe_producer_handle,
62 MojoWriteDataFlags flags);
63 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle,
65 uint32_t* buffer_num_bytes,
66 MojoWriteDataFlags flags);
67 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle,
68 uint32_t num_bytes_written);
69 MojoResult ReadData(MojoHandle data_pipe_consumer_handle,
72 MojoReadDataFlags flags);
73 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle,
75 uint32_t* buffer_num_bytes,
76 MojoReadDataFlags flags);
77 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle,
78 uint32_t num_bytes_read);
79 MojoResult CreateSharedBuffer(
80 const MojoCreateSharedBufferOptions* options,
82 MojoHandle* shared_buffer_handle);
83 MojoResult DuplicateBufferHandle(
84 MojoHandle buffer_handle,
85 const MojoDuplicateBufferHandleOptions* options,
86 MojoHandle* new_buffer_handle);
87 MojoResult MapBuffer(MojoHandle buffer_handle,
91 MojoMapBufferFlags flags);
92 MojoResult UnmapBuffer(void* buffer);
95 friend bool internal::ShutdownCheckNoLeaks(Core*);
96 // The |busy| member is used only to deal with functions (in particular
97 // |WriteMessage()|) that want to hold on to a dispatcher and later remove it
98 // from the handle table, without holding on to the handle table lock.
100 // For example, if |WriteMessage()| is called with a handle to be sent, (under
101 // the handle table lock) it must first check that that handle is not busy (if
102 // it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks it as
103 // busy. To avoid deadlock, it should also try to acquire the locks for all
104 // the dispatchers for the handles that it is sending (and fail with
105 // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the
106 // handle table lock.
108 // If |Close()| is simultaneously called on that handle, it too checks if the
109 // handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|). This
110 // prevents |WriteMessage()| from sending a handle that has been closed (or
111 // learning about this too late).
112 struct HandleTableEntry {
114 explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher);
117 scoped_refptr<Dispatcher> dispatcher;
120 typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap;
122 // Looks up the dispatcher for the given handle. Returns null if the handle is
124 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle);
126 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic
127 // validation of arguments.
128 MojoResult WaitManyInternal(const MojoHandle* handles,
129 const MojoWaitFlags* flags,
130 uint32_t num_handles,
131 MojoDeadline deadline);
133 // ---------------------------------------------------------------------------
135 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we
137 base::Lock handle_table_lock_; // Protects |handle_table_|.
138 HandleTable handle_table_;
140 base::Lock mapping_table_lock_; // Protects |mapping_table_|.
141 MappingTable mapping_table_;
143 // ---------------------------------------------------------------------------
145 DISALLOW_COPY_AND_ASSIGN(Core);
148 } // namespace system
151 #endif // MOJO_SYSTEM_CORE_H_