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