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/public/c/system/buffer.h" 13 #include "mojo/public/c/system/data_pipe.h" 14 #include "mojo/public/c/system/message_pipe.h" 15 #include "mojo/public/c/system/types.h" 16 #include "mojo/system/handle_table.h" 17 #include "mojo/system/mapping_table.h" 18 #include "mojo/system/system_impl_export.h" 19 20 namespace mojo { 21 namespace system { 22 23 class Dispatcher; 24 25 // |Core| is an object that implements the Mojo system calls. All public methods 26 // are thread-safe. 27 class MOJO_SYSTEM_IMPL_EXPORT Core { 28 public: 29 // These methods are only to be used by via the embedder API (and internally). 30 Core(); 31 virtual ~Core(); 32 33 // Adds |dispatcher| to the handle table, returning the handle for it. Returns 34 // |MOJO_HANDLE_INVALID| on failure, namely if the handle table is full. 35 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); 36 37 // Looks up the dispatcher for the given handle. Returns null if the handle is 38 // invalid. 39 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); 40 41 // System calls implementation. 42 MojoTimeTicks GetTimeTicksNow(); 43 MojoResult Close(MojoHandle handle); 44 MojoResult Wait(MojoHandle handle, 45 MojoHandleSignals signals, 46 MojoDeadline deadline); 47 MojoResult WaitMany(const MojoHandle* handles, 48 const MojoHandleSignals* signals, 49 uint32_t num_handles, 50 MojoDeadline deadline); 51 MojoResult CreateMessagePipe(const MojoCreateMessagePipeOptions* options, 52 MojoHandle* message_pipe_handle0, 53 MojoHandle* message_pipe_handle1); 54 MojoResult WriteMessage(MojoHandle message_pipe_handle, 55 const void* bytes, 56 uint32_t num_bytes, 57 const MojoHandle* handles, 58 uint32_t num_handles, 59 MojoWriteMessageFlags flags); 60 MojoResult ReadMessage(MojoHandle message_pipe_handle, 61 void* bytes, 62 uint32_t* num_bytes, 63 MojoHandle* handles, 64 uint32_t* num_handles, 65 MojoReadMessageFlags flags); 66 MojoResult CreateDataPipe(const MojoCreateDataPipeOptions* options, 67 MojoHandle* data_pipe_producer_handle, 68 MojoHandle* data_pipe_consumer_handle); 69 MojoResult WriteData(MojoHandle data_pipe_producer_handle, 70 const void* elements, 71 uint32_t* num_bytes, 72 MojoWriteDataFlags flags); 73 MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, 74 void** buffer, 75 uint32_t* buffer_num_bytes, 76 MojoWriteDataFlags flags); 77 MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, 78 uint32_t num_bytes_written); 79 MojoResult ReadData(MojoHandle data_pipe_consumer_handle, 80 void* elements, 81 uint32_t* num_bytes, 82 MojoReadDataFlags flags); 83 MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, 84 const void** buffer, 85 uint32_t* buffer_num_bytes, 86 MojoReadDataFlags flags); 87 MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, 88 uint32_t num_bytes_read); 89 MojoResult CreateSharedBuffer(const MojoCreateSharedBufferOptions* options, 90 uint64_t num_bytes, 91 MojoHandle* shared_buffer_handle); 92 MojoResult DuplicateBufferHandle( 93 MojoHandle buffer_handle, 94 const MojoDuplicateBufferHandleOptions* options, 95 MojoHandle* new_buffer_handle); 96 MojoResult MapBuffer(MojoHandle buffer_handle, 97 uint64_t offset, 98 uint64_t num_bytes, 99 void** buffer, 100 MojoMapBufferFlags flags); 101 MojoResult UnmapBuffer(void* buffer); 102 103 private: 104 friend bool internal::ShutdownCheckNoLeaks(Core*); 105 106 // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic 107 // validation of arguments. 108 MojoResult WaitManyInternal(const MojoHandle* handles, 109 const MojoHandleSignals* signals, 110 uint32_t num_handles, 111 MojoDeadline deadline); 112 113 // --------------------------------------------------------------------------- 114 115 // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we 116 // had them). 117 base::Lock handle_table_lock_; // Protects |handle_table_|. 118 HandleTable handle_table_; 119 120 base::Lock mapping_table_lock_; // Protects |mapping_table_|. 121 MappingTable mapping_table_; 122 123 // --------------------------------------------------------------------------- 124 125 DISALLOW_COPY_AND_ASSIGN(Core); 126 }; 127 128 } // namespace system 129 } // namespace mojo 130 131 #endif // MOJO_SYSTEM_CORE_H_ 132