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_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ | 5 #ifndef MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
6 #define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ | 6 #define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 void RemoveObserver(Observer*); | 64 void RemoveObserver(Observer*); |
65 | 65 |
66 // MessagePump: | 66 // MessagePump: |
67 void Run(Delegate* delegate) override; | 67 void Run(Delegate* delegate) override; |
68 void Quit() override; | 68 void Quit() override; |
69 void ScheduleWork() override; | 69 void ScheduleWork() override; |
70 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; | 70 void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; |
71 | 71 |
72 private: | 72 private: |
73 struct RunState; | 73 struct RunState; |
74 struct WaitState; | |
75 | 74 |
76 // Contains the data needed to track a request to AddHandler(). | 75 // Contains the data needed to track a request to AddHandler(). |
77 struct Handler { | 76 struct Handler { |
78 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} | 77 Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} |
79 | 78 |
80 MessagePumpMojoHandler* handler; | 79 MessagePumpMojoHandler* handler; |
81 MojoHandleSignals wait_signals; | 80 MojoHandleSignals wait_signals; |
82 base::TimeTicks deadline; | 81 base::TimeTicks deadline; |
83 // See description of |MessagePumpMojo::next_handler_id_| for details. | 82 // See description of |MessagePumpMojo::next_handler_id_| for details. |
84 int id; | 83 int id; |
85 }; | 84 }; |
86 | 85 |
87 typedef std::map<Handle, Handler> HandleToHandler; | 86 typedef std::map<Handle, Handler> HandleToHandler; |
88 | 87 |
89 // Implementation of Run(). | 88 // Implementation of Run(). |
90 void DoRunLoop(RunState* run_state, Delegate* delegate); | 89 void DoRunLoop(RunState* run_state, Delegate* delegate); |
91 | 90 |
92 // Services the set of handles ready. If |block| is true this waits for a | 91 // Services the set of handles ready. If |block| is true this waits for a |
93 // handle to become ready, otherwise this does not block. Returns |true| if a | 92 // handle to become ready, otherwise this does not block. Returns |true| if a |
94 // handle has become ready, |false| otherwise. | 93 // handle has become ready, |false| otherwise. |
95 bool DoInternalWork(const RunState& run_state, bool block); | 94 bool DoInternalWork(const RunState& run_state, bool block); |
96 | 95 |
97 // Removes the given invalid handle. This is called if MojoWaitMany finds an | 96 // Waits for handles in the wait set to become ready. Returns |true| if ready |
98 // invalid handle. | 97 // handles may be available, or |false| if the wait's deadline was exceeded. |
99 void RemoveInvalidHandle(const WaitState& wait_state, | 98 // Note, ready handles may be unavailable, even though |true| was returned. |
100 MojoResult result, | 99 bool WaitForReadyHandles(const RunState& run_state) const; |
101 uint32_t result_index); | 100 |
| 101 // Retrieves any 'ready' handles from the wait set, and runs the handler's |
| 102 // OnHandleReady() or OnHandleError() functions as necessary. Returns |true| |
| 103 // if any handles were ready and processed. |
| 104 bool ProcessReadyHandles(); |
| 105 |
| 106 // Removes the given invalid handle. This is called if MojoGetReadyHandles |
| 107 // finds an invalid or closed handle. |
| 108 void RemoveInvalidHandle(MojoResult result, Handle handle); |
| 109 |
| 110 // Removes any handles that have expired their deadline. Runs the handler's |
| 111 // OnHandleError() function with |MOJO_RESULT_DEADLINE_EXCEEDED| as the |
| 112 // result. Returns |true| if any handles were removed. |
| 113 bool RemoveExpiredHandles(); |
102 | 114 |
103 void SignalControlPipe(); | 115 void SignalControlPipe(); |
104 | 116 |
105 WaitState GetWaitState() const; | 117 // Returns the deadline for the call to MojoWait(). |
| 118 MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
106 | 119 |
107 // Returns the deadline for the call to MojoWaitMany(). | 120 // Run |OnHandleReady()| for the handler registered with |handle|. |handle| |
108 MojoDeadline GetDeadlineForWait(const RunState& run_state) const; | 121 // must be registered. |
| 122 void SignalHandleReady(Handle handle); |
109 | 123 |
110 void WillSignalHandler(); | 124 void WillSignalHandler(); |
111 void DidSignalHandler(); | 125 void DidSignalHandler(); |
112 | 126 |
113 // If non-NULL we're running (inside Run()). Member is reference to value on | 127 // If non-NULL we're running (inside Run()). Member is reference to value on |
114 // stack. | 128 // stack. |
115 RunState* run_state_; | 129 RunState* run_state_; |
116 | 130 |
117 // Lock for accessing |run_state_|. In general the only method that we have to | 131 // Lock for accessing |run_state_|. In general the only method that we have to |
118 // worry about is ScheduleWork(). All other methods are invoked on the same | 132 // worry about is ScheduleWork(). All other methods are invoked on the same |
119 // thread. | 133 // thread. |
120 base::Lock run_state_lock_; | 134 base::Lock run_state_lock_; |
121 | 135 |
122 HandleToHandler handlers_; | 136 HandleToHandler handlers_; |
123 // Set of handles that have a deadline set. Avoids iterating over all elements | 137 // Set of handles that have a deadline set. Avoids iterating over all elements |
124 // in |handles_| in the common case (no deadline set). | 138 // in |handles_| in the common case (no deadline set). |
125 // TODO(amistry): Make this better and avoid special-casing deadlines. | 139 // TODO(amistry): Make this better and avoid special-casing deadlines. |
126 std::set<Handle> deadline_handles_; | 140 std::set<Handle> deadline_handles_; |
127 | 141 |
128 // An ever increasing value assigned to each Handler::id. Used to detect | 142 // An ever increasing value assigned to each Handler::id. Used to detect |
129 // uniqueness while notifying. That is, while notifying expired timers we copy | 143 // uniqueness while notifying. That is, while notifying expired timers we copy |
130 // |handlers_| and only notify handlers whose id match. If the id does not | 144 // |handlers_| and only notify handlers whose id match. If the id does not |
131 // match it means the handler was removed then added so that we shouldn't | 145 // match it means the handler was removed then added so that we shouldn't |
132 // notify it. | 146 // notify it. |
133 int next_handler_id_; | 147 int next_handler_id_; |
134 | 148 |
135 base::ObserverList<Observer> observers_; | 149 base::ObserverList<Observer> observers_; |
136 | 150 |
| 151 // Mojo handle for the wait set. |
| 152 ScopedHandle wait_set_handle_; |
137 // Used to wake up run loop from |SignalControlPipe()|. | 153 // Used to wake up run loop from |SignalControlPipe()|. |
138 ScopedMessagePipeHandle read_handle_; | 154 ScopedMessagePipeHandle read_handle_; |
139 ScopedMessagePipeHandle write_handle_; | 155 ScopedMessagePipeHandle write_handle_; |
140 | 156 |
141 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); | 157 DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
142 }; | 158 }; |
143 | 159 |
144 } // namespace common | 160 } // namespace common |
145 } // namespace mojo | 161 } // namespace mojo |
146 | 162 |
147 #endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ | 163 #endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
OLD | NEW |