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