Chromium Code Reviews| 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; | |
| 22 struct timeval delta; | |
| 23 // Convert the millis to a timeval delta. | |
| 24 int64_t secs = millis / 1000; | 21 int64_t secs = millis / 1000; |
|
siva
2011/11/14 18:32:28
Maybe we should have 3 constants to make this more
cshapiro
2011/11/14 22:24:25
Good idea. I have added to globals.h time constan
| |
| 25 int64_t micros = (millis - (secs * 1000)) * 1000; | 22 int64_t nanos = (millis - (secs * 1000)) * 1000 * 1000; |
| 26 delta.tv_sec = secs; | 23 int result = clock_gettime(CLOCK_MONOTONIC, ts); |
| 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); | 24 ASSERT(result == 0); |
| 31 timeradd(&time, &delta, &time); | 25 ts->tv_sec += secs; |
| 32 TIMEVAL_TO_TIMESPEC(&time, ts); | 26 ts->tv_nsec += nanos; |
| 27 if (ts->tv_nsec >= (1000 * 1000 * 1000)) { | |
| 28 ts->tv_sec += 1; | |
| 29 ts->tv_nsec -= (1000 * 1000 * 1000); | |
| 30 } | |
| 33 } | 31 } |
| 34 | 32 |
| 35 | 33 |
| 36 class ThreadStartData { | 34 class ThreadStartData { |
| 37 public: | 35 public: |
| 38 ThreadStartData(Thread::ThreadStartFunction function, | 36 ThreadStartData(Thread::ThreadStartFunction function, |
| 39 uword parameter, | 37 uword parameter, |
| 40 Thread* thread) | 38 Thread* thread) |
| 41 : function_(function), parameter_(parameter), thread_(thread) {} | 39 : function_(function), parameter_(parameter), thread_(thread) {} |
| 42 | 40 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 void Mutex::Unlock() { | 153 void Mutex::Unlock() { |
| 156 // TODO(iposva): Do we need to track lock owners? | 154 // TODO(iposva): Do we need to track lock owners? |
| 157 int result = pthread_mutex_unlock(data_.mutex()); | 155 int result = pthread_mutex_unlock(data_.mutex()); |
| 158 // Specifically check for wrong thread unlocking to aid debugging. | 156 // Specifically check for wrong thread unlocking to aid debugging. |
| 159 ASSERT(result != EPERM); | 157 ASSERT(result != EPERM); |
| 160 ASSERT(result == 0); // Verify no other errors. | 158 ASSERT(result == 0); // Verify no other errors. |
| 161 } | 159 } |
| 162 | 160 |
| 163 | 161 |
| 164 Monitor::Monitor() { | 162 Monitor::Monitor() { |
| 165 pthread_mutexattr_t attr; | 163 { |
|
siva
2011/11/14 18:32:28
The scoping feels weird why not just name the vari
cshapiro
2011/11/14 22:24:25
Yeah, in fact, I wrote it that way originally. Re
| |
| 166 int result = pthread_mutexattr_init(&attr); | 164 pthread_mutexattr_t attr; |
| 167 VALIDATE_PTHREAD_RESULT(result); | 165 int result = pthread_mutexattr_init(&attr); |
| 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(&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(), &attr); |
| 175 VALIDATE_PTHREAD_RESULT(result); | 174 VALIDATE_PTHREAD_RESULT(result); |
| 176 | 175 |
| 177 result = pthread_mutexattr_destroy(&attr); | 176 result = pthread_mutexattr_destroy(&attr); |
| 178 VALIDATE_PTHREAD_RESULT(result); | 177 VALIDATE_PTHREAD_RESULT(result); |
| 178 } | |
| 179 | 179 |
| 180 result = pthread_cond_init(data_.cond(), NULL); | 180 { |
| 181 VALIDATE_PTHREAD_RESULT(result); | 181 pthread_condattr_t attr; |
| 182 int result = pthread_condattr_init(&attr); | |
| 183 VALIDATE_PTHREAD_RESULT(result); | |
| 184 | |
| 185 result = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); | |
| 186 VALIDATE_PTHREAD_RESULT(result); | |
| 187 | |
| 188 result = pthread_cond_init(data_.cond(), &attr); | |
| 189 VALIDATE_PTHREAD_RESULT(result); | |
| 190 | |
| 191 result = pthread_condattr_destroy(&attr); | |
| 192 VALIDATE_PTHREAD_RESULT(result); | |
| 193 } | |
| 182 } | 194 } |
| 183 | 195 |
| 184 | 196 |
| 185 Monitor::~Monitor() { | 197 Monitor::~Monitor() { |
| 186 int result = pthread_mutex_destroy(data_.mutex()); | 198 int result = pthread_mutex_destroy(data_.mutex()); |
| 187 VALIDATE_PTHREAD_RESULT(result); | 199 VALIDATE_PTHREAD_RESULT(result); |
| 188 | 200 |
| 189 result = pthread_cond_destroy(data_.cond()); | 201 result = pthread_cond_destroy(data_.cond()); |
| 190 VALIDATE_PTHREAD_RESULT(result); | 202 VALIDATE_PTHREAD_RESULT(result); |
| 191 } | 203 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 } | 244 } |
| 233 | 245 |
| 234 | 246 |
| 235 void Monitor::NotifyAll() { | 247 void Monitor::NotifyAll() { |
| 236 // TODO(iposva): Do we need to track lock owners? | 248 // TODO(iposva): Do we need to track lock owners? |
| 237 int result = pthread_cond_broadcast(data_.cond()); | 249 int result = pthread_cond_broadcast(data_.cond()); |
| 238 VALIDATE_PTHREAD_RESULT(result); | 250 VALIDATE_PTHREAD_RESULT(result); |
| 239 } | 251 } |
| 240 | 252 |
| 241 } // namespace dart | 253 } // namespace dart |
| OLD | NEW |