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