[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 5 | #ifndef MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
| 6 | #define MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 7 | |
Avi Drissman | 2e88ac37 | 2015-12-21 18:14:57 | [diff] [blame^] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 10 | #include <map> |
amistry | e8de0bc0 | 2015-11-24 08:42:49 | [diff] [blame] | 11 | #include <set> |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 12 | |
viettrungluu | 473e913 | 2014-09-30 05:04:53 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 15 | #include "base/message_loop/message_pump.h" |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 17 | #include "base/synchronization/lock.h" |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 18 | #include "base/time/time.h" |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 19 | #include "mojo/message_pump/mojo_message_pump_export.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 20 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 21 | |
| 22 | namespace mojo { |
| 23 | namespace common { |
| 24 | |
| 25 | class MessagePumpMojoHandler; |
| 26 | |
| 27 | // Mojo implementation of MessagePump. |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 28 | class MOJO_MESSAGE_PUMP_EXPORT MessagePumpMojo : public base::MessagePump { |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 29 | public: |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 30 | class MOJO_MESSAGE_PUMP_EXPORT Observer { |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 31 | public: |
| 32 | Observer() {} |
| 33 | |
| 34 | virtual void WillSignalHandler() = 0; |
| 35 | virtual void DidSignalHandler() = 0; |
| 36 | |
| 37 | protected: |
| 38 | virtual ~Observer() {} |
| 39 | }; |
| 40 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 41 | MessagePumpMojo(); |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 42 | ~MessagePumpMojo() override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 43 | |
[email protected] | d7dc6add | 2014-03-20 05:59:58 | [diff] [blame] | 44 | // Static factory function (for using with |base::Thread::Options|, wrapped |
| 45 | // using |base::Bind()|). |
| 46 | static scoped_ptr<base::MessagePump> Create(); |
| 47 | |
yzshen | d928bdd | 2014-08-29 22:51:42 | [diff] [blame] | 48 | // Returns the MessagePumpMojo instance of the current thread, if it exists. |
| 49 | static MessagePumpMojo* current(); |
| 50 | |
| 51 | static bool IsCurrent() { return !!current(); } |
| 52 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 53 | // Registers a MessagePumpMojoHandler for the specified handle. Only one |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 54 | // handler can be registered for a specified handle. |
[email protected] | 9a41292 | 2014-08-14 15:48:24 | [diff] [blame] | 55 | // NOTE: a value of 0 for |deadline| indicates an indefinite timeout. |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 56 | void AddHandler(MessagePumpMojoHandler* handler, |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 57 | const Handle& handle, |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 58 | MojoHandleSignals wait_signals, |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 59 | base::TimeTicks deadline); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 60 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 61 | void RemoveHandler(const Handle& handle); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 62 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 63 | void AddObserver(Observer*); |
| 64 | void RemoveObserver(Observer*); |
| 65 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 66 | // MessagePump: |
dcheng | 3fc12db | 2014-10-21 12:16:28 | [diff] [blame] | 67 | void Run(Delegate* delegate) override; |
| 68 | void Quit() override; |
| 69 | void ScheduleWork() override; |
| 70 | void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | struct RunState; |
| 74 | struct WaitState; |
| 75 | |
[email protected] | 273e217 | 2013-12-10 07:24:16 | [diff] [blame] | 76 | // Contains the data needed to track a request to AddHandler(). |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 77 | struct Handler { |
rockot | 2efa3385 | 2015-10-12 17:52:28 | [diff] [blame] | 78 | Handler() : handler(NULL), wait_signals(MOJO_HANDLE_SIGNAL_NONE), id(0) {} |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 79 | |
| 80 | MessagePumpMojoHandler* handler; |
[email protected] | e3da5f9 | 2014-06-18 10:18:11 | [diff] [blame] | 81 | MojoHandleSignals wait_signals; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 82 | base::TimeTicks deadline; |
| 83 | // See description of |MessagePumpMojo::next_handler_id_| for details. |
| 84 | int id; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 85 | }; |
| 86 | |
[email protected] | bd6cb4bc | 2013-11-25 23:57:16 | [diff] [blame] | 87 | typedef std::map<Handle, Handler> HandleToHandler; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 88 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 89 | // Implementation of Run(). |
| 90 | void DoRunLoop(RunState* run_state, Delegate* delegate); |
| 91 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 92 | // Services the set of handles ready. If |block| is true this waits for a |
qsr | d0a9ca1 | 2014-10-06 16:33:58 | [diff] [blame] | 93 | // handle to become ready, otherwise this does not block. Returns |true| if a |
| 94 | // handle has become ready, |false| otherwise. |
| 95 | bool DoInternalWork(const RunState& run_state, bool block); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 96 | |
rockot | 6dcbf7c | 2015-01-16 00:41:13 | [diff] [blame] | 97 | // Removes the given invalid handle. This is called if MojoWaitMany finds an |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 98 | // invalid handle. |
rockot | 6dcbf7c | 2015-01-16 00:41:13 | [diff] [blame] | 99 | void RemoveInvalidHandle(const WaitState& wait_state, |
| 100 | MojoResult result, |
| 101 | uint32_t result_index); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 102 | |
amistry | 240e4e5 | 2015-11-18 00:38:40 | [diff] [blame] | 103 | void SignalControlPipe(); |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 104 | |
amistry | 240e4e5 | 2015-11-18 00:38:40 | [diff] [blame] | 105 | WaitState GetWaitState() const; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 106 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 107 | // Returns the deadline for the call to MojoWaitMany(). |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 108 | MojoDeadline GetDeadlineForWait(const RunState& run_state) const; |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 109 | |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 110 | void WillSignalHandler(); |
| 111 | void DidSignalHandler(); |
| 112 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 113 | // If non-NULL we're running (inside Run()). Member is reference to value on |
| 114 | // stack. |
| 115 | RunState* run_state_; |
| 116 | |
[email protected] | 5273d8ec | 2014-04-16 05:34:18 | [diff] [blame] | 117 | // 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 |
| 119 | // thread. |
| 120 | base::Lock run_state_lock_; |
| 121 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 122 | HandleToHandler handlers_; |
amistry | e8de0bc0 | 2015-11-24 08:42:49 | [diff] [blame] | 123 | // Set of handles that have a deadline set. Avoids iterating over all elements |
| 124 | // in |handles_| in the common case (no deadline set). |
| 125 | // TODO(amistry): Make this better and avoid special-casing deadlines. |
| 126 | std::set<Handle> deadline_handles_; |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 127 | |
[email protected] | a6881fd | 2013-11-23 19:04:19 | [diff] [blame] | 128 | // An ever increasing value assigned to each Handler::id. Used to detect |
| 129 | // 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 |
| 131 | // match it means the handler was removed then added so that we shouldn't |
| 132 | // notify it. |
| 133 | int next_handler_id_; |
| 134 | |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 135 | base::ObserverList<Observer> observers_; |
abarth | 4ba3cad | 2014-10-17 22:02:15 | [diff] [blame] | 136 | |
amistry | 240e4e5 | 2015-11-18 00:38:40 | [diff] [blame] | 137 | // Used to wake up run loop from |SignalControlPipe()|. |
| 138 | ScopedMessagePipeHandle read_handle_; |
| 139 | ScopedMessagePipeHandle write_handle_; |
| 140 | |
[email protected] | 3fee57b | 2013-11-12 16:35:02 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(MessagePumpMojo); |
| 142 | }; |
| 143 | |
| 144 | } // namespace common |
| 145 | } // namespace mojo |
| 146 | |
sky | add030b | 2015-08-07 01:06:18 | [diff] [blame] | 147 | #endif // MOJO_MESSAGE_PUMP_MESSAGE_PUMP_MOJO_H_ |