From a688c89c25dd5c966b112cd5dd8ef084698d363e Mon Sep 17 00:00:00 2001
From: "epenner@chromium.org"
 <epenner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Tue, 21 May 2013 03:15:49 +0000
Subject: base: Support setting thread priorities generically.

This patch supports setting priorities across platforms
at the PlatformThread level, by stashing thread id into the
thread handle on linux/android.

Since this adds more platform specific code, and #ifdefs
were starting to get unwieldy, all platform specific code
is moved into _platform.cc files, with the exception of the
'default' implementation, which stay in _posix.

BUG=170549

Review URL: https://chromiumcodereview.appspot.com/12741012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@201202 0039d316-1c4b-4281-b951-d872f2087c98
---
 media/audio/audio_device_thread.cc            |  8 ++++----
 media/audio/cross_process_notification_win.cc | 10 ++++++----
 media/filters/video_renderer_base.cc          |  8 ++++----
 3 files changed, 14 insertions(+), 12 deletions(-)

(limited to 'media')

diff --git a/media/audio/audio_device_thread.cc b/media/audio/audio_device_thread.cc
index 7583c9e..f73ede8 100644
--- a/media/audio/audio_device_thread.cc
+++ b/media/audio/audio_device_thread.cc
@@ -101,18 +101,18 @@ AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback,
 }
 
 AudioDeviceThread::Thread::~Thread() {
-  DCHECK_EQ(thread_, base::kNullThreadHandle) << "Stop wasn't called";
+  DCHECK(thread_.is_null());
 }
 
 void AudioDeviceThread::Thread::Start() {
   base::AutoLock auto_lock(callback_lock_);
-  DCHECK_EQ(thread_, base::kNullThreadHandle);
+  DCHECK(thread_.is_null());
   // This reference will be released when the thread exists.
   AddRef();
 
   PlatformThread::CreateWithPriority(0, this, &thread_,
                                      base::kThreadPriority_RealtimeAudio);
-  CHECK(thread_ != base::kNullThreadHandle);
+  CHECK(!thread_.is_null());
 }
 
 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) {
@@ -126,7 +126,7 @@ void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) {
     std::swap(thread, thread_);
   }
 
-  if (thread != base::kNullThreadHandle) {
+  if (!thread.is_null()) {
     if (loop_for_join) {
       loop_for_join->PostTask(FROM_HERE,
           base::Bind(&base::PlatformThread::Join, thread));
diff --git a/media/audio/cross_process_notification_win.cc b/media/audio/cross_process_notification_win.cc
index 65e0e43..ec6a4aa 100644
--- a/media/audio/cross_process_notification_win.cc
+++ b/media/audio/cross_process_notification_win.cc
@@ -121,7 +121,9 @@ class ExtraWaitThread : public base::PlatformThread::Delegate {
           &events_[MAXIMUM_WAIT_OBJECTS - 2],
           count_ - (MAXIMUM_WAIT_OBJECTS - 2),
           &thread_signaled_event));
-      base::PlatformThread::Create(0, extra_wait_thread.get(), &next_thread);
+      base::PlatformThreadHandle handle = base::kNullThreadHandle;
+      base::PlatformThread::Create(0, extra_wait_thread.get(), &handle);
+      next_thread = handle.platform_handle();
 
       event_count = MAXIMUM_WAIT_OBJECTS;
       events[MAXIMUM_WAIT_OBJECTS - 1] = next_thread;
@@ -140,7 +142,7 @@ class ExtraWaitThread : public base::PlatformThread::Delegate {
         // so on), we must wait for ours to exit before we can check the
         // propagated event offset.
         if (next_thread) {
-          base::PlatformThread::Join(next_thread);
+          base::PlatformThread::Join(base::PlatformThreadHandle(next_thread));
           next_thread = NULL;
         }
         if (thread_signaled_event != -1)
@@ -156,7 +158,7 @@ class ExtraWaitThread : public base::PlatformThread::Delegate {
     }
 
     if (next_thread)
-      base::PlatformThread::Join(next_thread);
+      base::PlatformThread::Join(base::PlatformThreadHandle(next_thread));
   }
 
  private:
@@ -232,7 +234,7 @@ int CrossProcessNotification::WaitMultiple(const Notifications& notifications,
     base::PlatformThread::Create(0, &wait_thread, &thread);
     HANDLE events[MAXIMUM_WAIT_OBJECTS];
     std::copy(&handles[0], &handles[MAXIMUM_WAIT_OBJECTS - 1], &events[0]);
-    events[MAXIMUM_WAIT_OBJECTS - 1] = thread;
+    events[MAXIMUM_WAIT_OBJECTS - 1] = thread.platform_handle();
     wait = ::WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, &events[0], FALSE,
                                     INFINITE);
     wait_failed = wait < WAIT_OBJECT_0 ||
diff --git a/media/filters/video_renderer_base.cc b/media/filters/video_renderer_base.cc
index f79e99d..6d1d838 100644
--- a/media/filters/video_renderer_base.cc
+++ b/media/filters/video_renderer_base.cc
@@ -48,7 +48,7 @@ VideoRendererBase::VideoRendererBase(
 VideoRendererBase::~VideoRendererBase() {
   base::AutoLock auto_lock(lock_);
   CHECK(state_ == kStopped || state_ == kUninitialized) << state_;
-  CHECK_EQ(thread_, base::kNullThreadHandle);
+  CHECK(thread_.is_null());
 }
 
 void VideoRendererBase::Play(const base::Closure& callback) {
@@ -101,14 +101,14 @@ void VideoRendererBase::Stop(const base::Closure& callback) {
 
   // Clean up our thread if present.
   base::PlatformThreadHandle thread_to_join = base::kNullThreadHandle;
-  if (thread_ != base::kNullThreadHandle) {
+  if (!thread_.is_null()) {
     // Signal the thread since it's possible to get stopped with the video
     // thread waiting for a read to complete.
     frame_available_.Signal();
     std::swap(thread_, thread_to_join);
   }
 
-  if (thread_to_join != base::kNullThreadHandle) {
+  if (!thread_to_join.is_null()) {
     base::AutoUnlock auto_unlock(lock_);
     base::PlatformThread::Join(thread_to_join);
   }
@@ -212,7 +212,7 @@ void VideoRendererBase::OnVideoFrameStreamInitialized(bool success,
 #if defined(OS_WIN)
   // Bump up our priority so our sleeping is more accurate.
   // TODO(scherkus): find out if this is necessary, but it seems to help.
-  ::SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL);
+  ::SetThreadPriority(thread_.platform_handle(), THREAD_PRIORITY_ABOVE_NORMAL);
 #endif  // defined(OS_WIN)
   base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
 }
-- 
cgit v1.1