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