Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: ipc/ipc_sync_channel.h

Issue 12094106: Refactor: Simplify WaitableEventWatcher. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/plugin_service_impl.cc ('k') | ipc/ipc_sync_channel.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 {
63 public: 62 public:
64 enum RestrictDispatchGroup { 63 enum RestrictDispatchGroup {
65 kRestrictDispatchGroup_None = 0, 64 kRestrictDispatchGroup_None = 0,
66 }; 65 };
67 66
68 // Creates and initializes a sync channel. If create_pipe_now is specified, 67 // Creates and initializes a sync channel. If create_pipe_now is specified,
69 // the channel will be initialized synchronously. 68 // the channel will be initialized synchronously.
70 SyncChannel(const IPC::ChannelHandle& channel_handle, 69 SyncChannel(const IPC::ChannelHandle& channel_handle,
71 Channel::Mode mode, 70 Channel::Mode mode,
72 Listener* listener, 71 Listener* listener,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // default) will be dispatched in any case. 107 // default) will be dispatched in any case.
109 void SetRestrictDispatchChannelGroup(int group); 108 void SetRestrictDispatchChannelGroup(int group);
110 109
111 protected: 110 protected:
112 class ReceivedSyncMsgQueue; 111 class ReceivedSyncMsgQueue;
113 friend class ReceivedSyncMsgQueue; 112 friend class ReceivedSyncMsgQueue;
114 113
115 // SyncContext holds the per object data for SyncChannel, so that SyncChannel 114 // SyncContext holds the per object data for SyncChannel, so that SyncChannel
116 // can be deleted while it's being used in a different thread. See 115 // can be deleted while it's being used in a different thread. See
117 // ChannelProxy::Context for more information. 116 // ChannelProxy::Context for more information.
118 class SyncContext : public Context, 117 class SyncContext : public Context {
119 public base::WaitableEventWatcher::Delegate {
120 public: 118 public:
121 SyncContext(Listener* listener, 119 SyncContext(Listener* listener,
122 base::SingleThreadTaskRunner* ipc_task_runner, 120 base::SingleThreadTaskRunner* ipc_task_runner,
123 base::WaitableEvent* shutdown_event); 121 base::WaitableEvent* shutdown_event);
124 122
125 // Adds information about an outgoing sync message to the context so that 123 // Adds information about an outgoing sync message to the context so that
126 // we know how to deserialize the reply. 124 // we know how to deserialize the reply.
127 void Push(SyncMessage* sync_msg); 125 void Push(SyncMessage* sync_msg);
128 126
129 // Cleanly remove the top deserializer (and throw it away). Returns the 127 // Cleanly remove the top deserializer (and throw it away). Returns the
(...skipping 26 matching lines...) Expand all
156 } 154 }
157 155
158 void set_restrict_dispatch_group(int group) { 156 void set_restrict_dispatch_group(int group) {
159 restrict_dispatch_group_ = group; 157 restrict_dispatch_group_ = group;
160 } 158 }
161 159
162 int restrict_dispatch_group() const { 160 int restrict_dispatch_group() const {
163 return restrict_dispatch_group_; 161 return restrict_dispatch_group_;
164 } 162 }
165 163
164 base::WaitableEventWatcher::EventCallback MakeWaitableEventCallback();
165
166 private: 166 private:
167 virtual ~SyncContext(); 167 virtual ~SyncContext();
168 // ChannelProxy methods that we override. 168 // ChannelProxy methods that we override.
169 169
170 // Called on the listener thread. 170 // Called on the listener thread.
171 virtual void Clear() OVERRIDE; 171 virtual void Clear() OVERRIDE;
172 172
173 // Called on the IPC thread. 173 // Called on the IPC thread.
174 virtual bool OnMessageReceived(const Message& msg) OVERRIDE; 174 virtual bool OnMessageReceived(const Message& msg) OVERRIDE;
175 virtual void OnChannelError() OVERRIDE; 175 virtual void OnChannelError() OVERRIDE;
176 virtual void OnChannelOpened() OVERRIDE; 176 virtual void OnChannelOpened() OVERRIDE;
177 virtual void OnChannelClosed() OVERRIDE; 177 virtual void OnChannelClosed() OVERRIDE;
178 178
179 // Cancels all pending Send calls. 179 // Cancels all pending Send calls.
180 void CancelPendingSends(); 180 void CancelPendingSends();
181 181
182 // WaitableEventWatcher::Delegate implementation. 182 void OnWaitableEventSignaled(base::WaitableEvent* event);
183 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg) OVERRIDE;
184 183
185 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue; 184 typedef std::deque<PendingSyncMsg> PendingSyncMessageQueue;
186 PendingSyncMessageQueue deserializers_; 185 PendingSyncMessageQueue deserializers_;
187 base::Lock deserializers_lock_; 186 base::Lock deserializers_lock_;
188 187
189 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_; 188 scoped_refptr<ReceivedSyncMsgQueue> received_sync_msgs_;
190 189
191 base::WaitableEvent* shutdown_event_; 190 base::WaitableEvent* shutdown_event_;
192 base::WaitableEventWatcher shutdown_watcher_; 191 base::WaitableEventWatcher shutdown_watcher_;
192 base::WaitableEventWatcher::EventCallback shutdown_watcher_callback_;
193 int restrict_dispatch_group_; 193 int restrict_dispatch_group_;
194 }; 194 };
195 195
196 private: 196 private:
197 // WaitableEventWatcher::Delegate implementation. 197 void OnWaitableEventSignaled(base::WaitableEvent* arg);
198 virtual void OnWaitableEventSignaled(base::WaitableEvent* arg) OVERRIDE;
199 198
200 SyncContext* sync_context() { 199 SyncContext* sync_context() {
201 return reinterpret_cast<SyncContext*>(context()); 200 return reinterpret_cast<SyncContext*>(context());
202 } 201 }
203 202
204 // Both these functions wait for a reply, timeout or process shutdown. The 203 // Both these functions wait for a reply, timeout or process shutdown. The
205 // latter one also runs a nested message loop in the meantime. 204 // latter one also runs a nested message loop in the meantime.
206 static void WaitForReply( 205 static void WaitForReply(
207 SyncContext* context, base::WaitableEvent* pump_messages_event); 206 SyncContext* context, base::WaitableEvent* pump_messages_event);
208 207
209 // Runs a nested message loop until a reply arrives, times out, or the process 208 // Runs a nested message loop until a reply arrives, times out, or the process
210 // shuts down. 209 // shuts down.
211 static void WaitForReplyWithNestedMessageLoop(SyncContext* context); 210 static void WaitForReplyWithNestedMessageLoop(SyncContext* context);
212 211
213 // Starts the dispatch watcher. 212 // Starts the dispatch watcher.
214 void StartWatching(); 213 void StartWatching();
215 214
216 bool sync_messages_with_no_timeout_allowed_; 215 bool sync_messages_with_no_timeout_allowed_;
217 216
218 // Used to signal events between the IPC and listener threads. 217 // Used to signal events between the IPC and listener threads.
219 base::WaitableEventWatcher dispatch_watcher_; 218 base::WaitableEventWatcher dispatch_watcher_;
219 base::WaitableEventWatcher::EventCallback dispatch_watcher_callback_;
220 220
221 DISALLOW_COPY_AND_ASSIGN(SyncChannel); 221 DISALLOW_COPY_AND_ASSIGN(SyncChannel);
222 }; 222 };
223 223
224 } // namespace IPC 224 } // namespace IPC
225 225
226 #endif // IPC_IPC_SYNC_CHANNEL_H_ 226 #endif // IPC_IPC_SYNC_CHANNEL_H_
OLDNEW
« no previous file with comments | « content/browser/plugin_service_impl.cc ('k') | ipc/ipc_sync_channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698