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 <errno.h> | 7 #include <errno.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 | 9 |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 const ThreadHandle Thread::kInvalidThreadHandle = 0; | |
15 | |
14 #define VALIDATE_PTHREAD_RESULT(result) \ | 16 #define VALIDATE_PTHREAD_RESULT(result) \ |
15 if (result != 0) { \ | 17 if (result != 0) { \ |
16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ | 18 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ |
17 } | 19 } |
18 | 20 |
19 | 21 |
20 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { | 22 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
21 int64_t secs = millis / kMillisecondsPerSecond; | 23 int64_t secs = millis / kMillisecondsPerSecond; |
22 int64_t nanos = | 24 int64_t nanos = |
23 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; | 25 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; |
24 int result = clock_gettime(CLOCK_MONOTONIC, ts); | 26 int result = clock_gettime(CLOCK_MONOTONIC, ts); |
25 ASSERT(result == 0); | 27 ASSERT(result == 0); |
26 ts->tv_sec += secs; | 28 ts->tv_sec += secs; |
27 ts->tv_nsec += nanos; | 29 ts->tv_nsec += nanos; |
28 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 30 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
29 ts->tv_sec += 1; | 31 ts->tv_sec += 1; |
30 ts->tv_nsec -= kNanosecondsPerSecond; | 32 ts->tv_nsec -= kNanosecondsPerSecond; |
31 } | 33 } |
32 } | 34 } |
33 | 35 |
34 | 36 |
35 class ThreadStartData { | 37 ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) { |
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; | 38 pthread_attr_t attr; |
78 int result = pthread_attr_init(&attr); | 39 int result = pthread_attr_init(&attr); |
79 VALIDATE_PTHREAD_RESULT(result); | 40 VALIDATE_PTHREAD_RESULT(result); |
80 | 41 |
81 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 42 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
82 VALIDATE_PTHREAD_RESULT(result); | 43 VALIDATE_PTHREAD_RESULT(result); |
83 | 44 |
84 result = pthread_attr_setstacksize(&attr, 1024 * KB); | 45 result = pthread_attr_setstacksize(&attr, 1024 * KB); |
85 VALIDATE_PTHREAD_RESULT(result); | 46 VALIDATE_PTHREAD_RESULT(result); |
86 | 47 |
87 ThreadStartData* data = new ThreadStartData(function, parameter, this); | |
88 | |
89 pthread_t tid; | 48 pthread_t tid; |
90 result = pthread_create(&tid, | 49 result = pthread_create(&tid, |
91 &attr, | 50 &attr, |
92 ThreadStart, | 51 reinterpret_cast<void* (*)(void*)>(function), // NOLI NT |
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
| |
93 data); | 52 reinterpret_cast<void*>(parameter)); |
94 VALIDATE_PTHREAD_RESULT(result); | 53 VALIDATE_PTHREAD_RESULT(result); |
95 | 54 |
96 data_.tid_ = tid; | |
97 | |
98 result = pthread_attr_destroy(&attr); | 55 result = pthread_attr_destroy(&attr); |
99 VALIDATE_PTHREAD_RESULT(result); | 56 VALIDATE_PTHREAD_RESULT(result); |
57 | |
58 return tid; | |
100 } | 59 } |
101 | 60 |
102 | 61 |
103 Thread::~Thread() { | 62 bool Thread::Join(ThreadHandle thread, int64_t millis) { |
63 int result; | |
64 if (millis == kNoTimeout) { | |
65 result = pthread_join(thread, NULL); | |
66 VALIDATE_PTHREAD_RESULT(result); | |
67 return true; | |
68 } else { | |
69 struct timespec ts; | |
70 ComputeTimeSpec(&ts, millis); | |
71 result = pthread_timedjoin_np(thread, NULL, &ts); | |
72 if (result == ETIMEDOUT) { | |
73 return false; | |
74 } | |
75 VALIDATE_PTHREAD_RESULT(result); | |
76 return true; | |
77 } | |
104 } | 78 } |
105 | 79 |
106 | 80 |
107 Mutex::Mutex() { | 81 Mutex::Mutex() { |
108 pthread_mutexattr_t attr; | 82 pthread_mutexattr_t attr; |
109 int result = pthread_mutexattr_init(&attr); | 83 int result = pthread_mutexattr_init(&attr); |
110 VALIDATE_PTHREAD_RESULT(result); | 84 VALIDATE_PTHREAD_RESULT(result); |
111 | 85 |
112 #if defined(DEBUG) | 86 #if defined(DEBUG) |
113 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 87 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 } | 215 } |
242 | 216 |
243 | 217 |
244 void Monitor::NotifyAll() { | 218 void Monitor::NotifyAll() { |
245 // TODO(iposva): Do we need to track lock owners? | 219 // TODO(iposva): Do we need to track lock owners? |
246 int result = pthread_cond_broadcast(data_.cond()); | 220 int result = pthread_cond_broadcast(data_.cond()); |
247 VALIDATE_PTHREAD_RESULT(result); | 221 VALIDATE_PTHREAD_RESULT(result); |
248 } | 222 } |
249 | 223 |
250 } // namespace dart | 224 } // namespace dart |
OLD | NEW |