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