| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 delegate->ThreadMain(); | 57 delegate->ThreadMain(); |
| 58 return NULL; | 58 return NULL; |
| 59 } | 59 } |
| 60 | 60 |
| 61 // CreateThreadInternal() matches PlatformThread::Create(), except that | 61 // CreateThreadInternal() matches PlatformThread::Create(), except that |
| 62 // |out_thread_handle| may be NULL, in which case a non-joinable thread is | 62 // |out_thread_handle| may be NULL, in which case a non-joinable thread is |
| 63 // created. | 63 // created. |
| 64 bool CreateThreadInternal(size_t stack_size, | 64 bool CreateThreadInternal(size_t stack_size, |
| 65 PlatformThread::Delegate* delegate, | 65 PlatformThread::Delegate* delegate, |
| 66 PlatformThreadHandle* out_thread_handle) { | 66 PlatformThreadHandle* out_thread_handle) { |
| 67 PlatformThreadHandle thread_handle; | |
| 68 unsigned int flags = 0; | 67 unsigned int flags = 0; |
| 69 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { | 68 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { |
| 70 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; | 69 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; |
| 71 } else { | 70 } else { |
| 72 stack_size = 0; | 71 stack_size = 0; |
| 73 } | 72 } |
| 74 | 73 |
| 75 ThreadParams* params = new ThreadParams; | 74 ThreadParams* params = new ThreadParams; |
| 76 params->delegate = delegate; | 75 params->delegate = delegate; |
| 77 params->joinable = out_thread_handle != NULL; | 76 params->joinable = out_thread_handle != NULL; |
| 78 | 77 |
| 79 // Using CreateThread here vs _beginthreadex makes thread creation a bit | 78 // Using CreateThread here vs _beginthreadex makes thread creation a bit |
| 80 // faster and doesn't require the loader lock to be available. Our code will | 79 // faster and doesn't require the loader lock to be available. Our code will |
| 81 // have to work running on CreateThread() threads anyway, since we run code | 80 // have to work running on CreateThread() threads anyway, since we run code |
| 82 // on the Windows thread pool, etc. For some background on the difference: | 81 // on the Windows thread pool, etc. For some background on the difference: |
| 83 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 82 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
| 84 thread_handle = CreateThread( | 83 void* thread_handle = CreateThread( |
| 85 NULL, stack_size, ThreadFunc, params, flags, NULL); | 84 NULL, stack_size, ThreadFunc, params, flags, NULL); |
| 86 if (!thread_handle) { | 85 if (!thread_handle) { |
| 87 delete params; | 86 delete params; |
| 88 return false; | 87 return false; |
| 89 } | 88 } |
| 90 | 89 |
| 91 if (out_thread_handle) | 90 if (out_thread_handle) |
| 92 *out_thread_handle = thread_handle; | 91 *out_thread_handle = PlatformThreadHandle(thread_handle); |
| 93 else | 92 else |
| 94 CloseHandle(thread_handle); | 93 CloseHandle(thread_handle); |
| 95 return true; | 94 return true; |
| 96 } | 95 } |
| 97 | 96 |
| 98 } // namespace | 97 } // namespace |
| 99 | 98 |
| 100 // static | 99 // static |
| 101 PlatformThreadId PlatformThread::CurrentId() { | 100 PlatformThreadId PlatformThread::CurrentId() { |
| 102 return GetCurrentThreadId(); | 101 return GetCurrentThreadId(); |
| 103 } | 102 } |
| 104 | 103 |
| 105 // static | 104 // static |
| 105 PlatformThreadHandle PlatformThread::CurrentHandle() { |
| 106 NOTIMPLEMENTED(); // See OpenThread() |
| 107 return kNullThreadHandle; |
| 108 } |
| 109 |
| 110 // static |
| 106 void PlatformThread::YieldCurrentThread() { | 111 void PlatformThread::YieldCurrentThread() { |
| 107 ::Sleep(0); | 112 ::Sleep(0); |
| 108 } | 113 } |
| 109 | 114 |
| 110 // static | 115 // static |
| 111 void PlatformThread::Sleep(TimeDelta duration) { | 116 void PlatformThread::Sleep(TimeDelta duration) { |
| 112 ::Sleep(duration.InMillisecondsRoundedUp()); | 117 ::Sleep(duration.InMillisecondsRoundedUp()); |
| 113 } | 118 } |
| 114 | 119 |
| 115 // static | 120 // static |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return result; | 162 return result; |
| 158 } | 163 } |
| 159 | 164 |
| 160 // static | 165 // static |
| 161 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 166 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 162 return CreateThreadInternal(stack_size, delegate, NULL); | 167 return CreateThreadInternal(stack_size, delegate, NULL); |
| 163 } | 168 } |
| 164 | 169 |
| 165 // static | 170 // static |
| 166 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 171 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 167 DCHECK(thread_handle); | 172 DCHECK(thread_handle.handle_); |
| 168 // TODO(willchan): Enable this check once I can get it to work for Windows | 173 // TODO(willchan): Enable this check once I can get it to work for Windows |
| 169 // shutdown. | 174 // shutdown. |
| 170 // Joining another thread may block the current thread for a long time, since | 175 // Joining another thread may block the current thread for a long time, since |
| 171 // the thread referred to by |thread_handle| may still be running long-lived / | 176 // the thread referred to by |thread_handle| may still be running long-lived / |
| 172 // blocking tasks. | 177 // blocking tasks. |
| 173 #if 0 | 178 #if 0 |
| 174 base::ThreadRestrictions::AssertIOAllowed(); | 179 base::ThreadRestrictions::AssertIOAllowed(); |
| 175 #endif | 180 #endif |
| 176 | 181 |
| 177 // Wait for the thread to exit. It should already have terminated but make | 182 // Wait for the thread to exit. It should already have terminated but make |
| 178 // sure this assumption is valid. | 183 // sure this assumption is valid. |
| 179 DWORD result = WaitForSingleObject(thread_handle, INFINITE); | 184 DWORD result = WaitForSingleObject(thread_handle.handle_, INFINITE); |
| 180 if (result != WAIT_OBJECT_0) { | 185 if (result != WAIT_OBJECT_0) { |
| 181 // Debug info for bug 127931. | 186 // Debug info for bug 127931. |
| 182 DWORD error = GetLastError(); | 187 DWORD error = GetLastError(); |
| 183 debug::Alias(&error); | 188 debug::Alias(&error); |
| 184 debug::Alias(&result); | 189 debug::Alias(&result); |
| 185 debug::Alias(&thread_handle); | 190 debug::Alias(&thread_handle.handle_); |
| 186 CHECK(false); | 191 CHECK(false); |
| 187 } | 192 } |
| 188 | 193 |
| 189 CloseHandle(thread_handle); | 194 CloseHandle(thread_handle.handle_); |
| 190 } | 195 } |
| 191 | 196 |
| 192 // static | 197 // static |
| 193 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, | 198 void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
| 194 ThreadPriority priority) { | 199 ThreadPriority priority) { |
| 195 switch (priority) { | 200 switch (priority) { |
| 196 case kThreadPriority_Normal: | 201 case kThreadPriority_Normal: |
| 197 ::SetThreadPriority(handle, THREAD_PRIORITY_NORMAL); | 202 ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_NORMAL); |
| 198 break; | 203 break; |
| 199 case kThreadPriority_RealtimeAudio: | 204 case kThreadPriority_RealtimeAudio: |
| 200 ::SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL); | 205 ::SetThreadPriority(handle.handle_, THREAD_PRIORITY_TIME_CRITICAL); |
| 206 break; |
| 207 default: |
| 208 NOTREACHED() << "Unknown priority."; |
| 201 break; | 209 break; |
| 202 } | 210 } |
| 203 } | 211 } |
| 204 | 212 |
| 205 } // namespace base | 213 } // namespace base |
| OLD | NEW |