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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ 6 #define MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ 7 8 #include <stdint.h> 9 10 #include <deque> 11 #include <memory> 12 #include <unordered_map> 13 #include <utility> 14 15 #include "base/macros.h" 16 #include "base/synchronization/lock.h" 17 #include "mojo/edk/system/awakable_list.h" 18 #include "mojo/edk/system/dispatcher.h" 19 #include "mojo/edk/system/system_impl_export.h" 20 21 namespace mojo { 22 namespace edk { 23 24 class MOJO_SYSTEM_IMPL_EXPORT WaitSetDispatcher : public Dispatcher { 25 public: 26 WaitSetDispatcher(); 27 28 // Dispatcher: 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; 48 49 private: 50 // Internal implementation of Awakable. 51 class Waiter; 52 53 struct WaitState { 54 WaitState(); 55 WaitState(const WaitState& other); 56 ~WaitState(); 57 58 scoped_refptr<Dispatcher> dispatcher; 59 MojoHandleSignals signals; 60 uintptr_t context; 61 }; 62 63 ~WaitSetDispatcher() override; 64 65 HandleSignalsState GetHandleSignalsStateNoLock() const; 66 67 // Signal that the dispatcher indexed by |context| has been woken up with 68 // |result| and is now ready. 69 void WakeDispatcher(MojoResult result, uintptr_t context); 70 71 // Guards |is_closed_|, |waiting_dispatchers_|, and |waiter_|. 72 // 73 // TODO: Consider removing this. 74 mutable base::Lock lock_; 75 bool is_closed_ = false; 76 77 // Map of dispatchers being waited on. Key is a Dispatcher* casted to a 78 // uintptr_t, and should be treated as an opaque value and not casted back. 79 std::unordered_map<uintptr_t, WaitState> waiting_dispatchers_; 80 81 // Separate lock that can be locked without locking |lock_|. 82 mutable base::Lock awoken_lock_; 83 // List of dispatchers that have been woken up. Any dispatcher in this queue 84 // will NOT currently be waited on. 85 std::deque<std::pair<uintptr_t, MojoResult>> awoken_queue_; 86 // List of dispatchers that have been woken up and retrieved. 87 std::deque<uintptr_t> processed_dispatchers_; 88 89 // Separate lock that can be locked without locking |lock_|. 90 base::Lock awakable_lock_; 91 // List of dispatchers being waited on. 92 AwakableList awakable_list_; 93 94 // Waiter used to wait on dispatchers. 95 std::unique_ptr<Waiter> waiter_; 96 97 DISALLOW_COPY_AND_ASSIGN(WaitSetDispatcher); 98 }; 99 100 } // namespace edk 101 } // namespace mojo 102 103 #endif // MOJO_EDK_SYSTEM_WAIT_SET_DISPATCHER_H_ 104