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 | |
| 13 | #include <assert.h> |
| 14 | #include <process.h> |
| 15 | #include <stdio.h> |
| 16 | #include <windows.h> |
| 17 | |
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 { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | ThreadWindows::ThreadWindows(ThreadRunFunction func, ThreadObj obj, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 24 | ThreadPriority prio, const char* thread_name) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | : ThreadWrapper(), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 26 | run_function_(func), |
| 27 | obj_(obj), |
| 28 | alive_(false), |
| 29 | dead_(true), |
| 30 | do_not_close_handle_(false), |
| 31 | prio_(prio), |
| 32 | event_(NULL), |
| 33 | thread_(NULL), |
| 34 | id_(0), |
| 35 | name_(), |
| 36 | set_thread_name_(false) { |
| 37 | event_ = EventWrapper::Create(); |
| 38 | critsect_stop_ = CriticalSectionWrapper::CreateCriticalSection(); |
| 39 | if (thread_name != NULL) { |
| 40 | // Set the thread name to appear in the VS debugger. |
| 41 | set_thread_name_ = true; |
| 42 | strncpy(name_, thread_name, kThreadMaxNameLength); |
| 43 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 46 | ThreadWindows::~ThreadWindows() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | #ifdef _DEBUG |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 48 | assert(!alive_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 50 | if (thread_) { |
| 51 | CloseHandle(thread_); |
| 52 | } |
| 53 | if (event_) { |
| 54 | delete event_; |
| 55 | } |
| 56 | if (critsect_stop_) { |
| 57 | delete critsect_stop_; |
| 58 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 61 | uint32_t ThreadWrapper::GetThreadId() { |
| 62 | return GetCurrentThreadId(); |
| 63 | } |
| 64 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 65 | unsigned int WINAPI ThreadWindows::StartThread(LPVOID lp_parameter) { |
| 66 | static_cast<ThreadWindows*>(lp_parameter)->Run(); |
| 67 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 70 | bool ThreadWindows::Start(unsigned int& thread_id) { |
| 71 | if (!run_function_) { |
| 72 | return false; |
| 73 | } |
| 74 | do_not_close_handle_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 76 | // Set stack size to 1M |
| 77 | thread_ = (HANDLE)_beginthreadex(NULL, 1024 * 1024, StartThread, (void*)this, |
| 78 | 0, &thread_id); |
| 79 | if (thread_ == NULL) { |
| 80 | return false; |
| 81 | } |
| 82 | id_ = thread_id; |
| 83 | event_->Wait(INFINITE); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 85 | switch (prio_) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | case kLowPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 87 | SetThreadPriority(thread_, THREAD_PRIORITY_BELOW_NORMAL); |
| 88 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | case kNormalPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 90 | SetThreadPriority(thread_, THREAD_PRIORITY_NORMAL); |
| 91 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | case kHighPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 93 | SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL); |
| 94 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | case kHighestPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 96 | SetThreadPriority(thread_, THREAD_PRIORITY_HIGHEST); |
| 97 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | case kRealtimePriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 99 | SetThreadPriority(thread_, THREAD_PRIORITY_TIME_CRITICAL); |
| 100 | break; |
| 101 | }; |
| 102 | return true; |
| 103 | } |
| 104 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 105 | void ThreadWindows::SetNotAlive() { |
| 106 | alive_ = false; |
| 107 | } |
| 108 | |
| 109 | bool ThreadWindows::Stop() { |
| 110 | critsect_stop_->Enter(); |
| 111 | |
| 112 | // Prevents the handle from being closed in ThreadWindows::Run() |
| 113 | do_not_close_handle_ = true; |
| 114 | alive_ = false; |
| 115 | bool signaled = false; |
| 116 | if (thread_ && !dead_) { |
| 117 | critsect_stop_->Leave(); |
| 118 | |
| 119 | // Wait up to 2 seconds for the thread to complete. |
| 120 | if (WAIT_OBJECT_0 == WaitForSingleObject(thread_, 2000)) { |
| 121 | signaled = true; |
| 122 | } |
| 123 | critsect_stop_->Enter(); |
| 124 | } |
| 125 | if (thread_) { |
| 126 | CloseHandle(thread_); |
| 127 | thread_ = NULL; |
| 128 | } |
| 129 | critsect_stop_->Leave(); |
| 130 | |
| 131 | if (dead_ || signaled) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | return true; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 133 | } else { |
| 134 | return false; |
| 135 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | } |
| 137 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 138 | void ThreadWindows::Run() { |
| 139 | alive_ = true; |
| 140 | dead_ = false; |
| 141 | event_->Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 143 | // All tracing must be after event_->Set to avoid deadlock in Trace. |
| 144 | if (set_thread_name_) { |
| 145 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, id_, |
| 146 | "Thread with name:%s started ", name_); |
tommi@webrtc.org | eec6ecd | 2014-07-11 19:09:59 +0000 | [diff] [blame] | 147 | SetThreadName(static_cast<DWORD>(-1), name_); // -1 == caller thread. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 148 | } else { |
| 149 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, id_, |
| 150 | "Thread without name started"); |
| 151 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 153 | do { |
| 154 | if (run_function_) { |
| 155 | if (!run_function_(obj_)) { |
| 156 | alive_ = false; |
| 157 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | } else { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 159 | alive_ = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 161 | } while (alive_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 163 | if (set_thread_name_) { |
| 164 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, id_, |
| 165 | "Thread with name:%s stopped", name_); |
| 166 | } else { |
| 167 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, id_, |
| 168 | "Thread without name stopped"); |
| 169 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 171 | critsect_stop_->Enter(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 173 | if (thread_ && !do_not_close_handle_) { |
| 174 | HANDLE thread = thread_; |
| 175 | thread_ = NULL; |
| 176 | CloseHandle(thread); |
| 177 | } |
| 178 | dead_ = true; |
| 179 | |
| 180 | critsect_stop_->Leave(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | }; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 182 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 183 | } // namespace webrtc |