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

Side by Side Diff: runtime/bin/thread_fuchsia.cc

Issue 2974233002: VM: Re-format to use at most one newline between functions (Closed)
Patch Set: Rebase and merge Created 3 years, 5 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
« no previous file with comments | « runtime/bin/thread_fuchsia.h ('k') | runtime/bin/thread_linux.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_FUCHSIA) 6 #if defined(HOST_OS_FUCHSIA)
7 7
8 #include "bin/thread.h" 8 #include "bin/thread.h"
9 #include "bin/thread_fuchsia.h" 9 #include "bin/thread_fuchsia.h"
10 10
11 #include <errno.h> // NOLINT 11 #include <errno.h> // NOLINT
12 #include <sys/resource.h> // NOLINT 12 #include <sys/resource.h> // NOLINT
13 #include <sys/time.h> // NOLINT 13 #include <sys/time.h> // NOLINT
14 14
15 #include "platform/assert.h" 15 #include "platform/assert.h"
16 #include "platform/utils.h" 16 #include "platform/utils.h"
17 17
18 namespace dart { 18 namespace dart {
19 namespace bin { 19 namespace bin {
20 20
21 #define VALIDATE_PTHREAD_RESULT(result) \ 21 #define VALIDATE_PTHREAD_RESULT(result) \
22 if (result != 0) { \ 22 if (result != 0) { \
23 const int kBufferSize = 1024; \ 23 const int kBufferSize = 1024; \
24 char error_buf[kBufferSize]; \ 24 char error_buf[kBufferSize]; \
25 FATAL2("pthread error: %d (%s)", result, \ 25 FATAL2("pthread error: %d (%s)", result, \
26 Utils::StrError(result, error_buf, kBufferSize)); \ 26 Utils::StrError(result, error_buf, kBufferSize)); \
27 } 27 }
28 28
29
30 #ifdef DEBUG 29 #ifdef DEBUG
31 #define RETURN_ON_PTHREAD_FAILURE(result) \ 30 #define RETURN_ON_PTHREAD_FAILURE(result) \
32 if (result != 0) { \ 31 if (result != 0) { \
33 const int kBufferSize = 1024; \ 32 const int kBufferSize = 1024; \
34 char error_buf[kBufferSize]; \ 33 char error_buf[kBufferSize]; \
35 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \ 34 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \
36 result, Utils::StrError(result, error_buf, kBufferSize)); \ 35 result, Utils::StrError(result, error_buf, kBufferSize)); \
37 return result; \ 36 return result; \
38 } 37 }
39 #else 38 #else
40 #define RETURN_ON_PTHREAD_FAILURE(result) \ 39 #define RETURN_ON_PTHREAD_FAILURE(result) \
41 if (result != 0) { \ 40 if (result != 0) { \
42 return result; \ 41 return result; \
43 } 42 }
44 #endif 43 #endif
45 44
46
47 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { 45 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) {
48 int64_t secs = micros / kMicrosecondsPerSecond; 46 int64_t secs = micros / kMicrosecondsPerSecond;
49 int64_t nanos = 47 int64_t nanos =
50 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; 48 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond;
51 int result = clock_gettime(CLOCK_MONOTONIC, ts); 49 int result = clock_gettime(CLOCK_MONOTONIC, ts);
52 ASSERT(result == 0); 50 ASSERT(result == 0);
53 ts->tv_sec += secs; 51 ts->tv_sec += secs;
54 ts->tv_nsec += nanos; 52 ts->tv_nsec += nanos;
55 if (ts->tv_nsec >= kNanosecondsPerSecond) { 53 if (ts->tv_nsec >= kNanosecondsPerSecond) {
56 ts->tv_sec += 1; 54 ts->tv_sec += 1;
57 ts->tv_nsec -= kNanosecondsPerSecond; 55 ts->tv_nsec -= kNanosecondsPerSecond;
58 } 56 }
59 } 57 }
60 58
61
62 class ThreadStartData { 59 class ThreadStartData {
63 public: 60 public:
64 ThreadStartData(Thread::ThreadStartFunction function, uword parameter) 61 ThreadStartData(Thread::ThreadStartFunction function, uword parameter)
65 : function_(function), parameter_(parameter) {} 62 : function_(function), parameter_(parameter) {}
66 63
67 Thread::ThreadStartFunction function() const { return function_; } 64 Thread::ThreadStartFunction function() const { return function_; }
68 uword parameter() const { return parameter_; } 65 uword parameter() const { return parameter_; }
69 66
70 private: 67 private:
71 Thread::ThreadStartFunction function_; 68 Thread::ThreadStartFunction function_;
72 uword parameter_; 69 uword parameter_;
73 70
74 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); 71 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
75 }; 72 };
76 73
77
78 // Dispatch to the thread start function provided by the caller. This trampoline 74 // Dispatch to the thread start function provided by the caller. This trampoline
79 // is used to ensure that the thread is properly destroyed if the thread just 75 // is used to ensure that the thread is properly destroyed if the thread just
80 // exits. 76 // exits.
81 static void* ThreadStart(void* data_ptr) { 77 static void* ThreadStart(void* data_ptr) {
82 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); 78 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
83 79
84 Thread::ThreadStartFunction function = data->function(); 80 Thread::ThreadStartFunction function = data->function();
85 uword parameter = data->parameter(); 81 uword parameter = data->parameter();
86 delete data; 82 delete data;
87 83
88 // Call the supplied thread start function handing it its parameters. 84 // Call the supplied thread start function handing it its parameters.
89 function(parameter); 85 function(parameter);
90 86
91 return NULL; 87 return NULL;
92 } 88 }
93 89
94
95 int Thread::Start(ThreadStartFunction function, uword parameter) { 90 int Thread::Start(ThreadStartFunction function, uword parameter) {
96 pthread_attr_t attr; 91 pthread_attr_t attr;
97 int result = pthread_attr_init(&attr); 92 int result = pthread_attr_init(&attr);
98 RETURN_ON_PTHREAD_FAILURE(result); 93 RETURN_ON_PTHREAD_FAILURE(result);
99 94
100 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 95 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
101 RETURN_ON_PTHREAD_FAILURE(result); 96 RETURN_ON_PTHREAD_FAILURE(result);
102 97
103 result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize()); 98 result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize());
104 RETURN_ON_PTHREAD_FAILURE(result); 99 RETURN_ON_PTHREAD_FAILURE(result);
105 100
106 ThreadStartData* data = new ThreadStartData(function, parameter); 101 ThreadStartData* data = new ThreadStartData(function, parameter);
107 102
108 pthread_t tid; 103 pthread_t tid;
109 result = pthread_create(&tid, &attr, ThreadStart, data); 104 result = pthread_create(&tid, &attr, ThreadStart, data);
110 RETURN_ON_PTHREAD_FAILURE(result); 105 RETURN_ON_PTHREAD_FAILURE(result);
111 106
112 result = pthread_attr_destroy(&attr); 107 result = pthread_attr_destroy(&attr);
113 RETURN_ON_PTHREAD_FAILURE(result); 108 RETURN_ON_PTHREAD_FAILURE(result);
114 109
115 return 0; 110 return 0;
116 } 111 }
117 112
118
119 const ThreadLocalKey Thread::kUnsetThreadLocalKey = 113 const ThreadLocalKey Thread::kUnsetThreadLocalKey =
120 static_cast<pthread_key_t>(-1); 114 static_cast<pthread_key_t>(-1);
121 const ThreadId Thread::kInvalidThreadId = static_cast<ThreadId>(0); 115 const ThreadId Thread::kInvalidThreadId = static_cast<ThreadId>(0);
122 116
123 ThreadLocalKey Thread::CreateThreadLocal() { 117 ThreadLocalKey Thread::CreateThreadLocal() {
124 pthread_key_t key = kUnsetThreadLocalKey; 118 pthread_key_t key = kUnsetThreadLocalKey;
125 int result = pthread_key_create(&key, NULL); 119 int result = pthread_key_create(&key, NULL);
126 VALIDATE_PTHREAD_RESULT(result); 120 VALIDATE_PTHREAD_RESULT(result);
127 ASSERT(key != kUnsetThreadLocalKey); 121 ASSERT(key != kUnsetThreadLocalKey);
128 return key; 122 return key;
129 } 123 }
130 124
131
132 void Thread::DeleteThreadLocal(ThreadLocalKey key) { 125 void Thread::DeleteThreadLocal(ThreadLocalKey key) {
133 ASSERT(key != kUnsetThreadLocalKey); 126 ASSERT(key != kUnsetThreadLocalKey);
134 int result = pthread_key_delete(key); 127 int result = pthread_key_delete(key);
135 VALIDATE_PTHREAD_RESULT(result); 128 VALIDATE_PTHREAD_RESULT(result);
136 } 129 }
137 130
138
139 void Thread::SetThreadLocal(ThreadLocalKey key, uword value) { 131 void Thread::SetThreadLocal(ThreadLocalKey key, uword value) {
140 ASSERT(key != kUnsetThreadLocalKey); 132 ASSERT(key != kUnsetThreadLocalKey);
141 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); 133 int result = pthread_setspecific(key, reinterpret_cast<void*>(value));
142 VALIDATE_PTHREAD_RESULT(result); 134 VALIDATE_PTHREAD_RESULT(result);
143 } 135 }
144 136
145
146 intptr_t Thread::GetMaxStackSize() { 137 intptr_t Thread::GetMaxStackSize() {
147 const int kStackSize = (128 * kWordSize * KB); 138 const int kStackSize = (128 * kWordSize * KB);
148 return kStackSize; 139 return kStackSize;
149 } 140 }
150 141
151
152 ThreadId Thread::GetCurrentThreadId() { 142 ThreadId Thread::GetCurrentThreadId() {
153 return pthread_self(); 143 return pthread_self();
154 } 144 }
155 145
156
157 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { 146 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) {
158 ASSERT(sizeof(id) == sizeof(intptr_t)); 147 ASSERT(sizeof(id) == sizeof(intptr_t));
159 return static_cast<intptr_t>(id); 148 return static_cast<intptr_t>(id);
160 } 149 }
161 150
162
163 bool Thread::Compare(ThreadId a, ThreadId b) { 151 bool Thread::Compare(ThreadId a, ThreadId b) {
164 return (pthread_equal(a, b) != 0); 152 return (pthread_equal(a, b) != 0);
165 } 153 }
166 154
167
168 void Thread::InitOnce() { 155 void Thread::InitOnce() {
169 // Nothing to be done. 156 // Nothing to be done.
170 } 157 }
171 158
172
173 Mutex::Mutex() { 159 Mutex::Mutex() {
174 pthread_mutexattr_t attr; 160 pthread_mutexattr_t attr;
175 int result = pthread_mutexattr_init(&attr); 161 int result = pthread_mutexattr_init(&attr);
176 VALIDATE_PTHREAD_RESULT(result); 162 VALIDATE_PTHREAD_RESULT(result);
177 163
178 #if defined(DEBUG) 164 #if defined(DEBUG)
179 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); 165 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
180 VALIDATE_PTHREAD_RESULT(result); 166 VALIDATE_PTHREAD_RESULT(result);
181 #endif // defined(DEBUG) 167 #endif // defined(DEBUG)
182 168
183 result = pthread_mutex_init(data_.mutex(), &attr); 169 result = pthread_mutex_init(data_.mutex(), &attr);
184 // Verify that creating a pthread_mutex succeeded. 170 // Verify that creating a pthread_mutex succeeded.
185 VALIDATE_PTHREAD_RESULT(result); 171 VALIDATE_PTHREAD_RESULT(result);
186 172
187 result = pthread_mutexattr_destroy(&attr); 173 result = pthread_mutexattr_destroy(&attr);
188 VALIDATE_PTHREAD_RESULT(result); 174 VALIDATE_PTHREAD_RESULT(result);
189 } 175 }
190 176
191
192 Mutex::~Mutex() { 177 Mutex::~Mutex() {
193 int result = pthread_mutex_destroy(data_.mutex()); 178 int result = pthread_mutex_destroy(data_.mutex());
194 // Verify that the pthread_mutex was destroyed. 179 // Verify that the pthread_mutex was destroyed.
195 VALIDATE_PTHREAD_RESULT(result); 180 VALIDATE_PTHREAD_RESULT(result);
196 } 181 }
197 182
198
199 void Mutex::Lock() { 183 void Mutex::Lock() {
200 int result = pthread_mutex_lock(data_.mutex()); 184 int result = pthread_mutex_lock(data_.mutex());
201 // Specifically check for dead lock to help debugging. 185 // Specifically check for dead lock to help debugging.
202 ASSERT(result != EDEADLK); 186 ASSERT(result != EDEADLK);
203 ASSERT(result == 0); // Verify no other errors. 187 ASSERT(result == 0); // Verify no other errors.
204 // TODO(iposva): Do we need to track lock owners? 188 // TODO(iposva): Do we need to track lock owners?
205 } 189 }
206 190
207
208 bool Mutex::TryLock() { 191 bool Mutex::TryLock() {
209 int result = pthread_mutex_trylock(data_.mutex()); 192 int result = pthread_mutex_trylock(data_.mutex());
210 // Return false if the lock is busy and locking failed. 193 // Return false if the lock is busy and locking failed.
211 if (result == EBUSY) { 194 if (result == EBUSY) {
212 return false; 195 return false;
213 } 196 }
214 ASSERT(result == 0); // Verify no other errors. 197 ASSERT(result == 0); // Verify no other errors.
215 // TODO(iposva): Do we need to track lock owners? 198 // TODO(iposva): Do we need to track lock owners?
216 return true; 199 return true;
217 } 200 }
218 201
219
220 void Mutex::Unlock() { 202 void Mutex::Unlock() {
221 // TODO(iposva): Do we need to track lock owners? 203 // TODO(iposva): Do we need to track lock owners?
222 int result = pthread_mutex_unlock(data_.mutex()); 204 int result = pthread_mutex_unlock(data_.mutex());
223 // Specifically check for wrong thread unlocking to aid debugging. 205 // Specifically check for wrong thread unlocking to aid debugging.
224 ASSERT(result != EPERM); 206 ASSERT(result != EPERM);
225 ASSERT(result == 0); // Verify no other errors. 207 ASSERT(result == 0); // Verify no other errors.
226 } 208 }
227 209
228
229 Monitor::Monitor() { 210 Monitor::Monitor() {
230 pthread_mutexattr_t mutex_attr; 211 pthread_mutexattr_t mutex_attr;
231 int result = pthread_mutexattr_init(&mutex_attr); 212 int result = pthread_mutexattr_init(&mutex_attr);
232 VALIDATE_PTHREAD_RESULT(result); 213 VALIDATE_PTHREAD_RESULT(result);
233 214
234 #if defined(DEBUG) 215 #if defined(DEBUG)
235 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); 216 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
236 VALIDATE_PTHREAD_RESULT(result); 217 VALIDATE_PTHREAD_RESULT(result);
237 #endif // defined(DEBUG) 218 #endif // defined(DEBUG)
238 219
(...skipping 10 matching lines...) Expand all
249 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); 230 result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
250 VALIDATE_PTHREAD_RESULT(result); 231 VALIDATE_PTHREAD_RESULT(result);
251 232
252 result = pthread_cond_init(data_.cond(), &cond_attr); 233 result = pthread_cond_init(data_.cond(), &cond_attr);
253 VALIDATE_PTHREAD_RESULT(result); 234 VALIDATE_PTHREAD_RESULT(result);
254 235
255 result = pthread_condattr_destroy(&cond_attr); 236 result = pthread_condattr_destroy(&cond_attr);
256 VALIDATE_PTHREAD_RESULT(result); 237 VALIDATE_PTHREAD_RESULT(result);
257 } 238 }
258 239
259
260 Monitor::~Monitor() { 240 Monitor::~Monitor() {
261 int result = pthread_mutex_destroy(data_.mutex()); 241 int result = pthread_mutex_destroy(data_.mutex());
262 VALIDATE_PTHREAD_RESULT(result); 242 VALIDATE_PTHREAD_RESULT(result);
263 243
264 result = pthread_cond_destroy(data_.cond()); 244 result = pthread_cond_destroy(data_.cond());
265 VALIDATE_PTHREAD_RESULT(result); 245 VALIDATE_PTHREAD_RESULT(result);
266 } 246 }
267 247
268
269 void Monitor::Enter() { 248 void Monitor::Enter() {
270 int result = pthread_mutex_lock(data_.mutex()); 249 int result = pthread_mutex_lock(data_.mutex());
271 VALIDATE_PTHREAD_RESULT(result); 250 VALIDATE_PTHREAD_RESULT(result);
272 // TODO(iposva): Do we need to track lock owners? 251 // TODO(iposva): Do we need to track lock owners?
273 } 252 }
274 253
275
276 void Monitor::Exit() { 254 void Monitor::Exit() {
277 // TODO(iposva): Do we need to track lock owners? 255 // TODO(iposva): Do we need to track lock owners?
278 int result = pthread_mutex_unlock(data_.mutex()); 256 int result = pthread_mutex_unlock(data_.mutex());
279 VALIDATE_PTHREAD_RESULT(result); 257 VALIDATE_PTHREAD_RESULT(result);
280 } 258 }
281 259
282
283 Monitor::WaitResult Monitor::Wait(int64_t millis) { 260 Monitor::WaitResult Monitor::Wait(int64_t millis) {
284 return WaitMicros(millis * kMicrosecondsPerMillisecond); 261 return WaitMicros(millis * kMicrosecondsPerMillisecond);
285 } 262 }
286 263
287
288 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { 264 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) {
289 // TODO(iposva): Do we need to track lock owners? 265 // TODO(iposva): Do we need to track lock owners?
290 Monitor::WaitResult retval = kNotified; 266 Monitor::WaitResult retval = kNotified;
291 if (micros == kNoTimeout) { 267 if (micros == kNoTimeout) {
292 // Wait forever. 268 // Wait forever.
293 int result = pthread_cond_wait(data_.cond(), data_.mutex()); 269 int result = pthread_cond_wait(data_.cond(), data_.mutex());
294 VALIDATE_PTHREAD_RESULT(result); 270 VALIDATE_PTHREAD_RESULT(result);
295 } else { 271 } else {
296 struct timespec ts; 272 struct timespec ts;
297 ComputeTimeSpecMicros(&ts, micros); 273 ComputeTimeSpecMicros(&ts, micros);
298 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); 274 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts);
299 ASSERT((result == 0) || (result == ETIMEDOUT)); 275 ASSERT((result == 0) || (result == ETIMEDOUT));
300 if (result == ETIMEDOUT) { 276 if (result == ETIMEDOUT) {
301 retval = kTimedOut; 277 retval = kTimedOut;
302 } 278 }
303 } 279 }
304 return retval; 280 return retval;
305 } 281 }
306 282
307
308 void Monitor::Notify() { 283 void Monitor::Notify() {
309 // TODO(iposva): Do we need to track lock owners? 284 // TODO(iposva): Do we need to track lock owners?
310 int result = pthread_cond_signal(data_.cond()); 285 int result = pthread_cond_signal(data_.cond());
311 VALIDATE_PTHREAD_RESULT(result); 286 VALIDATE_PTHREAD_RESULT(result);
312 } 287 }
313 288
314
315 void Monitor::NotifyAll() { 289 void Monitor::NotifyAll() {
316 // TODO(iposva): Do we need to track lock owners? 290 // TODO(iposva): Do we need to track lock owners?
317 int result = pthread_cond_broadcast(data_.cond()); 291 int result = pthread_cond_broadcast(data_.cond());
318 VALIDATE_PTHREAD_RESULT(result); 292 VALIDATE_PTHREAD_RESULT(result);
319 } 293 }
320 294
321 } // namespace bin 295 } // namespace bin
322 } // namespace dart 296 } // namespace dart
323 297
324 #endif // defined(HOST_OS_FUCHSIA) 298 #endif // defined(HOST_OS_FUCHSIA)
OLDNEW
« no previous file with comments | « runtime/bin/thread_fuchsia.h ('k') | runtime/bin/thread_linux.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698