| 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_DISPATCHER_H_ | 5 #ifndef MOJO_SYSTEM_DISPATCHER_H_ |
| 6 #define MOJO_SYSTEM_DISPATCHER_H_ | 6 #define MOJO_SYSTEM_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <vector> |
| 9 |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 10 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 11 #include "mojo/public/system/core.h" | 13 #include "mojo/public/system/core.h" |
| 12 #include "mojo/public/system/system_export.h" | 14 #include "mojo/public/system/system_export.h" |
| 13 | 15 |
| 14 namespace mojo { | 16 namespace mojo { |
| 15 namespace system { | 17 namespace system { |
| 16 | 18 |
| 19 class CoreImpl; |
| 17 class Waiter; | 20 class Waiter; |
| 18 | 21 |
| 19 // A |Dispatcher| implements Mojo primitives that are "attached" to a particular | 22 // A |Dispatcher| implements Mojo primitives that are "attached" to a particular |
| 20 // handle. This includes most (all?) primitives except for |MojoWait...()|. This | 23 // handle. This includes most (all?) primitives except for |MojoWait...()|. This |
| 21 // object is thread-safe, with its state being protected by a single lock | 24 // object is thread-safe, with its state being protected by a single lock |
| 22 // |lock_|, which is also made available to implementation subclasses (via the | 25 // |lock_|, which is also made available to implementation subclasses (via the |
| 23 // |lock()| method). | 26 // |lock()| method). |
| 24 class MOJO_SYSTEM_EXPORT Dispatcher : | 27 class MOJO_SYSTEM_EXPORT Dispatcher : |
| 25 public base::RefCountedThreadSafe<Dispatcher> { | 28 public base::RefCountedThreadSafe<Dispatcher> { |
| 26 public: | 29 public: |
| 27 // These methods implement the various primitives named |Mojo...()|. These | 30 // These methods implement the various primitives named |Mojo...()|. These |
| 28 // take |lock_| and handle races with |Close()|. Then they call out to | 31 // take |lock_| and handle races with |Close()|. Then they call out to |
| 29 // subclasses' |...ImplNoLock()| methods (still under |lock_|), which actually | 32 // subclasses' |...ImplNoLock()| methods (still under |lock_|), which actually |
| 30 // implement the primitives. | 33 // implement the primitives. |
| 31 // NOTE(vtl): This puts a big lock around each dispatcher (i.e., handle), and | 34 // NOTE(vtl): This puts a big lock around each dispatcher (i.e., handle), and |
| 32 // prevents the various |...ImplNoLock()|s from releasing the lock as soon as | 35 // prevents the various |...ImplNoLock()|s from releasing the lock as soon as |
| 33 // possible. If this becomes an issue, we can rethink this. | 36 // possible. If this becomes an issue, we can rethink this. |
| 34 MojoResult Close(); | 37 MojoResult Close(); |
| 35 MojoResult WriteMessage(const void* bytes, | 38 // |dispatchers| may be non-null if and only if there are handles to be |
| 36 uint32_t num_bytes, | 39 // written, in which case this will be called with all the dispatchers' locks |
| 37 const MojoHandle* handles, | 40 // held. On success, all the dispatchers must have been moved to a closed |
| 38 uint32_t num_handles, | 41 // state; on failure, they should remain in their original state. |
| 42 MojoResult WriteMessage(const void* bytes, uint32_t num_bytes, |
| 43 const std::vector<Dispatcher*>* dispatchers, |
| 39 MojoWriteMessageFlags flags); | 44 MojoWriteMessageFlags flags); |
| 40 MojoResult ReadMessage(void* bytes, | 45 // |dispatchers| must be non-null if |max_num_dispatchers| is nonzero. On |
| 41 uint32_t* num_bytes, | 46 // success, it will be set to the dispatchers to be received (and assigned |
| 42 MojoHandle* handles, | 47 // handles) as part of the message. |
| 43 uint32_t* num_handles, | 48 MojoResult ReadMessage( |
| 44 MojoReadMessageFlags flags); | 49 void* bytes, uint32_t* num_bytes, |
| 50 uint32_t max_num_dispatchers, |
| 51 std::vector<scoped_refptr<Dispatcher> >* dispatchers, |
| 52 MojoReadMessageFlags flags); |
| 45 | 53 |
| 46 // Adds a waiter to this dispatcher. The waiter will be woken up when this | 54 // Adds a waiter to this dispatcher. The waiter will be woken up when this |
| 47 // object changes state to satisfy |flags| with result |wake_result| (which | 55 // object changes state to satisfy |flags| with result |wake_result| (which |
| 48 // must be >= 0, i.e., a success status). It will also be woken up when it | 56 // must be >= 0, i.e., a success status). It will also be woken up when it |
| 49 // becomes impossible for the object to ever satisfy |flags| with a suitable | 57 // becomes impossible for the object to ever satisfy |flags| with a suitable |
| 50 // error status. | 58 // error status. |
| 51 // | 59 // |
| 52 // Returns: | 60 // Returns: |
| 53 // - |MOJO_RESULT_OK| if the waiter was added; | 61 // - |MOJO_RESULT_OK| if the waiter was added; |
| 54 // - |MOJO_RESULT_ALREADY_EXISTS| if |flags| is already satisfied; | 62 // - |MOJO_RESULT_ALREADY_EXISTS| if |flags| is already satisfied; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 virtual ~Dispatcher(); | 75 virtual ~Dispatcher(); |
| 68 | 76 |
| 69 // These are to be overridden by subclasses (if necessary). They are called | 77 // These are to be overridden by subclasses (if necessary). They are called |
| 70 // exactly once -- first |CancelAllWaitersNoLock()|, then |CloseImplNoLock()|, | 78 // exactly once -- first |CancelAllWaitersNoLock()|, then |CloseImplNoLock()|, |
| 71 // when the dispatcher is being closed. They are called under |lock_|. | 79 // when the dispatcher is being closed. They are called under |lock_|. |
| 72 virtual void CancelAllWaitersNoLock(); | 80 virtual void CancelAllWaitersNoLock(); |
| 73 virtual MojoResult CloseImplNoLock(); | 81 virtual MojoResult CloseImplNoLock(); |
| 74 | 82 |
| 75 // These are to be overridden by subclasses (if necessary). They are never | 83 // These are to be overridden by subclasses (if necessary). They are never |
| 76 // called after the dispatcher has been closed. They are called under |lock_|. | 84 // called after the dispatcher has been closed. They are called under |lock_|. |
| 77 virtual MojoResult WriteMessageImplNoLock(const void* bytes, | 85 // See the descriptions of the methods without the "ImplNoLock" for more |
| 78 uint32_t num_bytes, | 86 // information. |
| 79 const MojoHandle* handles, | 87 virtual MojoResult WriteMessageImplNoLock( |
| 80 uint32_t num_handles, | 88 const void* bytes, uint32_t num_bytes, |
| 81 MojoWriteMessageFlags flags); | 89 const std::vector<Dispatcher*>* dispatchers, |
| 82 virtual MojoResult ReadMessageImplNoLock(void* bytes, | 90 MojoWriteMessageFlags flags); |
| 83 uint32_t* num_bytes, | 91 virtual MojoResult ReadMessageImplNoLock( |
| 84 MojoHandle* handles, | 92 void* bytes, uint32_t* num_bytes, |
| 85 uint32_t* num_handles, | 93 uint32_t max_num_dispatchers, |
| 86 MojoReadMessageFlags flags); | 94 std::vector<scoped_refptr<Dispatcher> >* dispatchers, |
| 95 MojoReadMessageFlags flags); |
| 87 virtual MojoResult AddWaiterImplNoLock(Waiter* waiter, | 96 virtual MojoResult AddWaiterImplNoLock(Waiter* waiter, |
| 88 MojoWaitFlags flags, | 97 MojoWaitFlags flags, |
| 89 MojoResult wake_result); | 98 MojoResult wake_result); |
| 90 virtual void RemoveWaiterImplNoLock(Waiter* waiter); | 99 virtual void RemoveWaiterImplNoLock(Waiter* waiter); |
| 91 | 100 |
| 92 // Available to subclasses. (Note: Returns a non-const reference, just like | 101 // Available to subclasses. (Note: Returns a non-const reference, just like |
| 93 // |base::AutoLock|'s constructor takes a non-const reference. | 102 // |base::AutoLock|'s constructor takes a non-const reference. |
| 94 base::Lock& lock() const { return lock_; } | 103 base::Lock& lock() const { return lock_; } |
| 95 | 104 |
| 96 private: | 105 private: |
| 106 // For |WriteMessage()|, |CoreImpl| needs access to |lock()|. |
| 107 friend class CoreImpl; |
| 108 |
| 97 // This protects the following members as well as any state added by | 109 // This protects the following members as well as any state added by |
| 98 // subclasses. | 110 // subclasses. |
| 99 mutable base::Lock lock_; | 111 mutable base::Lock lock_; |
| 100 bool is_closed_; | 112 bool is_closed_; |
| 101 | 113 |
| 102 DISALLOW_COPY_AND_ASSIGN(Dispatcher); | 114 DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
| 103 }; | 115 }; |
| 104 | 116 |
| 105 } // namespace system | 117 } // namespace system |
| 106 } // namespace mojo | 118 } // namespace mojo |
| 107 | 119 |
| 108 #endif // MOJO_SYSTEM_DISPATCHER_H_ | 120 #endif // MOJO_SYSTEM_DISPATCHER_H_ |
| OLD | NEW |