OLD | NEW |
---|---|
(Empty) | |
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_SYSTEM_CORE_H_ | |
6 #define MOJO_SYSTEM_CORE_H_ | |
7 | |
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" | |
15 | |
16 namespace mojo { | |
17 namespace system { | |
18 | |
19 class Dispatcher; | |
20 | |
21 // |Core| is a singleton object that implements the Mojo system calls. All | |
22 // public methods are thread-safe. | |
23 class MOJO_SYSTEM_IMPL_EXPORT Core { | |
24 public: | |
25 Core(); | |
26 virtual ~Core(); | |
27 | |
28 static Core* GetInstance(); | |
29 // These methods are only to be used by via the embedder API. | |
viettrungluu
2014/04/09 23:06:44
You mangled the comments in this file:
This comme
DaveMoore
2014/04/10 18:58:58
Done.
| |
30 | |
31 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); | |
32 MojoTimeTicks GetTimeTicksNow(); | |
33 MojoResult Close(MojoHandle handle); | |
34 MojoResult Wait(MojoHandle handle, | |
35 MojoWaitFlags flags, | |
36 MojoDeadline deadline); | |
37 MojoResult WaitMany(const MojoHandle* handles, | |
38 const MojoWaitFlags* flags, | |
39 uint32_t num_handles, | |
40 MojoDeadline deadline); | |
41 MojoResult CreateMessagePipe( | |
42 MojoHandle* message_pipe_handle0, | |
43 MojoHandle* message_pipe_handle1); | |
44 MojoResult WriteMessage(MojoHandle message_pipe_handle, | |
45 const void* bytes, | |
46 uint32_t num_bytes, | |
47 const MojoHandle* handles, | |
48 uint32_t num_handles, | |
49 MojoWriteMessageFlags flags); | |
50 MojoResult ReadMessage(MojoHandle message_pipe_handle, | |
51 void* bytes, | |
52 uint32_t* num_bytes, | |
53 MojoHandle* handles, | |
54 uint32_t* num_handles, | |
55 MojoReadMessageFlags flags); | |
56 MojoResult CreateDataPipe( | |
57 const MojoCreateDataPipeOptions* options, | |
58 MojoHandle* data_pipe_producer_handle, | |
59 MojoHandle* data_pipe_consumer_handle); | |
60 MojoResult WriteData(MojoHandle data_pipe_producer_handle, | |
61 const void* elements, | |
62 uint32_t* num_bytes, | |
63 MojoWriteDataFlags flags); | |
64 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, | |
65 void** buffer, | |
66 uint32_t* buffer_num_bytes, | |
67 MojoWriteDataFlags flags); | |
68 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, | |
69 uint32_t num_bytes_written); | |
70 MojoResult ReadData(MojoHandle data_pipe_consumer_handle, | |
71 void* elements, | |
72 uint32_t* num_bytes, | |
73 MojoReadDataFlags flags); | |
74 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, | |
75 const void** buffer, | |
76 uint32_t* buffer_num_bytes, | |
77 MojoReadDataFlags flags); | |
78 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, | |
79 uint32_t num_bytes_read); | |
80 MojoResult CreateSharedBuffer( | |
81 const MojoCreateSharedBufferOptions* options, | |
82 uint64_t num_bytes, | |
83 MojoHandle* shared_buffer_handle); | |
84 MojoResult DuplicateBufferHandle( | |
85 MojoHandle buffer_handle, | |
86 const MojoDuplicateBufferHandleOptions* options, | |
87 MojoHandle* new_buffer_handle); | |
88 MojoResult MapBuffer(MojoHandle buffer_handle, | |
89 uint64_t offset, | |
90 uint64_t num_bytes, | |
91 void** buffer, | |
92 MojoMapBufferFlags flags); | |
93 MojoResult UnmapBuffer(void* buffer); | |
94 | |
95 private: | |
96 friend bool internal::ShutdownCheckNoLeaks(Core*); | |
97 // The |busy| member is used only to deal with functions (in particular | |
98 // |WriteMessage()|) that want to hold on to a dispatcher and later remove it | |
99 // from the handle table, without holding on to the handle table lock. | |
100 // | |
101 // For example, if |WriteMessage()| is called with a handle to be sent, (under | |
102 // the handle table lock) it must first check that that handle is not busy (if | |
103 // it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks it as | |
104 // busy. To avoid deadlock, it should also try to acquire the locks for all | |
105 // the dispatchers for the handles that it is sending (and fail with | |
106 // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the | |
107 // handle table lock. | |
108 // | |
109 // If |Close()| is simultaneously called on that handle, it too checks if the | |
110 // handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|). This | |
111 // prevents |WriteMessage()| from sending a handle that has been closed (or | |
112 // learning about this too late). | |
113 struct HandleTableEntry { | |
114 HandleTableEntry(); | |
115 explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher); | |
116 ~HandleTableEntry(); | |
117 | |
118 scoped_refptr<Dispatcher> dispatcher; | |
119 bool busy; | |
120 }; | |
121 typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap; | |
122 | |
123 // Looks up the dispatcher for the given handle. Returns null if the handle is | |
124 // invalid. | |
125 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); | |
126 | |
127 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic | |
128 // validation of arguments. | |
129 MojoResult WaitManyInternal(const MojoHandle* handles, | |
130 const MojoWaitFlags* flags, | |
131 uint32_t num_handles, | |
132 MojoDeadline deadline); | |
133 | |
134 // --------------------------------------------------------------------------- | |
135 | |
136 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we | |
137 // had them). | |
138 base::Lock handle_table_lock_; // Protects |handle_table_|. | |
139 HandleTable handle_table_; | |
140 | |
141 base::Lock mapping_table_lock_; // Protects |mapping_table_|. | |
142 MappingTable mapping_table_; | |
143 | |
144 // --------------------------------------------------------------------------- | |
145 | |
146 DISALLOW_COPY_AND_ASSIGN(Core); | |
147 }; | |
148 | |
149 } // namespace system | |
150 } // namespace mojo | |
151 | |
152 #endif // MOJO_SYSTEM_CORE_H_ | |
OLD | NEW |