| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <sys/time.h> | 6 #include <sys/time.h> |
| 7 | 7 |
| 8 #include "vm/thread.h" | 8 #include "vm/thread.h" |
| 9 | 9 |
| 10 #include "vm/assert.h" | 10 #include "vm/assert.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 #define VALIDATE_PTHREAD_RESULT(result) \ | 14 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 15 if (result != 0) { \ | 15 if (result != 0) { \ |
| 16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ | 16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ |
| 17 } | 17 } |
| 18 | 18 |
| 19 | 19 |
| 20 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { | 20 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
| 21 struct timeval time; | 21 int64_t secs = millis / kMillisecondsPerSecond; |
| 22 struct timeval delta; | 22 int64_t nanos = |
| 23 // Convert the millis to a timeval delta. | 23 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; |
| 24 int64_t secs = millis / 1000; | 24 int result = clock_gettime(CLOCK_MONOTONIC, ts); |
| 25 int64_t micros = (millis - (secs * 1000)) * 1000; | |
| 26 delta.tv_sec = secs; | |
| 27 delta.tv_usec = micros; | |
| 28 // Get the current time and add the delta. Convert the result to a timespec. | |
| 29 int result = gettimeofday(&time, NULL); | |
| 30 ASSERT(result == 0); | 25 ASSERT(result == 0); |
| 31 timeradd(&time, &delta, &time); | 26 ts->tv_sec += secs; |
| 32 TIMEVAL_TO_TIMESPEC(&time, ts); | 27 ts->tv_nsec += nanos; |
| 28 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
| 29 ts->tv_sec += 1; |
| 30 ts->tv_nsec -= kNanosecondsPerSecond; |
| 31 } |
| 33 } | 32 } |
| 34 | 33 |
| 35 | 34 |
| 36 class ThreadStartData { | 35 class ThreadStartData { |
| 37 public: | 36 public: |
| 38 ThreadStartData(Thread::ThreadStartFunction function, | 37 ThreadStartData(Thread::ThreadStartFunction function, |
| 39 uword parameter, | 38 uword parameter, |
| 40 Thread* thread) | 39 Thread* thread) |
| 41 : function_(function), parameter_(parameter), thread_(thread) {} | 40 : function_(function), parameter_(parameter), thread_(thread) {} |
| 42 | 41 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void Mutex::Unlock() { | 154 void Mutex::Unlock() { |
| 156 // TODO(iposva): Do we need to track lock owners? | 155 // TODO(iposva): Do we need to track lock owners? |
| 157 int result = pthread_mutex_unlock(data_.mutex()); | 156 int result = pthread_mutex_unlock(data_.mutex()); |
| 158 // Specifically check for wrong thread unlocking to aid debugging. | 157 // Specifically check for wrong thread unlocking to aid debugging. |
| 159 ASSERT(result != EPERM); | 158 ASSERT(result != EPERM); |
| 160 ASSERT(result == 0); // Verify no other errors. | 159 ASSERT(result == 0); // Verify no other errors. |
| 161 } | 160 } |
| 162 | 161 |
| 163 | 162 |
| 164 Monitor::Monitor() { | 163 Monitor::Monitor() { |
| 165 pthread_mutexattr_t attr; | 164 pthread_mutexattr_t mutex_attr; |
| 166 int result = pthread_mutexattr_init(&attr); | 165 int result = pthread_mutexattr_init(&mutex_attr); |
| 167 VALIDATE_PTHREAD_RESULT(result); | 166 VALIDATE_PTHREAD_RESULT(result); |
| 168 | 167 |
| 169 #if defined(DEBUG) | 168 #if defined(DEBUG) |
| 170 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 169 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); |
| 171 VALIDATE_PTHREAD_RESULT(result); | 170 VALIDATE_PTHREAD_RESULT(result); |
| 172 #endif // defined(DEBUG) | 171 #endif // defined(DEBUG) |
| 173 | 172 |
| 174 result = pthread_mutex_init(data_.mutex(), &attr); | 173 result = pthread_mutex_init(data_.mutex(), &mutex_attr); |
| 175 VALIDATE_PTHREAD_RESULT(result); | 174 VALIDATE_PTHREAD_RESULT(result); |
| 176 | 175 |
| 177 result = pthread_mutexattr_destroy(&attr); | 176 result = pthread_mutexattr_destroy(&mutex_attr); |
| 178 VALIDATE_PTHREAD_RESULT(result); | 177 VALIDATE_PTHREAD_RESULT(result); |
| 179 | 178 |
| 180 result = pthread_cond_init(data_.cond(), NULL); | 179 pthread_condattr_t cond_attr; |
| 180 result = pthread_condattr_init(&cond_attr); |
| 181 VALIDATE_PTHREAD_RESULT(result); |
| 182 |
| 183 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
| 184 VALIDATE_PTHREAD_RESULT(result); |
| 185 |
| 186 result = pthread_cond_init(data_.cond(), &cond_attr); |
| 187 VALIDATE_PTHREAD_RESULT(result); |
| 188 |
| 189 result = pthread_condattr_destroy(&cond_attr); |
| 181 VALIDATE_PTHREAD_RESULT(result); | 190 VALIDATE_PTHREAD_RESULT(result); |
| 182 } | 191 } |
| 183 | 192 |
| 184 | 193 |
| 185 Monitor::~Monitor() { | 194 Monitor::~Monitor() { |
| 186 int result = pthread_mutex_destroy(data_.mutex()); | 195 int result = pthread_mutex_destroy(data_.mutex()); |
| 187 VALIDATE_PTHREAD_RESULT(result); | 196 VALIDATE_PTHREAD_RESULT(result); |
| 188 | 197 |
| 189 result = pthread_cond_destroy(data_.cond()); | 198 result = pthread_cond_destroy(data_.cond()); |
| 190 VALIDATE_PTHREAD_RESULT(result); | 199 VALIDATE_PTHREAD_RESULT(result); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 241 } |
| 233 | 242 |
| 234 | 243 |
| 235 void Monitor::NotifyAll() { | 244 void Monitor::NotifyAll() { |
| 236 // TODO(iposva): Do we need to track lock owners? | 245 // TODO(iposva): Do we need to track lock owners? |
| 237 int result = pthread_cond_broadcast(data_.cond()); | 246 int result = pthread_cond_broadcast(data_.cond()); |
| 238 VALIDATE_PTHREAD_RESULT(result); | 247 VALIDATE_PTHREAD_RESULT(result); |
| 239 } | 248 } |
| 240 | 249 |
| 241 } // namespace dart | 250 } // namespace dart |
| OLD | NEW |