OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SYSTEM_CORE_IMPL_H_ | 5 #ifndef MOJO_SYSTEM_CORE_IMPL_H_ |
6 #define MOJO_SYSTEM_CORE_IMPL_H_ | 6 #define MOJO_SYSTEM_CORE_IMPL_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | |
9 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
12 #include "mojo/public/system/core.h" | 13 #include "mojo/public/system/core_private.h" |
14 #include "mojo/system/system_impl_export.h" | |
13 | 15 |
14 namespace mojo { | 16 namespace mojo { |
15 namespace system { | 17 namespace system { |
16 | 18 |
17 class CoreImpl; | 19 class CoreImpl; |
18 class Dispatcher; | 20 class Dispatcher; |
19 | 21 |
20 namespace test { | 22 namespace test { |
21 class CoreTestBase; | 23 class CoreTestBase; |
22 } | 24 } |
23 | 25 |
24 // |CoreImpl| is a singleton object that implements the Mojo system calls. With | 26 // |CoreImpl| is a singleton object that implements the Mojo system calls. With |
25 // the (obvious) exception of |Init()|, which must be called first (and the call | 27 // the (obvious) exception of |Init()|, which must be called first (and the call |
26 // completed) before making any other calls, all the public methods are | 28 // completed) before making any other calls, all the public methods are |
27 // thread-safe. | 29 // thread-safe. |
28 class MOJO_SYSTEM_EXPORT CoreImpl { | 30 class MOJO_SYSTEM_IMPL_EXPORT CoreImpl : public Core { |
29 public: | 31 public: |
30 static void Init(); | 32 static void Init(); |
31 | 33 |
32 static CoreImpl* Get() { | 34 virtual MojoResult Close(MojoHandle handle) OVERRIDE; |
viettrungluu
2013/12/09 19:25:59
Imposing even more virtual dispatch seems to be ba
abarth-chromium
2013/12/09 21:19:34
Jumping through a function pointer is the same as
| |
33 return singleton_; | |
34 } | |
35 | 35 |
36 MojoResult Close(MojoHandle handle); | 36 virtual MojoResult Wait(MojoHandle handle, |
37 MojoWaitFlags flags, | |
38 MojoDeadline deadline) OVERRIDE; | |
37 | 39 |
38 MojoResult Wait(MojoHandle handle, | 40 virtual MojoResult WaitMany(const MojoHandle* handles, |
39 MojoWaitFlags flags, | 41 const MojoWaitFlags* flags, |
40 MojoDeadline deadline); | 42 uint32_t num_handles, |
43 MojoDeadline deadline) OVERRIDE; | |
41 | 44 |
42 MojoResult WaitMany(const MojoHandle* handles, | 45 virtual MojoResult CreateMessagePipe(MojoHandle* handle_0, |
43 const MojoWaitFlags* flags, | 46 MojoHandle* handle_1) OVERRIDE; |
44 uint32_t num_handles, | |
45 MojoDeadline deadline); | |
46 | 47 |
47 MojoResult CreateMessagePipe(MojoHandle* handle_0, MojoHandle* handle_1); | 48 virtual MojoResult WriteMessage(MojoHandle handle, |
49 const void* bytes, | |
50 uint32_t num_bytes, | |
51 const MojoHandle* handles, | |
52 uint32_t num_handles, | |
53 MojoWriteMessageFlags flags) OVERRIDE; | |
48 | 54 |
49 MojoResult WriteMessage(MojoHandle handle, | 55 virtual MojoResult ReadMessage(MojoHandle handle, |
50 const void* bytes, uint32_t num_bytes, | 56 void* bytes, |
51 const MojoHandle* handles, uint32_t num_handles, | 57 uint32_t* num_bytes, |
52 MojoWriteMessageFlags flags); | 58 MojoHandle* handles, |
59 uint32_t* num_handles, | |
60 MojoReadMessageFlags flags) OVERRIDE; | |
53 | 61 |
54 MojoResult ReadMessage(MojoHandle handle, | 62 virtual MojoTimeTicks GetTimeTicksNow() OVERRIDE; |
55 void* bytes, uint32_t* num_bytes, | |
56 MojoHandle* handles, uint32_t* num_handles, | |
57 MojoReadMessageFlags flags); | |
58 | |
59 MojoTimeTicks GetTimeTicksNow(); | |
60 | 63 |
61 private: | 64 private: |
62 friend class test::CoreTestBase; | 65 friend class test::CoreTestBase; |
63 | 66 |
64 // The |busy| member is used only to deal with functions (in particular | 67 // The |busy| member is used only to deal with functions (in particular |
65 // |WriteMessage()|) that want to hold on to a dispatcher and later remove it | 68 // |WriteMessage()|) that want to hold on to a dispatcher and later remove it |
66 // from the handle table, without holding on to the handle table lock. | 69 // from the handle table, without holding on to the handle table lock. |
67 // | 70 // |
68 // For example, if |WriteMessage()| is called with a handle to be sent, (under | 71 // For example, if |WriteMessage()| is called with a handle to be sent, (under |
69 // the handle table lock) it must first check that that handle is not busy (if | 72 // the handle table lock) it must first check that that handle is not busy (if |
(...skipping 20 matching lines...) Expand all Loading... | |
90 HandleTableEntry(); | 93 HandleTableEntry(); |
91 explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher); | 94 explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher); |
92 ~HandleTableEntry(); | 95 ~HandleTableEntry(); |
93 | 96 |
94 scoped_refptr<Dispatcher> dispatcher; | 97 scoped_refptr<Dispatcher> dispatcher; |
95 bool busy; | 98 bool busy; |
96 }; | 99 }; |
97 typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap; | 100 typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap; |
98 | 101 |
99 CoreImpl(); | 102 CoreImpl(); |
100 ~CoreImpl(); | 103 virtual ~CoreImpl(); |
101 | 104 |
102 // Looks up the dispatcher for the given handle. Returns null if the handle is | 105 // Looks up the dispatcher for the given handle. Returns null if the handle is |
103 // invalid. | 106 // invalid. |
104 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); | 107 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); |
105 | 108 |
106 // Assigns a new handle for the given dispatcher (which must be valid); | 109 // Assigns a new handle for the given dispatcher (which must be valid); |
107 // returns |MOJO_HANDLE_INVALID| on failure (due to hitting resource limits). | 110 // returns |MOJO_HANDLE_INVALID| on failure (due to hitting resource limits). |
108 // Must be called under |handle_table_lock_|. | 111 // Must be called under |handle_table_lock_|. |
109 MojoHandle AddDispatcherNoLock(const scoped_refptr<Dispatcher>& dispatcher); | 112 MojoHandle AddDispatcherNoLock(const scoped_refptr<Dispatcher>& dispatcher); |
110 | 113 |
111 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic | 114 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic |
112 // validation of arguments. | 115 // validation of arguments. |
113 MojoResult WaitManyInternal(const MojoHandle* handles, | 116 MojoResult WaitManyInternal(const MojoHandle* handles, |
114 const MojoWaitFlags* flags, | 117 const MojoWaitFlags* flags, |
115 uint32_t num_handles, | 118 uint32_t num_handles, |
116 MojoDeadline deadline); | 119 MojoDeadline deadline); |
117 | 120 |
118 // --------------------------------------------------------------------------- | 121 // --------------------------------------------------------------------------- |
119 | 122 |
120 static CoreImpl* singleton_; | |
121 | |
122 // --------------------------------------------------------------------------- | |
123 | |
124 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we | 123 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we |
125 // had them). | 124 // had them). |
126 base::Lock handle_table_lock_; // Protects the immediately-following members. | 125 base::Lock handle_table_lock_; // Protects the immediately-following members. |
127 HandleTableMap handle_table_; | 126 HandleTableMap handle_table_; |
128 MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|. | 127 MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|. |
129 | 128 |
130 // --------------------------------------------------------------------------- | 129 // --------------------------------------------------------------------------- |
131 | 130 |
132 DISALLOW_COPY_AND_ASSIGN(CoreImpl); | 131 DISALLOW_COPY_AND_ASSIGN(CoreImpl); |
133 }; | 132 }; |
134 | 133 |
135 } // namespace system | 134 } // namespace system |
136 } // namespace mojo | 135 } // namespace mojo |
137 | 136 |
138 #endif // MOJO_SYSTEM_CORE_IMPL_H_ | 137 #endif // MOJO_SYSTEM_CORE_IMPL_H_ |
OLD | NEW |