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

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: 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 <sys/errno.h> 5 #include <sys/errno.h>
6 6
7 #include "platform/assert.h" 7 #include "platform/assert.h"
8 #include "vm/thread.h" 8 #include "vm/thread.h"
9 9
10 namespace dart { 10 namespace dart {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 data_.tid_ = tid; 79 data_.tid_ = tid;
80 80
81 result = pthread_attr_destroy(&attr); 81 result = pthread_attr_destroy(&attr);
82 VALIDATE_PTHREAD_RESULT(result); 82 VALIDATE_PTHREAD_RESULT(result);
83 } 83 }
84 84
85 85
86 Thread::~Thread() { 86 Thread::~Thread() {
87 } 87 }
88 88
89
90 Mutex::Mutex() {
91 pthread_mutexattr_t attr;
92 int result = pthread_mutexattr_init(&attr);
93 VALIDATE_PTHREAD_RESULT(result);
94
95 #if defined(DEBUG)
96 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
97 VALIDATE_PTHREAD_RESULT(result);
98 #endif // defined(DEBUG)
99
100 result = pthread_mutex_init(data_.mutex(), &attr);
101 // Verify that creating a pthread_mutex succeeded.
102 VALIDATE_PTHREAD_RESULT(result);
103
104 result = pthread_mutexattr_destroy(&attr);
105 VALIDATE_PTHREAD_RESULT(result);
106 }
107
108
109 Mutex::~Mutex() {
110 int result = pthread_mutex_destroy(data_.mutex());
111 // Verify that the pthread_mutex was destroyed.
112 VALIDATE_PTHREAD_RESULT(result);
113 }
114
115
116 void Mutex::Lock() {
117 int result = pthread_mutex_lock(data_.mutex());
118 // Specifically check for dead lock to help debugging.
119 ASSERT(result != EDEADLK);
120 ASSERT(result == 0); // Verify no other errors.
121 // TODO(iposva): Do we need to track lock owners?
122 }
123
124
125 bool Mutex::TryLock() {
126 int result = pthread_mutex_trylock(data_.mutex());
127 // Return false if the lock is busy and locking failed.
128 if ((result == EBUSY) || (result == EDEADLK)) {
129 return false;
130 }
131 ASSERT(result == 0); // Verify no other errors.
132 // TODO(iposva): Do we need to track lock owners?
133 return true;
134 }
135
136
137 void Mutex::Unlock() {
138 // TODO(iposva): Do we need to track lock owners?
139 int result = pthread_mutex_unlock(data_.mutex());
140 // Specifically check for wrong thread unlocking to aid debugging.
141 ASSERT(result != EPERM);
142 ASSERT(result == 0); // Verify no other errors.
143 }
144
145
146 Monitor::Monitor() {
147 pthread_mutexattr_t attr;
148 int result = pthread_mutexattr_init(&attr);
149 VALIDATE_PTHREAD_RESULT(result);
150
151 #if defined(DEBUG)
152 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
153 VALIDATE_PTHREAD_RESULT(result);
154 #endif // defined(DEBUG)
155
156 result = pthread_mutex_init(data_.mutex(), &attr);
157 VALIDATE_PTHREAD_RESULT(result);
158
159 result = pthread_mutexattr_destroy(&attr);
160 VALIDATE_PTHREAD_RESULT(result);
161
162 result = pthread_cond_init(data_.cond(), NULL);
163 VALIDATE_PTHREAD_RESULT(result);
164 }
165
166
167 Monitor::~Monitor() {
168 int result = pthread_mutex_destroy(data_.mutex());
169 VALIDATE_PTHREAD_RESULT(result);
170
171 result = pthread_cond_destroy(data_.cond());
172 VALIDATE_PTHREAD_RESULT(result);
173 }
174
175
176 void Monitor::Enter() {
177 int result = pthread_mutex_lock(data_.mutex());
178 VALIDATE_PTHREAD_RESULT(result);
179 // TODO(iposva): Do we need to track lock owners?
180 }
181
182
183 void Monitor::Exit() {
184 // TODO(iposva): Do we need to track lock owners?
185 int result = pthread_mutex_unlock(data_.mutex());
186 VALIDATE_PTHREAD_RESULT(result);
187 }
188
189
190 Monitor::WaitResult Monitor::Wait(int64_t millis) {
191 // TODO(iposva): Do we need to track lock owners?
192 Monitor::WaitResult retval = kNotified;
193 if (millis == 0) {
194 // Wait forever.
195 int result = pthread_cond_wait(data_.cond(), data_.mutex());
196 VALIDATE_PTHREAD_RESULT(result);
197 } else {
198 struct timespec ts;
199 int64_t secs = millis / 1000;
200 int64_t nanos = (millis - (secs * 1000)) * 1000000;
201 ts.tv_sec = secs;
202 ts.tv_nsec = nanos;
203 int result = pthread_cond_timedwait_relative_np(data_.cond(),
204 data_.mutex(),
205 &ts);
206 ASSERT((result == 0) || (result == ETIMEDOUT));
207 if (result == ETIMEDOUT) {
208 retval = kTimedOut;
209 }
210 }
211 return retval;
212 }
213
214
215 void Monitor::Notify() {
216 // TODO(iposva): Do we need to track lock owners?
217 int result = pthread_cond_signal(data_.cond());
218 VALIDATE_PTHREAD_RESULT(result);
219 }
220
221
222 void Monitor::NotifyAll() {
223 // TODO(iposva): Do we need to track lock owners?
224 int result = pthread_cond_broadcast(data_.cond());
225 VALIDATE_PTHREAD_RESULT(result);
226 }
227
228 } // namespace dart 89 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698