Index: media/audio/audio_device_thread.cc |
diff --git a/media/audio/audio_device_thread.cc b/media/audio/audio_device_thread.cc |
index 20874599e68f5f7cfddeff0022291e64aa718b6c..fd45545edd7580b2140eaae40bb0d4d4a0320055 100644 |
--- a/media/audio/audio_device_thread.cc |
+++ b/media/audio/audio_device_thread.cc |
@@ -16,6 +16,8 @@ |
#include "base/memory/aligned_memory.h" |
#include "base/message_loop/message_loop.h" |
#include "base/numerics/safe_conversions.h" |
+#include "base/synchronization/lock.h" |
+#include "base/synchronization/waitable_event.h" |
#include "base/threading/platform_thread.h" |
#include "base/threading/thread_restrictions.h" |
#include "media/base/audio_bus.h" |
@@ -45,6 +47,9 @@ class AudioDeviceThread::Thread |
// synchronously for the thread to terminate. |
void Stop(base::MessageLoop* loop_for_join); |
+ // Returns true if called on the device thread, otherwise false. |
+ bool BelongsToRenderingThread(); |
+ |
private: |
friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; |
~Thread() override; |
@@ -63,6 +68,10 @@ class AudioDeviceThread::Thread |
const char* thread_name_; |
const bool synchronized_buffers_; |
+ // The thread's id once it has started. |
+ base::PlatformThreadId id_; |
tommi (sloooow) - chröme
2016/06/13 14:15:21
thread_id_
Henrik Grunell
2016/06/14 11:30:01
Removed.
|
+ mutable base::WaitableEvent id_event_; // Protects |id_|. |
tommi (sloooow) - chröme
2016/06/13 14:15:21
how does the event protect the thread id variable?
Henrik Grunell
2016/06/13 20:38:35
I'll clarify that. (It's set once only, and when d
Henrik Grunell
2016/06/14 11:30:01
Removed.
|
+ |
DISALLOW_COPY_AND_ASSIGN(Thread); |
}; |
@@ -97,6 +106,11 @@ bool AudioDeviceThread::IsStopped() { |
return !thread_.get(); |
} |
+bool AudioDeviceThread::BelongsToRenderingThread() { |
+ base::AutoLock auto_lock(thread_lock_); |
tommi (sloooow) - chröme
2016/06/13 14:15:21
It seems unfortunate to require a lock for this...
Henrik Grunell
2016/06/14 11:30:01
This has been removed altogether.
|
+ return thread_.get() ? thread_->BelongsToRenderingThread() : false; |
tommi (sloooow) - chröme
2016/06/13 14:15:21
nit:
return thread_ && thread_->CurrentThreadIsRe
Henrik Grunell
2016/06/14 11:30:01
Removed.
|
+} |
+ |
// AudioDeviceThread::Thread implementation |
AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, |
base::SyncSocket::Handle socket, |
@@ -106,8 +120,10 @@ AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, |
callback_(callback), |
socket_(socket), |
thread_name_(thread_name), |
- synchronized_buffers_(synchronized_buffers) { |
-} |
+ synchronized_buffers_(synchronized_buffers), |
+ id_(base::kInvalidThreadId), |
+ id_event_(base::WaitableEvent::ResetPolicy::MANUAL, |
+ base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
AudioDeviceThread::Thread::~Thread() { |
DCHECK(thread_.is_null()); |
@@ -145,7 +161,18 @@ void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { |
} |
} |
+bool AudioDeviceThread::Thread::BelongsToRenderingThread() { |
+ // If the thread is created but not started yet, wait for |id_| being ready. |
+ base::ThreadRestrictions::ScopedAllowWait allow_wait; |
+ id_event_.Wait(); |
tommi (sloooow) - chröme
2016/06/13 14:15:21
would be great to not require an event object for
Henrik Grunell
2016/06/13 20:38:35
So I shall.
Henrik Grunell
2016/06/14 11:30:01
This has been removed.
|
+ return id_ == PlatformThread::CurrentId(); |
+} |
+ |
void AudioDeviceThread::Thread::ThreadMain() { |
+ id_ = PlatformThread::CurrentId(); |
+ DCHECK_NE(base::kInvalidThreadId, id_); |
+ id_event_.Signal(); |
+ |
PlatformThread::SetName(thread_name_); |
// Singleton access is safe from this thread as long as callback is non-NULL. |