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/globals.h" // NOLINT | 5 #include "platform/globals.h" // NOLINT |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_OPENBSD) |
7 | 7 |
8 #include "vm/os_thread.h" | 8 #include "vm/os_thread.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
11 #include <sys/time.h> // NOLINT | 11 #include <sys/time.h> // NOLINT |
12 | 12 |
13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
14 #include "platform/utils.h" | 14 #include "platform/utils.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 OSThread::SetCurrent(thread); | 100 OSThread::SetCurrent(thread); |
101 thread->set_name(name); | 101 thread->set_name(name); |
102 | 102 |
103 // Call the supplied thread start function handing it its parameters. | 103 // Call the supplied thread start function handing it its parameters. |
104 function(parameter); | 104 function(parameter); |
105 } | 105 } |
106 | 106 |
107 return NULL; | 107 return NULL; |
108 } | 108 } |
109 | 109 |
110 | |
111 int OSThread::Start(const char* name, | 110 int OSThread::Start(const char* name, |
112 ThreadStartFunction function, | 111 ThreadStartFunction function, |
113 uword parameter) { | 112 uword parameter) { |
114 pthread_attr_t attr; | 113 pthread_attr_t attr; |
115 int result = pthread_attr_init(&attr); | 114 int result = pthread_attr_init(&attr); |
116 RETURN_ON_PTHREAD_FAILURE(result); | 115 RETURN_ON_PTHREAD_FAILURE(result); |
117 | 116 |
118 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); | 117 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); |
119 RETURN_ON_PTHREAD_FAILURE(result); | 118 RETURN_ON_PTHREAD_FAILURE(result); |
120 | 119 |
121 ThreadStartData* data = new ThreadStartData(name, function, parameter); | 120 ThreadStartData* data = new ThreadStartData(name, function, parameter); |
122 | 121 |
123 pthread_t tid; | 122 pthread_t tid; |
124 result = pthread_create(&tid, &attr, ThreadStart, data); | 123 result = pthread_create(&tid, &attr, ThreadStart, data); |
125 RETURN_ON_PTHREAD_FAILURE(result); | 124 RETURN_ON_PTHREAD_FAILURE(result); |
126 | 125 |
127 result = pthread_attr_destroy(&attr); | 126 result = pthread_attr_destroy(&attr); |
128 RETURN_ON_PTHREAD_FAILURE(result); | 127 RETURN_ON_PTHREAD_FAILURE(result); |
129 | 128 |
130 return 0; | 129 return 0; |
131 } | 130 } |
132 | 131 |
133 | 132 |
134 const ThreadId OSThread::kInvalidThreadId = static_cast<ThreadId>(0); | 133 const ThreadId OSThread::kInvalidThreadId = static_cast<ThreadId>(0); |
135 const ThreadJoinId OSThread::kInvalidThreadJoinId = | 134 const ThreadJoinId OSThread::kInvalidThreadJoinId = |
136 static_cast<ThreadJoinId>(0); | 135 static_cast<ThreadJoinId>(0); |
137 | 136 |
138 | |
139 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { | 137 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { |
140 pthread_key_t key = kUnsetThreadLocalKey; | 138 pthread_key_t key = kUnsetThreadLocalKey; |
141 int result = pthread_key_create(&key, destructor); | 139 int result = pthread_key_create(&key, destructor); |
142 VALIDATE_PTHREAD_RESULT(result); | 140 VALIDATE_PTHREAD_RESULT(result); |
143 ASSERT(key != kUnsetThreadLocalKey); | 141 ASSERT(key != kUnsetThreadLocalKey); |
144 return key; | 142 return key; |
145 } | 143 } |
146 | 144 |
147 | 145 |
148 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { | 146 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
(...skipping 10 matching lines...) Expand all Loading... |
159 } | 157 } |
160 | 158 |
161 | 159 |
162 intptr_t OSThread::GetMaxStackSize() { | 160 intptr_t OSThread::GetMaxStackSize() { |
163 const int kStackSize = (128 * kWordSize * KB); | 161 const int kStackSize = (128 * kWordSize * KB); |
164 return kStackSize; | 162 return kStackSize; |
165 } | 163 } |
166 | 164 |
167 | 165 |
168 ThreadId OSThread::GetCurrentThreadId() { | 166 ThreadId OSThread::GetCurrentThreadId() { |
169 return gettid(); | 167 return pthread_self(); |
170 } | 168 } |
171 | 169 |
172 | |
173 ThreadId OSThread::GetCurrentThreadTraceId() { | 170 ThreadId OSThread::GetCurrentThreadTraceId() { |
174 return GetCurrentThreadId(); | 171 return GetCurrentThreadId(); |
175 } | 172 } |
176 | 173 |
177 | |
178 ThreadJoinId OSThread::GetCurrentThreadJoinId() { | 174 ThreadJoinId OSThread::GetCurrentThreadJoinId() { |
179 return pthread_self(); | 175 return pthread_self(); |
180 } | 176 } |
181 | 177 |
182 | |
183 void OSThread::Join(ThreadJoinId id) { | 178 void OSThread::Join(ThreadJoinId id) { |
184 int result = pthread_join(id, NULL); | 179 int result = pthread_join(id, NULL); |
185 ASSERT(result == 0); | 180 ASSERT(result == 0); |
186 } | 181 } |
187 | 182 |
188 | |
189 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { | 183 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
190 ASSERT(sizeof(id) == sizeof(intptr_t)); | 184 ASSERT(sizeof(id) == sizeof(intptr_t)); |
191 return static_cast<intptr_t>(id); | 185 return reinterpret_cast<intptr_t>(id); |
192 } | 186 } |
193 | 187 |
194 | 188 |
195 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { | 189 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { |
196 return static_cast<ThreadId>(id); | 190 return reinterpret_cast<ThreadId>(id); |
197 } | 191 } |
198 | 192 |
199 | 193 |
200 bool OSThread::Compare(ThreadId a, ThreadId b) { | 194 bool OSThread::Compare(ThreadId a, ThreadId b) { |
201 return a == b; | 195 return pthread_equal(a, b) != 0; |
202 } | 196 } |
203 | 197 |
204 | 198 |
205 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 199 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
206 ASSERT(thread_id == GetCurrentThreadId()); | 200 ASSERT(thread_id == GetCurrentThreadId()); |
207 ASSERT(cpu_usage != NULL); | 201 ASSERT(cpu_usage != NULL); |
208 struct timespec ts; | 202 struct timespec ts; |
209 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | 203 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
210 ASSERT(r == 0); | 204 ASSERT(r == 0); |
211 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / | 205 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / |
(...skipping 11 matching lines...) Expand all Loading... |
223 VALIDATE_PTHREAD_RESULT(result); | 217 VALIDATE_PTHREAD_RESULT(result); |
224 #endif // defined(DEBUG) | 218 #endif // defined(DEBUG) |
225 | 219 |
226 result = pthread_mutex_init(data_.mutex(), &attr); | 220 result = pthread_mutex_init(data_.mutex(), &attr); |
227 // Verify that creating a pthread_mutex succeeded. | 221 // Verify that creating a pthread_mutex succeeded. |
228 VALIDATE_PTHREAD_RESULT(result); | 222 VALIDATE_PTHREAD_RESULT(result); |
229 | 223 |
230 result = pthread_mutexattr_destroy(&attr); | 224 result = pthread_mutexattr_destroy(&attr); |
231 VALIDATE_PTHREAD_RESULT(result); | 225 VALIDATE_PTHREAD_RESULT(result); |
232 | 226 |
| 227 // When running with assertions enabled we do track the owner. |
233 #if defined(DEBUG) | 228 #if defined(DEBUG) |
234 // When running with assertions enabled we do track the owner. | |
235 owner_ = OSThread::kInvalidThreadId; | 229 owner_ = OSThread::kInvalidThreadId; |
236 #endif // defined(DEBUG) | 230 #endif // defined(DEBUG) |
237 } | 231 } |
238 | 232 |
239 | 233 |
240 Mutex::~Mutex() { | 234 Mutex::~Mutex() { |
241 int result = pthread_mutex_destroy(data_.mutex()); | 235 int result = pthread_mutex_destroy(data_.mutex()); |
242 // Verify that the pthread_mutex was destroyed. | 236 // Verify that the pthread_mutex was destroyed. |
243 VALIDATE_PTHREAD_RESULT(result); | 237 VALIDATE_PTHREAD_RESULT(result); |
244 | 238 |
| 239 // When running with assertions enabled we do track the owner. |
245 #if defined(DEBUG) | 240 #if defined(DEBUG) |
246 // When running with assertions enabled we do track the owner. | |
247 ASSERT(owner_ == OSThread::kInvalidThreadId); | 241 ASSERT(owner_ == OSThread::kInvalidThreadId); |
248 #endif // defined(DEBUG) | 242 #endif // defined(DEBUG) |
249 } | 243 } |
250 | 244 |
251 | 245 |
252 void Mutex::Lock() { | 246 void Mutex::Lock() { |
253 int result = pthread_mutex_lock(data_.mutex()); | 247 int result = pthread_mutex_lock(data_.mutex()); |
254 // Specifically check for dead lock to help debugging. | 248 // Specifically check for dead lock to help debugging. |
255 ASSERT(result != EDEADLK); | 249 ASSERT(result != EDEADLK); |
256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 250 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 251 // When running with assertions enabled we do track the owner. |
257 #if defined(DEBUG) | 252 #if defined(DEBUG) |
258 // When running with assertions enabled we do track the owner. | |
259 owner_ = OSThread::GetCurrentThreadId(); | 253 owner_ = OSThread::GetCurrentThreadId(); |
260 #endif // defined(DEBUG) | 254 #endif // defined(DEBUG) |
261 } | 255 } |
262 | 256 |
263 | 257 |
264 bool Mutex::TryLock() { | 258 bool Mutex::TryLock() { |
265 int result = pthread_mutex_trylock(data_.mutex()); | 259 int result = pthread_mutex_trylock(data_.mutex()); |
266 // Return false if the lock is busy and locking failed. | 260 // Return false if the lock is busy and locking failed. |
267 if (result == EBUSY) { | 261 if (result == EBUSY) { |
268 return false; | 262 return false; |
269 } | 263 } |
270 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 264 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 265 // When running with assertions enabled we do track the owner. |
271 #if defined(DEBUG) | 266 #if defined(DEBUG) |
272 // When running with assertions enabled we do track the owner. | |
273 owner_ = OSThread::GetCurrentThreadId(); | 267 owner_ = OSThread::GetCurrentThreadId(); |
274 #endif // defined(DEBUG) | 268 #endif // defined(DEBUG) |
275 return true; | 269 return true; |
276 } | 270 } |
277 | 271 |
278 | 272 |
279 void Mutex::Unlock() { | 273 void Mutex::Unlock() { |
| 274 // When running with assertions enabled we do track the owner. |
280 #if defined(DEBUG) | 275 #if defined(DEBUG) |
281 // When running with assertions enabled we do track the owner. | |
282 ASSERT(IsOwnedByCurrentThread()); | 276 ASSERT(IsOwnedByCurrentThread()); |
283 owner_ = OSThread::kInvalidThreadId; | 277 owner_ = OSThread::kInvalidThreadId; |
284 #endif // defined(DEBUG) | 278 #endif // defined(DEBUG) |
285 int result = pthread_mutex_unlock(data_.mutex()); | 279 int result = pthread_mutex_unlock(data_.mutex()); |
286 // Specifically check for wrong thread unlocking to aid debugging. | 280 // Specifically check for wrong thread unlocking to aid debugging. |
287 ASSERT(result != EPERM); | 281 ASSERT(result != EPERM); |
288 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 282 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
289 } | 283 } |
290 | 284 |
291 | 285 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 | 402 |
409 void Monitor::NotifyAll() { | 403 void Monitor::NotifyAll() { |
410 // When running with assertions enabled we track the owner. | 404 // When running with assertions enabled we track the owner. |
411 ASSERT(IsOwnedByCurrentThread()); | 405 ASSERT(IsOwnedByCurrentThread()); |
412 int result = pthread_cond_broadcast(data_.cond()); | 406 int result = pthread_cond_broadcast(data_.cond()); |
413 VALIDATE_PTHREAD_RESULT(result); | 407 VALIDATE_PTHREAD_RESULT(result); |
414 } | 408 } |
415 | 409 |
416 } // namespace dart | 410 } // namespace dart |
417 | 411 |
418 #endif // defined(TARGET_OS_ANDROID) | 412 #endif // defined(TARGET_OS_OPENBSD) |
OLD | NEW |