OLD | NEW |
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 <errno.h> | 7 #include <errno.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 | 9 |
10 #include "platform/assert.h" | 10 #include "platform/assert.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 28 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
29 ts->tv_sec += 1; | 29 ts->tv_sec += 1; |
30 ts->tv_nsec -= kNanosecondsPerSecond; | 30 ts->tv_nsec -= kNanosecondsPerSecond; |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 | 34 |
35 class ThreadStartData { | 35 class ThreadStartData { |
36 public: | 36 public: |
37 ThreadStartData(Thread::ThreadStartFunction function, | 37 ThreadStartData(Thread::ThreadStartFunction function, |
38 uword parameter, | 38 uword parameter) |
39 Thread* thread) | 39 : function_(function), parameter_(parameter) {} |
40 : function_(function), parameter_(parameter), thread_(thread) {} | |
41 | 40 |
42 Thread::ThreadStartFunction function() const { return function_; } | 41 Thread::ThreadStartFunction function() const { return function_; } |
43 uword parameter() const { return parameter_; } | 42 uword parameter() const { return parameter_; } |
44 Thread* thread() const { return thread_; } | |
45 | 43 |
46 private: | 44 private: |
47 Thread::ThreadStartFunction function_; | 45 Thread::ThreadStartFunction function_; |
48 uword parameter_; | 46 uword parameter_; |
49 Thread* thread_; | |
50 | 47 |
51 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 48 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
52 }; | 49 }; |
53 | 50 |
54 | 51 |
55 // Dispatch to the thread start function provided by the caller. This trampoline | 52 // Dispatch to the thread start function provided by the caller. This trampoline |
56 // is used to ensure that the thread is properly destroyed if the thread just | 53 // is used to ensure that the thread is properly destroyed if the thread just |
57 // exits. | 54 // exits. |
58 static void* ThreadStart(void* data_ptr) { | 55 static void* ThreadStart(void* data_ptr) { |
59 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 56 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
60 | 57 |
61 Thread::ThreadStartFunction function = data->function(); | 58 Thread::ThreadStartFunction function = data->function(); |
62 uword parameter = data->parameter(); | 59 uword parameter = data->parameter(); |
63 Thread* thread = data->thread(); | |
64 delete data; | 60 delete data; |
65 | 61 |
66 // Call the supplied thread start function handing it its parameters. | 62 // Call the supplied thread start function handing it its parameters. |
67 function(parameter); | 63 function(parameter); |
68 | 64 |
69 // When the function returns here, make sure that the thread is deleted. | |
70 delete thread; | |
71 | |
72 return NULL; | 65 return NULL; |
73 } | 66 } |
74 | 67 |
75 | 68 |
76 Thread::Thread(ThreadStartFunction function, uword parameter) { | 69 void Thread::Start(ThreadStartFunction function, uword parameter) { |
77 pthread_attr_t attr; | 70 pthread_attr_t attr; |
78 int result = pthread_attr_init(&attr); | 71 int result = pthread_attr_init(&attr); |
79 VALIDATE_PTHREAD_RESULT(result); | 72 VALIDATE_PTHREAD_RESULT(result); |
80 | 73 |
81 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 74 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
82 VALIDATE_PTHREAD_RESULT(result); | 75 VALIDATE_PTHREAD_RESULT(result); |
83 | 76 |
84 result = pthread_attr_setstacksize(&attr, 1024 * KB); | 77 result = pthread_attr_setstacksize(&attr, 1024 * KB); |
85 VALIDATE_PTHREAD_RESULT(result); | 78 VALIDATE_PTHREAD_RESULT(result); |
86 | 79 |
87 ThreadStartData* data = new ThreadStartData(function, parameter, this); | 80 ThreadStartData* data = new ThreadStartData(function, parameter); |
88 | 81 |
89 pthread_t tid; | 82 pthread_t tid; |
90 result = pthread_create(&tid, | 83 result = pthread_create(&tid, &attr, ThreadStart, data); |
91 &attr, | |
92 ThreadStart, | |
93 data); | |
94 VALIDATE_PTHREAD_RESULT(result); | 84 VALIDATE_PTHREAD_RESULT(result); |
95 | 85 |
96 data_.tid_ = tid; | |
97 | |
98 result = pthread_attr_destroy(&attr); | 86 result = pthread_attr_destroy(&attr); |
99 VALIDATE_PTHREAD_RESULT(result); | 87 VALIDATE_PTHREAD_RESULT(result); |
100 } | 88 } |
101 | 89 |
102 | 90 |
103 Thread::~Thread() { | |
104 } | |
105 | |
106 | |
107 Mutex::Mutex() { | 91 Mutex::Mutex() { |
108 pthread_mutexattr_t attr; | 92 pthread_mutexattr_t attr; |
109 int result = pthread_mutexattr_init(&attr); | 93 int result = pthread_mutexattr_init(&attr); |
110 VALIDATE_PTHREAD_RESULT(result); | 94 VALIDATE_PTHREAD_RESULT(result); |
111 | 95 |
112 #if defined(DEBUG) | 96 #if defined(DEBUG) |
113 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 97 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
114 VALIDATE_PTHREAD_RESULT(result); | 98 VALIDATE_PTHREAD_RESULT(result); |
115 #endif // defined(DEBUG) | 99 #endif // defined(DEBUG) |
116 | 100 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 } | 225 } |
242 | 226 |
243 | 227 |
244 void Monitor::NotifyAll() { | 228 void Monitor::NotifyAll() { |
245 // TODO(iposva): Do we need to track lock owners? | 229 // TODO(iposva): Do we need to track lock owners? |
246 int result = pthread_cond_broadcast(data_.cond()); | 230 int result = pthread_cond_broadcast(data_.cond()); |
247 VALIDATE_PTHREAD_RESULT(result); | 231 VALIDATE_PTHREAD_RESULT(result); |
248 } | 232 } |
249 | 233 |
250 } // namespace dart | 234 } // namespace dart |
OLD | NEW |