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 | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 65 | void* ThreadPosix::StartThread(void* param) { |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 66 | static_cast<ThreadPosix*>(param)->Run(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 67 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 70 | ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 71 | ThreadPriority prio, const char* thread_name) |
| 72 | : run_function_(func), |
| 73 | obj_(obj), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 74 | prio_(prio), |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 75 | started_(false), |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 76 | stop_event_(true, false), |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 77 | name_(thread_name ? thread_name : "webrtc"), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 78 | thread_(0) { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 79 | DCHECK(name_.length() < kThreadMaxNameLength); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | } |
| 81 | |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 82 | uint32_t ThreadWrapper::GetThreadId() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 83 | return rtc::CurrentThreadId(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | } |
| 85 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 86 | ThreadPosix::~ThreadPosix() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 87 | DCHECK(thread_checker_.CalledOnValidThread()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 90 | // TODO(pbos): Make Start void, calling code really doesn't support failures |
| 91 | // here. |
| 92 | bool ThreadPosix::Start() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 93 | DCHECK(thread_checker_.CalledOnValidThread()); |
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); |
| 98 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 99 | CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); |
| 100 | started_ = true; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 101 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 104 | bool ThreadPosix::Stop() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 105 | DCHECK(thread_checker_.CalledOnValidThread()); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 106 | if (!started_) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 107 | return true; |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 108 | |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 109 | stop_event_.Set(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 110 | CHECK_EQ(0, pthread_join(thread_, nullptr)); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 111 | started_ = false; |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 112 | stop_event_.Reset(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 113 | |
| 114 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 117 | void ThreadPosix::Run() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 118 | if (!name_.empty()) { |
| 119 | // Setting the thread name may fail (harmlessly) if running inside a |
| 120 | // sandbox. Ignore failures if they happen. |
| 121 | #if defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 122 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name_.c_str())); |
| 123 | #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 124 | pthread_setname_np(name_.substr(0, 63).c_str()); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 125 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 126 | } |
henrike@webrtc.org | ea6c12e | 2014-09-29 18:25:27 +0000 | [diff] [blame] | 127 | |
| 128 | #ifdef WEBRTC_THREAD_RR |
| 129 | const int policy = SCHED_RR; |
| 130 | #else |
| 131 | const int policy = SCHED_FIFO; |
| 132 | #endif |
| 133 | const int min_prio = sched_get_priority_min(policy); |
| 134 | const int max_prio = sched_get_priority_max(policy); |
| 135 | if ((min_prio == -1) || (max_prio == -1)) { |
| 136 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 137 | "unable to retreive min or max priority for threads"); |
| 138 | } |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 139 | |
henrike@webrtc.org | ea6c12e | 2014-09-29 18:25:27 +0000 | [diff] [blame] | 140 | if (max_prio - min_prio > 2) { |
| 141 | sched_param param; |
| 142 | param.sched_priority = ConvertToSystemPriority(prio_, min_prio, max_prio); |
| 143 | if (pthread_setschedparam(pthread_self(), policy, ¶m) != 0) { |
| 144 | WEBRTC_TRACE( |
| 145 | kTraceError, kTraceUtility, -1, "unable to set thread priority"); |
| 146 | } |
| 147 | } |
| 148 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 149 | // It's a requirement that for successful thread creation that the run |
| 150 | // function be called at least once (see RunFunctionIsCalled unit test), |
| 151 | // so to fullfill that requirement, we use a |do| loop and not |while|. |
| 152 | do { |
| 153 | if (!run_function_(obj_)) |
| 154 | break; |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 155 | } while (!stop_event_.Wait(0)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 157 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 158 | } // namespace webrtc |