OLD | NEW |
| (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 <process.h> | |
8 | |
9 #include "platform/assert.h" | |
10 | |
11 namespace dart { | |
12 | |
13 class ThreadStartData { | |
14 public: | |
15 ThreadStartData(Thread::ThreadStartFunction function, | |
16 uword parameter, | |
17 Thread* thread) | |
18 : function_(function), parameter_(parameter), thread_(thread) {} | |
19 | |
20 Thread::ThreadStartFunction function() const { return function_; } | |
21 uword parameter() const { return parameter_; } | |
22 Thread* thread() const { return thread_; } | |
23 | |
24 private: | |
25 Thread::ThreadStartFunction function_; | |
26 uword parameter_; | |
27 Thread* thread_; | |
28 | |
29 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | |
30 }; | |
31 | |
32 | |
33 // Dispatch to the thread start function provided by the caller. This trampoline | |
34 // is used to ensure that the thread is properly destroyed if the thread just | |
35 // exits. | |
36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { | |
37 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | |
38 | |
39 Thread::ThreadStartFunction function = data->function(); | |
40 uword parameter = data->parameter(); | |
41 Thread* thread = data->thread(); | |
42 delete data; | |
43 | |
44 // Call the supplied thread start function handing it its parameters. | |
45 function(parameter); | |
46 | |
47 // When the function returns here, make sure that the thread is deleted. | |
48 delete thread; | |
49 | |
50 return 0; | |
51 } | |
52 | |
53 | |
54 Thread::Thread(ThreadStartFunction function, uword parameter) { | |
55 ThreadStartData* start_data = new ThreadStartData(function, parameter, this); | |
56 uint32_t tid; | |
57 data_.thread_handle_ = | |
58 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); | |
59 if (data_.thread_handle_ == -1) { | |
60 FATAL("Thread creation failed"); | |
61 } | |
62 data_.tid_ = tid; | |
63 } | |
64 | |
65 | |
66 Thread::~Thread() { | |
67 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); | |
68 } | |
69 | |
70 | |
71 Mutex::Mutex() { | |
72 // Allocate unnamed semaphore with initial count 1 and max count 1. | |
73 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); | |
74 if (data_.semaphore_ == NULL) { | |
75 FATAL("Mutex allocation failed"); | |
76 } | |
77 } | |
78 | |
79 | |
80 Mutex::~Mutex() { | |
81 CloseHandle(data_.semaphore_); | |
82 } | |
83 | |
84 | |
85 void Mutex::Lock() { | |
86 DWORD result = WaitForSingleObject(data_.semaphore_, INFINITE); | |
87 if (result != WAIT_OBJECT_0) { | |
88 FATAL("Mutex lock failed"); | |
89 } | |
90 } | |
91 | |
92 | |
93 bool Mutex::TryLock() { | |
94 // Attempt to pass the semaphore but return immediately. | |
95 DWORD result = WaitForSingleObject(data_.semaphore_, 0); | |
96 if (result == WAIT_OBJECT_0) { | |
97 return true; | |
98 } | |
99 if (result == WAIT_ABANDONED || result == WAIT_FAILED) { | |
100 FATAL("Mutex try lock failed"); | |
101 } | |
102 ASSERT(result == WAIT_TIMEOUT); | |
103 return false; | |
104 } | |
105 | |
106 | |
107 void Mutex::Unlock() { | |
108 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); | |
109 if (result == 0) { | |
110 FATAL("Mutex unlock failed"); | |
111 } | |
112 } | |
113 | |
114 | |
115 Monitor::Monitor() { | |
116 InitializeCriticalSection(&data_.cs_); | |
117 InitializeConditionVariable(&data_.cond_); | |
118 } | |
119 | |
120 | |
121 Monitor::~Monitor() { | |
122 DeleteCriticalSection(&data_.cs_); | |
123 } | |
124 | |
125 | |
126 void Monitor::Enter() { | |
127 EnterCriticalSection(&data_.cs_); | |
128 } | |
129 | |
130 | |
131 void Monitor::Exit() { | |
132 LeaveCriticalSection(&data_.cs_); | |
133 } | |
134 | |
135 | |
136 Monitor::WaitResult Monitor::Wait(int64_t millis) { | |
137 Monitor::WaitResult retval = kNotified; | |
138 if (millis == 0) { | |
139 // Wait forever. | |
140 BOOL result = SleepConditionVariableCS(&data_.cond_, &data_.cs_, INFINITE); | |
141 if (result == 0) { | |
142 FATAL("Monitor::Wait failed"); | |
143 } | |
144 } else { | |
145 BOOL result = SleepConditionVariableCS(&data_.cond_, &data_.cs_, millis); | |
146 if (result == 0) { | |
147 DWORD error = GetLastError(); | |
148 // Windows condition variables should set error to WAIT_TIMEOUT | |
149 // but occationally sets it to ERROR_TIMEOUT for timeouts. On | |
150 // Windows 7 it seems to pretty consistently set it to | |
151 // ERROR_TIMEOUT. | |
152 if ((error == WAIT_TIMEOUT) || (error == ERROR_TIMEOUT)) { | |
153 retval = kTimedOut; | |
154 } else { | |
155 FATAL("Monitor::Wait failed"); | |
156 } | |
157 } | |
158 } | |
159 return retval; | |
160 } | |
161 | |
162 | |
163 void Monitor::Notify() { | |
164 WakeConditionVariable(&data_.cond_); | |
165 } | |
166 | |
167 | |
168 void Monitor::NotifyAll() { | |
169 WakeAllConditionVariable(&data_.cond_); | |
170 } | |
171 | |
172 } // namespace dart | |
OLD | NEW |