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