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, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 72 | ThreadPriority prio, const char* thread_name) |
| 73 | : run_function_(func), |
| 74 | obj_(obj), |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 75 | prio_(prio), |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 76 | stop_event_(false, 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 | 27c0be9 | 2015-03-19 14:35:58 +0000 | [diff] [blame^] | 79 | DCHECK(name_.length() < 64); |
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()); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 94 | DCHECK(!thread_) << "Thread already started?"; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 95 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 96 | ThreadAttributes attr; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 97 | // Set the stack stack size to 1M. |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 98 | pthread_attr_setstacksize(&attr, 1024 * 1024); |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 99 | CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 100 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 103 | bool ThreadPosix::Stop() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 104 | DCHECK(thread_checker_.CalledOnValidThread()); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 105 | if (!thread_) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 106 | return true; |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 107 | |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 108 | stop_event_.Set(); |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 109 | CHECK_EQ(0, pthread_join(thread_, nullptr)); |
tommi@webrtc.org | aba9219 | 2015-03-16 16:05:50 +0000 | [diff] [blame] | 110 | thread_ = 0; |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 111 | |
| 112 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 115 | void ThreadPosix::Run() { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 116 | if (!name_.empty()) { |
| 117 | // Setting the thread name may fail (harmlessly) if running inside a |
| 118 | // sandbox. Ignore failures if they happen. |
| 119 | #if defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 120 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name_.c_str())); |
| 121 | #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 122 | pthread_setname_np(name_.substr(0, 63).c_str()); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 123 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 124 | } |
henrike@webrtc.org | ea6c12e | 2014-09-29 18:25:27 +0000 | [diff] [blame] | 125 | |
| 126 | #ifdef WEBRTC_THREAD_RR |
| 127 | const int policy = SCHED_RR; |
| 128 | #else |
| 129 | const int policy = SCHED_FIFO; |
| 130 | #endif |
| 131 | const int min_prio = sched_get_priority_min(policy); |
| 132 | const int max_prio = sched_get_priority_max(policy); |
| 133 | if ((min_prio == -1) || (max_prio == -1)) { |
| 134 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 135 | "unable to retreive min or max priority for threads"); |
| 136 | } |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 137 | |
henrike@webrtc.org | ea6c12e | 2014-09-29 18:25:27 +0000 | [diff] [blame] | 138 | if (max_prio - min_prio > 2) { |
| 139 | sched_param param; |
| 140 | param.sched_priority = ConvertToSystemPriority(prio_, min_prio, max_prio); |
| 141 | if (pthread_setschedparam(pthread_self(), policy, ¶m) != 0) { |
| 142 | WEBRTC_TRACE( |
| 143 | kTraceError, kTraceUtility, -1, "unable to set thread priority"); |
| 144 | } |
| 145 | } |
| 146 | |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 147 | // It's a requirement that for successful thread creation that the run |
| 148 | // function be called at least once (see RunFunctionIsCalled unit test), |
| 149 | // so to fullfill that requirement, we use a |do| loop and not |while|. |
| 150 | do { |
| 151 | if (!run_function_(obj_)) |
| 152 | break; |
tommi@webrtc.org | d0165c6 | 2015-02-09 11:47:57 +0000 | [diff] [blame] | 153 | } while (!stop_event_.Wait(0)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame] | 155 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 156 | } // namespace webrtc |