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

Side by Side Diff: media/midi/midi_manager.cc

Issue 1576323002: Prevent a race condition with MidiHost IPC sending. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style Created 4 years, 11 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/media/midi_host.cc ('k') | no next file » | 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 #include "media/midi/midi_manager.h" 5 #include "media/midi/midi_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // Call StartInitialization() only for the first request. 119 // Call StartInitialization() only for the first request.
120 if (pending_clients_.empty()) { 120 if (pending_clients_.empty()) {
121 completion = Completion::INVOKE_INITIALIZATION; 121 completion = Completion::INVOKE_INITIALIZATION;
122 session_thread_runner_ = base::MessageLoop::current()->task_runner(); 122 session_thread_runner_ = base::MessageLoop::current()->task_runner();
123 } else { 123 } else {
124 completion = Completion::COMPLETE_ASYNCHRONOUSLY; 124 completion = Completion::COMPLETE_ASYNCHRONOUSLY;
125 } 125 }
126 pending_clients_.insert(client); 126 pending_clients_.insert(client);
127 } 127 }
128 } 128 }
129
130 if (completion == Completion::COMPLETE_SYNCHRONOUSLY) {
131 client->CompleteStartSession(result);
132 return;
133 }
129 } 134 }
130 135
131 if (completion == Completion::COMPLETE_SYNCHRONOUSLY) { 136 if (completion == Completion::INVOKE_INITIALIZATION) {
132 client->CompleteStartSession(result);
133 } else if (completion == Completion::INVOKE_INITIALIZATION) {
134 // Lazily initialize the MIDI back-end. 137 // Lazily initialize the MIDI back-end.
135 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); 138 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
136 // CompleteInitialization() will be called asynchronously when platform 139 // CompleteInitialization() will be called asynchronously when platform
137 // dependent initialization is finished. 140 // dependent initialization is finished.
138 StartInitialization(); 141 StartInitialization();
139 } 142 }
140 } 143 }
141 144
142 void MidiManager::EndSession(MidiManagerClient* client) { 145 void MidiManager::EndSession(MidiManagerClient* client) {
143 ReportUsage(Usage::SESSION_ENDED); 146 ReportUsage(Usage::SESSION_ENDED);
144 147
145 // At this point, |client| can be in the destruction process, and calling 148 // At this point, |client| can be in the destruction process, and calling
146 // any method of |client| is dangerous. 149 // any method of |client| is dangerous. Calls on clients *must* be protected
150 // by |lock_| to prevent race conditions.
147 base::AutoLock auto_lock(lock_); 151 base::AutoLock auto_lock(lock_);
148 clients_.erase(client); 152 clients_.erase(client);
149 pending_clients_.erase(client); 153 pending_clients_.erase(client);
150 } 154 }
151 155
152 void MidiManager::AccumulateMidiBytesSent(MidiManagerClient* client, size_t n) { 156 void MidiManager::AccumulateMidiBytesSent(MidiManagerClient* client, size_t n) {
153 base::AutoLock auto_lock(lock_); 157 base::AutoLock auto_lock(lock_);
154 if (clients_.find(client) == clients_.end()) 158 if (clients_.find(client) == clients_.end())
155 return; 159 return;
156 160
157 // Continue to hold lock_ here in case another thread is currently doing 161 // Continue to hold lock_ here in case another thread is currently doing
158 // EndSession. 162 // EndSession.
159 // Note that if we are in EndSession, then a destructor is being called and
160 // it isn't really safe to call this method. But we don't have another way to
161 // check this right now.
162 client->AccumulateMidiBytesSent(n); 163 client->AccumulateMidiBytesSent(n);
163 } 164 }
164 165
165 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, 166 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
166 uint32_t port_index, 167 uint32_t port_index,
167 const std::vector<uint8_t>& data, 168 const std::vector<uint8_t>& data,
168 double timestamp) { 169 double timestamp) {
169 NOTREACHED(); 170 NOTREACHED();
170 } 171 }
171 172
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 base::AutoLock auto_lock(lock_); 265 base::AutoLock auto_lock(lock_);
265 finalized_ = true; 266 finalized_ = true;
266 267
267 // Detach all clients so that they do not call MidiManager methods any more. 268 // Detach all clients so that they do not call MidiManager methods any more.
268 for (auto client : clients_) 269 for (auto client : clients_)
269 client->Detach(); 270 client->Detach();
270 } 271 }
271 272
272 } // namespace midi 273 } // namespace midi
273 } // namespace media 274 } // namespace media
OLDNEW
« no previous file with comments | « content/browser/media/midi_host.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698