| 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 <sys/errno.h> | 5 #include <sys/errno.h> |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "vm/thread.h" | 8 #include "platform/thread.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 #define VALIDATE_PTHREAD_RESULT(result) \ | 12 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 13 if (result != 0) { \ | 13 if (result != 0) { \ |
| 14 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ | 14 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ |
| 15 } | 15 } |
| 16 | 16 |
| 17 | 17 |
| 18 class ThreadStartData { | |
| 19 public: | |
| 20 ThreadStartData(Thread::ThreadStartFunction function, | |
| 21 uword parameter, | |
| 22 Thread* thread) | |
| 23 : function_(function), parameter_(parameter), thread_(thread) {} | |
| 24 | |
| 25 Thread::ThreadStartFunction function() const { return function_; } | |
| 26 uword parameter() const { return parameter_; } | |
| 27 Thread* thread() const { return thread_; } | |
| 28 | |
| 29 private: | |
| 30 Thread::ThreadStartFunction function_; | |
| 31 uword parameter_; | |
| 32 Thread* thread_; | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | |
| 35 }; | |
| 36 | |
| 37 | |
| 38 // Dispatch to the thread start function provided by the caller. This trampoline | |
| 39 // is used to ensure that the thread is properly destroyed if the thread just | |
| 40 // exits. | |
| 41 static void* ThreadStart(void* data_ptr) { | |
| 42 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | |
| 43 | |
| 44 Thread::ThreadStartFunction function = data->function(); | |
| 45 uword parameter = data->parameter(); | |
| 46 Thread* thread = data->thread(); | |
| 47 delete data; | |
| 48 | |
| 49 // Call the supplied thread start function handing it its parameters. | |
| 50 function(parameter); | |
| 51 | |
| 52 // When the function returns here, make sure that the thread is deleted. | |
| 53 delete thread; | |
| 54 | |
| 55 return NULL; | |
| 56 } | |
| 57 | |
| 58 | |
| 59 Thread::Thread(ThreadStartFunction function, uword parameter) { | |
| 60 pthread_attr_t attr; | |
| 61 int result = pthread_attr_init(&attr); | |
| 62 VALIDATE_PTHREAD_RESULT(result); | |
| 63 | |
| 64 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
| 65 VALIDATE_PTHREAD_RESULT(result); | |
| 66 | |
| 67 result = pthread_attr_setstacksize(&attr, 64 * KB); | |
| 68 VALIDATE_PTHREAD_RESULT(result); | |
| 69 | |
| 70 ThreadStartData* data = new ThreadStartData(function, parameter, this); | |
| 71 | |
| 72 pthread_t tid; | |
| 73 result = pthread_create(&tid, | |
| 74 &attr, | |
| 75 ThreadStart, | |
| 76 data); | |
| 77 VALIDATE_PTHREAD_RESULT(result); | |
| 78 | |
| 79 data_.tid_ = tid; | |
| 80 | |
| 81 result = pthread_attr_destroy(&attr); | |
| 82 VALIDATE_PTHREAD_RESULT(result); | |
| 83 } | |
| 84 | |
| 85 | |
| 86 Thread::~Thread() { | |
| 87 } | |
| 88 | |
| 89 | |
| 90 Mutex::Mutex() { | 18 Mutex::Mutex() { |
| 91 pthread_mutexattr_t attr; | 19 pthread_mutexattr_t attr; |
| 92 int result = pthread_mutexattr_init(&attr); | 20 int result = pthread_mutexattr_init(&attr); |
| 93 VALIDATE_PTHREAD_RESULT(result); | 21 VALIDATE_PTHREAD_RESULT(result); |
| 94 | 22 |
| 95 #if defined(DEBUG) | 23 #if defined(DEBUG) |
| 96 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 24 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
| 97 VALIDATE_PTHREAD_RESULT(result); | 25 VALIDATE_PTHREAD_RESULT(result); |
| 98 #endif // defined(DEBUG) | 26 #endif // defined(DEBUG) |
| 99 | 27 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 } | 147 } |
| 220 | 148 |
| 221 | 149 |
| 222 void Monitor::NotifyAll() { | 150 void Monitor::NotifyAll() { |
| 223 // TODO(iposva): Do we need to track lock owners? | 151 // TODO(iposva): Do we need to track lock owners? |
| 224 int result = pthread_cond_broadcast(data_.cond()); | 152 int result = pthread_cond_broadcast(data_.cond()); |
| 225 VALIDATE_PTHREAD_RESULT(result); | 153 VALIDATE_PTHREAD_RESULT(result); |
| 226 } | 154 } |
| 227 | 155 |
| 228 } // namespace dart | 156 } // namespace dart |
| OLD | NEW |