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 | |
tommi@webrtc.org | 27c0be9 | 2015-03-19 14:35:58 +0000 | [diff] [blame] | 57 | ThreadWindows::ThreadWindows(ThreadRunFunction func, void* obj, |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 58 | 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), |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 61 | stop_(false), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 62 | thread_(NULL), |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 63 | name_(thread_name ? thread_name : "webrtc") { |
| 64 | DCHECK(func); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 67 | ThreadWindows::~ThreadWindows() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 68 | DCHECK(main_thread_.CalledOnValidThread()); |
| 69 | DCHECK(!thread_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 72 | // static |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 73 | uint32_t ThreadWrapper::GetThreadId() { |
| 74 | return GetCurrentThreadId(); |
| 75 | } |
| 76 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 77 | // static |
| 78 | DWORD WINAPI ThreadWindows::StartThread(void* param) { |
| 79 | static_cast<ThreadWindows*>(param)->Run(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 80 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 83 | bool ThreadWindows::Start() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 84 | DCHECK(main_thread_.CalledOnValidThread()); |
| 85 | DCHECK(!thread_); |
| 86 | |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 87 | stop_ = false; |
| 88 | |
| 89 | // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. |
| 90 | // Set the reserved stack stack size to 1M, which is the default on Windows |
| 91 | // and Linux. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 92 | DWORD thread_id; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 93 | thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 94 | STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); |
| 95 | if (!thread_ ) { |
| 96 | DCHECK(false) << "CreateThread failed"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 97 | return false; |
| 98 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 103 | bool ThreadWindows::Stop() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 104 | DCHECK(main_thread_.CalledOnValidThread()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 105 | if (thread_) { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 106 | // Set stop_ to |true| on the worker thread. |
| 107 | QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 108 | WaitForSingleObject(thread_, INFINITE); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 109 | CloseHandle(thread_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 110 | thread_ = nullptr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 111 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 112 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 113 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 116 | bool ThreadWindows::SetPriority(ThreadPriority priority) { |
| 117 | DCHECK(main_thread_.CalledOnValidThread()); |
| 118 | return thread_ && SetThreadPriority(thread_, priority); |
| 119 | } |
| 120 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 121 | void ThreadWindows::Run() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 122 | if (!name_.empty()) |
| 123 | SetThreadName(static_cast<DWORD>(-1), name_.c_str()); |
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 | do { |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 126 | // The interface contract of Start/Stop is that for a successfull call to |
| 127 | // Start, there should be at least one call to the run function. So we |
| 128 | // call the function before checking |stop_|. |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 129 | if (!run_function_(obj_)) |
| 130 | break; |
tommi@webrtc.org | 9d94a0c | 2015-02-11 14:16:08 +0000 | [diff] [blame] | 131 | // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 132 | SleepEx(0, true); |
| 133 | } while (!stop_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame] | 134 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 135 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 136 | } // namespace webrtc |