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

Unified Diff: base/threading/platform_thread_win.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_win.cc
diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc
index 3df371943f5e6fdf1a91175802b7fb36f78d9aba..3759b705042305280a6679e9941f6ef0a48fca4e 100644
--- a/base/threading/platform_thread_win.cc
+++ b/base/threading/platform_thread_win.cc
@@ -126,18 +126,17 @@ bool CreateThreadInternal(size_t stack_size,
// static
PlatformThreadId PlatformThread::CurrentId() {
- return GetCurrentThreadId();
+ return ::GetCurrentThreadId();
}
// static
PlatformThreadRef PlatformThread::CurrentRef() {
- return PlatformThreadRef(GetCurrentThreadId());
+ return PlatformThreadRef(::GetCurrentThreadId());
}
// static
PlatformThreadHandle PlatformThread::CurrentHandle() {
- NOTIMPLEMENTED(); // See OpenThread()
- return PlatformThreadHandle();
+ return PlatformThreadHandle(::GetCurrentThread());
rvargas (doing something else) 2015/03/19 22:19:21 Sounds like it will leak.
gab 2015/03/30 20:14:45 ::GetCurrentThread() returns a pseudo-handle which
rvargas (doing something else) 2015/03/30 22:31:23 Yes, but now you have a valid PlatformThreadHandle
gab 2015/03/31 14:02:33 Right, actually adjusted the API comment on Platf
rvargas (doing something else) 2015/03/31 19:27:09 I'm not really sure what's the right hing to do no
gab 2015/03/31 20:18:20 I'm not convinced that it not applying to existing
rvargas (doing something else) 2015/03/31 22:43:44 I agree that the current situation is not good bec
gab 2015/04/01 01:14:39 How about we make a single call that returns an ob
rvargas (doing something else) 2015/04/01 02:04:55 How does this play in the context of crbug.com/468
gab 2015/04/01 14:46:50 Non-thread-safe objects yes, but not implementers
rvargas (doing something else) 2015/04/01 16:31:30 How about this: if PlatformThread::CurrentHandle i
gab 2015/04/01 18:12:19 Okay, I will work on this next, expect a CL soon.
}
// static
@@ -234,17 +233,85 @@ void PlatformThread::Join(PlatformThreadHandle thread_handle) {
// static
void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
ThreadPriority priority) {
+ DCHECK(!handle.is_null());
+
+ if (base::win::GetVersion() >= base::win::VERSION_VISTA &&
+ handle.is_equal(CurrentHandle()) &&
+ priority != kThreadPriority_Background) {
+ // Make sure to end any active background mode.
+ bool unset =
rvargas (doing something else) 2015/03/19 22:19:21 BOOL?
rvargas (doing something else) 2015/03/19 22:19:21 BOOL?
gab 2015/03/30 20:14:45 Done.
gab 2015/03/30 20:14:45 Done.
+ !!::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_END);
rvargas (doing something else) 2015/03/19 22:19:21 ... I really hate !! to convert BOOL to bool
gab 2015/03/30 20:14:45 Done.
+ DPCHECK(unset ||
+ ERROR_THREAD_MODE_NOT_BACKGROUND ==
+ static_cast<int>(::GetLastError()));
+ }
+
+ bool success = false;
switch (priority) {
case kThreadPriority_Normal:
- ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_NORMAL);
+ success = !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_NORMAL);
break;
case kThreadPriority_RealtimeAudio:
- ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_TIME_CRITICAL);
+ success =
+ !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_TIME_CRITICAL);
+ break;
+ case kThreadPriority_Display:
+ success =
+ !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_ABOVE_NORMAL);
+ break;
+ case kThreadPriority_Background:
+ if (base::win::GetVersion() >= base::win::VERSION_VISTA &&
+ handle.is_equal(CurrentHandle())) {
+ // Full background mode (low IO/memory on top of low CPU priority) can
+ // only be set from the current thread.
+ success =
rvargas (doing something else) 2015/03/19 22:19:21 This is problematic as this is a non-reentrant pro
gab 2015/03/30 20:14:45 Good point, fixed.
+ !!::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_BEGIN);
+ } else {
+ success = !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_LOWEST);
gab 2015/03/19 18:06:07 One question, do you think we should even allow ba
rvargas (doing something else) 2015/03/19 22:19:21 We should either allow any priority to be set from
gab 2015/03/30 20:14:45 Ok, for now going with the simple change to get th
+ }
break;
default:
NOTREACHED() << "Unknown priority.";
break;
}
+ DPCHECK(success);
rvargas (doing something else) 2015/03/19 22:19:21 Should not check on a OS function return (and else
gab 2015/03/30 20:14:45 To me the goal of a debug check is to find out if
rvargas (doing something else) 2015/03/30 22:31:22 A DCHECK is by definition some invariant of the co
gab 2015/03/31 14:02:33 Okay changed to use DPLOG_IF(ERROR, !success)
gab 2015/03/31 18:35:25 But FWIW, I'm more in favor of explicitly checking
rvargas (doing something else) 2015/03/31 19:27:09 Well played, sir. I should have removed that one.
+}
+
+// static
+ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) {
+ DCHECK(!handle.is_null());
+
+ if (base::win::GetVersion() >= base::win::VERSION_VISTA &&
+ handle.is_equal(CurrentHandle())) {
+ // The only way to test for background mode is to explicitly try to exit it
rvargas (doing something else) 2015/03/19 22:19:21 Why not always set the priority to lowest (any os)
gab 2015/03/30 20:14:46 Well because we desire to use true background mode
rvargas (doing something else) 2015/03/30 22:31:23 That's not what I meant. What I meant was that we
gab 2015/03/31 14:02:33 Ah I see, still think it will be easier to do back
rvargas (doing something else) 2015/03/31 19:27:09 Acknowledged.
+ // (success meaning it was enabled)...
+ if (::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_END)) {
+ ::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_BEGIN);
+ return kThreadPriority_Background;
+ } else {
+ // The only expected failure condition to the above test is not being in
+ // background mode in the first place.
+ DCHECK_EQ(ERROR_THREAD_MODE_NOT_BACKGROUND,
+ static_cast<int>(::GetLastError()));
+ }
+ }
+
+ int priority = ::GetThreadPriority(handle.handle_);
+ switch (priority) {
+ case THREAD_PRIORITY_NORMAL:
+ return kThreadPriority_Normal;
+ case THREAD_PRIORITY_TIME_CRITICAL:
+ return kThreadPriority_RealtimeAudio;
+ case THREAD_PRIORITY_ABOVE_NORMAL:
+ return kThreadPriority_Display;
+ case THREAD_PRIORITY_LOWEST:
+ return kThreadPriority_Background;
+ case THREAD_PRIORITY_ERROR_RETURN:
+ DPCHECK(false) << "GetThreadPriority error"; // Falls through.
+ default:
+ NOTREACHED() << "Unexpected priority: " << priority;
+ return kThreadPriority_Normal;
+ }
}
} // namespace base
« base/threading/platform_thread_posix.cc ('K') | « base/threading/platform_thread_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698