[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / libcxx / src / support / win32 / thread_win32.cpp
blobc25aefcc342f337836c1c771858f19e8bef20593
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include <__threading_support>
10 #include <chrono>
12 #define NOMINMAX
13 #define WIN32_LEAN_AND_MEAN
14 #include <windows.h>
15 #include <process.h>
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),
24 "");
25 static_assert(alignof(__libcpp_recursive_mutex_t) == alignof(CRITICAL_SECTION),
26 "");
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), "");
43 // Mutex
44 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
46 InitializeCriticalSection((LPCRITICAL_SECTION)__m);
47 return 0;
50 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
52 EnterCriticalSection((LPCRITICAL_SECTION)__m);
53 return 0;
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);
64 return 0;
67 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
69 DeleteCriticalSection((LPCRITICAL_SECTION)__m);
70 return 0;
73 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
75 AcquireSRWLockExclusive((PSRWLOCK)__m);
76 return 0;
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);
87 return 0;
90 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
92 static_cast<void>(__m);
93 return 0;
96 // Condition Variable
97 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
99 WakeConditionVariable((PCONDITION_VARIABLE)__cv);
100 return 0;
103 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
105 WakeAllConditionVariable((PCONDITION_VARIABLE)__cv);
106 return 0;
109 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
111 SleepConditionVariableSRW((PCONDITION_VARIABLE)__cv, (PSRWLOCK)__m, INFINITE, 0);
112 return 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);
121 auto abstime =
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()
127 : 0,
130 auto __ec = GetLastError();
131 return __ec == ERROR_TIMEOUT ? ETIMEDOUT : __ec;
133 return 0;
136 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
138 static_cast<void>(__cv);
139 return 0;
142 // Execute Once
143 static inline _LIBCPP_INLINE_VISIBILITY BOOL CALLBACK
144 __libcpp_init_once_execute_once_thunk(PINIT_ONCE __init_once, PVOID __parameter,
145 PVOID *__context)
147 static_cast<void>(__init_once);
148 static_cast<void>(__context);
150 void (*init_routine)(void) = reinterpret_cast<void (*)(void)>(__parameter);
151 init_routine();
152 return TRUE;
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();
161 return 0;
164 // Thread ID
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;
176 // Thread
177 struct __libcpp_beginthreadex_thunk_data
179 void *(*__func)(void *);
180 void *__arg;
183 static inline _LIBCPP_INLINE_VISIBILITY unsigned WINAPI
184 __libcpp_beginthreadex_thunk(void *__raw_data)
186 auto *__data =
187 static_cast<__libcpp_beginthreadex_thunk_data *>(__raw_data);
188 auto *__func = __data->__func;
189 void *__arg = __data->__arg;
190 delete __data;
191 return static_cast<unsigned>(reinterpret_cast<uintptr_t>(__func(__arg)));
194 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
195 return *__t == 0;
198 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
199 void *__arg)
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));
209 if (*__t)
210 return 0;
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();
230 return 0;
233 int __libcpp_thread_detach(__libcpp_thread_t *__t)
235 if (!CloseHandle(*__t))
236 return GetLastError();
237 return 0;
240 void __libcpp_thread_yield()
242 SwitchToThread();
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)
250 Sleep(__ms.count());
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();
260 *__key = index;
261 return 0;
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();
273 return 0;
276 _LIBCPP_END_NAMESPACE_STD