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

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

Issue 9196002: Move Mutex and Monitor from vm/ to platform/ (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed revire 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
« no previous file with comments | « runtime/vm/thread_macos.h ('k') | runtime/vm/thread_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "vm/thread.h"
6
7 #include <sys/errno.h>
8
9 #include "platform/assert.h"
10
11 namespace dart {
12
13 #define VALIDATE_PTHREAD_RESULT(result) \
14 if (result != 0) { \
15 FATAL2("pthread error: %d (%s)", result, strerror(result)); \
16 }
17
18
19 class ThreadStartData {
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;
62 int result = pthread_attr_init(&attr);
63 VALIDATE_PTHREAD_RESULT(result);
64
65 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
66 VALIDATE_PTHREAD_RESULT(result);
67
68 result = pthread_attr_setstacksize(&attr, 64 * KB);
69 VALIDATE_PTHREAD_RESULT(result);
70
71 ThreadStartData* data = new ThreadStartData(function, parameter, this);
72
73 pthread_t tid;
74 result = pthread_create(&tid,
75 &attr,
76 ThreadStart,
77 data);
78 VALIDATE_PTHREAD_RESULT(result);
79
80 data_.tid_ = tid;
81
82 result = pthread_attr_destroy(&attr);
83 VALIDATE_PTHREAD_RESULT(result);
84 }
85
86
87 Thread::~Thread() {
88 }
89
90
91 Mutex::Mutex() {
92 pthread_mutexattr_t attr;
93 int result = pthread_mutexattr_init(&attr);
94 VALIDATE_PTHREAD_RESULT(result);
95
96 #if defined(DEBUG)
97 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
98 VALIDATE_PTHREAD_RESULT(result);
99 #endif // defined(DEBUG)
100
101 result = pthread_mutex_init(data_.mutex(), &attr);
102 // Verify that creating a pthread_mutex succeeded.
103 VALIDATE_PTHREAD_RESULT(result);
104
105 result = pthread_mutexattr_destroy(&attr);
106 VALIDATE_PTHREAD_RESULT(result);
107 }
108
109
110 Mutex::~Mutex() {
111 int result = pthread_mutex_destroy(data_.mutex());
112 // Verify that the pthread_mutex was destroyed.
113 VALIDATE_PTHREAD_RESULT(result);
114 }
115
116
117 void Mutex::Lock() {
118 int result = pthread_mutex_lock(data_.mutex());
119 // Specifically check for dead lock to help debugging.
120 ASSERT(result != EDEADLK);
121 ASSERT(result == 0); // Verify no other errors.
122 // TODO(iposva): Do we need to track lock owners?
123 }
124
125
126 bool Mutex::TryLock() {
127 int result = pthread_mutex_trylock(data_.mutex());
128 // Return false if the lock is busy and locking failed.
129 if ((result == EBUSY) || (result == EDEADLK)) {
130 return false;
131 }
132 ASSERT(result == 0); // Verify no other errors.
133 // TODO(iposva): Do we need to track lock owners?
134 return true;
135 }
136
137
138 void Mutex::Unlock() {
139 // TODO(iposva): Do we need to track lock owners?
140 int result = pthread_mutex_unlock(data_.mutex());
141 // Specifically check for wrong thread unlocking to aid debugging.
142 ASSERT(result != EPERM);
143 ASSERT(result == 0); // Verify no other errors.
144 }
145
146
147 Monitor::Monitor() {
148 pthread_mutexattr_t attr;
149 int result = pthread_mutexattr_init(&attr);
150 VALIDATE_PTHREAD_RESULT(result);
151
152 #if defined(DEBUG)
153 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
154 VALIDATE_PTHREAD_RESULT(result);
155 #endif // defined(DEBUG)
156
157 result = pthread_mutex_init(data_.mutex(), &attr);
158 VALIDATE_PTHREAD_RESULT(result);
159
160 result = pthread_mutexattr_destroy(&attr);
161 VALIDATE_PTHREAD_RESULT(result);
162
163 result = pthread_cond_init(data_.cond(), NULL);
164 VALIDATE_PTHREAD_RESULT(result);
165 }
166
167
168 Monitor::~Monitor() {
169 int result = pthread_mutex_destroy(data_.mutex());
170 VALIDATE_PTHREAD_RESULT(result);
171
172 result = pthread_cond_destroy(data_.cond());
173 VALIDATE_PTHREAD_RESULT(result);
174 }
175
176
177 void Monitor::Enter() {
178 int result = pthread_mutex_lock(data_.mutex());
179 VALIDATE_PTHREAD_RESULT(result);
180 // TODO(iposva): Do we need to track lock owners?
181 }
182
183
184 void Monitor::Exit() {
185 // TODO(iposva): Do we need to track lock owners?
186 int result = pthread_mutex_unlock(data_.mutex());
187 VALIDATE_PTHREAD_RESULT(result);
188 }
189
190
191 Monitor::WaitResult Monitor::Wait(int64_t millis) {
192 // TODO(iposva): Do we need to track lock owners?
193 Monitor::WaitResult retval = kNotified;
194 if (millis == 0) {
195 // Wait forever.
196 int result = pthread_cond_wait(data_.cond(), data_.mutex());
197 VALIDATE_PTHREAD_RESULT(result);
198 } else {
199 struct timespec ts;
200 int64_t secs = millis / 1000;
201 int64_t nanos = (millis - (secs * 1000)) * 1000000;
202 ts.tv_sec = secs;
203 ts.tv_nsec = nanos;
204 int result = pthread_cond_timedwait_relative_np(data_.cond(),
205 data_.mutex(),
206 &ts);
207 ASSERT((result == 0) || (result == ETIMEDOUT));
208 if (result == ETIMEDOUT) {
209 retval = kTimedOut;
210 }
211 }
212 return retval;
213 }
214
215
216 void Monitor::Notify() {
217 // TODO(iposva): Do we need to track lock owners?
218 int result = pthread_cond_signal(data_.cond());
219 VALIDATE_PTHREAD_RESULT(result);
220 }
221
222
223 void Monitor::NotifyAll() {
224 // TODO(iposva): Do we need to track lock owners?
225 int result = pthread_cond_broadcast(data_.cond());
226 VALIDATE_PTHREAD_RESULT(result);
227 }
228
229 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/thread_macos.h ('k') | runtime/vm/thread_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698