niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #include <errno.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | #ifdef WEBRTC_LINUX |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | #include <linux/unistd.h> |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 19 | #include <sched.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #include <sys/prctl.h> |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 21 | #include <sys/syscall.h> |
| 22 | #include <sys/types.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 25 | #include "webrtc/base/checks.h" |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 26 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 27 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
hta@webrtc.org | 2cec0b1 | 2013-03-21 14:02:29 +0000 | [diff] [blame] | 28 | #include "webrtc/system_wrappers/interface/sleep.h" |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 29 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | struct ThreadAttributes { |
| 34 | ThreadAttributes() { pthread_attr_init(&attr); } |
| 35 | ~ThreadAttributes() { pthread_attr_destroy(&attr); } |
| 36 | pthread_attr_t* operator&() { return &attr; } |
| 37 | pthread_attr_t attr; |
| 38 | }; |
| 39 | } // namespace |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 40 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 41 | int ConvertToSystemPriority(ThreadPriority priority, int min_prio, |
| 42 | int max_prio) { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 43 | DCHECK(max_prio - min_prio > 2); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 44 | const int top_prio = max_prio - 1; |
| 45 | const int low_prio = min_prio + 1; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 46 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 47 | switch (priority) { |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 48 | case kLowPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 49 | return low_prio; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 50 | case kNormalPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 51 | // The -1 ensures that the kHighPriority is always greater or equal to |
| 52 | // kNormalPriority. |
| 53 | return (low_prio + top_prio - 1) / 2; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 54 | case kHighPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 55 | return std::max(top_prio - 2, low_prio); |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 56 | case kHighestPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 57 | return std::max(top_prio - 1, low_prio); |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 58 | case kRealtimePriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 59 | return top_prio; |
| 60 | } |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 61 | DCHECK(false); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 62 | return low_prio; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 65 | // static |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 66 | void* ThreadPosix::StartThread(void* param) { |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 67 | static_cast<ThreadPosix*>(param)->Run(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 68 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
| 70 | |
tommi@webrtc.org | 27c0be9 | 2015-03-19 14:35:58 +0000 | [diff] [blame] | 71 | ThreadPosix::ThreadPosix(ThreadRunFunction func, void* obj, |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 72 | const char* thread_name) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 73 | : run_function_(func), |
| 74 | obj_(obj), |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 75 | stop_event_(false, false), |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 76 | name_(thread_name ? thread_name : "webrtc"), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 77 | thread_(0) { |
tommi@webrtc.org | 27c0be9 | 2015-03-19 14:35:58 +0000 | [diff] [blame] | 78 | DCHECK(name_.length() < 64); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 81 | uint32_t ThreadWrapper::GetThreadId() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 82 | return rtc::CurrentThreadId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 85 | ThreadPosix::~ThreadPosix() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 86 | DCHECK(thread_checker_.CalledOnValidThread()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 89 | // TODO(pbos): Make Start void, calling code really doesn't support failures |
| 90 | // here. |
| 91 | bool ThreadPosix::Start() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 92 | DCHECK(thread_checker_.CalledOnValidThread()); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 93 | DCHECK(!thread_) << "Thread already started?"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 94 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 95 | ThreadAttributes attr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 96 | // Set the stack stack size to 1M. |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 97 | pthread_attr_setstacksize(&attr, 1024 * 1024); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 98 | CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 99 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 102 | bool ThreadPosix::Stop() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 103 | DCHECK(thread_checker_.CalledOnValidThread()); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 104 | if (!thread_) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 105 | return true; |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 106 | |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 107 | stop_event_.Set(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 108 | CHECK_EQ(0, pthread_join(thread_, nullptr)); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 109 | thread_ = 0; |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 110 | |
| 111 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
tommi@webrtc.org | b6817d7 | 2015-03-20 15:51:39 +0000 | [diff] [blame^] | 114 | bool ThreadPosix::SetPriority(ThreadPriority priority) { |
| 115 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | if (!thread_) |
| 117 | return false; |
| 118 | |
| 119 | #ifdef WEBRTC_THREAD_RR |
| 120 | const int policy = SCHED_RR; |
| 121 | #else |
| 122 | const int policy = SCHED_FIFO; |
| 123 | #endif |
| 124 | const int min_prio = sched_get_priority_min(policy); |
| 125 | const int max_prio = sched_get_priority_max(policy); |
| 126 | if (min_prio == -1 || max_prio == -1) { |
| 127 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 128 | "unable to retreive min or max priority for threads"); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | if (max_prio - min_prio <= 2) |
| 133 | return false; |
| 134 | |
| 135 | sched_param param; |
| 136 | param.sched_priority = ConvertToSystemPriority(priority, min_prio, max_prio); |
| 137 | if (pthread_setschedparam(thread_, policy, ¶m) != 0) { |
| 138 | WEBRTC_TRACE( |
| 139 | kTraceError, kTraceUtility, -1, "unable to set thread priority"); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 146 | void ThreadPosix::Run() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 147 | if (!name_.empty()) { |
| 148 | // Setting the thread name may fail (harmlessly) if running inside a |
| 149 | // sandbox. Ignore failures if they happen. |
| 150 | #if defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 151 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name_.c_str())); |
| 152 | #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 153 | pthread_setname_np(name_.substr(0, 63).c_str()); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 154 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 155 | } |
henrike@webrtc.org | ea6c12e | 2014-09-29 18:25:27 +0000 | [diff] [blame] | 156 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 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; |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 163 | } while (!stop_event_.Wait(0)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 165 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 166 | } // namespace webrtc |