Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 7 | 7 |
| 8 #include "platform/thread.h" | 8 #include "platform/thread.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 11 #include <sys/resource.h> // NOLINT | |
| 11 #include <sys/time.h> // NOLINT | 12 #include <sys/time.h> // NOLINT |
| 12 | 13 |
| 13 #include "platform/assert.h" | 14 #include "platform/assert.h" |
| 14 | 15 |
| 15 namespace dart { | 16 namespace dart { |
| 16 | 17 |
| 17 #define VALIDATE_PTHREAD_RESULT(result) \ | 18 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 18 if (result != 0) { \ | 19 if (result != 0) { \ |
| 19 const int kBufferSize = 1024; \ | 20 const int kBufferSize = 1024; \ |
| 20 char error_buf[kBufferSize]; \ | 21 char error_buf[kBufferSize]; \ |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 47 ASSERT(result == 0); | 48 ASSERT(result == 0); |
| 48 ts->tv_sec += secs; | 49 ts->tv_sec += secs; |
| 49 ts->tv_nsec += nanos; | 50 ts->tv_nsec += nanos; |
| 50 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 51 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
| 51 ts->tv_sec += 1; | 52 ts->tv_sec += 1; |
| 52 ts->tv_nsec -= kNanosecondsPerSecond; | 53 ts->tv_nsec -= kNanosecondsPerSecond; |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 | 57 |
| 58 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { | |
| 59 int64_t secs = micros / kMicrosecondsPerSecond; | |
| 60 int64_t nanos = | |
| 61 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; | |
| 62 int result = clock_gettime(CLOCK_MONOTONIC, ts); | |
| 63 ASSERT(result == 0); | |
| 64 ts->tv_sec += secs; | |
| 65 ts->tv_nsec += nanos; | |
| 66 if (ts->tv_nsec >= kNanosecondsPerSecond) { | |
| 67 ts->tv_sec += 1; | |
| 68 ts->tv_nsec -= kNanosecondsPerSecond; | |
| 69 } | |
|
siva
2013/10/28 05:19:21
This seems a repeat of the code above why not repl
Cutch
2013/11/04 20:36:05
Done.
| |
| 70 } | |
| 71 | |
| 72 | |
| 57 class ThreadStartData { | 73 class ThreadStartData { |
| 58 public: | 74 public: |
| 59 ThreadStartData(Thread::ThreadStartFunction function, | 75 ThreadStartData(Thread::ThreadStartFunction function, |
| 60 uword parameter) | 76 uword parameter) |
| 61 : function_(function), parameter_(parameter) {} | 77 : function_(function), parameter_(parameter) {} |
| 62 | 78 |
| 63 Thread::ThreadStartFunction function() const { return function_; } | 79 Thread::ThreadStartFunction function() const { return function_; } |
| 64 uword parameter() const { return parameter_; } | 80 uword parameter() const { return parameter_; } |
| 65 | 81 |
| 66 private: | 82 private: |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 VALIDATE_PTHREAD_RESULT(result); | 153 VALIDATE_PTHREAD_RESULT(result); |
| 138 } | 154 } |
| 139 | 155 |
| 140 | 156 |
| 141 intptr_t Thread::GetMaxStackSize() { | 157 intptr_t Thread::GetMaxStackSize() { |
| 142 const int kStackSize = (128 * kWordSize * KB); | 158 const int kStackSize = (128 * kWordSize * KB); |
| 143 return kStackSize; | 159 return kStackSize; |
| 144 } | 160 } |
| 145 | 161 |
| 146 | 162 |
| 163 ThreadId Thread::GetCurrentThreadId() { | |
| 164 return pthread_self(); | |
| 165 } | |
| 166 | |
| 167 | |
| 168 void Thread::GetThreadCPUUsage(int64_t* cpu_usage) { | |
| 169 ASSERT(cpu_usage != NULL); | |
| 170 struct timespec ts; | |
| 171 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | |
| 172 ASSERT(r == 0); | |
| 173 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / | |
| 174 kNanosecondsPerMicrosecond; | |
| 175 } | |
| 176 | |
| 177 | |
| 147 Mutex::Mutex() { | 178 Mutex::Mutex() { |
| 148 pthread_mutexattr_t attr; | 179 pthread_mutexattr_t attr; |
| 149 int result = pthread_mutexattr_init(&attr); | 180 int result = pthread_mutexattr_init(&attr); |
| 150 VALIDATE_PTHREAD_RESULT(result); | 181 VALIDATE_PTHREAD_RESULT(result); |
| 151 | 182 |
| 152 #if defined(DEBUG) | 183 #if defined(DEBUG) |
| 153 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 184 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
| 154 VALIDATE_PTHREAD_RESULT(result); | 185 VALIDATE_PTHREAD_RESULT(result); |
| 155 #endif // defined(DEBUG) | 186 #endif // defined(DEBUG) |
| 156 | 187 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); | 298 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); |
| 268 ASSERT((result == 0) || (result == ETIMEDOUT)); | 299 ASSERT((result == 0) || (result == ETIMEDOUT)); |
| 269 if (result == ETIMEDOUT) { | 300 if (result == ETIMEDOUT) { |
| 270 retval = kTimedOut; | 301 retval = kTimedOut; |
| 271 } | 302 } |
| 272 } | 303 } |
| 273 return retval; | 304 return retval; |
| 274 } | 305 } |
| 275 | 306 |
| 276 | 307 |
| 308 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { | |
| 309 // TODO(iposva): Do we need to track lock owners? | |
| 310 Monitor::WaitResult retval = kNotified; | |
| 311 if (micros == 0) { | |
|
siva
2013/10/28 05:19:21
should be if (micros == dart::Monitor::kNoTimeout)
Cutch
2013/11/04 20:36:05
Done.
| |
| 312 // Wait forever. | |
| 313 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | |
| 314 VALIDATE_PTHREAD_RESULT(result); | |
| 315 } else { | |
| 316 struct timespec ts; | |
| 317 ComputeTimeSpecMicros(&ts, micros); | |
| 318 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); | |
| 319 ASSERT((result == 0) || (result == ETIMEDOUT)); | |
| 320 if (result == ETIMEDOUT) { | |
| 321 retval = kTimedOut; | |
| 322 } | |
|
siva
2013/10/28 05:19:21
else {
VALIDATE_PTHREAD_RESULT(result); ?
}
Cutch
2013/11/04 20:36:05
The above ASSERT((result == 0) || (result == ETIME
| |
| 323 } | |
| 324 return retval; | |
| 325 } | |
| 326 | |
| 327 | |
| 277 void Monitor::Notify() { | 328 void Monitor::Notify() { |
| 278 // TODO(iposva): Do we need to track lock owners? | 329 // TODO(iposva): Do we need to track lock owners? |
| 279 int result = pthread_cond_signal(data_.cond()); | 330 int result = pthread_cond_signal(data_.cond()); |
| 280 VALIDATE_PTHREAD_RESULT(result); | 331 VALIDATE_PTHREAD_RESULT(result); |
| 281 } | 332 } |
| 282 | 333 |
| 283 | 334 |
| 284 void Monitor::NotifyAll() { | 335 void Monitor::NotifyAll() { |
| 285 // TODO(iposva): Do we need to track lock owners? | 336 // TODO(iposva): Do we need to track lock owners? |
| 286 int result = pthread_cond_broadcast(data_.cond()); | 337 int result = pthread_cond_broadcast(data_.cond()); |
| 287 VALIDATE_PTHREAD_RESULT(result); | 338 VALIDATE_PTHREAD_RESULT(result); |
| 288 } | 339 } |
| 289 | 340 |
| 290 } // namespace dart | 341 } // namespace dart |
| 291 | 342 |
| 292 #endif // defined(TARGET_OS_LINUX) | 343 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |