| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "vm/thread.h" | |
| 6 | |
| 7 #include <errno.h> | |
| 8 #include <sys/time.h> | |
| 9 | |
| 10 #include "platform/assert.h" | |
| 11 | |
| 12 namespace dart { | |
| 13 | |
| 14 #define VALIDATE_PTHREAD_RESULT(result) \ | |
| 15 if (result != 0) { \ | |
| 16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ | |
| 17 } | |
| 18 | |
| 19 | |
| 20 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { | |
| 21 int64_t secs = millis / kMillisecondsPerSecond; | |
| 22 int64_t nanos = | |
| 23 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; | |
| 24 int result = clock_gettime(CLOCK_MONOTONIC, ts); | |
| 25 ASSERT(result == 0); | |
| 26 ts->tv_sec += secs; | |
| 27 ts->tv_nsec += nanos; | |
| 28 if (ts->tv_nsec >= kNanosecondsPerSecond) { | |
| 29 ts->tv_sec += 1; | |
| 30 ts->tv_nsec -= kNanosecondsPerSecond; | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 | |
| 35 class ThreadStartData { | |
| 36 public: | |
| 37 ThreadStartData(Thread::ThreadStartFunction function, | |
| 38 uword parameter, | |
| 39 Thread* thread) | |
| 40 : function_(function), parameter_(parameter), thread_(thread) {} | |
| 41 | |
| 42 Thread::ThreadStartFunction function() const { return function_; } | |
| 43 uword parameter() const { return parameter_; } | |
| 44 Thread* thread() const { return thread_; } | |
| 45 | |
| 46 private: | |
| 47 Thread::ThreadStartFunction function_; | |
| 48 uword parameter_; | |
| 49 Thread* thread_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | |
| 52 }; | |
| 53 | |
| 54 | |
| 55 // Dispatch to the thread start function provided by the caller. This trampoline | |
| 56 // is used to ensure that the thread is properly destroyed if the thread just | |
| 57 // exits. | |
| 58 static void* ThreadStart(void* data_ptr) { | |
| 59 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | |
| 60 | |
| 61 Thread::ThreadStartFunction function = data->function(); | |
| 62 uword parameter = data->parameter(); | |
| 63 Thread* thread = data->thread(); | |
| 64 delete data; | |
| 65 | |
| 66 // Call the supplied thread start function handing it its parameters. | |
| 67 function(parameter); | |
| 68 | |
| 69 // When the function returns here, make sure that the thread is deleted. | |
| 70 delete thread; | |
| 71 | |
| 72 return NULL; | |
| 73 } | |
| 74 | |
| 75 | |
| 76 Thread::Thread(ThreadStartFunction function, uword parameter) { | |
| 77 pthread_attr_t attr; | |
| 78 int result = pthread_attr_init(&attr); | |
| 79 VALIDATE_PTHREAD_RESULT(result); | |
| 80 | |
| 81 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
| 82 VALIDATE_PTHREAD_RESULT(result); | |
| 83 | |
| 84 result = pthread_attr_setstacksize(&attr, 1024 * KB); | |
| 85 VALIDATE_PTHREAD_RESULT(result); | |
| 86 | |
| 87 ThreadStartData* data = new ThreadStartData(function, parameter, this); | |
| 88 | |
| 89 pthread_t tid; | |
| 90 result = pthread_create(&tid, | |
| 91 &attr, | |
| 92 ThreadStart, | |
| 93 data); | |
| 94 VALIDATE_PTHREAD_RESULT(result); | |
| 95 | |
| 96 data_.tid_ = tid; | |
| 97 | |
| 98 result = pthread_attr_destroy(&attr); | |
| 99 VALIDATE_PTHREAD_RESULT(result); | |
| 100 } | |
| 101 | |
| 102 | |
| 103 Thread::~Thread() { | |
| 104 } | |
| 105 | |
| 106 | |
| 107 Mutex::Mutex() { | |
| 108 pthread_mutexattr_t attr; | |
| 109 int result = pthread_mutexattr_init(&attr); | |
| 110 VALIDATE_PTHREAD_RESULT(result); | |
| 111 | |
| 112 #if defined(DEBUG) | |
| 113 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | |
| 114 VALIDATE_PTHREAD_RESULT(result); | |
| 115 #endif // defined(DEBUG) | |
| 116 | |
| 117 result = pthread_mutex_init(data_.mutex(), &attr); | |
| 118 // Verify that creating a pthread_mutex succeeded. | |
| 119 VALIDATE_PTHREAD_RESULT(result); | |
| 120 | |
| 121 result = pthread_mutexattr_destroy(&attr); | |
| 122 VALIDATE_PTHREAD_RESULT(result); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 Mutex::~Mutex() { | |
| 127 int result = pthread_mutex_destroy(data_.mutex()); | |
| 128 // Verify that the pthread_mutex was destroyed. | |
| 129 VALIDATE_PTHREAD_RESULT(result); | |
| 130 } | |
| 131 | |
| 132 | |
| 133 void Mutex::Lock() { | |
| 134 int result = pthread_mutex_lock(data_.mutex()); | |
| 135 // Specifically check for dead lock to help debugging. | |
| 136 ASSERT(result != EDEADLK); | |
| 137 ASSERT(result == 0); // Verify no other errors. | |
| 138 // TODO(iposva): Do we need to track lock owners? | |
| 139 } | |
| 140 | |
| 141 | |
| 142 bool Mutex::TryLock() { | |
| 143 int result = pthread_mutex_trylock(data_.mutex()); | |
| 144 // Return false if the lock is busy and locking failed. | |
| 145 if (result == EBUSY) { | |
| 146 return false; | |
| 147 } | |
| 148 ASSERT(result == 0); // Verify no other errors. | |
| 149 // TODO(iposva): Do we need to track lock owners? | |
| 150 return true; | |
| 151 } | |
| 152 | |
| 153 | |
| 154 void Mutex::Unlock() { | |
| 155 // TODO(iposva): Do we need to track lock owners? | |
| 156 int result = pthread_mutex_unlock(data_.mutex()); | |
| 157 // Specifically check for wrong thread unlocking to aid debugging. | |
| 158 ASSERT(result != EPERM); | |
| 159 ASSERT(result == 0); // Verify no other errors. | |
| 160 } | |
| 161 | |
| 162 | |
| 163 Monitor::Monitor() { | |
| 164 pthread_mutexattr_t mutex_attr; | |
| 165 int result = pthread_mutexattr_init(&mutex_attr); | |
| 166 VALIDATE_PTHREAD_RESULT(result); | |
| 167 | |
| 168 #if defined(DEBUG) | |
| 169 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); | |
| 170 VALIDATE_PTHREAD_RESULT(result); | |
| 171 #endif // defined(DEBUG) | |
| 172 | |
| 173 result = pthread_mutex_init(data_.mutex(), &mutex_attr); | |
| 174 VALIDATE_PTHREAD_RESULT(result); | |
| 175 | |
| 176 result = pthread_mutexattr_destroy(&mutex_attr); | |
| 177 VALIDATE_PTHREAD_RESULT(result); | |
| 178 | |
| 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); | |
| 190 VALIDATE_PTHREAD_RESULT(result); | |
| 191 } | |
| 192 | |
| 193 | |
| 194 Monitor::~Monitor() { | |
| 195 int result = pthread_mutex_destroy(data_.mutex()); | |
| 196 VALIDATE_PTHREAD_RESULT(result); | |
| 197 | |
| 198 result = pthread_cond_destroy(data_.cond()); | |
| 199 VALIDATE_PTHREAD_RESULT(result); | |
| 200 } | |
| 201 | |
| 202 | |
| 203 void Monitor::Enter() { | |
| 204 int result = pthread_mutex_lock(data_.mutex()); | |
| 205 VALIDATE_PTHREAD_RESULT(result); | |
| 206 // TODO(iposva): Do we need to track lock owners? | |
| 207 } | |
| 208 | |
| 209 | |
| 210 void Monitor::Exit() { | |
| 211 // TODO(iposva): Do we need to track lock owners? | |
| 212 int result = pthread_mutex_unlock(data_.mutex()); | |
| 213 VALIDATE_PTHREAD_RESULT(result); | |
| 214 } | |
| 215 | |
| 216 | |
| 217 Monitor::WaitResult Monitor::Wait(int64_t millis) { | |
| 218 // TODO(iposva): Do we need to track lock owners? | |
| 219 Monitor::WaitResult retval = kNotified; | |
| 220 if (millis == 0) { | |
| 221 // Wait forever. | |
| 222 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | |
| 223 VALIDATE_PTHREAD_RESULT(result); | |
| 224 } else { | |
| 225 struct timespec ts; | |
| 226 ComputeTimeSpec(&ts, millis); | |
| 227 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); | |
| 228 ASSERT((result == 0) || (result == ETIMEDOUT)); | |
| 229 if (result == ETIMEDOUT) { | |
| 230 retval = kTimedOut; | |
| 231 } | |
| 232 } | |
| 233 return retval; | |
| 234 } | |
| 235 | |
| 236 | |
| 237 void Monitor::Notify() { | |
| 238 // TODO(iposva): Do we need to track lock owners? | |
| 239 int result = pthread_cond_signal(data_.cond()); | |
| 240 VALIDATE_PTHREAD_RESULT(result); | |
| 241 } | |
| 242 | |
| 243 | |
| 244 void Monitor::NotifyAll() { | |
| 245 // TODO(iposva): Do we need to track lock owners? | |
| 246 int result = pthread_cond_broadcast(data_.cond()); | |
| 247 VALIDATE_PTHREAD_RESULT(result); | |
| 248 } | |
| 249 | |
| 250 } // namespace dart | |
| OLD | NEW |