OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
6 | 6 |
7 #include "base/debug/alias.h" | 7 #include "base/debug/alias.h" |
8 #include "base/debug/profiler.h" | 8 #include "base/debug/profiler.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/thread_id_name_manager.h" | 10 #include "base/threading/thread_id_name_manager.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 *out_thread_handle = PlatformThreadHandle(thread_handle); | 119 *out_thread_handle = PlatformThreadHandle(thread_handle); |
120 else | 120 else |
121 CloseHandle(thread_handle); | 121 CloseHandle(thread_handle); |
122 return true; | 122 return true; |
123 } | 123 } |
124 | 124 |
125 } // namespace | 125 } // namespace |
126 | 126 |
127 // static | 127 // static |
128 PlatformThreadId PlatformThread::CurrentId() { | 128 PlatformThreadId PlatformThread::CurrentId() { |
129 return GetCurrentThreadId(); | 129 return ::GetCurrentThreadId(); |
130 } | 130 } |
131 | 131 |
132 // static | 132 // static |
133 PlatformThreadRef PlatformThread::CurrentRef() { | 133 PlatformThreadRef PlatformThread::CurrentRef() { |
134 return PlatformThreadRef(GetCurrentThreadId()); | 134 return PlatformThreadRef(::GetCurrentThreadId()); |
135 } | 135 } |
136 | 136 |
137 // static | 137 // static |
138 PlatformThreadHandle PlatformThread::CurrentHandle() { | 138 PlatformThreadHandle PlatformThread::CurrentHandle() { |
139 NOTIMPLEMENTED(); // See OpenThread() | 139 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.
| |
140 return PlatformThreadHandle(); | |
141 } | 140 } |
142 | 141 |
143 // static | 142 // static |
144 void PlatformThread::YieldCurrentThread() { | 143 void PlatformThread::YieldCurrentThread() { |
145 ::Sleep(0); | 144 ::Sleep(0); |
146 } | 145 } |
147 | 146 |
148 // static | 147 // static |
149 void PlatformThread::Sleep(TimeDelta duration) { | 148 void PlatformThread::Sleep(TimeDelta duration) { |
150 // When measured with a high resolution clock, Sleep() sometimes returns much | 149 // When measured with a high resolution clock, Sleep() sometimes returns much |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 debug::Alias(&thread_handle.handle_); | 226 debug::Alias(&thread_handle.handle_); |
228 CHECK(false); | 227 CHECK(false); |
229 } | 228 } |
230 | 229 |
231 CloseHandle(thread_handle.handle_); | 230 CloseHandle(thread_handle.handle_); |
232 } | 231 } |
233 | 232 |
234 // static | 233 // static |
235 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 234 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
236 ThreadPriority priority) { | 235 ThreadPriority priority) { |
236 DCHECK(!handle.is_null()); | |
237 | |
238 if (base::win::GetVersion() >= base::win::VERSION_VISTA && | |
239 handle.is_equal(CurrentHandle()) && | |
240 priority != kThreadPriority_Background) { | |
241 // Make sure to end any active background mode. | |
242 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.
| |
243 !!::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.
| |
244 DPCHECK(unset || | |
245 ERROR_THREAD_MODE_NOT_BACKGROUND == | |
246 static_cast<int>(::GetLastError())); | |
247 } | |
248 | |
249 bool success = false; | |
237 switch (priority) { | 250 switch (priority) { |
238 case kThreadPriority_Normal: | 251 case kThreadPriority_Normal: |
239 ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_NORMAL); | 252 success = !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_NORMAL); |
240 break; | 253 break; |
241 case kThreadPriority_RealtimeAudio: | 254 case kThreadPriority_RealtimeAudio: |
242 ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_TIME_CRITICAL); | 255 success = |
256 !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_TIME_CRITICAL); | |
257 break; | |
258 case kThreadPriority_Display: | |
259 success = | |
260 !!::SetThreadPriority(handle.handle_, THREAD_PRIORITY_ABOVE_NORMAL); | |
261 break; | |
262 case kThreadPriority_Background: | |
263 if (base::win::GetVersion() >= base::win::VERSION_VISTA && | |
264 handle.is_equal(CurrentHandle())) { | |
265 // Full background mode (low IO/memory on top of low CPU priority) can | |
266 // only be set from the current thread. | |
267 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.
| |
268 !!::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_BEGIN); | |
269 } else { | |
270 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
| |
271 } | |
243 break; | 272 break; |
244 default: | 273 default: |
245 NOTREACHED() << "Unknown priority."; | 274 NOTREACHED() << "Unknown priority."; |
246 break; | 275 break; |
247 } | 276 } |
277 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.
| |
278 } | |
279 | |
280 // static | |
281 ThreadPriority PlatformThread::GetThreadPriority(PlatformThreadHandle handle) { | |
282 DCHECK(!handle.is_null()); | |
283 | |
284 if (base::win::GetVersion() >= base::win::VERSION_VISTA && | |
285 handle.is_equal(CurrentHandle())) { | |
286 // 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.
| |
287 // (success meaning it was enabled)... | |
288 if (::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_END)) { | |
289 ::SetThreadPriority(handle.handle_, THREAD_MODE_BACKGROUND_BEGIN); | |
290 return kThreadPriority_Background; | |
291 } else { | |
292 // The only expected failure condition to the above test is not being in | |
293 // background mode in the first place. | |
294 DCHECK_EQ(ERROR_THREAD_MODE_NOT_BACKGROUND, | |
295 static_cast<int>(::GetLastError())); | |
296 } | |
297 } | |
298 | |
299 int priority = ::GetThreadPriority(handle.handle_); | |
300 switch (priority) { | |
301 case THREAD_PRIORITY_NORMAL: | |
302 return kThreadPriority_Normal; | |
303 case THREAD_PRIORITY_TIME_CRITICAL: | |
304 return kThreadPriority_RealtimeAudio; | |
305 case THREAD_PRIORITY_ABOVE_NORMAL: | |
306 return kThreadPriority_Display; | |
307 case THREAD_PRIORITY_LOWEST: | |
308 return kThreadPriority_Background; | |
309 case THREAD_PRIORITY_ERROR_RETURN: | |
310 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | |
311 default: | |
312 NOTREACHED() << "Unexpected priority: " << priority; | |
313 return kThreadPriority_Normal; | |
314 } | |
248 } | 315 } |
249 | 316 |
250 } // namespace base | 317 } // namespace base |
OLD | NEW |