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 #define VALIDATE_PTHREAD_RESULT(result) \ | 14 #define VALIDATE_PTHREAD_RESULT(result) \ |
15 if (result != 0) { \ | 15 if (result != 0) { \ |
16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ | 16 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ |
17 } | 17 } |
18 | 18 |
19 | 19 |
| 20 #ifdef DEBUG |
| 21 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 22 if (result != 0) { \ |
| 23 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \ |
| 24 __FILE__, __LINE__, result, strerror(result)); \ |
| 25 return result; \ |
| 26 } |
| 27 #else |
| 28 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 29 if (result != 0) return result; |
| 30 #endif |
| 31 |
| 32 |
20 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { | 33 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
21 int64_t secs = millis / kMillisecondsPerSecond; | 34 int64_t secs = millis / kMillisecondsPerSecond; |
22 int64_t nanos = | 35 int64_t nanos = |
23 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; | 36 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; |
24 int result = clock_gettime(CLOCK_MONOTONIC, ts); | 37 int result = clock_gettime(CLOCK_MONOTONIC, ts); |
25 ASSERT(result == 0); | 38 ASSERT(result == 0); |
26 ts->tv_sec += secs; | 39 ts->tv_sec += secs; |
27 ts->tv_nsec += nanos; | 40 ts->tv_nsec += nanos; |
28 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 41 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
29 ts->tv_sec += 1; | 42 ts->tv_sec += 1; |
30 ts->tv_nsec -= kNanosecondsPerSecond; | 43 ts->tv_nsec -= kNanosecondsPerSecond; |
31 } | 44 } |
32 } | 45 } |
33 | 46 |
34 | 47 |
35 class ThreadStartData { | 48 class ThreadStartData { |
36 public: | 49 public: |
37 ThreadStartData(Thread::ThreadStartFunction function, | 50 ThreadStartData(Thread::ThreadStartFunction function, |
38 uword parameter, | 51 uword parameter) |
39 Thread* thread) | 52 : function_(function), parameter_(parameter) {} |
40 : function_(function), parameter_(parameter), thread_(thread) {} | |
41 | 53 |
42 Thread::ThreadStartFunction function() const { return function_; } | 54 Thread::ThreadStartFunction function() const { return function_; } |
43 uword parameter() const { return parameter_; } | 55 uword parameter() const { return parameter_; } |
44 Thread* thread() const { return thread_; } | |
45 | 56 |
46 private: | 57 private: |
47 Thread::ThreadStartFunction function_; | 58 Thread::ThreadStartFunction function_; |
48 uword parameter_; | 59 uword parameter_; |
49 Thread* thread_; | |
50 | 60 |
51 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 61 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
52 }; | 62 }; |
53 | 63 |
54 | 64 |
55 // Dispatch to the thread start function provided by the caller. This trampoline | 65 // 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 | 66 // is used to ensure that the thread is properly destroyed if the thread just |
57 // exits. | 67 // exits. |
58 static void* ThreadStart(void* data_ptr) { | 68 static void* ThreadStart(void* data_ptr) { |
59 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 69 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
60 | 70 |
61 Thread::ThreadStartFunction function = data->function(); | 71 Thread::ThreadStartFunction function = data->function(); |
62 uword parameter = data->parameter(); | 72 uword parameter = data->parameter(); |
63 Thread* thread = data->thread(); | |
64 delete data; | 73 delete data; |
65 | 74 |
66 // Call the supplied thread start function handing it its parameters. | 75 // Call the supplied thread start function handing it its parameters. |
67 function(parameter); | 76 function(parameter); |
68 | 77 |
69 // When the function returns here, make sure that the thread is deleted. | |
70 delete thread; | |
71 | |
72 return NULL; | 78 return NULL; |
73 } | 79 } |
74 | 80 |
75 | 81 |
76 Thread::Thread(ThreadStartFunction function, uword parameter) { | 82 int Thread::Start(ThreadStartFunction function, uword parameter) { |
77 pthread_attr_t attr; | 83 pthread_attr_t attr; |
78 int result = pthread_attr_init(&attr); | 84 int result = pthread_attr_init(&attr); |
79 VALIDATE_PTHREAD_RESULT(result); | 85 RETURN_ON_PTHREAD_FAILURE(result); |
80 | 86 |
81 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 87 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
82 VALIDATE_PTHREAD_RESULT(result); | 88 RETURN_ON_PTHREAD_FAILURE(result); |
83 | 89 |
84 result = pthread_attr_setstacksize(&attr, 1024 * KB); | 90 result = pthread_attr_setstacksize(&attr, 1024 * KB); |
85 VALIDATE_PTHREAD_RESULT(result); | 91 RETURN_ON_PTHREAD_FAILURE(result); |
86 | 92 |
87 ThreadStartData* data = new ThreadStartData(function, parameter, this); | 93 ThreadStartData* data = new ThreadStartData(function, parameter); |
88 | 94 |
89 pthread_t tid; | 95 pthread_t tid; |
90 result = pthread_create(&tid, | 96 result = pthread_create(&tid, &attr, ThreadStart, data); |
91 &attr, | 97 RETURN_ON_PTHREAD_FAILURE(result); |
92 ThreadStart, | |
93 data); | |
94 VALIDATE_PTHREAD_RESULT(result); | |
95 | |
96 data_.tid_ = tid; | |
97 | 98 |
98 result = pthread_attr_destroy(&attr); | 99 result = pthread_attr_destroy(&attr); |
99 VALIDATE_PTHREAD_RESULT(result); | 100 RETURN_ON_PTHREAD_FAILURE(result); |
| 101 |
| 102 return 0; |
100 } | 103 } |
101 | 104 |
102 | 105 |
103 Thread::~Thread() { | |
104 } | |
105 | |
106 | |
107 Mutex::Mutex() { | 106 Mutex::Mutex() { |
108 pthread_mutexattr_t attr; | 107 pthread_mutexattr_t attr; |
109 int result = pthread_mutexattr_init(&attr); | 108 int result = pthread_mutexattr_init(&attr); |
110 VALIDATE_PTHREAD_RESULT(result); | 109 VALIDATE_PTHREAD_RESULT(result); |
111 | 110 |
112 #if defined(DEBUG) | 111 #if defined(DEBUG) |
113 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 112 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
114 VALIDATE_PTHREAD_RESULT(result); | 113 VALIDATE_PTHREAD_RESULT(result); |
115 #endif // defined(DEBUG) | 114 #endif // defined(DEBUG) |
116 | 115 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 240 } |
242 | 241 |
243 | 242 |
244 void Monitor::NotifyAll() { | 243 void Monitor::NotifyAll() { |
245 // TODO(iposva): Do we need to track lock owners? | 244 // TODO(iposva): Do we need to track lock owners? |
246 int result = pthread_cond_broadcast(data_.cond()); | 245 int result = pthread_cond_broadcast(data_.cond()); |
247 VALIDATE_PTHREAD_RESULT(result); | 246 VALIDATE_PTHREAD_RESULT(result); |
248 } | 247 } |
249 | 248 |
250 } // namespace dart | 249 } // namespace dart |
OLD | NEW |