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

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

Issue 2038053002: Change audio render thread checking to use new AudioRendererSink::BelongsToRendererThread() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Using the new BelongsTo...() function for thread checking. Created 4 years, 6 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
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 #include "media/audio/audio_device_thread.h" 5 #include "media/audio/audio_device_thread.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <limits> 11 #include <limits>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/aligned_memory.h" 16 #include "base/memory/aligned_memory.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
19 #include "base/synchronization/lock.h"
19 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
20 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
21 #include "media/base/audio_bus.h" 22 #include "media/base/audio_bus.h"
22 23
23 using base::PlatformThread; 24 using base::PlatformThread;
24 25
25 namespace media { 26 namespace media {
26 27
27 // The actual worker thread implementation. It's very bare bones and much 28 // The actual worker thread implementation. It's very bare bones and much
28 // simpler than SimpleThread (no synchronization in Start, etc) and supports 29 // simpler than SimpleThread (no synchronization in Start, etc) and supports
29 // joining the thread handle asynchronously via a provided message loop even 30 // joining the thread handle asynchronously via a provided message loop even
30 // after the Thread object itself has been deleted. 31 // after the Thread object itself has been deleted.
31 class AudioDeviceThread::Thread 32 class AudioDeviceThread::Thread
32 : public PlatformThread::Delegate, 33 : public PlatformThread::Delegate,
33 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> { 34 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> {
34 public: 35 public:
35 Thread(AudioDeviceThread::Callback* callback, 36 Thread(AudioDeviceThread::Callback* callback,
36 base::SyncSocket::Handle socket, 37 base::SyncSocket::Handle socket,
37 const char* thread_name, 38 const char* thread_name,
38 bool synchronized_buffers); 39 bool synchronized_buffers);
39 40
40 void Start(); 41 void Start();
41 42
42 // Stops the thread. If |loop_for_join| is non-NULL, the function posts 43 // Stops the thread. If |loop_for_join| is non-NULL, the function posts
43 // a task to join (close) the thread handle later instead of waiting for 44 // a task to join (close) the thread handle later instead of waiting for
44 // the thread. If loop_for_join is NULL, then the function waits 45 // the thread. If loop_for_join is NULL, then the function waits
45 // synchronously for the thread to terminate. 46 // synchronously for the thread to terminate.
46 void Stop(base::MessageLoop* loop_for_join); 47 void Stop(base::MessageLoop* loop_for_join);
47 48
49 // Returns true if called on the device thread, otherwise false.
50 bool BelongsToRenderingThread();
51
48 private: 52 private:
49 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; 53 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>;
50 ~Thread() override; 54 ~Thread() override;
51 55
52 // Overrides from PlatformThread::Delegate. 56 // Overrides from PlatformThread::Delegate.
53 void ThreadMain() override; 57 void ThreadMain() override;
54 58
55 // Runs the loop that reads from the socket. 59 // Runs the loop that reads from the socket.
56 void Run(); 60 void Run();
57 61
58 private: 62 private:
59 base::PlatformThreadHandle thread_; 63 base::PlatformThreadHandle thread_;
60 AudioDeviceThread::Callback* callback_; 64 AudioDeviceThread::Callback* callback_;
61 base::CancelableSyncSocket socket_; 65 base::CancelableSyncSocket socket_;
62 base::Lock callback_lock_; 66 base::Lock callback_lock_;
63 const char* thread_name_; 67 const char* thread_name_;
64 const bool synchronized_buffers_; 68 const bool synchronized_buffers_;
65 69
70 #if DCHECK_IS_ON()
71 base::PlatformThreadRef device_thread_id_;
72 base::Lock device_thread_id_lock_;
73 #endif
74
66 DISALLOW_COPY_AND_ASSIGN(Thread); 75 DISALLOW_COPY_AND_ASSIGN(Thread);
67 }; 76 };
68 77
69 // AudioDeviceThread implementation 78 // AudioDeviceThread implementation
70 79
71 AudioDeviceThread::AudioDeviceThread() { 80 AudioDeviceThread::AudioDeviceThread() {
72 } 81 }
73 82
74 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); } 83 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); }
75 84
(...skipping 14 matching lines...) Expand all
90 thread_->Stop(loop_for_join); 99 thread_->Stop(loop_for_join);
91 thread_ = NULL; 100 thread_ = NULL;
92 } 101 }
93 } 102 }
94 103
95 bool AudioDeviceThread::IsStopped() { 104 bool AudioDeviceThread::IsStopped() {
96 base::AutoLock auto_lock(thread_lock_); 105 base::AutoLock auto_lock(thread_lock_);
97 return !thread_.get(); 106 return !thread_.get();
98 } 107 }
99 108
109 bool AudioDeviceThread::BelongsToRenderingThread() {
DaleCurtis 2016/06/10 18:24:49 Instead of hopping through two locks, it seems lik
Henrik Grunell 2016/06/13 12:46:15 base::Thread corresponds to ADT::Thread. We can re
110 base::AutoLock auto_lock(thread_lock_);
111 return thread_.get() ? thread_->BelongsToRenderingThread() : false;
112 }
113
100 // AudioDeviceThread::Thread implementation 114 // AudioDeviceThread::Thread implementation
101 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, 115 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback,
102 base::SyncSocket::Handle socket, 116 base::SyncSocket::Handle socket,
103 const char* thread_name, 117 const char* thread_name,
104 bool synchronized_buffers) 118 bool synchronized_buffers)
105 : thread_(), 119 : thread_(),
106 callback_(callback), 120 callback_(callback),
107 socket_(socket), 121 socket_(socket),
108 thread_name_(thread_name), 122 thread_name_(thread_name),
109 synchronized_buffers_(synchronized_buffers) { 123 synchronized_buffers_(synchronized_buffers) {
(...skipping 28 matching lines...) Expand all
138 if (!thread.is_null()) { 152 if (!thread.is_null()) {
139 if (loop_for_join) { 153 if (loop_for_join) {
140 loop_for_join->PostTask(FROM_HERE, 154 loop_for_join->PostTask(FROM_HERE,
141 base::Bind(&base::PlatformThread::Join, thread)); 155 base::Bind(&base::PlatformThread::Join, thread));
142 } else { 156 } else {
143 base::PlatformThread::Join(thread); 157 base::PlatformThread::Join(thread);
144 } 158 }
145 } 159 }
146 } 160 }
147 161
162 bool AudioDeviceThread::Thread::BelongsToRenderingThread() {
163 #if DCHECK_IS_ON()
DaleCurtis 2016/06/10 18:24:49 Only ThreadCheckers disable for DCHECK, typically
Henrik Grunell 2016/06/13 12:46:15 OK, that's fine I think. Done.
164 base::AutoLock auto_lock(device_thread_id_lock_);
165 return device_thread_id_ == PlatformThread::CurrentRef();
166 #else
167 return true;
168 #endif
169 }
170
148 void AudioDeviceThread::Thread::ThreadMain() { 171 void AudioDeviceThread::Thread::ThreadMain() {
172 #if DCHECK_IS_ON()
173 base::AutoLock auto_lock(device_thread_id_lock_);
174 device_thread_id_ = PlatformThread::CurrentRef();
175 #endif
176
149 PlatformThread::SetName(thread_name_); 177 PlatformThread::SetName(thread_name_);
150 178
151 // Singleton access is safe from this thread as long as callback is non-NULL. 179 // Singleton access is safe from this thread as long as callback is non-NULL.
152 // The callback is the only point where the thread calls out to 'unknown' code 180 // The callback is the only point where the thread calls out to 'unknown' code
153 // that might touch singletons and the lifetime of the callback is controlled 181 // that might touch singletons and the lifetime of the callback is controlled
154 // by another thread on which singleton access is OK as well. 182 // by another thread on which singleton access is OK as well.
155 base::ThreadRestrictions::SetSingletonAllowed(true); 183 base::ThreadRestrictions::SetSingletonAllowed(true);
156 184
157 { // NOLINT 185 { // NOLINT
158 base::AutoLock auto_lock(callback_lock_); 186 base::AutoLock auto_lock(callback_lock_);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 } 261 }
234 262
235 AudioDeviceThread::Callback::~Callback() {} 263 AudioDeviceThread::Callback::~Callback() {}
236 264
237 void AudioDeviceThread::Callback::InitializeOnAudioThread() { 265 void AudioDeviceThread::Callback::InitializeOnAudioThread() {
238 MapSharedMemory(); 266 MapSharedMemory();
239 CHECK(shared_memory_.memory()); 267 CHECK(shared_memory_.memory());
240 } 268 }
241 269
242 } // namespace media. 270 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698