Chromium Code Reviews| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 | 80 |
| 81 delete thread_params; | 81 delete thread_params; |
| 82 delegate->ThreadMain(); | 82 delegate->ThreadMain(); |
| 83 | 83 |
| 84 if (did_dup) { | 84 if (did_dup) { |
| 85 ThreadIdNameManager::GetInstance()->RemoveName( | 85 ThreadIdNameManager::GetInstance()->RemoveName( |
| 86 scoped_platform_handle.Get(), | 86 scoped_platform_handle.Get(), |
| 87 PlatformThread::CurrentId()); | 87 PlatformThread::CurrentId()); |
| 88 } | 88 } |
| 89 | 89 |
| 90 return NULL; | 90 return 0; |
| 91 } | 91 } |
| 92 | 92 |
| 93 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except | 93 // CreateThreadInternal() matches PlatformThread::CreateWithPriority(), except |
| 94 // that |out_thread_handle| may be NULL, in which case a non-joinable thread is | 94 // that |out_thread_handle| may be nullptr, in which case a non-joinable thread |
| 95 // created. | 95 // is created. |
| 96 bool CreateThreadInternal(size_t stack_size, | 96 bool CreateThreadInternal(size_t stack_size, |
| 97 PlatformThread::Delegate* delegate, | 97 PlatformThread::Delegate* delegate, |
| 98 PlatformThreadHandle* out_thread_handle, | 98 PlatformThreadHandle* out_thread_handle, |
| 99 ThreadPriority priority) { | 99 ThreadPriority priority) { |
| 100 unsigned int flags = 0; | 100 unsigned int flags = 0; |
| 101 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { | 101 if (stack_size > 0 && base::win::GetVersion() >= base::win::VERSION_XP) { |
| 102 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; | 102 flags = STACK_SIZE_PARAM_IS_A_RESERVATION; |
| 103 } else { | 103 } else { |
| 104 stack_size = 0; | 104 stack_size = 0; |
| 105 } | 105 } |
| 106 | 106 |
| 107 ThreadParams* params = new ThreadParams; | 107 ThreadParams* params = new ThreadParams; |
| 108 params->delegate = delegate; | 108 params->delegate = delegate; |
| 109 params->joinable = out_thread_handle != NULL; | 109 params->joinable = out_thread_handle != nullptr; |
| 110 params->priority = priority; | 110 params->priority = priority; |
| 111 | 111 |
| 112 // Using CreateThread here vs _beginthreadex makes thread creation a bit | 112 // Using CreateThread here vs _beginthreadex makes thread creation a bit |
| 113 // faster and doesn't require the loader lock to be available. Our code will | 113 // faster and doesn't require the loader lock to be available. Our code will |
| 114 // have to work running on CreateThread() threads anyway, since we run code | 114 // have to work running on CreateThread() threads anyway, since we run code |
| 115 // on the Windows thread pool, etc. For some background on the difference: | 115 // on the Windows thread pool, etc. For some background on the difference: |
| 116 // http://www.microsoft.com/msj/1099/win32/win321099.aspx | 116 // http://www.microsoft.com/msj/1099/win32/win321099.aspx |
| 117 PlatformThreadId thread_id; | 117 void* thread_handle = |
| 118 void* thread_handle = CreateThread( | 118 CreateThread(nullptr, stack_size, ThreadFunc, params, flags, nullptr); |
|
gab
2015/07/23 17:16:15
At first I thought we should remove the ID paramet
Takashi Toyoshima
2015/07/24 03:47:11
Agreed. Win32 API often has a similar name with lo
| |
| 119 NULL, stack_size, ThreadFunc, params, flags, &thread_id); | |
| 120 if (!thread_handle) { | 119 if (!thread_handle) { |
| 121 delete params; | 120 delete params; |
| 122 return false; | 121 return false; |
| 123 } | 122 } |
| 124 | 123 |
| 125 if (out_thread_handle) | 124 if (out_thread_handle) |
| 126 *out_thread_handle = PlatformThreadHandle(thread_handle, thread_id); | 125 *out_thread_handle = PlatformThreadHandle(thread_handle); |
| 127 else | 126 else |
| 128 CloseHandle(thread_handle); | 127 CloseHandle(thread_handle); |
| 129 return true; | 128 return true; |
| 130 } | 129 } |
| 131 | 130 |
| 132 } // namespace | 131 } // namespace |
| 133 | 132 |
| 134 // static | 133 // static |
| 135 PlatformThreadId PlatformThread::CurrentId() { | 134 PlatformThreadId PlatformThread::CurrentId() { |
| 136 return ::GetCurrentThreadId(); | 135 return ::GetCurrentThreadId(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 // static | 197 // static |
| 199 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, | 198 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, |
| 200 PlatformThreadHandle* thread_handle, | 199 PlatformThreadHandle* thread_handle, |
| 201 ThreadPriority priority) { | 200 ThreadPriority priority) { |
| 202 DCHECK(thread_handle); | 201 DCHECK(thread_handle); |
| 203 return CreateThreadInternal(stack_size, delegate, thread_handle, priority); | 202 return CreateThreadInternal(stack_size, delegate, thread_handle, priority); |
| 204 } | 203 } |
| 205 | 204 |
| 206 // static | 205 // static |
| 207 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { | 206 bool PlatformThread::CreateNonJoinable(size_t stack_size, Delegate* delegate) { |
| 208 return CreateThreadInternal( | 207 return CreateThreadInternal(stack_size, delegate, nullptr, |
| 209 stack_size, delegate, NULL, ThreadPriority::NORMAL); | 208 ThreadPriority::NORMAL); |
| 210 } | 209 } |
| 211 | 210 |
| 212 // static | 211 // static |
| 213 void PlatformThread::Join(PlatformThreadHandle thread_handle) { | 212 void PlatformThread::Join(PlatformThreadHandle thread_handle) { |
| 214 DCHECK(thread_handle.platform_handle()); | 213 DCHECK(thread_handle.platform_handle()); |
| 215 // TODO(willchan): Enable this check once I can get it to work for Windows | 214 // TODO(willchan): Enable this check once I can get it to work for Windows |
| 216 // shutdown. | 215 // shutdown. |
| 217 // Joining another thread may block the current thread for a long time, since | 216 // Joining another thread may block the current thread for a long time, since |
| 218 // the thread referred to by |thread_handle| may still be running long-lived / | 217 // the thread referred to by |thread_handle| may still be running long-lived / |
| 219 // blocking tasks. | 218 // blocking tasks. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 return ThreadPriority::REALTIME_AUDIO; | 280 return ThreadPriority::REALTIME_AUDIO; |
| 282 case THREAD_PRIORITY_ERROR_RETURN: | 281 case THREAD_PRIORITY_ERROR_RETURN: |
| 283 DPCHECK(false) << "GetThreadPriority error"; // Falls through. | 282 DPCHECK(false) << "GetThreadPriority error"; // Falls through. |
| 284 default: | 283 default: |
| 285 NOTREACHED() << "Unexpected priority: " << priority; | 284 NOTREACHED() << "Unexpected priority: " << priority; |
| 286 return ThreadPriority::NORMAL; | 285 return ThreadPriority::NORMAL; |
| 287 } | 286 } |
| 288 } | 287 } |
| 289 | 288 |
| 290 } // namespace base | 289 } // namespace base |
| OLD | NEW |