OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 IPC_IPC_SYNC_CHANNEL_H_ | 5 #ifndef IPC_IPC_SYNC_CHANNEL_H_ |
6 #define IPC_IPC_SYNC_CHANNEL_H_ | 6 #define IPC_IPC_SYNC_CHANNEL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <deque> | 9 #include <deque> |
10 | 10 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // Significant complexity results from the fact that messages are still coming | 51 // Significant complexity results from the fact that messages are still coming |
52 // in while the original thread is blocked. Normal async messages are queued | 52 // in while the original thread is blocked. Normal async messages are queued |
53 // and dispatched after the blocking call is complete. Sync messages must | 53 // and dispatched after the blocking call is complete. Sync messages must |
54 // be dispatched in a reentrant manner to avoid deadlock. | 54 // be dispatched in a reentrant manner to avoid deadlock. |
55 // | 55 // |
56 // | 56 // |
57 // Note that care must be taken that the lifetime of the ipc_thread argument | 57 // Note that care must be taken that the lifetime of the ipc_thread argument |
58 // is more than this object. If the message loop goes away while this object | 58 // is more than this object. If the message loop goes away while this object |
59 // is running and it's used to send a message, then it will use the invalid | 59 // is running and it's used to send a message, then it will use the invalid |
60 // message loop pointer to proxy it to the ipc thread. | 60 // message loop pointer to proxy it to the ipc thread. |
61 class IPC_EXPORT SyncChannel : public ChannelProxy { | 61 class IPC_EXPORT SyncChannel : public ChannelProxy, |
| 62 public base::WaitableEventWatcher::Delegate { |
62 public: | 63 public: |
63 enum RestrictDispatchGroup { | 64 enum RestrictDispatchGroup { |
64 kRestrictDispatchGroup_None = 0, | 65 kRestrictDispatchGroup_None = 0, |
65 }; | 66 }; |
66 | 67 |
67 // Creates and initializes a sync channel. If create_pipe_now is specified, | 68 // Creates and initializes a sync channel. If create_pipe_now is specified, |
68 // the channel will be initialized synchronously. | 69 // the channel will be initialized synchronously. |
69 SyncChannel(const IPC::ChannelHandle& channel_handle, | 70 SyncChannel(const IPC::ChannelHandle& channel_handle, |
70 Channel::Mode mode, | 71 Channel::Mode mode, |
71 Listener* listener, | 72 Listener* listener, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // default) will be dispatched in any case. | 108 // default) will be dispatched in any case. |
108 void SetRestrictDispatchChannelGroup(int group); | 109 void SetRestrictDispatchChannelGroup(int group); |
109 | 110 |
110 protected: | 111 protected: |
111 class ReceivedSyncMsgQueue; | 112 class ReceivedSyncMsgQueue; |
112 friend class ReceivedSyncMsgQueue; | 113 friend class ReceivedSyncMsgQueue; |
113 | 114 |
114 // SyncContext holds the per object data for SyncChannel, so that SyncChannel | 115 // SyncContext holds the per object data for SyncChannel, so that SyncChannel |
115 // can be deleted while it's being used in a different thread. See | 116 // can be deleted while it's being used in a different thread. See |
116 // ChannelProxy::Context for more information. | 117 // ChannelProxy::Context for more information. |
117 class SyncContext : public Context { | 118 class SyncContext : public Context, |
| 119 public base::WaitableEventWatcher::Delegate { |
118 public: | 120 public: |
119 SyncContext(Listener* listener, | 121 SyncContext(Listener* listener, |
120 base::SingleThreadTaskRunner* ipc_task_runner, | 122 base::SingleThreadTaskRunner* ipc_task_runner, |
121 base::WaitableEvent* shutdown_event); | 123 base::WaitableEvent* shutdown_event); |
122 | 124 |
123 // Adds information about an outgoing sync message to the context so that | 125 // Adds information about an outgoing sync message to the context so that |
124 // we know how to deserialize the reply. | 126 // we know how to deserialize the reply. |
125 void Push(SyncMessage* sync_msg); | 127 void Push(SyncMessage* sync_msg); |
126 | 128 |
127 // Cleanly remove the top deserializer (and throw it away). Returns the | 129 // Cleanly remove the top deserializer (and throw it away). Returns the |
(...skipping 13 matching lines...) Expand all Loading... |
141 // Checks if the given message is blocking the listener thread because of a | 143 // Checks if the given message is blocking the listener thread because of a |
142 // synchronous send. If it is, the thread is unblocked and true is | 144 // synchronous send. If it is, the thread is unblocked and true is |
143 // returned. Otherwise the function returns false. | 145 // returned. Otherwise the function returns false. |
144 bool TryToUnblockListener(const Message* msg); | 146 bool TryToUnblockListener(const Message* msg); |
145 | 147 |
146 // Called on the IPC thread when a sync send that runs a nested message loop | 148 // Called on the IPC thread when a sync send that runs a nested message loop |
147 // times out. | 149 // times out. |
148 void OnSendTimeout(int message_id); | 150 void OnSendTimeout(int message_id); |
149 | 151 |
150 base::WaitableEvent* shutdown_event() { return shutdown_event_; } | 152 base::WaitableEvent* shutdown_event() { return shutdown_event_; } |
151 base::WaitableEventWatcher::EventCallback | |
152 shutdown_watcher_callback() const { | |
153 return shutdown_watcher_callback_; | |
154 } | |
155 | 153 |
156 ReceivedSyncMsgQueue* received_sync_msgs() { | 154 ReceivedSyncMsgQueue* received_sync_msgs() { |
157 return received_sync_msgs_; | 155 return received_sync_msgs_; |
158 } | 156 } |
159 | 157 |
160 void set_restrict_dispatch_group(int group) { | 158 void set_restrict_dispatch_group(int group) { |
161 restrict_dispatch_group_ = group; | 159 restrict_dispatch_group_ = group; |
162 } | 160 } |
163 | 161 |
164 int restrict_dispatch_group() const { | 162 int restrict_dispatch_group() const { |
165 return restrict_dispatch_group_; | 163 return restrict_dispatch_group_; |
166 } | 164 } |
167 | 165 |
168 private: | 166 private: |
169 virtual ~SyncContext(); | 167 virtual ~SyncContext(); |
170 // ChannelProxy methods that we override. | 168 // ChannelProxy methods that we override. |
171 | 169 |
172 // Called on the listener thread. | 170 // Called on the listener thread. |
173 virtual void Clear() OVERRIDE; | 171 virtual void Clear() OVERRIDE; |
174 | 172 |
175 // Called on the IPC thread. | 173 // Called on the IPC thread. |
176 virtual bool OnMessageReceived(const Message& msg) OVERRIDE; | 174 virtual bool OnMessageReceived(const Message& msg) OVERRIDE; |
177 virtual void OnChannelError() OVERRIDE; | 175 virtual void OnChannelError() OVERRIDE; |
178 virtual void OnChannelOpened() OVERRIDE; | 176 virtual void OnChannelOpened() OVERRIDE; |
179 virtual void OnChannelClosed() OVERRIDE; | 177 virtual void OnChannelClosed() OVERRIDE; |
180 | 178 |
181 // Cancels all pending Send calls. | 179 // Cancels all pending Send calls. |
182 void CancelPendingSends(); | 180 void CancelPendingSends(); |
183 | 181 |
184 void OnWaitableEventSignaled(base::WaitableEvent* arg); | 182 // WaitableEventWatcher::Delegate implementation. |
| 183 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg) OVERRIDE; |
185 | 184 |
186 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; | 185 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; |
187 PendingSyncMessageQueue deserializers_; | 186 PendingSyncMessageQueue deserializers_; |
188 base::Lock deserializers_lock_; | 187 base::Lock deserializers_lock_; |
189 | 188 |
190 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; | 189 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; |
191 | 190 |
192 base::WaitableEvent* shutdown_event_; | 191 base::WaitableEvent* shutdown_event_; |
193 base::WaitableEventWatcher shutdown_watcher_; | 192 base::WaitableEventWatcher shutdown_watcher_; |
194 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_; | |
195 int restrict_dispatch_group_; | 193 int restrict_dispatch_group_; |
196 }; | 194 }; |
197 | 195 |
198 private: | 196 private: |
199 void OnWaitableEventSignaled(base::WaitableEvent* arg); | 197 // WaitableEventWatcher::Delegate implementation. |
| 198 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg) OVERRIDE; |
200 | 199 |
201 SyncContext* sync_context() { | 200 SyncContext* sync_context() { |
202 return reinterpret_cast<SyncContext*>(context()); | 201 return reinterpret_cast<SyncContext*>(context()); |
203 } | 202 } |
204 | 203 |
205 // Both these functions wait for a reply, timeout or process shutdown. The | 204 // Both these functions wait for a reply, timeout or process shutdown. The |
206 // latter one also runs a nested message loop in the meantime. | 205 // latter one also runs a nested message loop in the meantime. |
207 static void WaitForReply( | 206 static void WaitForReply( |
208 SyncContext* context, base::WaitableEvent* pump_messages_event); | 207 SyncContext* context, base::WaitableEvent* pump_messages_event); |
209 | 208 |
210 // Runs a nested message loop until a reply arrives, times out, or the process | 209 // Runs a nested message loop until a reply arrives, times out, or the process |
211 // shuts down. | 210 // shuts down. |
212 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); | 211 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); |
213 | 212 |
214 // Starts the dispatch watcher. | 213 // Starts the dispatch watcher. |
215 void StartWatching(); | 214 void StartWatching(); |
216 | 215 |
217 bool sync_messages_with_no_timeout_allowed_; | 216 bool sync_messages_with_no_timeout_allowed_; |
218 | 217 |
219 // Used to signal events between the IPC and listener threads. | 218 // Used to signal events between the IPC and listener threads. |
220 base::WaitableEventWatcher dispatch_watcher_; | 219 base::WaitableEventWatcher dispatch_watcher_; |
221 base::Callback<void(base::WaitableEvent*)> dispatch_watcher_callback_; | |
222 | 220 |
223 DISALLOW_COPY_AND_ASSIGN(SyncChannel); | 221 DISALLOW_COPY_AND_ASSIGN(SyncChannel); |
224 }; | 222 }; |
225 | 223 |
226 } // namespace IPC | 224 } // namespace IPC |
227 | 225 |
228 #endif // IPC_IPC_SYNC_CHANNEL_H_ | 226 #endif // IPC_IPC_SYNC_CHANNEL_H_ |
OLD | NEW |