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 | |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 11 | // The state of a thread is controlled by the two member variables |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 12 | // alive_ and dead_. |
| 13 | // alive_ represents the state the thread has been ordered to achieve. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 14 | // It is set to true by the thread at startup, and is set to false by |
| 15 | // other threads, using SetNotAlive() and Stop(). |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 16 | // dead_ represents the state the thread has achieved. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 17 | // It is written by the thread encapsulated by this class only |
| 18 | // (except at init). It is read only by the Stop() method. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 19 | // The Run() method fires event_ when it's started; this ensures that the |
| 20 | // Start() method does not continue until after dead_ is false. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 21 | // This protects against premature Stop() calls from the creator thread, but |
| 22 | // not from other threads. |
| 23 | |
| 24 | // Their transitions and states: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 25 | // alive_ dead_ Set by |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 26 | // false true Constructor |
| 27 | // true false Run() method entry |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 28 | // false any Run() method run_function failure |
| 29 | // any false Run() method exit (happens only with alive_ false) |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 30 | // false any SetNotAlive |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 31 | // false any Stop Stop waits for dead_ to become true. |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 32 | // |
| 33 | // Summarized a different way: |
| 34 | // Variable Writer Reader |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 35 | // alive_ Constructor(false) Run.loop |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 36 | // Run.start(true) |
| 37 | // Run.fail(false) |
| 38 | // SetNotAlive(false) |
| 39 | // Stop(false) |
| 40 | // |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 41 | // dead_ Constructor(true) Stop.loop |
hta@webrtc.org | e1919f4 | 2012-05-22 15:57:34 +0000 | [diff] [blame] | 42 | // Run.start(false) |
| 43 | // Run.exit(true) |
| 44 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 45 | #include "webrtc/system_wrappers/source/thread_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 47 | #include <algorithm> |
| 48 | |
| 49 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | #include <errno.h> |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 51 | #include <string.h> // strncpy |
| 52 | #include <time.h> // nanosleep |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | #include <unistd.h> |
| 54 | #ifdef WEBRTC_LINUX |
| 55 | #include <sys/types.h> |
| 56 | #include <sched.h> |
| 57 | #include <sys/syscall.h> |
| 58 | #include <linux/unistd.h> |
| 59 | #include <sys/prctl.h> |
| 60 | #endif |
| 61 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 62 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 63 | #include "webrtc/system_wrappers/interface/event_wrapper.h" |
| 64 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
| 66 | namespace webrtc { |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 67 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 68 | int ConvertToSystemPriority(ThreadPriority priority, int min_prio, |
| 69 | int max_prio) { |
| 70 | assert(max_prio - min_prio > 2); |
| 71 | const int top_prio = max_prio - 1; |
| 72 | const int low_prio = min_prio + 1; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 73 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 74 | switch (priority) { |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 75 | case kLowPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 76 | return low_prio; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 77 | case kNormalPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 78 | // The -1 ensures that the kHighPriority is always greater or equal to |
| 79 | // kNormalPriority. |
| 80 | return (low_prio + top_prio - 1) / 2; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 81 | case kHighPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 82 | return std::max(top_prio - 2, low_prio); |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 83 | case kHighestPriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 84 | return std::max(top_prio - 1, low_prio); |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 85 | case kRealtimePriority: |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 86 | return top_prio; |
| 87 | } |
| 88 | assert(false); |
| 89 | return low_prio; |
henrike@webrtc.org | 5ba4411 | 2012-10-05 14:36:54 +0000 | [diff] [blame] | 90 | } |
| 91 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | extern "C" |
| 93 | { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 94 | static void* StartThread(void* lp_parameter) { |
| 95 | static_cast<ThreadPosix*>(lp_parameter)->Run(); |
| 96 | return 0; |
| 97 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | } |
| 99 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 100 | ThreadWrapper* ThreadPosix::Create(ThreadRunFunction func, |
| 101 | ThreadObj obj, |
| 102 | ThreadPriority prio, |
| 103 | const char* thread_name) { |
| 104 | ThreadPosix* ptr = new ThreadPosix(func, obj, prio, thread_name); |
| 105 | if (!ptr) { |
| 106 | return NULL; |
| 107 | } |
| 108 | const int error = ptr->Construct(); |
| 109 | if (error) { |
| 110 | delete ptr; |
| 111 | return NULL; |
| 112 | } |
| 113 | return ptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 116 | ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 117 | ThreadPriority prio, const char* thread_name) |
| 118 | : run_function_(func), |
| 119 | obj_(obj), |
| 120 | crit_state_(CriticalSectionWrapper::CreateCriticalSection()), |
| 121 | alive_(false), |
| 122 | dead_(true), |
| 123 | prio_(prio), |
| 124 | event_(EventWrapper::Create()), |
| 125 | name_(), |
| 126 | set_thread_name_(false), |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 127 | #if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 128 | pid_(-1), |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 129 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 130 | attr_(), |
| 131 | thread_(0) { |
| 132 | if (thread_name != NULL) { |
| 133 | set_thread_name_ = true; |
| 134 | strncpy(name_, thread_name, kThreadMaxNameLength); |
| 135 | name_[kThreadMaxNameLength - 1] = '\0'; |
| 136 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 139 | uint32_t ThreadWrapper::GetThreadId() { |
| 140 | #if defined(WEBRTC_ANDROID) || defined(WEBRTC_LINUX) |
| 141 | return static_cast<uint32_t>(syscall(__NR_gettid)); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 142 | #else |
| 143 | return reinterpret_cast<uint32_t>(pthread_self()); |
| 144 | #endif |
| 145 | } |
| 146 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 147 | int ThreadPosix::Construct() { |
| 148 | int result = 0; |
leozwang@google.com | b352700 | 2011-07-26 17:29:38 +0000 | [diff] [blame] | 149 | #if !defined(WEBRTC_ANDROID) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 150 | // Enable immediate cancellation if requested, see Shutdown(). |
| 151 | result = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); |
| 152 | if (result != 0) { |
| 153 | return -1; |
| 154 | } |
| 155 | result = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); |
| 156 | if (result != 0) { |
| 157 | return -1; |
| 158 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 160 | result = pthread_attr_init(&attr_); |
| 161 | if (result != 0) { |
| 162 | return -1; |
| 163 | } |
| 164 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | } |
| 166 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 167 | ThreadPosix::~ThreadPosix() { |
| 168 | pthread_attr_destroy(&attr_); |
| 169 | delete event_; |
| 170 | delete crit_state_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | } |
| 172 | |
sjlee@webrtc.org | 414fa7f | 2012-09-11 17:25:46 +0000 | [diff] [blame] | 173 | #define HAS_THREAD_ID !defined(WEBRTC_IOS) && !defined(WEBRTC_MAC) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 174 | |
| 175 | bool ThreadPosix::Start(unsigned int& thread_id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 177 | if (!run_function_) { |
| 178 | return false; |
| 179 | } |
| 180 | int result = pthread_attr_setdetachstate(&attr_, PTHREAD_CREATE_DETACHED); |
| 181 | // Set the stack stack size to 1M. |
| 182 | result |= pthread_attr_setstacksize(&attr_, 1024 * 1024); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | #ifdef WEBRTC_THREAD_RR |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 184 | const int policy = SCHED_RR; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | #else |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 186 | const int policy = SCHED_FIFO; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 188 | event_->Reset(); |
| 189 | // If pthread_create was successful, a thread was created and is running. |
| 190 | // Don't return false if it was successful since if there are any other |
| 191 | // failures the state will be: thread was started but not configured as |
| 192 | // asked for. However, the caller of this API will assume that a false |
| 193 | // return value means that the thread never started. |
| 194 | result |= pthread_create(&thread_, &attr_, &StartThread, this); |
| 195 | if (result != 0) { |
| 196 | return false; |
| 197 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 199 | // Wait up to 10 seconds for the OS to call the callback function. Prevents |
| 200 | // race condition if Stop() is called too quickly after start. |
| 201 | if (kEventSignaled != event_->Wait(WEBRTC_EVENT_10_SEC)) { |
| 202 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 203 | "posix thread event never triggered"); |
| 204 | |
| 205 | // Timed out. Something went wrong. |
| 206 | run_function_ = NULL; |
| 207 | return true; |
| 208 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | |
| 210 | #if HAS_THREAD_ID |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 211 | thread_id = static_cast<unsigned int>(thread_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 212 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 213 | sched_param param; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 215 | const int min_prio = sched_get_priority_min(policy); |
| 216 | const int max_prio = sched_get_priority_max(policy); |
| 217 | |
| 218 | if ((min_prio == EINVAL) || (max_prio == EINVAL)) { |
| 219 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 220 | "unable to retreive min or max priority for threads"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | return true; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 222 | } |
| 223 | if (max_prio - min_prio <= 2) { |
| 224 | // There is no room for setting priorities with any granularity. |
| 225 | return true; |
| 226 | } |
| 227 | param.sched_priority = ConvertToSystemPriority(prio_, min_prio, max_prio); |
| 228 | result = pthread_setschedparam(thread_, policy, ¶m); |
| 229 | if (result == EINVAL) { |
| 230 | WEBRTC_TRACE(kTraceError, kTraceUtility, -1, |
| 231 | "unable to set thread priority"); |
| 232 | } |
| 233 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 234 | } |
| 235 | |
leozwang@webrtc.org | 0a272eb | 2012-02-15 22:35:29 +0000 | [diff] [blame] | 236 | // CPU_ZERO and CPU_SET are not available in NDK r7, so disable |
| 237 | // SetAffinity on Android for now. |
| 238 | #if (defined(WEBRTC_LINUX) && (!defined(WEBRTC_ANDROID))) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 239 | bool ThreadPosix::SetAffinity(const int* processor_numbers, |
| 240 | const unsigned int amount_of_processors) { |
| 241 | if (!processor_numbers || (amount_of_processors == 0)) { |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | cpu_set_t mask; |
| 245 | CPU_ZERO(&mask); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 247 | for (unsigned int processor = 0; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 248 | processor < amount_of_processors; |
| 249 | ++processor) { |
| 250 | CPU_SET(processor_numbers[processor], &mask); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 251 | } |
| 252 | #if defined(WEBRTC_ANDROID) |
| 253 | // Android. |
| 254 | const int result = syscall(__NR_sched_setaffinity, |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 255 | pid_, |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 256 | sizeof(mask), |
| 257 | &mask); |
| 258 | #else |
| 259 | // "Normal" Linux. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 260 | const int result = sched_setaffinity(pid_, |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 261 | sizeof(mask), |
| 262 | &mask); |
| 263 | #endif |
| 264 | if (result != 0) { |
| 265 | return false; |
| 266 | } |
| 267 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 268 | } |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 269 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | #else |
| 271 | // NOTE: On Mac OS X, use the Thread affinity API in |
| 272 | // /usr/include/mach/thread_policy.h: thread_policy_set and mach_thread_self() |
| 273 | // instead of Linux gettid() syscall. |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 274 | bool ThreadPosix::SetAffinity(const int* , const unsigned int) { |
| 275 | return false; |
| 276 | } |
| 277 | #endif |
| 278 | |
| 279 | void ThreadPosix::SetNotAlive() { |
| 280 | CriticalSectionScoped cs(crit_state_); |
| 281 | alive_ = false; |
| 282 | } |
| 283 | |
| 284 | bool ThreadPosix::Stop() { |
| 285 | bool dead = false; |
| 286 | { |
| 287 | CriticalSectionScoped cs(crit_state_); |
| 288 | alive_ = false; |
| 289 | dead = dead_; |
| 290 | } |
| 291 | |
| 292 | // TODO(hellner) why not use an event here? |
| 293 | // Wait up to 10 seconds for the thread to terminate |
| 294 | for (int i = 0; i < 1000 && !dead; i++) { |
| 295 | timespec t; |
| 296 | t.tv_sec = 0; |
| 297 | t.tv_nsec = 10 * 1000 * 1000; |
| 298 | nanosleep(&t, NULL); |
| 299 | { |
| 300 | CriticalSectionScoped cs(crit_state_); |
| 301 | dead = dead_; |
| 302 | } |
| 303 | } |
| 304 | if (dead) { |
| 305 | return true; |
| 306 | } else { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | return false; |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 308 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 311 | void ThreadPosix::Run() { |
| 312 | { |
| 313 | CriticalSectionScoped cs(crit_state_); |
| 314 | alive_ = true; |
| 315 | dead_ = false; |
| 316 | } |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 317 | #if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)) |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 318 | pid_ = GetThreadId(); |
pwestin@webrtc.org | df9866f | 2012-01-11 08:57:47 +0000 | [diff] [blame] | 319 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 320 | // The event the Start() is waiting for. |
| 321 | event_->Set(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 323 | if (set_thread_name_) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 324 | #ifdef WEBRTC_LINUX |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 325 | prctl(PR_SET_NAME, (unsigned long)name_, 0, 0, 0); |
pwestin@webrtc.org | b54d727 | 2012-01-11 08:28:04 +0000 | [diff] [blame] | 326 | #endif |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 327 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 328 | "Thread with name:%s started ", name_); |
| 329 | } else { |
| 330 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 331 | "Thread without name started"); |
| 332 | } |
| 333 | bool alive = true; |
| 334 | do { |
| 335 | if (run_function_) { |
| 336 | if (!run_function_(obj_)) { |
| 337 | alive = false; |
| 338 | } |
| 339 | } else { |
| 340 | alive = false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 342 | { |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 343 | CriticalSectionScoped cs(crit_state_); |
| 344 | if (!alive) { |
| 345 | alive_ = false; |
| 346 | } |
| 347 | alive = alive_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 348 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 349 | } while (alive); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 350 | |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 351 | if (set_thread_name_) { |
| 352 | // Don't set the name for the trace thread because it may cause a |
| 353 | // deadlock. TODO(hellner) there should be a better solution than |
| 354 | // coupling the thread and the trace class like this. |
| 355 | if (strcmp(name_, "Trace")) { |
| 356 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 357 | "Thread with name:%s stopped", name_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 358 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 359 | } else { |
| 360 | WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, |
| 361 | "Thread without name stopped"); |
| 362 | } |
| 363 | { |
| 364 | CriticalSectionScoped cs(crit_state_); |
| 365 | dead_ = true; |
| 366 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | } |
phoglund@webrtc.org | ec9c942 | 2013-01-02 08:45:03 +0000 | [diff] [blame^] | 368 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 369 | } // namespace webrtc |