OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 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_posix.h" | |
12 | |
13 #include <algorithm> | |
14 | |
15 #include <errno.h> | |
16 #include <unistd.h> | |
17 #ifdef WEBRTC_LINUX | |
18 #include <linux/unistd.h> | |
19 #include <sched.h> | |
20 #include <sys/types.h> | |
21 #endif | |
22 | |
23 #include "webrtc/base/checks.h" | |
24 #include "webrtc/base/platform_thread.h" | |
25 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
26 #include "webrtc/system_wrappers/include/event_wrapper.h" | |
27 #include "webrtc/system_wrappers/include/sleep.h" | |
28 #include "webrtc/system_wrappers/include/trace.h" | |
29 | |
30 namespace webrtc { | |
31 namespace { | |
32 struct ThreadAttributes { | |
33 ThreadAttributes() { pthread_attr_init(&attr); } | |
34 ~ThreadAttributes() { pthread_attr_destroy(&attr); } | |
35 pthread_attr_t* operator&() { return &attr; } | |
36 pthread_attr_t attr; | |
37 }; | |
38 } // namespace | |
39 | |
40 int ConvertToSystemPriority(ThreadPriority priority, int min_prio, | |
41 int max_prio) { | |
42 RTC_DCHECK(max_prio - min_prio > 2); | |
43 const int top_prio = max_prio - 1; | |
44 const int low_prio = min_prio + 1; | |
45 | |
46 switch (priority) { | |
47 case kLowPriority: | |
48 return low_prio; | |
49 case kNormalPriority: | |
50 // The -1 ensures that the kHighPriority is always greater or equal to | |
51 // kNormalPriority. | |
52 return (low_prio + top_prio - 1) / 2; | |
53 case kHighPriority: | |
54 return std::max(top_prio - 2, low_prio); | |
55 case kHighestPriority: | |
56 return std::max(top_prio - 1, low_prio); | |
57 case kRealtimePriority: | |
58 return top_prio; | |
59 } | |
60 RTC_DCHECK(false); | |
61 return low_prio; | |
62 } | |
63 | |
64 // static | |
65 void* ThreadPosix::StartThread(void* param) { | |
66 static_cast<ThreadPosix*>(param)->Run(); | |
67 return 0; | |
68 } | |
69 | |
70 ThreadPosix::ThreadPosix(ThreadRunFunction func, void* obj, | |
71 const char* thread_name) | |
72 : run_function_(func), | |
73 obj_(obj), | |
74 stop_event_(false, false), | |
75 name_(thread_name ? thread_name : "webrtc"), | |
76 thread_(0) { | |
77 RTC_DCHECK(name_.length() < 64); | |
78 } | |
79 | |
80 uint32_t ThreadWrapper::GetThreadId() { | |
81 return rtc::CurrentThreadId(); | |
82 } | |
83 | |
84 ThreadPosix::~ThreadPosix() { | |
85 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
86 } | |
87 | |
88 // TODO(pbos): Make Start void, calling code really doesn't support failures | |
89 // here. | |
90 bool ThreadPosix::Start() { | |
91 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
92 RTC_DCHECK(!thread_) << "Thread already started?"; | |
93 | |
94 ThreadAttributes attr; | |
95 // Set the stack stack size to 1M. | |
96 pthread_attr_setstacksize(&attr, 1024 * 1024); | |
97 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); | |
98 return true; | |
99 } | |
100 | |
101 bool ThreadPosix::Stop() { | |
102 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
103 if (!thread_) | |
104 return true; | |
105 | |
106 stop_event_.Set(); | |
107 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr)); | |
108 thread_ = 0; | |
109 | |
110 return true; | |
111 } | |
112 | |
113 bool ThreadPosix::SetPriority(ThreadPriority priority) { | |
114 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
115 if (!thread_) | |
116 return false; | |
117 #if defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX) | |
118 // TODO(tommi): Switch to the same mechanism as Chromium uses for | |
119 // changing thread priorities. | |
120 return true; | |
121 #else | |
122 #ifdef WEBRTC_THREAD_RR | |
123 const int policy = SCHED_RR; | |
124 #else | |
125 const int policy = SCHED_FIFO; | |
126 #endif | |
127 const int min_prio = sched_get_priority_min(policy); | |
128 const int max_prio = sched_get_priority_max(policy); | |
129 if (min_prio == -1 || max_prio == -1) { | |
130 WEBRTC_TRACE(kTraceError, kTraceUtility, -1, | |
131 "unable to retreive min or max priority for threads"); | |
132 return false; | |
133 } | |
134 | |
135 if (max_prio - min_prio <= 2) | |
136 return false; | |
137 | |
138 sched_param param; | |
139 param.sched_priority = ConvertToSystemPriority(priority, min_prio, max_prio); | |
140 if (pthread_setschedparam(thread_, policy, ¶m) != 0) { | |
141 WEBRTC_TRACE( | |
142 kTraceError, kTraceUtility, -1, "unable to set thread priority"); | |
143 return false; | |
144 } | |
145 | |
146 return true; | |
147 #endif // defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX) | |
148 } | |
149 | |
150 void ThreadPosix::Run() { | |
151 if (!name_.empty()) { | |
152 // Setting the thread name may fail (harmlessly) if running inside a | |
153 // sandbox. Ignore failures if they happen. | |
154 rtc::SetCurrentThreadName(name_.substr(0, 63).c_str()); | |
155 } | |
156 | |
157 // It's a requirement that for successful thread creation that the run | |
158 // function be called at least once (see RunFunctionIsCalled unit test), | |
159 // so to fullfill that requirement, we use a |do| loop and not |while|. | |
160 do { | |
161 if (!run_function_(obj_)) | |
162 break; | |
163 } while (!stop_event_.Wait(0)); | |
164 } | |
165 | |
166 } // namespace webrtc | |
OLD | NEW |