Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ |
| 6 #define MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 16 #include "mojo/edk/system/awakable_list.h" | 17 #include "mojo/edk/system/awakable_list.h" |
| 17 #include "mojo/edk/system/dispatcher.h" | 18 #include "mojo/edk/system/dispatcher.h" |
| 18 #include "mojo/edk/system/system_impl_export.h" | 19 #include "mojo/edk/system/system_impl_export.h" |
| 19 #include "mojo/public/cpp/system/macros.h" | |
| 20 | 20 |
| 21 namespace mojo { | 21 namespace mojo { |
| 22 namespace edk { | 22 namespace edk { |
| 23 | 23 |
| 24 class MOJO_SYSTEM_IMPL_EXPORT WaitSetDispatcher : public Dispatcher { | 24 class MOJO_SYSTEM_IMPL_EXPORT WaitSetDispatcher : public Dispatcher { |
| 25 public: | 25 public: |
| 26 WaitSetDispatcher(); | 26 WaitSetDispatcher(); |
| 27 | 27 |
| 28 // |Dispatcher| public methods: | 28 // Dispatcher: |
| 29 Type GetType() const override; | 29 Type GetType() const override; |
| 30 MojoResult Close() override; | |
| 31 MojoResult AddWaitingDispatcher(const scoped_refptr<Dispatcher>& dispatcher, | |
| 32 MojoHandleSignals signals, | |
| 33 uintptr_t context) override; | |
| 34 MojoResult RemoveWaitingDispatcher( | |
| 35 const scoped_refptr<Dispatcher>& dispatcher) override; | |
| 36 MojoResult GetReadyDispatchers(uint32_t* count, | |
| 37 DispatcherVector* dispatchers, | |
| 38 MojoResult* results, | |
| 39 uintptr_t* contexts) override; | |
| 40 HandleSignalsState GetHandleSignalsState() const override; | |
| 41 MojoResult AddAwakable(Awakable* awakable, | |
| 42 MojoHandleSignals signals, | |
| 43 uintptr_t context, | |
| 44 HandleSignalsState* signals_state) override; | |
| 45 void RemoveAwakable(Awakable* awakable, | |
| 46 HandleSignalsState* signals_state) override; | |
| 47 bool BeginTransit() override; | |
| 30 | 48 |
| 31 private: | 49 private: |
| 32 // Internal implementation of Awakable. | 50 // Internal implementation of Awakable. |
| 33 class Waiter; | 51 class Waiter; |
| 34 | 52 |
| 35 struct WaitState { | 53 struct WaitState { |
| 36 WaitState(); | 54 WaitState(); |
| 37 ~WaitState(); | 55 ~WaitState(); |
| 38 | 56 |
| 39 scoped_refptr<Dispatcher> dispatcher; | 57 scoped_refptr<Dispatcher> dispatcher; |
| 40 MojoHandleSignals signals; | 58 MojoHandleSignals signals; |
| 41 uintptr_t context; | 59 uintptr_t context; |
| 42 }; | 60 }; |
| 43 | 61 |
| 44 ~WaitSetDispatcher() override; | 62 ~WaitSetDispatcher() override; |
| 45 | 63 |
| 46 // |Dispatcher| protected methods: | |
| 47 void CloseImplNoLock() override; | |
| 48 void CancelAllAwakablesNoLock() override; | |
| 49 MojoResult AddAwakableImplNoLock(Awakable* awakable, | |
| 50 MojoHandleSignals signals, | |
| 51 uintptr_t context, | |
| 52 HandleSignalsState* signals_state) override; | |
| 53 void RemoveAwakableImplNoLock(Awakable* awakable, | |
| 54 HandleSignalsState* signals_state) override; | |
| 55 MojoResult AddWaitingDispatcherImplNoLock( | |
| 56 const scoped_refptr<Dispatcher>& dispatcher, | |
| 57 MojoHandleSignals signals, | |
| 58 uintptr_t context) override; | |
| 59 MojoResult RemoveWaitingDispatcherImplNoLock( | |
| 60 const scoped_refptr<Dispatcher>& dispatcher) override; | |
| 61 MojoResult GetReadyDispatchersImplNoLock(uint32_t* count, | |
| 62 DispatcherVector* dispatchers, | |
| 63 MojoResult* results, | |
| 64 uintptr_t* contexts) override; | |
| 65 HandleSignalsState GetHandleSignalsStateImplNoLock() const override; | |
| 66 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | |
| 67 override; | |
| 68 | |
| 69 // Signal that the dispatcher indexed by |context| has been woken up with | 64 // Signal that the dispatcher indexed by |context| has been woken up with |
| 70 // |result| and is now ready. | 65 // |result| and is now ready. |
| 71 void WakeDispatcher(MojoResult result, uintptr_t context); | 66 void WakeDispatcher(MojoResult result, uintptr_t context); |
| 72 | 67 |
| 68 // Guards |is_closed_|, |waiting_dispatchers_|, and |waiter_|. | |
|
Anand Mistry (off Chromium)
2016/01/28 02:26:25
nit: not |waiter_|, which doesn't need locking.
| |
| 69 // | |
| 70 // TODO: Consider removing this. | |
| 71 base::Lock lock_; | |
| 72 bool is_closed_ = false; | |
| 73 | |
| 73 // Map of dispatchers being waited on. Key is a Dispatcher* casted to a | 74 // Map of dispatchers being waited on. Key is a Dispatcher* casted to a |
| 74 // uintptr_t, and should be treated as an opaque value and not casted back. | 75 // uintptr_t, and should be treated as an opaque value and not casted back. |
| 75 std::map<uintptr_t, WaitState> waiting_dispatchers_; | 76 std::map<uintptr_t, WaitState> waiting_dispatchers_; |
| 76 | 77 |
| 77 // Separate lock that can be locked without locking |lock()|. | 78 // Separate lock that can be locked without locking |lock_|. |
| 78 mutable base::Lock awoken_lock_; | 79 mutable base::Lock awoken_lock_; |
| 79 // List of dispatchers that have been woken up. Any dispatcher in this queue | 80 // List of dispatchers that have been woken up. Any dispatcher in this queue |
| 80 // will NOT currently be waited on. | 81 // will NOT currently be waited on. |
| 81 std::deque<std::pair<uintptr_t, MojoResult>> awoken_queue_; | 82 std::deque<std::pair<uintptr_t, MojoResult>> awoken_queue_; |
| 82 // List of dispatchers that have been woken up and retrieved. | 83 // List of dispatchers that have been woken up and retrieved. |
| 83 std::deque<uintptr_t> processed_dispatchers_; | 84 std::deque<uintptr_t> processed_dispatchers_; |
| 84 | 85 |
| 85 // Separate lock that can be locked without locking |lock()|. | 86 // Separate lock that can be locked without locking |lock_|. |
| 86 base::Lock awakable_lock_; | 87 base::Lock awakable_lock_; |
| 87 // List of dispatchers being waited on. | 88 // List of dispatchers being waited on. |
| 88 AwakableList awakable_list_; | 89 AwakableList awakable_list_; |
| 89 | 90 |
| 90 // Waiter used to wait on dispatchers. | 91 // Waiter used to wait on dispatchers. |
| 91 scoped_ptr<Waiter> waiter_; | 92 scoped_ptr<Waiter> waiter_; |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(WaitSetDispatcher); | |
| 92 }; | 95 }; |
| 93 | 96 |
| 94 } // namespace edk | 97 } // namespace edk |
| 95 } // namespace mojo | 98 } // namespace mojo |
| 96 | 99 |
| 97 #endif // MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ | 100 #endif // MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ |
| OLD | NEW |