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" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | void CALLBACK RaiseFlag(ULONG_PTR param) { |
| 23 | *reinterpret_cast<bool*>(param) = true; |
| 24 | } |
tommi@webrtc.org | d3d3baa | 2015-02-19 19:18:32 +0000 | [diff] [blame] | 25 | |
| 26 | // TODO(tommi): This is borrowed from webrtc/base/thread.cc, but we can't |
| 27 | // include thread.h from here since thread.h pulls in libjingle dependencies. |
| 28 | // Would be good to consolidate. |
| 29 | |
| 30 | // As seen on MSDN. |
| 31 | // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx |
| 32 | #define MSDEV_SET_THREAD_NAME 0x406D1388 |
| 33 | typedef struct tagTHREADNAME_INFO { |
| 34 | DWORD dwType; |
| 35 | LPCSTR szName; |
| 36 | DWORD dwThreadID; |
| 37 | DWORD dwFlags; |
| 38 | } THREADNAME_INFO; |
| 39 | |
| 40 | void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) { |
| 41 | THREADNAME_INFO info; |
| 42 | info.dwType = 0x1000; |
| 43 | info.szName = szThreadName; |
| 44 | info.dwThreadID = dwThreadID; |
| 45 | info.dwFlags = 0; |
| 46 | |
| 47 | __try { |
| 48 | RaiseException(MSDEV_SET_THREAD_NAME, 0, sizeof(info) / sizeof(DWORD), |
| 49 | reinterpret_cast<ULONG_PTR*>(&info)); |
| 50 | } |
| 51 | __except(EXCEPTION_CONTINUE_EXECUTION) { |
| 52 | } |
| 53 | } |
| 54 | |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 55 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 56 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | ThreadWindows::ThreadWindows(ThreadRunFunction func, ThreadObj obj, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 58 | ThreadPriority prio, const char* thread_name) |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 59 | : run_function_(func), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 60 | obj_(obj), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 61 | prio_(prio), |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 62 | stop_(false), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 63 | thread_(NULL), |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 64 | name_(thread_name ? thread_name : "webrtc") { |
| 65 | DCHECK(func); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 68 | ThreadWindows::~ThreadWindows() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 69 | DCHECK(main_thread_.CalledOnValidThread()); |
| 70 | DCHECK(!thread_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 73 | // static |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 74 | uint32_t ThreadWrapper::GetThreadId() { |
| 75 | return GetCurrentThreadId(); |
| 76 | } |
| 77 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 78 | // static |
| 79 | DWORD WINAPI ThreadWindows::StartThread(void* param) { |
| 80 | static_cast<ThreadWindows*>(param)->Run(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 81 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 84 | bool ThreadWindows::Start(unsigned int& id) { |
| 85 | DCHECK(main_thread_.CalledOnValidThread()); |
| 86 | DCHECK(!thread_); |
| 87 | |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 88 | stop_ = false; |
| 89 | |
| 90 | // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. |
| 91 | // Set the reserved stack stack size to 1M, which is the default on Windows |
| 92 | // and Linux. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 93 | DWORD thread_id; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 94 | thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 95 | STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); |
| 96 | if (!thread_ ) { |
| 97 | DCHECK(false) << "CreateThread failed"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 101 | id = thread_id; |
| 102 | |
| 103 | if (prio_ != kNormalPriority) { |
| 104 | int priority = THREAD_PRIORITY_NORMAL; |
| 105 | switch (prio_) { |
| 106 | case kLowPriority: |
| 107 | priority = THREAD_PRIORITY_BELOW_NORMAL; |
| 108 | break; |
| 109 | case kHighPriority: |
| 110 | priority = THREAD_PRIORITY_ABOVE_NORMAL; |
| 111 | break; |
| 112 | case kHighestPriority: |
| 113 | priority = THREAD_PRIORITY_HIGHEST; |
| 114 | break; |
| 115 | case kRealtimePriority: |
| 116 | priority = THREAD_PRIORITY_TIME_CRITICAL; |
| 117 | break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | SetThreadPriority(thread_, priority); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 128 | bool ThreadWindows::Stop() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 129 | DCHECK(main_thread_.CalledOnValidThread()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 130 | if (thread_) { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 131 | // Set stop_ to |true| on the worker thread. |
| 132 | QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 133 | WaitForSingleObject(thread_, INFINITE); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 134 | CloseHandle(thread_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 135 | thread_ = nullptr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 136 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 137 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 138 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 141 | void ThreadWindows::Run() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 142 | if (!name_.empty()) |
| 143 | SetThreadName(static_cast<DWORD>(-1), name_.c_str()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 145 | do { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 146 | // The interface contract of Start/Stop is that for a successfull call to |
| 147 | // Start, there should be at least one call to the run function. So we |
| 148 | // call the function before checking |stop_|. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 149 | if (!run_function_(obj_)) |
| 150 | break; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 151 | // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 152 | SleepEx(0, true); |
| 153 | } while (!stop_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 154 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 155 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 156 | } // namespace webrtc |