Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: runtime/platform/thread_macos.cc

Issue 9141005: Change the thread interface in runtime/platform and use it starting all threads (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r3454 Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 const ThreadHandle Thread::kInvalidThreadHandle = 0;
14
15 #define VALIDATE_PTHREAD_RESULT(result) \
14 if (result != 0) { \ 16 if (result != 0) { \
15 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ 17 FATAL2("pthread error: %d (%s)", result, strerror(result)); \
16 } 18 }
17 19
18 20
19 class ThreadStartData { 21 ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) {
20 public:
21 ThreadStartData(Thread::ThreadStartFunction function,
22 uword parameter,
23 Thread* thread)
24 : function_(function), parameter_(parameter), thread_(thread) {}
25
26 Thread::ThreadStartFunction function() const { return function_; }
27 uword parameter() const { return parameter_; }
28 Thread* thread() const { return thread_; }
29
30 private:
31 Thread::ThreadStartFunction function_;
32 uword parameter_;
33 Thread* thread_;
34
35 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
36 };
37
38
39 // 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
41 // exits.
42 static void* ThreadStart(void* data_ptr) {
43 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
44
45 Thread::ThreadStartFunction function = data->function();
46 uword parameter = data->parameter();
47 Thread* thread = data->thread();
48 delete data;
49
50 // Call the supplied thread start function handing it its parameters.
51 function(parameter);
52
53 // When the function returns here, make sure that the thread is deleted.
54 delete thread;
55
56 return NULL;
57 }
58
59
60 Thread::Thread(ThreadStartFunction function, uword parameter) {
61 pthread_attr_t attr; 22 pthread_attr_t attr;
62 int result = pthread_attr_init(&attr); 23 int result = pthread_attr_init(&attr);
63 VALIDATE_PTHREAD_RESULT(result); 24 VALIDATE_PTHREAD_RESULT(result);
64 25
65 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 26 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
66 VALIDATE_PTHREAD_RESULT(result); 27 VALIDATE_PTHREAD_RESULT(result);
67 28
68 result = pthread_attr_setstacksize(&attr, 128 * KB); 29 result = pthread_attr_setstacksize(&attr, 128 * KB);
69 VALIDATE_PTHREAD_RESULT(result); 30 VALIDATE_PTHREAD_RESULT(result);
70 31
71 ThreadStartData* data = new ThreadStartData(function, parameter, this);
72
73 pthread_t tid; 32 pthread_t tid;
74 result = pthread_create(&tid, 33 result = pthread_create(&tid,
75 &attr, 34 &attr,
76 ThreadStart, 35 reinterpret_cast<void* (*)(void*)>(function), // NOLI NT
Mads Ager (google) 2012/01/20 12:35:34 Ditto.
Søren Gjesse 2012/01/20 13:25:45 Done.
77 data); 36 reinterpret_cast<void*>(parameter));
78 VALIDATE_PTHREAD_RESULT(result); 37 VALIDATE_PTHREAD_RESULT(result);
79 38
80 data_.tid_ = tid;
81
82 result = pthread_attr_destroy(&attr); 39 result = pthread_attr_destroy(&attr);
83 VALIDATE_PTHREAD_RESULT(result); 40 VALIDATE_PTHREAD_RESULT(result);
41
42 return tid;
84 } 43 }
85 44
86 45
87 Thread::~Thread() { 46 bool Thread::Join(ThreadHandle thread, int64_t millis) {
47 int result;
48 if (millis == kNoTimeout) {
49 result = pthread_join(thread, NULL);
50 VALIDATE_PTHREAD_RESULT(result);
51 return true;
52 } else {
53 // TODO(sgjesse): Is there a Mac OS alternative to pthread_timedjoin_np?
54 UNIMPLEMENTED();
55 return false;
56 }
88 } 57 }
89 58
90 59
91 Mutex::Mutex() { 60 Mutex::Mutex() {
92 pthread_mutexattr_t attr; 61 pthread_mutexattr_t attr;
93 int result = pthread_mutexattr_init(&attr); 62 int result = pthread_mutexattr_init(&attr);
94 VALIDATE_PTHREAD_RESULT(result); 63 VALIDATE_PTHREAD_RESULT(result);
95 64
96 #if defined(DEBUG) 65 #if defined(DEBUG)
97 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); 66 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 189 }
221 190
222 191
223 void Monitor::NotifyAll() { 192 void Monitor::NotifyAll() {
224 // TODO(iposva): Do we need to track lock owners? 193 // TODO(iposva): Do we need to track lock owners?
225 int result = pthread_cond_broadcast(data_.cond()); 194 int result = pthread_cond_broadcast(data_.cond());
226 VALIDATE_PTHREAD_RESULT(result); 195 VALIDATE_PTHREAD_RESULT(result);
227 } 196 }
228 197
229 } // namespace dart 198 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698