Chromium Code Reviews| Index: runtime/platform/thread_linux.cc |
| diff --git a/runtime/platform/thread_linux.cc b/runtime/platform/thread_linux.cc |
| index 338c4c8d1cab8cf9ebb01ae465117f5e20c6b779..2e2d4e1da2d2d0fd3a2fa0b9015ad7a1d8e6987d 100644 |
| --- a/runtime/platform/thread_linux.cc |
| +++ b/runtime/platform/thread_linux.cc |
| @@ -11,6 +11,8 @@ |
| namespace dart { |
| +const ThreadHandle Thread::kInvalidThreadHandle = 0; |
| + |
| #define VALIDATE_PTHREAD_RESULT(result) \ |
| if (result != 0) { \ |
| FATAL2("pthread error: %d (%s)", result, strerror(result)); \ |
| @@ -32,48 +34,7 @@ static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
| } |
| -class ThreadStartData { |
| - public: |
| - ThreadStartData(Thread::ThreadStartFunction function, |
| - uword parameter, |
| - Thread* thread) |
| - : function_(function), parameter_(parameter), thread_(thread) {} |
| - |
| - Thread::ThreadStartFunction function() const { return function_; } |
| - uword parameter() const { return parameter_; } |
| - Thread* thread() const { return thread_; } |
| - |
| - private: |
| - Thread::ThreadStartFunction function_; |
| - uword parameter_; |
| - Thread* thread_; |
| - |
| - DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| -}; |
| - |
| - |
| -// Dispatch to the thread start function provided by the caller. This trampoline |
| -// is used to ensure that the thread is properly destroyed if the thread just |
| -// exits. |
| -static void* ThreadStart(void* data_ptr) { |
| - ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| - |
| - Thread::ThreadStartFunction function = data->function(); |
| - uword parameter = data->parameter(); |
| - Thread* thread = data->thread(); |
| - delete data; |
| - |
| - // Call the supplied thread start function handing it its parameters. |
| - function(parameter); |
| - |
| - // When the function returns here, make sure that the thread is deleted. |
| - delete thread; |
| - |
| - return NULL; |
| -} |
| - |
| - |
| -Thread::Thread(ThreadStartFunction function, uword parameter) { |
| +ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) { |
| pthread_attr_t attr; |
| int result = pthread_attr_init(&attr); |
| VALIDATE_PTHREAD_RESULT(result); |
| @@ -84,23 +45,36 @@ Thread::Thread(ThreadStartFunction function, uword parameter) { |
| result = pthread_attr_setstacksize(&attr, 1024 * KB); |
| VALIDATE_PTHREAD_RESULT(result); |
| - ThreadStartData* data = new ThreadStartData(function, parameter, this); |
| - |
| pthread_t tid; |
| result = pthread_create(&tid, |
| &attr, |
| - ThreadStart, |
| - data); |
| + reinterpret_cast<void* (*)(void*)>(function), // NOLINT |
|
Mads Ager (google)
2012/01/20 12:35:34
Can we typedef the 'void* (*)(void*)' function typ
Søren Gjesse
2012/01/20 13:25:45
typedefed as PThreadStartFunction (in Mac OS file
|
| + reinterpret_cast<void*>(parameter)); |
| VALIDATE_PTHREAD_RESULT(result); |
| - data_.tid_ = tid; |
| - |
| result = pthread_attr_destroy(&attr); |
| VALIDATE_PTHREAD_RESULT(result); |
| + |
| + return tid; |
| } |
| -Thread::~Thread() { |
| +bool Thread::Join(ThreadHandle thread, int64_t millis) { |
| + int result; |
| + if (millis == kNoTimeout) { |
| + result = pthread_join(thread, NULL); |
| + VALIDATE_PTHREAD_RESULT(result); |
| + return true; |
| + } else { |
| + struct timespec ts; |
| + ComputeTimeSpec(&ts, millis); |
| + result = pthread_timedjoin_np(thread, NULL, &ts); |
| + if (result == ETIMEDOUT) { |
| + return false; |
| + } |
| + VALIDATE_PTHREAD_RESULT(result); |
| + return true; |
| + } |
| } |