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

Side by Side Diff: media/audio/cross_process_notification_win.cc

Issue 9605015: Add a SharedMemSynchronizer class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "media/audio/cross_process_notification.h"
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/threading/platform_thread.h"
10 #include "base/win/scoped_handle.h"
11
12 CrossProcessNotification::~CrossProcessNotification() {
scherkus (not reviewing) 2012/03/27 14:38:14 nit {}
13 }
14
15 CrossProcessNotification::CrossProcessNotification(IPCHandle handle_1,
16 IPCHandle handle_2)
17 : mine_(handle_1), other_(handle_2) {
18 DCHECK(IsValid());
19 }
20
21 void CrossProcessNotification::Signal() {
22 DCHECK(IsValid());
23 DCHECK_EQ(::WaitForSingleObject(mine_, 0), static_cast<DWORD>(WAIT_TIMEOUT))
24 << "Are you calling Signal() without calling Wait() first?";
25 BOOL ok = ::SetEvent(mine_);
26 CHECK(ok);
27 }
28
29 void CrossProcessNotification::Wait() {
30 DCHECK(IsValid());
31 DWORD wait = ::WaitForSingleObject(other_, INFINITE);
32 DCHECK_EQ(wait, WAIT_OBJECT_0);
33 BOOL ok = ::ResetEvent(other_);
34 CHECK(ok);
35 }
36
37 bool CrossProcessNotification::IsValid() const {
38 return mine_.IsValid() && other_.IsValid();
39 }
40
41 bool CrossProcessNotification::ShareToProcess(base::ProcessHandle process,
42 IPCHandle* handle_1,
43 IPCHandle* handle_2) {
44 DCHECK(IsValid());
45 HANDLE our_process = ::GetCurrentProcess();
46 if (!::DuplicateHandle(our_process, mine_, process, handle_1, 0, FALSE,
47 DUPLICATE_SAME_ACCESS)) {
48 return false;
49 }
50
51 if (!::DuplicateHandle(our_process, other_, process, handle_2, 0, FALSE,
52 DUPLICATE_SAME_ACCESS)) {
53 // In case we're sharing to ourselves, we can close the handle, but
54 // if the target process is a different process, we do nothing.
55 if (process == our_process)
56 ::CloseHandle(*handle_1);
57 *handle_1 = NULL;
58 return false;
59 }
60
61 return true;
62 }
63
64 // static
65 bool CrossProcessNotification::InitializePair(CrossProcessNotification* a,
66 CrossProcessNotification* b) {
67 DCHECK(!a->IsValid());
68 DCHECK(!b->IsValid());
69
70 bool success = false;
71
72 // Create two manually resettable events and give each party a handle
73 // to both events.
74 HANDLE event_a = ::CreateEvent(NULL, TRUE, FALSE, NULL);
75 HANDLE event_b = ::CreateEvent(NULL, TRUE, FALSE, NULL);
76 if (event_a && event_b) {
77 a->mine_.Set(event_a);
78 a->other_.Set(event_b);
79 success = a->ShareToProcess(GetCurrentProcess(), &event_a, &event_b);
80 if (success) {
81 b->mine_.Set(event_b);
82 b->other_.Set(event_a);
83 } else {
84 a->mine_.Close();
85 a->other_.Close();
86 }
87 } else {
88 if (event_a)
89 ::CloseHandle(event_a);
90 if (event_b)
91 ::CloseHandle(event_b);
92 }
93
94 DCHECK(!success || a->IsValid());
95 DCHECK(!success || b->IsValid());
96
97 return success;
98 }
99
100 namespace {
101 class ExtraWaitThread : public base::PlatformThread::Delegate {
102 public:
103 ExtraWaitThread(HANDLE stop, HANDLE* events, size_t count,
104 int* signaled_event)
105 : stop_(stop), events_(events), count_(count),
106 signaled_event_(signaled_event) {
107 *signaled_event_ = -1;
108 }
109 virtual ~ExtraWaitThread() {}
110
111 virtual void ThreadMain() OVERRIDE {
112 // Store the |stop_| event as the first event.
113 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { stop_ };
114 HANDLE next_thread = NULL;
115 DWORD event_count = MAXIMUM_WAIT_OBJECTS;
116 int thread_signaled_event = -1;
117 scoped_ptr<ExtraWaitThread> extra_wait_thread;
118 if (count_ > (MAXIMUM_WAIT_OBJECTS - 1)) {
119 std::copy(&events_[0], &events_[MAXIMUM_WAIT_OBJECTS - 2], &events[1]);
120
121 extra_wait_thread.reset(new ExtraWaitThread(stop_,
122 &events_[MAXIMUM_WAIT_OBJECTS - 2],
123 count_ - (MAXIMUM_WAIT_OBJECTS - 2),
124 &thread_signaled_event));
125 base::PlatformThread::Create(0, extra_wait_thread.get(), &next_thread);
126
127 event_count = MAXIMUM_WAIT_OBJECTS;
128 events[MAXIMUM_WAIT_OBJECTS - 1] = next_thread;
129 } else {
130 std::copy(&events_[0], &events_[count_], &events[1]);
131 event_count = count_ + 1;
132 }
133
134 DWORD wait = ::WaitForMultipleObjects(event_count, &events[0], FALSE,
135 INFINITE);
136 if (wait >= WAIT_OBJECT_0 && wait < (WAIT_OBJECT_0 + event_count)) {
137 wait -= WAIT_OBJECT_0;
138 if (wait == 0) {
139 // The stop event was signaled. Check if it was signaled by a
140 // sub thread. In case our sub thread had to spin another thread (and
141 // so on), we must wait for ours to exit before we can check the
142 // propagated event offset.
143 if (next_thread) {
144 base::PlatformThread::Join(next_thread);
145 next_thread = NULL;
146 }
147 if (thread_signaled_event != -1)
148 *signaled_event_ = thread_signaled_event + (MAXIMUM_WAIT_OBJECTS - 2);
149 } else if (events[wait] == next_thread) {
150 NOTREACHED();
151 } else {
152 *signaled_event_ = static_cast<int>(wait);
153 SetEvent(stop_);
154 }
155 } else {
156 NOTREACHED();
157 }
158
159 if (next_thread)
160 base::PlatformThread::Join(next_thread);
161 }
162
163 private:
164 HANDLE stop_;
165 HANDLE* events_;
166 size_t count_;
167 int* signaled_event_;
168 DISALLOW_COPY_AND_ASSIGN(ExtraWaitThread);
169 };
170 } // end namespace
171
172 // static
173 int CrossProcessNotification::WaitMultiple(const Notifications& notifications,
174 size_t wait_offset) {
175 DCHECK_LT(wait_offset, notifications.size());
176
177 for (size_t i = 0; i < notifications.size(); ++i) {
178 DCHECK(notifications[i]->IsValid());
179 }
180
181 // TODO(tommi): Should we wait in an alertable state so that we can be
182 // canceled via an APC?
183 scoped_array<HANDLE> handles(new HANDLE[notifications.size()]);
184
185 // Because of the way WaitForMultipleObjects works, we do a little trick here.
186 // When multiple events are signaled, WaitForMultipleObjects will return the
187 // index of the first signaled item (lowest). This means that if we always
188 // pass the array the same way to WaitForMultipleObjects, the objects that
189 // come first, have higher priority. In times of heavy load, this will cause
190 // elements at the back to become DOS-ed.
191 // So, we store the location of the item that was last signaled. Then we split
192 // up the array and move everything higher than the last signaled index to the
193 // front and the rest to the back (meaning that the last signaled item will
194 // become the last element in the list).
195 // Assuming equally busy events, this approach distributes the priority
196 // evenly.
197
198 size_t index = 0;
199 for (size_t i = wait_offset; i < notifications.size(); ++i)
200 handles[index++] = notifications[i]->other_;
201
202 for (size_t i = 0; i < wait_offset; ++i)
203 handles[index++] = notifications[i]->other_;
204 DCHECK_EQ(index, notifications.size());
205
206 DWORD wait = WAIT_FAILED;
207 bool wait_failed = false;
208 if (notifications.size() <= MAXIMUM_WAIT_OBJECTS) {
209 wait = ::WaitForMultipleObjects(notifications.size(), &handles[0], FALSE,
210 INFINITE);
211 wait_failed = wait < WAIT_OBJECT_0 ||
212 wait >= (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS);
213 } else {
214 // Used to stop the other wait threads when an event has been signaled.
215 base::win::ScopedHandle stop(::CreateEvent(NULL, TRUE, FALSE, NULL));
216
217 // Create the first thread and pass a pointer to all handles >63
218 // to the thread + 'stop'. Then implement the thread so that it checks
219 // if the number of handles is > 63. If so, spawns a new thread and
220 // passes >62 handles to that thread and waits for the 62 handles + stop +
221 // next thread. etc etc.
222
223 // Create a list of threads so that each thread waits on at most 62 events
224 // including one event for when a child thread signals completion and one
225 // event for when all of the threads must be stopped (due to some event
226 // being signaled).
227
228 int thread_signaled_event = -1;
229 ExtraWaitThread wait_thread(stop, &handles[MAXIMUM_WAIT_OBJECTS - 1],
230 notifications.size() - (MAXIMUM_WAIT_OBJECTS - 1),
231 &thread_signaled_event);
232 base::PlatformThreadHandle thread;
233 base::PlatformThread::Create(0, &wait_thread, &thread);
234 HANDLE events[MAXIMUM_WAIT_OBJECTS];
235 std::copy(&handles[0], &handles[MAXIMUM_WAIT_OBJECTS - 1], &events[0]);
236 events[MAXIMUM_WAIT_OBJECTS - 1] = thread;
237 wait = ::WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, &events[0], FALSE,
238 INFINITE);
239 wait_failed = wait < WAIT_OBJECT_0 ||
240 wait >= (WAIT_OBJECT_0 + MAXIMUM_WAIT_OBJECTS);
241 if (wait == WAIT_OBJECT_0 + (MAXIMUM_WAIT_OBJECTS - 1)) {
242 if (thread_signaled_event < 0) {
243 wait_failed = true;
244 NOTREACHED();
245 } else {
246 wait = WAIT_OBJECT_0 + (MAXIMUM_WAIT_OBJECTS - 2) +
247 thread_signaled_event;
248 }
249 } else {
250 ::SetEvent(stop);
251 }
252 base::PlatformThread::Join(thread);
253 }
254
255 int ret = -1;
256 if (!wait_failed) {
257 // Subtract to be politically correct (WAIT_OBJECT_0 is actually 0).
258 wait -= WAIT_OBJECT_0;
259 BOOL ok = ::ResetEvent(handles[wait]);
260 CHECK(ok);
261 ret = (wait + wait_offset) % notifications.size();
262 DCHECK_EQ(handles[wait], notifications[ret]->other_.Get());
263 } else {
264 NOTREACHED();
265 }
266
267 CHECK_NE(ret, -1);
268 return ret;
269 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698