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 <process.h> | 5 #include <process.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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 FATAL("Thread creation failed"); | 59 FATAL("Thread creation failed"); |
60 } | 60 } |
61 data_.tid_ = tid; | 61 data_.tid_ = tid; |
62 } | 62 } |
63 | 63 |
64 | 64 |
65 Thread::~Thread() { | 65 Thread::~Thread() { |
66 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); | 66 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); |
67 } | 67 } |
68 | 68 |
69 | |
70 Mutex::Mutex() { | |
71 // Allocate unnamed semaphore with initial count 1 and max count 1. | |
72 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); | |
73 if (data_.semaphore_ == NULL) { | |
74 FATAL("Mutex allocation failed"); | |
75 } | |
76 } | |
77 | |
78 | |
79 Mutex::~Mutex() { | |
80 CloseHandle(data_.semaphore_); | |
81 } | |
82 | |
83 | |
84 void Mutex::Lock() { | |
85 DWORD result = WaitForSingleObject(data_.semaphore_, INFINITE); | |
86 if (result != WAIT_OBJECT_0) { | |
87 FATAL("Mutex lock failed"); | |
88 } | |
89 } | |
90 | |
91 | |
92 bool Mutex::TryLock() { | |
93 // Attempt to pass the semaphore but return immediately. | |
94 DWORD result = WaitForSingleObject(data_.semaphore_, 0); | |
95 if (result == WAIT_OBJECT_0) { | |
96 return true; | |
97 } | |
98 if (result == WAIT_ABANDONED || result == WAIT_FAILED) { | |
99 FATAL("Mutex try lock failed"); | |
100 } | |
101 ASSERT(result == WAIT_TIMEOUT); | |
102 return false; | |
103 } | |
104 | |
105 | |
106 void Mutex::Unlock() { | |
107 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); | |
108 if (result == 0) { | |
109 FATAL("Mutex unlock failed"); | |
110 } | |
111 } | |
112 | |
113 | |
114 Monitor::Monitor() { | |
115 InitializeCriticalSection(&data_.cs_); | |
116 InitializeConditionVariable(&data_.cond_); | |
117 } | |
118 | |
119 | |
120 Monitor::~Monitor() { | |
121 DeleteCriticalSection(&data_.cs_); | |
122 } | |
123 | |
124 | |
125 void Monitor::Enter() { | |
126 EnterCriticalSection(&data_.cs_); | |
127 } | |
128 | |
129 | |
130 void Monitor::Exit() { | |
131 LeaveCriticalSection(&data_.cs_); | |
132 } | |
133 | |
134 | |
135 Monitor::WaitResult Monitor::Wait(int64_t millis) { | |
136 Monitor::WaitResult retval = kNotified; | |
137 if (millis == 0) { | |
138 // Wait forever. | |
139 BOOL result = SleepConditionVariableCS(&data_.cond_, &data_.cs_, INFINITE); | |
140 if (result == 0) { | |
141 FATAL("Monitor::Wait failed"); | |
142 } | |
143 } else { | |
144 BOOL result = SleepConditionVariableCS(&data_.cond_, &data_.cs_, millis); | |
145 if (result == 0) { | |
146 DWORD error = GetLastError(); | |
147 // Windows condition variables should set error to WAIT_TIMEOUT | |
148 // but occationally sets it to ERROR_TIMEOUT for timeouts. On | |
149 // Windows 7 it seems to pretty consistently set it to | |
150 // ERROR_TIMEOUT. | |
151 if ((error == WAIT_TIMEOUT) || (error == ERROR_TIMEOUT)) { | |
152 retval = kTimedOut; | |
153 } else { | |
154 FATAL("Monitor::Wait failed"); | |
155 } | |
156 } | |
157 } | |
158 return retval; | |
159 } | |
160 | |
161 | |
162 void Monitor::Notify() { | |
163 WakeConditionVariable(&data_.cond_); | |
164 } | |
165 | |
166 | |
167 void Monitor::NotifyAll() { | |
168 WakeAllConditionVariable(&data_.cond_); | |
169 } | |
170 | |
171 } // namespace dart | 69 } // namespace dart |
OLD | NEW |