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

Side by Side Diff: runtime/platform/thread_linux.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 <errno.h> 5 #include <errno.h>
6 #include <sys/time.h> 6 #include <sys/time.h>
7 7
8 #include "platform/assert.h" 8 #include "platform/assert.h"
9 #include "vm/thread.h" 9 #include "vm/thread.h"
Ivan Posva 2012/01/13 23:33:41 vm/thread.h does that even exist?
Søren Gjesse 2012/01/16 16:12:19 Changed to platform/thread.h. vm/thread.h does sti
10 10
11 namespace dart { 11 namespace dart {
12 12
13 #define VALIDATE_PTHREAD_RESULT(result) \ 13 #define VALIDATE_PTHREAD_RESULT(result) \
14 if (result != 0) { \ 14 if (result != 0) { \
15 FATAL2("pthread error: %d (%s)", result, strerror(result)); \ 15 FATAL2("pthread error: %d (%s)", result, strerror(result)); \
16 } 16 }
17 17
18 18
19 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { 19 static void ComputeTimeSpec(struct timespec* ts, int64_t millis) {
20 int64_t secs = millis / kMillisecondsPerSecond; 20 int64_t secs = millis / kMillisecondsPerSecond;
21 int64_t nanos = 21 int64_t nanos =
22 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; 22 (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond;
23 int result = clock_gettime(CLOCK_MONOTONIC, ts); 23 int result = clock_gettime(CLOCK_MONOTONIC, ts);
24 ASSERT(result == 0); 24 ASSERT(result == 0);
25 ts->tv_sec += secs; 25 ts->tv_sec += secs;
26 ts->tv_nsec += nanos; 26 ts->tv_nsec += nanos;
27 if (ts->tv_nsec >= kNanosecondsPerSecond) { 27 if (ts->tv_nsec >= kNanosecondsPerSecond) {
28 ts->tv_sec += 1; 28 ts->tv_sec += 1;
29 ts->tv_nsec -= kNanosecondsPerSecond; 29 ts->tv_nsec -= kNanosecondsPerSecond;
30 } 30 }
31 } 31 }
32 32
33 33
34 class ThreadStartData {
35 public:
36 ThreadStartData(Thread::ThreadStartFunction function,
37 uword parameter,
38 Thread* thread)
39 : function_(function), parameter_(parameter), thread_(thread) {}
40
41 Thread::ThreadStartFunction function() const { return function_; }
42 uword parameter() const { return parameter_; }
43 Thread* thread() const { return thread_; }
44
45 private:
46 Thread::ThreadStartFunction function_;
47 uword parameter_;
48 Thread* thread_;
49
50 DISALLOW_COPY_AND_ASSIGN(ThreadStartData);
51 };
52
53
54 // Dispatch to the thread start function provided by the caller. This trampoline
55 // is used to ensure that the thread is properly destroyed if the thread just
56 // exits.
57 static void* ThreadStart(void* data_ptr) {
58 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr);
59
60 Thread::ThreadStartFunction function = data->function();
61 uword parameter = data->parameter();
62 Thread* thread = data->thread();
63 delete data;
64
65 // Call the supplied thread start function handing it its parameters.
66 function(parameter);
67
68 // When the function returns here, make sure that the thread is deleted.
69 delete thread;
70
71 return NULL;
72 }
73
74
75 Thread::Thread(ThreadStartFunction function, uword parameter) {
76 pthread_attr_t attr;
77 int result = pthread_attr_init(&attr);
78 VALIDATE_PTHREAD_RESULT(result);
79
80 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
81 VALIDATE_PTHREAD_RESULT(result);
82
83 result = pthread_attr_setstacksize(&attr, 1024 * KB);
84 VALIDATE_PTHREAD_RESULT(result);
85
86 ThreadStartData* data = new ThreadStartData(function, parameter, this);
87
88 pthread_t tid;
89 result = pthread_create(&tid,
90 &attr,
91 ThreadStart,
92 data);
93 VALIDATE_PTHREAD_RESULT(result);
94
95 data_.tid_ = tid;
96
97 result = pthread_attr_destroy(&attr);
98 VALIDATE_PTHREAD_RESULT(result);
99 }
100
101
102 Thread::~Thread() {
103 }
104
105
106 Mutex::Mutex() { 34 Mutex::Mutex() {
107 pthread_mutexattr_t attr; 35 pthread_mutexattr_t attr;
108 int result = pthread_mutexattr_init(&attr); 36 int result = pthread_mutexattr_init(&attr);
109 VALIDATE_PTHREAD_RESULT(result); 37 VALIDATE_PTHREAD_RESULT(result);
110 38
111 #if defined(DEBUG) 39 #if defined(DEBUG)
112 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); 40 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
113 VALIDATE_PTHREAD_RESULT(result); 41 VALIDATE_PTHREAD_RESULT(result);
114 #endif // defined(DEBUG) 42 #endif // defined(DEBUG)
115 43
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 } 168 }
241 169
242 170
243 void Monitor::NotifyAll() { 171 void Monitor::NotifyAll() {
244 // TODO(iposva): Do we need to track lock owners? 172 // TODO(iposva): Do we need to track lock owners?
245 int result = pthread_cond_broadcast(data_.cond()); 173 int result = pthread_cond_broadcast(data_.cond());
246 VALIDATE_PTHREAD_RESULT(result); 174 VALIDATE_PTHREAD_RESULT(result);
247 } 175 }
248 176
249 } // namespace dart 177 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698