|
8 | 8 | //===----------------------------------------------------------------------===//
|
9 | 9 |
|
10 | 10 | #include <__threading_support>
|
| 11 | +#define NOMINMAX |
| 12 | +#define WIN32_LEAN_AND_MEAN |
11 | 13 | #include <windows.h>
|
12 | 14 | #include <process.h>
|
13 | 15 | #include <fibersapi.h>
|
@@ -37,6 +39,9 @@ static_assert(alignof(__libcpp_thread_t) == alignof(HANDLE), "");
|
37 | 39 | static_assert(sizeof(__libcpp_tls_key) == sizeof(DWORD), "");
|
38 | 40 | static_assert(alignof(__libcpp_tls_key) == alignof(DWORD), "");
|
39 | 41 |
|
| 42 | +static_assert(sizeof(__libcpp_semaphore_t) == sizeof(HANDLE), ""); |
| 43 | +static_assert(alignof(__libcpp_semaphore_t) == alignof(HANDLE), ""); |
| 44 | + |
40 | 45 | // Mutex
|
41 | 46 | int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
|
42 | 47 | {
|
@@ -272,4 +277,37 @@ int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
|
272 | 277 | return 0;
|
273 | 278 | }
|
274 | 279 |
|
| 280 | +// Semaphores |
| 281 | +bool __libcpp_semaphore_init(__libcpp_semaphore_t* __sem, int __init) |
| 282 | +{ |
| 283 | + *(PHANDLE)__sem = CreateSemaphoreEx(nullptr, __init, _LIBCPP_SEMAPHORE_MAX, |
| 284 | + nullptr, 0, SEMAPHORE_ALL_ACCESS); |
| 285 | + return *__sem != nullptr; |
| 286 | +} |
| 287 | + |
| 288 | +bool __libcpp_semaphore_destroy(__libcpp_semaphore_t* __sem) |
| 289 | +{ |
| 290 | + CloseHandle(*(PHANDLE)__sem); |
| 291 | + return true; |
| 292 | +} |
| 293 | + |
| 294 | +bool __libcpp_semaphore_post(__libcpp_semaphore_t* __sem) |
| 295 | +{ |
| 296 | + return ReleaseSemaphore(*(PHANDLE)__sem, 1, nullptr); |
| 297 | +} |
| 298 | + |
| 299 | +bool __libcpp_semaphore_wait(__libcpp_semaphore_t* __sem) |
| 300 | +{ |
| 301 | + return WaitForSingleObjectEx(*(PHANDLE)__sem, INFINITE, false) == |
| 302 | + WAIT_OBJECT_0; |
| 303 | +} |
| 304 | + |
| 305 | +bool __libcpp_semaphore_wait_timed(__libcpp_semaphore_t* __sem, |
| 306 | + chrono::nanoseconds const& __ns) |
| 307 | +{ |
| 308 | + chrono::milliseconds __ms = std::chrono::ceil<chrono::milliseconds>(__ns); |
| 309 | + return WaitForSingleObjectEx(*(PHANDLE)__sem, __ms.count(), false) == |
| 310 | + WAIT_OBJECT_0; |
| 311 | +} |
| 312 | + |
275 | 313 | _LIBCPP_END_NAMESPACE_STD
|
0 commit comments