17
17
#include " swift/Runtime/Once.h"
18
18
#include " swift/Runtime/Debug.h"
19
19
#include < type_traits>
20
+ #if defined(__CYGWIN__)
21
+ #include < mutex>
22
+ #endif
20
23
21
24
using namespace swift ;
22
25
@@ -31,9 +34,11 @@ static_assert(std::is_same<swift_once_t, dispatch_once_t>::value,
31
34
// The compiler generates the swift_once_t values as word-sized zero-initialized
32
35
// variables, so we want to make sure swift_once_t isn't larger than the
33
36
// platform word or the function below might overwrite something it shouldn't.
34
- #if !defined(__CYGWIN__)
35
37
static_assert (sizeof (swift_once_t ) <= sizeof(void *),
36
38
"swift_once_t must be no larger than the platform word");
39
+
40
+ #if defined(__CYGWIN__)
41
+ static std::mutex mutex_;
37
42
#endif
38
43
39
44
// / Runs the given function with the given context argument exactly once.
@@ -42,6 +47,18 @@ static_assert(sizeof(swift_once_t) <= sizeof(void*),
42
47
void swift::swift_once (swift_once_t *predicate, void (*fn)(void *)) {
43
48
#if defined(__APPLE__)
44
49
dispatch_once_f (predicate, nullptr , fn);
50
+ #else
51
+ #if defined(__CYGWIN__)
52
+
53
+ mutex_.lock ();
54
+ if (*predicate == 0 ) {
55
+ *predicate = 1ul ;
56
+ mutex_.unlock ();
57
+
58
+ fn (nullptr );
59
+ } else
60
+ mutex_.unlock ();
61
+
45
62
#else
46
63
// FIXME: We're relying here on the coincidence that libstdc++ uses pthread's
47
64
// pthread_once, and that on glibc pthread_once follows a compatible init
@@ -51,4 +68,5 @@ void swift::swift_once(swift_once_t *predicate, void (*fn)(void *)) {
51
68
// For more information, see rdar://problem/18499385
52
69
std::call_once (*predicate, [fn]() { fn (nullptr ); });
53
70
#endif
71
+ #endif
54
72
}
0 commit comments