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

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: Addressed review comments from ager@ 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 typedef void* (*PThreadStartFunction)(void* args);
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 22
38 23
39 // Dispatch to the thread start function provided by the caller. This trampoline 24 ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) {
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; 25 pthread_attr_t attr;
62 int result = pthread_attr_init(&attr); 26 int result = pthread_attr_init(&attr);
63 VALIDATE_PTHREAD_RESULT(result); 27 VALIDATE_PTHREAD_RESULT(result);
64 28
65 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 29 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
66 VALIDATE_PTHREAD_RESULT(result); 30 VALIDATE_PTHREAD_RESULT(result);
67 31
68 result = pthread_attr_setstacksize(&attr, 128 * KB); 32 result = pthread_attr_setstacksize(&attr, 128 * KB);
69 VALIDATE_PTHREAD_RESULT(result); 33 VALIDATE_PTHREAD_RESULT(result);
70 34
71 ThreadStartData* data = new ThreadStartData(function, parameter, this);
72
73 pthread_t tid; 35 pthread_t tid;
74 result = pthread_create(&tid, 36 result = pthread_create(&tid,
75 &attr, 37 &attr,
76 ThreadStart, 38 reinterpret_cast<PThreadStartFunction>(function),
77 data); 39 reinterpret_cast<void*>(parameter));
78 VALIDATE_PTHREAD_RESULT(result); 40 VALIDATE_PTHREAD_RESULT(result);
79 41
80 data_.tid_ = tid; 42 result = pthread_attr_destroy(&attr);
43 VALIDATE_PTHREAD_RESULT(result);
81 44
82 result = pthread_attr_destroy(&attr); 45 return tid;
46 }
47
48
49 void Thread::Join(ThreadHandle thread) {
50 int result = pthread_join(thread, NULL);
Ivan Posva 2012/01/20 16:45:55 The threads are created detached (as they should b
Søren Gjesse 2012/01/23 13:14:58 Obviously I did not run the VM tests. How about t
83 VALIDATE_PTHREAD_RESULT(result); 51 VALIDATE_PTHREAD_RESULT(result);
84 } 52 }
85 53
86 54
87 Thread::~Thread() {
88 }
89
90
91 Mutex::Mutex() { 55 Mutex::Mutex() {
92 pthread_mutexattr_t attr; 56 pthread_mutexattr_t attr;
93 int result = pthread_mutexattr_init(&attr); 57 int result = pthread_mutexattr_init(&attr);
94 VALIDATE_PTHREAD_RESULT(result); 58 VALIDATE_PTHREAD_RESULT(result);
95 59
96 #if defined(DEBUG) 60 #if defined(DEBUG)
97 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); 61 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
98 VALIDATE_PTHREAD_RESULT(result); 62 VALIDATE_PTHREAD_RESULT(result);
99 #endif // defined(DEBUG) 63 #endif // defined(DEBUG)
100 64
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 184 }
221 185
222 186
223 void Monitor::NotifyAll() { 187 void Monitor::NotifyAll() {
224 // TODO(iposva): Do we need to track lock owners? 188 // TODO(iposva): Do we need to track lock owners?
225 int result = pthread_cond_broadcast(data_.cond()); 189 int result = pthread_cond_broadcast(data_.cond());
226 VALIDATE_PTHREAD_RESULT(result); 190 VALIDATE_PTHREAD_RESULT(result);
227 } 191 }
228 192
229 } // namespace dart 193 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698