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_IMPL_H_
6 #define MOJO_SYSTEM_CORE_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/synchronization/lock.h"
12 #include "mojo/public/system/core.h"
24 // |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
26 // completed) before making any other calls, all the public methods are
28 class MOJO_SYSTEM_EXPORT CoreImpl {
32 static CoreImpl* Get() {
36 MojoResult Close(MojoHandle handle);
38 MojoResult Wait(MojoHandle handle,
40 MojoDeadline deadline);
42 MojoResult WaitMany(const MojoHandle* handles,
43 const MojoWaitFlags* flags,
45 MojoDeadline deadline);
47 MojoResult CreateMessagePipe(MojoHandle* handle_0, MojoHandle* handle_1);
49 MojoResult WriteMessage(MojoHandle handle,
50 const void* bytes, uint32_t num_bytes,
51 const MojoHandle* handles, uint32_t num_handles,
52 MojoWriteMessageFlags flags);
54 MojoResult ReadMessage(MojoHandle handle,
55 void* bytes, uint32_t* num_bytes,
56 MojoHandle* handles, uint32_t* num_handles,
57 MojoReadMessageFlags flags);
60 friend class test::CoreTestBase;
62 typedef base::hash_map<MojoHandle, scoped_refptr<Dispatcher> >
68 // Looks up the dispatcher for the given handle. Returns null if the handle is
70 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle);
72 // Assigns a new handle for the given dispatcher (which must be valid);
73 // returns |MOJO_HANDLE_INVALID| on failure (due to hitting resource limits).
74 // Must be called under |handle_table_lock_|.
75 MojoHandle AddDispatcherNoLock(scoped_refptr<Dispatcher> dispatcher);
77 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic
78 // validation of arguments.
79 MojoResult WaitManyInternal(const MojoHandle* handles,
80 const MojoWaitFlags* flags,
82 MojoDeadline deadline);
84 // ---------------------------------------------------------------------------
86 static CoreImpl* singleton_;
88 // ---------------------------------------------------------------------------
90 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we
92 base::Lock handle_table_lock_; // Protects the immediately-following members.
93 HandleTableMap handle_table_;
94 MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|.
96 // ---------------------------------------------------------------------------
98 DISALLOW_COPY_AND_ASSIGN(CoreImpl);
101 } // namespace system
104 #endif // MOJO_SYSTEM_CORE_IMPL_H_