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