diff options
-rw-r--r-- | base/threading/platform_thread.h | 24 | ||||
-rw-r--r-- | base/threading/platform_thread_android.cc | 10 | ||||
-rw-r--r-- | base/threading/platform_thread_freebsd.cc | 10 | ||||
-rw-r--r-- | base/threading/platform_thread_internal_posix.h | 20 | ||||
-rw-r--r-- | base/threading/platform_thread_linux.cc | 12 | ||||
-rw-r--r-- | base/threading/platform_thread_mac.mm | 8 | ||||
-rw-r--r-- | base/threading/platform_thread_posix.cc | 38 | ||||
-rw-r--r-- | base/threading/platform_thread_unittest.cc | 193 | ||||
-rw-r--r-- | base/threading/platform_thread_win.cc | 21 | ||||
-rw-r--r-- | content/browser/browser_main_loop.cc | 3 | ||||
-rw-r--r-- | content/gpu/gpu_child_thread.cc | 3 | ||||
-rw-r--r-- | content/renderer/gpu/compositor_output_surface.cc | 7 |
12 files changed, 157 insertions, 192 deletions
diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h index 3468f45..0b92265 100644 --- a/base/threading/platform_thread.h +++ b/base/threading/platform_thread.h @@ -89,6 +89,7 @@ class PlatformThreadHandle { id_(id) { } + // TODO(toyoshim): Remove id() and use PlatformThread::CurrentId() instead. PlatformThreadId id() const { return id_; } @@ -112,8 +113,8 @@ class PlatformThreadHandle { const PlatformThreadId kInvalidThreadId(0); -// Valid values for SetThreadPriority(), listed in increasing order of -// importance. +// Valid values for priority of Thread::Options and SimpleThread::Options, and +// SetCurrentThreadPriority(), listed in increasing order of importance. enum class ThreadPriority { // Suitable for threads that shouldn't disrupt high priority work. BACKGROUND, @@ -176,8 +177,7 @@ class BASE_EXPORT PlatformThread { PlatformThreadHandle* thread_handle); // CreateWithPriority() does the same thing as Create() except the priority of - // the thread is set based on |priority|. Can be used in place of Create() - // followed by SetThreadPriority(). + // the thread is set based on |priority|. static bool CreateWithPriority(size_t stack_size, Delegate* delegate, PlatformThreadHandle* thread_handle, ThreadPriority priority); @@ -192,15 +192,15 @@ class BASE_EXPORT PlatformThread { // |thread_handle|. static void Join(PlatformThreadHandle thread_handle); - // Toggles the target thread's priority at runtime. Prefer - // CreateWithPriority() to set the thread's initial priority. - // NOTE: The call may fail if the caller thread is not the same as the - // target thread on POSIX. For example, seccomp-bpf blocks it by default - // in the sandbox. - static void SetThreadPriority(PlatformThreadHandle handle, - ThreadPriority priority); + // Toggles the current thread's priority at runtime. A thread may not be able + // to raise its priority back up after lowering it if the process does not + // have a proper permission, e.g. CAP_SYS_NICE on Linux. + // Since changing other threads' priority is not permitted in favor of + // security, this interface is restricted to change only the current thread + // priority (https://crbug.com/399473). + static void SetCurrentThreadPriority(ThreadPriority priority); - static ThreadPriority GetThreadPriority(PlatformThreadHandle handle); + static ThreadPriority GetCurrentThreadPriority(); private: DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread); diff --git a/base/threading/platform_thread_android.cc b/base/threading/platform_thread_android.cc index e661af5..176a6bd 100644 --- a/base/threading/platform_thread_android.cc +++ b/base/threading/platform_thread_android.cc @@ -37,8 +37,7 @@ const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { {ThreadPriority::REALTIME_AUDIO, -16}, }; -bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority priority) { +bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) { // On Android, we set the Audio priority through JNI as Audio priority // will also allow the process to run while it is backgrounded. if (priority == ThreadPriority::REALTIME_AUDIO) { @@ -49,8 +48,8 @@ bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, return false; } -bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority* priority) { +bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) { + // See http://crbug.com/505474. NOTIMPLEMENTED(); return false; } @@ -81,8 +80,7 @@ void InitThreading() { void InitOnThread() { // Threads on linux/android may inherit their priority from the thread // where they were created. This sets all new threads to the default. - PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), - ThreadPriority::NORMAL); + PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL); } void TerminateOnThread() { diff --git a/base/threading/platform_thread_freebsd.cc b/base/threading/platform_thread_freebsd.cc index f4fded0..e29e865 100644 --- a/base/threading/platform_thread_freebsd.cc +++ b/base/threading/platform_thread_freebsd.cc @@ -36,11 +36,8 @@ const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { {ThreadPriority::REALTIME_AUDIO, -10}, } -bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority priority) { +bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) { #if !defined(OS_NACL) - // TODO(gab): Assess the correctness of using |pthread_self()| below instead - // of |handle|. http://crbug.com/468793. return priority == ThreadPriority::REALTIME_AUDIO && pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0; #else @@ -48,11 +45,8 @@ bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, #endif } -bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority* priority) { +bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) { #if !defined(OS_NACL) - // TODO(gab): Assess the correctness of using |pthread_self()| below instead - // of |handle|. http://crbug.com/468793. int maybe_sched_rr = 0; struct sched_param maybe_realtime_prio = {0}; if (pthread_getschedparam(pthread_self(), &maybe_sched_rr, diff --git a/base/threading/platform_thread_internal_posix.h b/base/threading/platform_thread_internal_posix.h index 62006ce..05a8d1e 100644 --- a/base/threading/platform_thread_internal_posix.h +++ b/base/threading/platform_thread_internal_posix.h @@ -26,17 +26,15 @@ int ThreadPriorityToNiceValue(ThreadPriority priority); ThreadPriority NiceValueToThreadPriority(int nice_value); // Allows platform specific tweaks to the generic POSIX solution for -// SetThreadPriority. Returns true if the platform-specific implementation -// handled this |priority| change, false if the generic implementation should -// instead proceed. -bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority priority); - -// Returns true if there is a platform-specific ThreadPriority set on |handle| -// (and returns the actual ThreadPriority via |priority|). Returns false -// otherwise, leaving |priority| untouched. -bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority* priority); +// SetCurrentThreadPriority. Returns true if the platform-specific +// implementation handled this |priority| change, false if the generic +// implementation should instead proceed. +bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority); + +// Returns true if there is a platform-specific ThreadPriority set on the +// current thread (and returns the actual ThreadPriority via |priority|). +// Returns false otherwise, leaving |priority| untouched. +bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority); } // namespace internal diff --git a/base/threading/platform_thread_linux.cc b/base/threading/platform_thread_linux.cc index e0f620e..48cf744 100644 --- a/base/threading/platform_thread_linux.cc +++ b/base/threading/platform_thread_linux.cc @@ -38,12 +38,11 @@ const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { {ThreadPriority::REALTIME_AUDIO, -10}, }; -bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority priority) { +bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) { #if !defined(OS_NACL) ThreadPriority current_priority; if (priority != ThreadPriority::REALTIME_AUDIO && - GetThreadPriorityForPlatform(handle, ¤t_priority) && + GetCurrentThreadPriorityForPlatform(¤t_priority) && current_priority == ThreadPriority::REALTIME_AUDIO) { // If the pthread's round-robin scheduler is already enabled, and the new // priority will use setpriority() instead, the pthread scheduler should be @@ -51,8 +50,6 @@ bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, pthread_setschedparam(pthread_self(), SCHED_OTHER, &kResetPrio); return false; } - // TODO(gab): Assess the correctness of using |pthread_self()| below instead - // of |handle|. http://crbug.com/468793. return priority == ThreadPriority::REALTIME_AUDIO && pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0; #else @@ -60,13 +57,10 @@ bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, #endif } -bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, - ThreadPriority* priority) { +bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) { #if !defined(OS_NACL) int maybe_sched_rr = 0; struct sched_param maybe_realtime_prio = {0}; - // TODO(gab): Assess the correctness of using |pthread_self()| below instead - // of |handle|. http://crbug.com/468793. if (pthread_getschedparam(pthread_self(), &maybe_sched_rr, &maybe_realtime_prio) == 0 && maybe_sched_rr == SCHED_RR && diff --git a/base/threading/platform_thread_mac.mm b/base/threading/platform_thread_mac.mm index 813cae2..1ecbcd6 100644 --- a/base/threading/platform_thread_mac.mm +++ b/base/threading/platform_thread_mac.mm @@ -155,10 +155,10 @@ void SetPriorityRealtimeAudio(mach_port_t mach_thread_id) { } // anonymous namespace // static -void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, - ThreadPriority priority) { +void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { // Convert from pthread_t to mach thread identifier. - mach_port_t mach_thread_id = pthread_mach_thread_np(handle.platform_handle()); + mach_port_t mach_thread_id = + pthread_mach_thread_np(PlatformThread::CurrentHandle().platform_handle()); switch (priority) { case ThreadPriority::NORMAL: @@ -174,7 +174,7 @@ void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, } // static -ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { +ThreadPriority PlatformThread::GetCurrentThreadPriority() { NOTIMPLEMENTED(); return ThreadPriority::NORMAL; } diff --git a/base/threading/platform_thread_posix.cc b/base/threading/platform_thread_posix.cc index 0d821a9..f3a835e 100644 --- a/base/threading/platform_thread_posix.cc +++ b/base/threading/platform_thread_posix.cc @@ -58,10 +58,8 @@ void* ThreadFunc(void* params) { if (!thread_params->joinable) base::ThreadRestrictions::SetSingletonAllowed(false); - if (thread_params->priority != ThreadPriority::NORMAL) { - PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), - thread_params->priority); - } + if (thread_params->priority != ThreadPriority::NORMAL) + PlatformThread::SetCurrentThreadPriority(thread_params->priority); // Stash the id in the handle so the calling thread has a complete // handle, and unblock the parent thread. @@ -231,16 +229,15 @@ void PlatformThread::Join(PlatformThreadHandle thread_handle) { CHECK_EQ(0, pthread_join(thread_handle.platform_handle(), NULL)); } -// Mac has its own Set/GetThreadPriority() implementations. +// Mac has its own Set/GetCurrentThreadPriority() implementations. #if !defined(OS_MACOSX) // static -void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, - ThreadPriority priority) { +void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { #if defined(OS_NACL) NOTIMPLEMENTED(); #else - if (internal::SetThreadPriorityForPlatform(handle, priority)) + if (internal::SetCurrentThreadPriorityForPlatform(priority)) return; // setpriority(2) should change the whole thread group's (i.e. process) @@ -249,39 +246,34 @@ void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, // Linux/NPTL implementation of POSIX threads, the nice value is a per-thread // attribute". Also, 0 is prefered to the current thread id since it is // equivalent but makes sandboxing easier (https://crbug.com/399473). - DCHECK_NE(handle.id(), kInvalidThreadId); const int nice_setting = internal::ThreadPriorityToNiceValue(priority); - const PlatformThreadId current_id = PlatformThread::CurrentId(); - if (setpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id(), - nice_setting)) { - DVPLOG(1) << "Failed to set nice value of thread (" << handle.id() - << ") to " << nice_setting; + if (setpriority(PRIO_PROCESS, 0, nice_setting)) { + DVPLOG(1) << "Failed to set nice value of thread (" + << PlatformThread::CurrentId() << ") to " << nice_setting; } #endif // defined(OS_NACL) } // static -ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { +ThreadPriority PlatformThread::GetCurrentThreadPriority() { #if defined(OS_NACL) NOTIMPLEMENTED(); return ThreadPriority::NORMAL; #else - // Mirrors SetThreadPriority()'s implementation. + // Mirrors SetCurrentThreadPriority()'s implementation. ThreadPriority platform_specific_priority; - if (internal::GetThreadPriorityForPlatform(handle, - &platform_specific_priority)) { + if (internal::GetCurrentThreadPriorityForPlatform( + &platform_specific_priority)) { return platform_specific_priority; } - DCHECK_NE(handle.id(), kInvalidThreadId); - const PlatformThreadId current_id = PlatformThread::CurrentId(); // Need to clear errno before calling getpriority(): // http://man7.org/linux/man-pages/man2/getpriority.2.html errno = 0; - int nice_value = - getpriority(PRIO_PROCESS, handle.id() == current_id ? 0 : handle.id()); + int nice_value = getpriority(PRIO_PROCESS, 0); if (errno != 0) { - DVPLOG(1) << "Failed to get nice value of thread (" << handle.id() << ")"; + DVPLOG(1) << "Failed to get nice value of thread (" + << PlatformThread::CurrentId() << ")"; return ThreadPriority::NORMAL; } diff --git a/base/threading/platform_thread_unittest.cc b/base/threading/platform_thread_unittest.cc index c4b3d5d..1ac08a7 100644 --- a/base/threading/platform_thread_unittest.cc +++ b/base/threading/platform_thread_unittest.cc @@ -8,7 +8,10 @@ #include "base/threading/platform_thread.h" #include "testing/gtest/include/gtest/gtest.h" -#if defined(OS_WIN) +#if defined(OS_POSIX) +#include <sys/types.h> +#include <unistd.h> +#elif defined(OS_WIN) #include <windows.h> #endif @@ -16,6 +19,8 @@ namespace base { // Trivial tests that thread runs and doesn't crash on create and join --------- +namespace { + class TrivialThread : public PlatformThread::Delegate { public: TrivialThread() : did_run_(false) {} @@ -30,6 +35,8 @@ class TrivialThread : public PlatformThread::Delegate { DISALLOW_COPY_AND_ASSIGN(TrivialThread); }; +} // namespace + TEST(PlatformThreadTest, Trivial) { TrivialThread thread; PlatformThreadHandle handle; @@ -56,11 +63,13 @@ TEST(PlatformThreadTest, TrivialTimesTen) { // Tests of basic thread functions --------------------------------------------- +namespace { + class FunctionTestThread : public PlatformThread::Delegate { public: FunctionTestThread() : thread_id_(kInvalidThreadId), - thread_started_(true, false), + termination_ready_(true, false), terminate_thread_(true, false), done_(false) {} ~FunctionTestThread() override { @@ -70,8 +79,9 @@ class FunctionTestThread : public PlatformThread::Delegate { << "WaitableEvent blocking the underlying thread's main."; } - // Grabs |thread_id_|, signals |thread_started_|, and then waits for - // |terminate_thread_| to be signaled before exiting. + // Grabs |thread_id_|, runs an optional test on that thread, signals + // |termination_ready_|, and then waits for |terminate_thread_| to be + // signaled before exiting. void ThreadMain() override { thread_id_ = PlatformThread::CurrentId(); EXPECT_NE(thread_id_, kInvalidThreadId); @@ -79,39 +89,46 @@ class FunctionTestThread : public PlatformThread::Delegate { // Make sure that the thread ID is the same across calls. EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); - thread_started_.Signal(); + // Run extra tests. + RunTest(); + termination_ready_.Signal(); terminate_thread_.Wait(); done_ = true; } PlatformThreadId thread_id() const { - EXPECT_TRUE(thread_started_.IsSignaled()) << "Thread ID still unknown"; + EXPECT_TRUE(termination_ready_.IsSignaled()) << "Thread ID still unknown"; return thread_id_; } bool IsRunning() const { - return thread_started_.IsSignaled() && !done_; + return termination_ready_.IsSignaled() && !done_; } - // Blocks until this thread is started. - void WaitForThreadStart() { thread_started_.Wait(); } + // Blocks until this thread is started and ready to be terminated. + void WaitForTerminationReady() { termination_ready_.Wait(); } - // Mark this thread for termination (callers must then join this thread to be + // Marks this thread for termination (callers must then join this thread to be // guaranteed of termination). void MarkForTermination() { terminate_thread_.Signal(); } private: + // Runs an optional test on the newly created thread. + virtual void RunTest() {} + PlatformThreadId thread_id_; - mutable WaitableEvent thread_started_; + mutable WaitableEvent termination_ready_; WaitableEvent terminate_thread_; bool done_; DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); }; +} // namespace + TEST(PlatformThreadTest, Function) { PlatformThreadId main_thread_id = PlatformThread::CurrentId(); @@ -120,7 +137,7 @@ TEST(PlatformThreadTest, Function) { ASSERT_FALSE(thread.IsRunning()); ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); - thread.WaitForThreadStart(); + thread.WaitForTerminationReady(); ASSERT_TRUE(thread.IsRunning()); EXPECT_NE(thread.thread_id(), main_thread_id); @@ -144,7 +161,7 @@ TEST(PlatformThreadTest, FunctionTimesTen) { for (size_t n = 0; n < arraysize(thread); n++) ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); for (size_t n = 0; n < arraysize(thread); n++) - thread[n].WaitForThreadStart(); + thread[n].WaitForTerminationReady(); for (size_t n = 0; n < arraysize(thread); n++) { ASSERT_TRUE(thread[n].IsRunning()); @@ -170,106 +187,86 @@ TEST(PlatformThreadTest, FunctionTimesTen) { namespace { const ThreadPriority kThreadPriorityTestValues[] = { -// Disable non-normal priority toggling on POSIX as it appears to be broken -// (http://crbug.com/468793). This is prefered to disabling the tests altogether -// on POSIX as it at least provides coverage for running this code under -// "normal" priority. -#if !defined(OS_POSIX) - ThreadPriority::DISPLAY, +// The order should be higher to lower to cover as much cases as possible on +// Linux trybots running without CAP_SYS_NICE permission. +#if !defined(OS_ANDROID) + // PlatformThread::GetCurrentThreadPriority() on Android does not support + // REALTIME_AUDIO case. See http://crbug.com/505474. ThreadPriority::REALTIME_AUDIO, - // Keep BACKGROUND second to last to test backgrounding from other - // priorities. +#endif + ThreadPriority::DISPLAY, + // This redundant BACKGROUND priority is to test backgrounding from other + // priorities, and unbackgrounding. ThreadPriority::BACKGROUND, -#endif // !defined(OS_POSIX) - // Keep NORMAL last to test unbackgrounding. - ThreadPriority::NORMAL -}; - -} // namespace - -// Test changing another thread's priority. -// NOTE: This test is partially disabled on POSIX, see note above and -// http://crbug.com/468793. -TEST(PlatformThreadTest, ThreadPriorityOtherThread) { - PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); - - // Confirm that the current thread's priority is as expected. - EXPECT_EQ(ThreadPriority::NORMAL, - PlatformThread::GetThreadPriority(current_handle)); - - // Create a test thread. - FunctionTestThread thread; - PlatformThreadHandle handle; - ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); - thread.WaitForThreadStart(); - EXPECT_NE(thread.thread_id(), kInvalidThreadId); - EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); - - // New threads should get normal priority by default. - EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle)); - - // Toggle each supported priority on the test thread and confirm it only - // affects it (and not the current thread). - for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { - SCOPED_TRACE(i); + ThreadPriority::NORMAL, + ThreadPriority::BACKGROUND}; + +bool IsBumpingPriorityAllowed() { +#if defined(OS_POSIX) + // Only root can raise thread priority on POSIX environment. On Linux, users + // who have CAP_SYS_NICE permission also can raise the thread priority, but + // libcap.so would be needed to check the capability. + return geteuid() == 0; +#else + return true; +#endif +} - // Alter and verify the test thread's priority. - PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]); - EXPECT_EQ(kThreadPriorityTestValues[i], - PlatformThread::GetThreadPriority(handle)); +class ThreadPriorityTestThread : public FunctionTestThread { + public: + ThreadPriorityTestThread() = default; + ~ThreadPriorityTestThread() override = default; - // Make sure the current thread was otherwise unaffected. + private: + void RunTest() override { + // Confirm that the current thread's priority is as expected. EXPECT_EQ(ThreadPriority::NORMAL, - PlatformThread::GetThreadPriority(current_handle)); + PlatformThread::GetCurrentThreadPriority()); + + // Toggle each supported priority on the current thread and confirm it + // affects it. + const bool bumping_priority_allowed = IsBumpingPriorityAllowed(); + for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { + SCOPED_TRACE(i); + if (!bumping_priority_allowed && + kThreadPriorityTestValues[i] > + PlatformThread::GetCurrentThreadPriority()) { + continue; + } + + // Alter and verify the current thread's priority. + PlatformThread::SetCurrentThreadPriority(kThreadPriorityTestValues[i]); + EXPECT_EQ(kThreadPriorityTestValues[i], + PlatformThread::GetCurrentThreadPriority()); + } } - thread.MarkForTermination(); - PlatformThread::Join(handle); -} + DISALLOW_COPY_AND_ASSIGN(ThreadPriorityTestThread); +}; -// Test changing the current thread's priority (which has different semantics on -// some platforms). -// NOTE: This test is partially disabled on POSIX, see note above and -// http://crbug.com/468793. -TEST(PlatformThreadTest, ThreadPriorityCurrentThread) { - PlatformThreadHandle current_handle(PlatformThread::CurrentHandle()); +} // namespace - // Confirm that the current thread's priority is as expected. - EXPECT_EQ(ThreadPriority::NORMAL, - PlatformThread::GetThreadPriority(current_handle)); +#if defined(OS_MACOSX) +// PlatformThread::GetCurrentThreadPriority() is not implemented on OS X. +#define MAYBE_ThreadPriorityCurrentThread DISABLED_ThreadPriorityCurrentThread +#else +#define MAYBE_ThreadPriorityCurrentThread ThreadPriorityCurrentThread +#endif - // Create a test thread for verification purposes only. - FunctionTestThread thread; +// Test changing a created thread's priority (which has different semantics on +// some platforms). +TEST(PlatformThreadTest, MAYBE_ThreadPriorityCurrentThread) { + ThreadPriorityTestThread thread; PlatformThreadHandle handle; - ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); - thread.WaitForThreadStart(); - EXPECT_NE(thread.thread_id(), kInvalidThreadId); - EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId()); - - // Confirm that the new thread's priority is as expected. - EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle)); - - // Toggle each supported priority on the current thread and confirm it only - // affects it (and not the test thread). - for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) { - SCOPED_TRACE(i); - - // Alter and verify the current thread's priority. - PlatformThread::SetThreadPriority(current_handle, - kThreadPriorityTestValues[i]); - EXPECT_EQ(kThreadPriorityTestValues[i], - PlatformThread::GetThreadPriority(current_handle)); - - // Make sure the test thread was otherwise unaffected. - EXPECT_EQ(ThreadPriority::NORMAL, - PlatformThread::GetThreadPriority(handle)); - } - // Restore current thread priority for follow-up tests. - PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL); + ASSERT_FALSE(thread.IsRunning()); + ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); + thread.WaitForTerminationReady(); + ASSERT_TRUE(thread.IsRunning()); thread.MarkForTermination(); PlatformThread::Join(handle); + ASSERT_FALSE(thread.IsRunning()); } } // namespace base diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc index c2eab6c..059547b 100644 --- a/base/threading/platform_thread_win.cc +++ b/base/threading/platform_thread_win.cc @@ -55,10 +55,8 @@ DWORD __stdcall ThreadFunc(void* params) { if (!thread_params->joinable) base::ThreadRestrictions::SetSingletonAllowed(false); - if (thread_params->priority != ThreadPriority::NORMAL) { - PlatformThread::SetThreadPriority( - PlatformThread::CurrentHandle(), thread_params->priority); - } + if (thread_params->priority != ThreadPriority::NORMAL) + PlatformThread::SetCurrentThreadPriority(thread_params->priority); // Retrieve a copy of the thread handle to use as the key in the // thread name mapping. @@ -238,10 +236,7 @@ void PlatformThread::Join(PlatformThreadHandle thread_handle) { } // static -void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, - ThreadPriority priority) { - DCHECK(!handle.is_null()); - +void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) { int desired_priority = THREAD_PRIORITY_ERROR_RETURN; switch (priority) { case ThreadPriority::BACKGROUND: @@ -265,16 +260,16 @@ void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, #ifndef NDEBUG const BOOL success = #endif - ::SetThreadPriority(handle.platform_handle(), desired_priority); + ::SetThreadPriority(PlatformThread::CurrentHandle().platform_handle(), + desired_priority); DPLOG_IF(ERROR, !success) << "Failed to set thread priority to " << desired_priority; } // static -ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { - DCHECK(!handle.is_null()); - - int priority = ::GetThreadPriority(handle.platform_handle()); +ThreadPriority PlatformThread::GetCurrentThreadPriority() { + int priority = + ::GetThreadPriority(PlatformThread::CurrentHandle().platform_handle()); switch (priority) { case THREAD_PRIORITY_LOWEST: return ThreadPriority::BACKGROUND; diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc index b0fa2c0..203400e 100644 --- a/content/browser/browser_main_loop.cc +++ b/content/browser/browser_main_loop.cc @@ -1108,8 +1108,7 @@ int BrowserMainLoop::BrowserThreadsStarted() { #if defined(OS_ANDROID) // Up the priority of the UI thread. - base::PlatformThread::SetThreadPriority(base::PlatformThread::CurrentHandle(), - base::ThreadPriority::DISPLAY); + base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::DISPLAY); #endif bool always_uses_gpu = true; diff --git a/content/gpu/gpu_child_thread.cc b/content/gpu/gpu_child_thread.cc index 7d7ffe8..dc399e0 100644 --- a/content/gpu/gpu_child_thread.cc +++ b/content/gpu/gpu_child_thread.cc @@ -230,8 +230,7 @@ void GpuChildThread::OnInitialize() { } #if defined(OS_ANDROID) - base::PlatformThread::SetThreadPriority(base::PlatformThread::CurrentHandle(), - base::ThreadPriority::DISPLAY); + base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::DISPLAY); #endif // We don't need to pipe log messages if we are running the GPU thread in diff --git a/content/renderer/gpu/compositor_output_surface.cc b/content/renderer/gpu/compositor_output_surface.cc index 76998d6..d1ad1cc2 100644 --- a/content/renderer/gpu/compositor_output_surface.cc +++ b/content/renderer/gpu/compositor_output_surface.cc @@ -207,12 +207,11 @@ bool CompositorOutputSurface::Send(IPC::Message* message) { #if defined(OS_ANDROID) namespace { void SetThreadPriorityToIdle() { - base::PlatformThread::SetThreadPriority(base::PlatformThread::CurrentHandle(), - base::ThreadPriority::BACKGROUND); + base::PlatformThread::SetCurrentThreadPriority( + base::ThreadPriority::BACKGROUND); } void SetThreadPriorityToDefault() { - base::PlatformThread::SetThreadPriority(base::PlatformThread::CurrentHandle(), - base::ThreadPriority::NORMAL); + base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::NORMAL); } } // namespace #endif |