1717#ifndef SWIFT_THREADING_IMPL_C11_H
1818#define SWIFT_THREADING_IMPL_C11_H
1919
20- #include < stdatomic.h>
20+ #include < atomic>
21+ #include < cstdint>
2122#include < threads.h>
2223
24+ #include " swift/Threading/Errors.h"
25+
2326namespace swift {
2427namespace threading_impl {
2528
@@ -58,11 +61,9 @@ inline bool threads_same(thread_id a, thread_id b) {
5861using mutex_handle = ::mtx_t ;
5962
6063inline void mutex_init (mutex_handle &handle, bool checked = false ) {
61- SWIFT_C11THREADS_CHECK (::mtx_init (&handle), ::mtx_plain);
62- }
63- inline void mutex_destroy (mutex_handle &handle) {
64- SWIFT_C11THREADS_CHECK (::mtx_destroy (&handle));
64+ SWIFT_C11THREADS_CHECK (::mtx_init (&handle, ::mtx_plain));
6565}
66+ inline void mutex_destroy (mutex_handle &handle) { ::mtx_destroy (&handle); }
6667
6768inline void mutex_lock (mutex_handle &handle) {
6869 SWIFT_C11THREADS_CHECK (::mtx_lock (&handle));
@@ -83,33 +84,38 @@ inline void mutex_unsafe_unlock(mutex_handle &handle) {
8384
8485struct lazy_mutex_handle {
8586 ::mtx_t mutex;
86- ::atomic_int once; // -1 = initialized, 0 = uninitialized, 1 = initializing
87+ std:: int32_t once; // -1 = initialized, 0 = uninitialized, 1 = initializing
8788};
8889
8990inline constexpr lazy_mutex_handle lazy_mutex_initializer () {
90- return (lazy_mutex_handle){0 };
91+ return (lazy_mutex_handle){};
9192}
9293inline void lazy_mutex_init (lazy_mutex_handle &handle) {
9394 // Sadly, we can't use call_once() for this as it doesn't have a context
94- if (::atomic_load_explicit (&handle.once , ::memory_order_acquire) < 0 )
95+ if (std::atomic_load_explicit ((std::atomic<std::int32_t > *)&handle.once ,
96+ std::memory_order_acquire) < 0 )
9597 return ;
9698
97- if (::atomic_compare_exchange_strong_explicit (&handle.once , &(int ){0 }, 1 ,
98- ::memory_order_relaxed,
99- ::memory_order_relaxed)) {
99+ int zero = 0 ;
100+ if (std::atomic_compare_exchange_strong_explicit (
101+ (std::atomic<std::int32_t > *)&handle.once , &zero, 1 ,
102+ std::memory_order_relaxed, std::memory_order_relaxed)) {
100103 SWIFT_C11THREADS_CHECK (::mtx_init (&handle.mutex , ::mtx_plain));
101- ::atomic_store_explicit (&handle.once, -1 , ::memory_order_release);
104+ std::atomic_store_explicit ((std::atomic<std::int32_t > *)&handle.once , -1 ,
105+ std::memory_order_release);
102106 return ;
103107 }
104108
105- while (::atomic_load_explicit (&handle.once , memory_order_acquire) >= 0 ) {
109+ while (std::atomic_load_explicit ((std::atomic<std::int32_t > *)&handle.once ,
110+ std::memory_order_acquire) >= 0 ) {
106111 // Just spin; ::mtx_init() is very likely to be fast
107112 }
108113}
109114
110115inline void lazy_mutex_destroy (lazy_mutex_handle &handle) {
111- if (::atomic_load_explicit (&handle.once , ::memory_order_acquire) < 0 )
112- SWIFT_C11THREADS_CHECK (::mtx_destroy (&handle.mutex ));
116+ if (std::atomic_load_explicit ((std::atomic<std::int32_t > *)&handle.once ,
117+ std::memory_order_acquire) < 0 )
118+ ::mtx_destroy (&handle.mutex);
113119}
114120
115121inline void lazy_mutex_lock (lazy_mutex_handle &handle) {
@@ -136,20 +142,24 @@ inline void lazy_mutex_unsafe_unlock(lazy_mutex_handle &handle) {
136142
137143// .. Once ...................................................................
138144
139- typedef ::atomic_int once_t ;
145+ typedef std::atomic<std:: int64_t > once_t ;
140146
141147void once_slow (once_t &predicate, void (*fn)(void *), void *context);
142148
143149inline void once_impl (once_t &predicate, void (*fn)(void *), void *context) {
144150 // Sadly we can't use call_once() for this (no context)
145- if (::atomic_load_explicit (&predicate, ::memory_order_acquire) < 0 )
151+ if (std ::atomic_load_explicit (&predicate, std ::memory_order_acquire) < 0 )
146152 return ;
147153
148154 once_slow (predicate, fn, context);
149155}
150156
151157// .. Thread local storage ...................................................
152158
159+ // Get rid of this, because it causes clashes with TokenKinds.def
160+ #undef thread_local
161+
162+ // We *can* use the C++ version though
153163#if __cplusplus >= 201103L || __has_feature(cxx_thread_local)
154164#define SWIFT_THREAD_LOCAL thread_local
155165#endif
0 commit comments