OLD | NEW |
| (Empty) |
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 | |
11 #include "webrtc/system_wrappers/source/thread_win.h" | |
12 | |
13 #include <process.h> | |
14 #include <stdio.h> | |
15 #include <windows.h> | |
16 | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/base/platform_thread.h" | |
19 #include "webrtc/system_wrappers/include/trace.h" | |
20 | |
21 namespace webrtc { | |
22 namespace { | |
23 void CALLBACK RaiseFlag(ULONG_PTR param) { | |
24 *reinterpret_cast<bool*>(param) = true; | |
25 } | |
26 } | |
27 | |
28 ThreadWindows::ThreadWindows(ThreadRunFunction func, void* obj, | |
29 const char* thread_name) | |
30 : run_function_(func), | |
31 obj_(obj), | |
32 stop_(false), | |
33 thread_(NULL), | |
34 name_(thread_name ? thread_name : "webrtc") { | |
35 RTC_DCHECK(func); | |
36 } | |
37 | |
38 ThreadWindows::~ThreadWindows() { | |
39 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
40 RTC_DCHECK(!thread_); | |
41 } | |
42 | |
43 // static | |
44 uint32_t ThreadWrapper::GetThreadId() { | |
45 return GetCurrentThreadId(); | |
46 } | |
47 | |
48 // static | |
49 DWORD WINAPI ThreadWindows::StartThread(void* param) { | |
50 static_cast<ThreadWindows*>(param)->Run(); | |
51 return 0; | |
52 } | |
53 | |
54 bool ThreadWindows::Start() { | |
55 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
56 RTC_DCHECK(!thread_); | |
57 | |
58 stop_ = false; | |
59 | |
60 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. | |
61 // Set the reserved stack stack size to 1M, which is the default on Windows | |
62 // and Linux. | |
63 DWORD thread_id; | |
64 thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, | |
65 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); | |
66 if (!thread_ ) { | |
67 RTC_DCHECK(false) << "CreateThread failed"; | |
68 return false; | |
69 } | |
70 | |
71 return true; | |
72 } | |
73 | |
74 bool ThreadWindows::Stop() { | |
75 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
76 if (thread_) { | |
77 // Set stop_ to |true| on the worker thread. | |
78 QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); | |
79 WaitForSingleObject(thread_, INFINITE); | |
80 CloseHandle(thread_); | |
81 thread_ = nullptr; | |
82 } | |
83 | |
84 return true; | |
85 } | |
86 | |
87 bool ThreadWindows::SetPriority(ThreadPriority priority) { | |
88 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
89 return thread_ && SetThreadPriority(thread_, priority); | |
90 } | |
91 | |
92 void ThreadWindows::Run() { | |
93 if (!name_.empty()) | |
94 rtc::SetCurrentThreadName(name_.c_str()); | |
95 | |
96 do { | |
97 // The interface contract of Start/Stop is that for a successfull call to | |
98 // Start, there should be at least one call to the run function. So we | |
99 // call the function before checking |stop_|. | |
100 if (!run_function_(obj_)) | |
101 break; | |
102 // Alertable sleep to permit RaiseFlag to run and update |stop_|. | |
103 SleepEx(0, true); | |
104 } while (!stop_); | |
105 } | |
106 | |
107 } // namespace webrtc | |
OLD | NEW |