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

Unified Diff: base/threading/platform_thread_unittest.cc

Issue 1006933003: Add full SetThreadPriority support to Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: base/threading/platform_thread_unittest.cc
diff --git a/base/threading/platform_thread_unittest.cc b/base/threading/platform_thread_unittest.cc
index 21260e5ec36bd27dcdc9bf1500167e32fbab2524..0a4dcb95002cc9c4c83b78a0c02a415e5137236d 100644
--- a/base/threading/platform_thread_unittest.cc
+++ b/base/threading/platform_thread_unittest.cc
@@ -3,10 +3,14 @@
// found in the LICENSE file.
#include "base/compiler_specific.h"
+#include "base/macros.h"
#include "base/threading/platform_thread.h"
-
#include "testing/gtest/include/gtest/gtest.h"
+#if defined(OS_WIN)
+#include <windows.h>
+#endif
+
namespace base {
// Trivial tests that thread runs and doesn't crash on create and join ---------
@@ -116,4 +120,121 @@ TEST(PlatformThreadTest, FunctionTimesTen) {
EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
}
+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)
+ kThreadPriority_RealtimeAudio,
+ kThreadPriority_Display,
+ kThreadPriority_Background,
+#endif // !defined(OS_POSIX)
+ // Keep normal last to test unbackgrounding.
+ kThreadPriority_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(kThreadPriority_Normal,
+ PlatformThread::GetThreadPriority(current_handle));
+
+ // Create a test thread.
+ FunctionTestThread thread;
+ PlatformThreadHandle handle;
+ ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
+ EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
+
+ // New threads should get normal priority by default.
+ EXPECT_EQ(kThreadPriority_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);
+
+ // Alter and verify the test thread's priority.
+ PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]);
+ EXPECT_EQ(kThreadPriorityTestValues[i],
+ PlatformThread::GetThreadPriority(handle));
+
+ // Make sure the current thread was otherwise unaffected.
+ EXPECT_EQ(kThreadPriority_Normal,
+ PlatformThread::GetThreadPriority(current_handle));
+ }
+}
+
+// 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());
+
+ // Confirm that the current thread's priority is as expected.
+ EXPECT_EQ(kThreadPriority_Normal,
+ PlatformThread::GetThreadPriority(current_handle));
+
+ // Create a test thread for verification purposes only.
+ FunctionTestThread thread;
+ PlatformThreadHandle handle;
+ ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
+ EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
+
+ // Confirm that the new thread's priority is as expected.
+ EXPECT_EQ(kThreadPriority_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);
+
+ // 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(kThreadPriority_Normal,
+ PlatformThread::GetThreadPriority(handle));
+ }
+
+ // Restore current thread priority for follow-up tests.
+ PlatformThread::SetThreadPriority(current_handle, kThreadPriority_Normal);
+}
+
+#if defined(OS_WIN)
+// Explicitly test Windows' advanced background mode from current thread.
+TEST(PlatformThreadTest, BackgroundModeCurrentThread) {
+ PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
+
+ // Confirm that the current thread's priority is as expected.
+ EXPECT_EQ(kThreadPriority_Normal,
+ PlatformThread::GetThreadPriority(current_handle));
+
+ // Confirm that explictly exiting background mode while not in it fails...
+ EXPECT_FALSE(::SetThreadPriority(current_handle.platform_handle(),
+ THREAD_MODE_BACKGROUND_END));
+ // ... and results in the documented error.
+ EXPECT_EQ(ERROR_THREAD_MODE_NOT_BACKGROUND,
+ static_cast<int>(::GetLastError()));
+
+ PlatformThread::SetThreadPriority(current_handle, kThreadPriority_Background);
+ // Confirm that the current thread was in Windows' advanced background mode by
+ // successfully explictly exiting it.
+ EXPECT_TRUE(::SetThreadPriority(current_handle.platform_handle(),
+ THREAD_MODE_BACKGROUND_END));
+}
+#endif // OS_WIN
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698