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