niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/thread_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <process.h> |
| 14 | #include <stdio.h> |
| 15 | #include <windows.h> |
| 16 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/interface/trace.h" |
| 19 | #include "webrtc/system_wrappers/source/set_thread_name_win.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | void CALLBACK RaiseFlag(ULONG_PTR param) { |
| 24 | *reinterpret_cast<bool*>(param) = true; |
| 25 | } |
| 26 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 27 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | ThreadWindows::ThreadWindows(ThreadRunFunction func, ThreadObj obj, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 29 | ThreadPriority prio, const char* thread_name) |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 30 | : run_function_(func), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 31 | obj_(obj), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 32 | prio_(prio), |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 33 | stop_(false), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 34 | thread_(NULL), |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 35 | name_(thread_name ? thread_name : "webrtc") { |
| 36 | DCHECK(func); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 39 | ThreadWindows::~ThreadWindows() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 40 | DCHECK(main_thread_.CalledOnValidThread()); |
| 41 | DCHECK(!thread_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 44 | // static |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 45 | uint32_t ThreadWrapper::GetThreadId() { |
| 46 | return GetCurrentThreadId(); |
| 47 | } |
| 48 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 49 | // static |
| 50 | DWORD WINAPI ThreadWindows::StartThread(void* param) { |
| 51 | static_cast<ThreadWindows*>(param)->Run(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 52 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | } |
| 54 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 55 | bool ThreadWindows::Start(unsigned int& id) { |
| 56 | DCHECK(main_thread_.CalledOnValidThread()); |
| 57 | DCHECK(!thread_); |
| 58 | |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 59 | stop_ = false; |
| 60 | |
| 61 | // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. |
| 62 | // Set the reserved stack stack size to 1M, which is the default on Windows |
| 63 | // and Linux. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 64 | DWORD thread_id; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 65 | thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 66 | STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); |
| 67 | if (!thread_ ) { |
| 68 | DCHECK(false) << "CreateThread failed"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 72 | id = thread_id; |
| 73 | |
| 74 | if (prio_ != kNormalPriority) { |
| 75 | int priority = THREAD_PRIORITY_NORMAL; |
| 76 | switch (prio_) { |
| 77 | case kLowPriority: |
| 78 | priority = THREAD_PRIORITY_BELOW_NORMAL; |
| 79 | break; |
| 80 | case kHighPriority: |
| 81 | priority = THREAD_PRIORITY_ABOVE_NORMAL; |
| 82 | break; |
| 83 | case kHighestPriority: |
| 84 | priority = THREAD_PRIORITY_HIGHEST; |
| 85 | break; |
| 86 | case kRealtimePriority: |
| 87 | priority = THREAD_PRIORITY_TIME_CRITICAL; |
| 88 | break; |
| 89 | default: |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | SetThreadPriority(thread_, priority); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 94 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 99 | bool ThreadWindows::Stop() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 100 | DCHECK(main_thread_.CalledOnValidThread()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 101 | if (thread_) { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 102 | // Set stop_ to |true| on the worker thread. |
| 103 | QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 104 | WaitForSingleObject(thread_, INFINITE); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 105 | CloseHandle(thread_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 106 | thread_ = nullptr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 107 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 108 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 109 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 112 | void ThreadWindows::Run() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 113 | if (!name_.empty()) |
| 114 | SetThreadName(static_cast<DWORD>(-1), name_.c_str()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 116 | do { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 117 | // The interface contract of Start/Stop is that for a successfull call to |
| 118 | // Start, there should be at least one call to the run function. So we |
| 119 | // call the function before checking |stop_|. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 120 | if (!run_function_(obj_)) |
| 121 | break; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 122 | // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 123 | SleepEx(0, true); |
| 124 | } while (!stop_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 125 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 126 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 127 | } // namespace webrtc |