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 { |
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) |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 25 | : run_function_(func), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 26 | obj_(obj), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 27 | prio_(prio), |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 28 | event_(CreateEvent(NULL, FALSE, FALSE, NULL)), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 29 | thread_(NULL), |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 30 | name_(thread_name ? thread_name : "webrtc") { |
| 31 | DCHECK(func); |
| 32 | DCHECK(event_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 35 | ThreadWindows::~ThreadWindows() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 36 | DCHECK(main_thread_.CalledOnValidThread()); |
| 37 | DCHECK(!thread_); |
| 38 | CloseHandle(event_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 41 | // static |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 42 | uint32_t ThreadWrapper::GetThreadId() { |
| 43 | return GetCurrentThreadId(); |
| 44 | } |
| 45 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 46 | // static |
| 47 | DWORD WINAPI ThreadWindows::StartThread(void* param) { |
| 48 | static_cast<ThreadWindows*>(param)->Run(); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 49 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 52 | bool ThreadWindows::Start(unsigned int& id) { |
| 53 | DCHECK(main_thread_.CalledOnValidThread()); |
| 54 | DCHECK(!thread_); |
| 55 | |
| 56 | // See bug 2902 for stack size. |
| 57 | DWORD thread_id; |
| 58 | thread_ = ::CreateThread(NULL, 0, &StartThread, this, |
| 59 | STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); |
| 60 | if (!thread_ ) { |
| 61 | DCHECK(false) << "CreateThread failed"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 65 | id = thread_id; |
| 66 | |
| 67 | if (prio_ != kNormalPriority) { |
| 68 | int priority = THREAD_PRIORITY_NORMAL; |
| 69 | switch (prio_) { |
| 70 | case kLowPriority: |
| 71 | priority = THREAD_PRIORITY_BELOW_NORMAL; |
| 72 | break; |
| 73 | case kHighPriority: |
| 74 | priority = THREAD_PRIORITY_ABOVE_NORMAL; |
| 75 | break; |
| 76 | case kHighestPriority: |
| 77 | priority = THREAD_PRIORITY_HIGHEST; |
| 78 | break; |
| 79 | case kRealtimePriority: |
| 80 | priority = THREAD_PRIORITY_TIME_CRITICAL; |
| 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | SetThreadPriority(thread_, priority); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 87 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 92 | void ThreadWindows::SetNotAlive() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 93 | DCHECK(main_thread_.CalledOnValidThread()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | bool ThreadWindows::Stop() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 97 | DCHECK(main_thread_.CalledOnValidThread()); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 98 | if (thread_) { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 99 | SetEvent(event_); |
| 100 | WaitForSingleObject(thread_, INFINITE); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 101 | CloseHandle(thread_); |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 102 | thread_ = nullptr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 103 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 104 | |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 105 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | } |
| 107 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 108 | void ThreadWindows::Run() { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 109 | if (!name_.empty()) |
| 110 | SetThreadName(static_cast<DWORD>(-1), name_.c_str()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 112 | do { |
tommi@webrtc.org | aef0779 | 2015-01-30 15:06:10 +0000 | [diff] [blame^] | 113 | if (!run_function_(obj_)) |
| 114 | break; |
| 115 | } while (WaitForSingleObject(event_, 0) == WAIT_TIMEOUT); |
| 116 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 117 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 118 | } // namespace webrtc |