1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include <__threading_support>
13 #define WIN32_LEAN_AND_MEAN
16 #include <fibersapi.h>
18 _LIBCPP_BEGIN_NAMESPACE_STD
20 static_assert(sizeof(__libcpp_mutex_t
) == sizeof(SRWLOCK
), "");
21 static_assert(alignof(__libcpp_mutex_t
) == alignof(SRWLOCK
), "");
23 static_assert(sizeof(__libcpp_recursive_mutex_t
) == sizeof(CRITICAL_SECTION
),
25 static_assert(alignof(__libcpp_recursive_mutex_t
) == alignof(CRITICAL_SECTION
),
28 static_assert(sizeof(__libcpp_condvar_t
) == sizeof(CONDITION_VARIABLE
), "");
29 static_assert(alignof(__libcpp_condvar_t
) == alignof(CONDITION_VARIABLE
), "");
31 static_assert(sizeof(__libcpp_exec_once_flag
) == sizeof(INIT_ONCE
), "");
32 static_assert(alignof(__libcpp_exec_once_flag
) == alignof(INIT_ONCE
), "");
34 static_assert(sizeof(__libcpp_thread_id
) == sizeof(DWORD
), "");
35 static_assert(alignof(__libcpp_thread_id
) == alignof(DWORD
), "");
37 static_assert(sizeof(__libcpp_thread_t
) == sizeof(HANDLE
), "");
38 static_assert(alignof(__libcpp_thread_t
) == alignof(HANDLE
), "");
40 static_assert(sizeof(__libcpp_tls_key
) == sizeof(DWORD
), "");
41 static_assert(alignof(__libcpp_tls_key
) == alignof(DWORD
), "");
44 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t
*__m
)
46 InitializeCriticalSection((LPCRITICAL_SECTION
)__m
);
50 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t
*__m
)
52 EnterCriticalSection((LPCRITICAL_SECTION
)__m
);
56 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t
*__m
)
58 return TryEnterCriticalSection((LPCRITICAL_SECTION
)__m
) != 0;
61 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t
*__m
)
63 LeaveCriticalSection((LPCRITICAL_SECTION
)__m
);
67 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t
*__m
)
69 DeleteCriticalSection((LPCRITICAL_SECTION
)__m
);
73 int __libcpp_mutex_lock(__libcpp_mutex_t
*__m
)
75 AcquireSRWLockExclusive((PSRWLOCK
)__m
);
79 bool __libcpp_mutex_trylock(__libcpp_mutex_t
*__m
)
81 return TryAcquireSRWLockExclusive((PSRWLOCK
)__m
) != 0;
84 int __libcpp_mutex_unlock(__libcpp_mutex_t
*__m
)
86 ReleaseSRWLockExclusive((PSRWLOCK
)__m
);
90 int __libcpp_mutex_destroy(__libcpp_mutex_t
*__m
)
92 static_cast<void>(__m
);
97 int __libcpp_condvar_signal(__libcpp_condvar_t
*__cv
)
99 WakeConditionVariable((PCONDITION_VARIABLE
)__cv
);
103 int __libcpp_condvar_broadcast(__libcpp_condvar_t
*__cv
)
105 WakeAllConditionVariable((PCONDITION_VARIABLE
)__cv
);
109 int __libcpp_condvar_wait(__libcpp_condvar_t
*__cv
, __libcpp_mutex_t
*__m
)
111 SleepConditionVariableSRW((PCONDITION_VARIABLE
)__cv
, (PSRWLOCK
)__m
, INFINITE
, 0);
115 int __libcpp_condvar_timedwait(__libcpp_condvar_t
*__cv
, __libcpp_mutex_t
*__m
,
116 __libcpp_timespec_t
*__ts
)
118 using namespace _VSTD::chrono
;
120 auto duration
= seconds(__ts
->tv_sec
) + nanoseconds(__ts
->tv_nsec
);
122 system_clock::time_point(duration_cast
<system_clock::duration
>(duration
));
123 auto timeout_ms
= duration_cast
<milliseconds
>(abstime
- system_clock::now());
125 if (!SleepConditionVariableSRW((PCONDITION_VARIABLE
)__cv
, (PSRWLOCK
)__m
,
126 timeout_ms
.count() > 0 ? timeout_ms
.count()
130 auto __ec
= GetLastError();
131 return __ec
== ERROR_TIMEOUT
? ETIMEDOUT
: __ec
;
136 int __libcpp_condvar_destroy(__libcpp_condvar_t
*__cv
)
138 static_cast<void>(__cv
);
143 static inline _LIBCPP_INLINE_VISIBILITY BOOL CALLBACK
144 __libcpp_init_once_execute_once_thunk(PINIT_ONCE __init_once
, PVOID __parameter
,
147 static_cast<void>(__init_once
);
148 static_cast<void>(__context
);
150 void (*init_routine
)(void) = reinterpret_cast<void (*)(void)>(__parameter
);
155 int __libcpp_execute_once(__libcpp_exec_once_flag
*__flag
,
156 void (*__init_routine
)(void))
158 if (!InitOnceExecuteOnce((PINIT_ONCE
)__flag
, __libcpp_init_once_execute_once_thunk
,
159 reinterpret_cast<void *>(__init_routine
), NULL
))
160 return GetLastError();
165 bool __libcpp_thread_id_equal(__libcpp_thread_id __lhs
,
166 __libcpp_thread_id __rhs
)
168 return __lhs
== __rhs
;
171 bool __libcpp_thread_id_less(__libcpp_thread_id __lhs
, __libcpp_thread_id __rhs
)
173 return __lhs
< __rhs
;
177 struct __libcpp_beginthreadex_thunk_data
179 void *(*__func
)(void *);
183 static inline _LIBCPP_INLINE_VISIBILITY
unsigned WINAPI
184 __libcpp_beginthreadex_thunk(void *__raw_data
)
187 static_cast<__libcpp_beginthreadex_thunk_data
*>(__raw_data
);
188 auto *__func
= __data
->__func
;
189 void *__arg
= __data
->__arg
;
191 return static_cast<unsigned>(reinterpret_cast<uintptr_t>(__func(__arg
)));
194 bool __libcpp_thread_isnull(const __libcpp_thread_t
*__t
) {
198 int __libcpp_thread_create(__libcpp_thread_t
*__t
, void *(*__func
)(void *),
201 auto *__data
= new __libcpp_beginthreadex_thunk_data
;
202 __data
->__func
= __func
;
203 __data
->__arg
= __arg
;
205 *__t
= reinterpret_cast<HANDLE
>(_beginthreadex(nullptr, 0,
206 __libcpp_beginthreadex_thunk
,
207 __data
, 0, nullptr));
211 return GetLastError();
214 __libcpp_thread_id
__libcpp_thread_get_current_id()
216 return GetCurrentThreadId();
219 __libcpp_thread_id
__libcpp_thread_get_id(const __libcpp_thread_t
*__t
)
221 return GetThreadId(*__t
);
224 int __libcpp_thread_join(__libcpp_thread_t
*__t
)
226 if (WaitForSingleObjectEx(*__t
, INFINITE
, FALSE
) == WAIT_FAILED
)
227 return GetLastError();
228 if (!CloseHandle(*__t
))
229 return GetLastError();
233 int __libcpp_thread_detach(__libcpp_thread_t
*__t
)
235 if (!CloseHandle(*__t
))
236 return GetLastError();
240 void __libcpp_thread_yield()
245 void __libcpp_thread_sleep_for(const chrono::nanoseconds
& __ns
)
247 // round-up to the nearest millisecond
248 chrono::milliseconds __ms
= chrono::ceil
<chrono::milliseconds
>(__ns
);
249 // FIXME(compnerd) this should be an alertable sleep (WFSO or SleepEx)
253 // Thread Local Storage
254 int __libcpp_tls_create(__libcpp_tls_key
* __key
,
255 void(_LIBCPP_TLS_DESTRUCTOR_CC
* __at_exit
)(void*))
257 DWORD index
= FlsAlloc(__at_exit
);
258 if (index
== FLS_OUT_OF_INDEXES
)
259 return GetLastError();
264 void *__libcpp_tls_get(__libcpp_tls_key __key
)
266 return FlsGetValue(__key
);
269 int __libcpp_tls_set(__libcpp_tls_key __key
, void *__p
)
271 if (!FlsSetValue(__key
, __p
))
272 return GetLastError();
276 _LIBCPP_END_NAMESPACE_STD