10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
//
13
- // Mutex, ConditionVariable, Read/Write lock, and Scoped lock implementations
13
+ // Mutex, Read/Write lock, and Scoped lock implementations
14
14
// using PThreads.
15
15
//
16
16
// ===----------------------------------------------------------------------===//
27
27
28
28
namespace swift {
29
29
30
- typedef pthread_cond_t ConditionHandle;
31
- typedef pthread_mutex_t ConditionMutexHandle;
32
30
typedef pthread_rwlock_t ReadWriteLockHandle;
33
31
34
32
#if HAS_OS_UNFAIR_LOCK
@@ -42,50 +40,18 @@ typedef pthread_mutex_t MutexHandle;
42
40
// constexpr for static allocation versions. The way they define things
43
41
// results in a reinterpret_cast which violates constexpr.
44
42
// WASI currently doesn't support threading/locking at all.
45
- #define SWIFT_CONDITION_SUPPORTS_CONSTEXPR 0
46
43
#define SWIFT_MUTEX_SUPPORTS_CONSTEXPR 0
47
44
#define SWIFT_READWRITELOCK_SUPPORTS_CONSTEXPR 0
48
45
#else
49
- #define SWIFT_CONDITION_SUPPORTS_CONSTEXPR 1
50
46
#define SWIFT_MUTEX_SUPPORTS_CONSTEXPR 1
51
47
#define SWIFT_READWRITELOCK_SUPPORTS_CONSTEXPR 1
52
48
#endif
53
49
54
- // / PThread low-level implementation that supports ConditionVariable
55
- // / found in Mutex.h
56
- // /
57
- // / See ConditionVariable
58
- struct ConditionPlatformHelper {
59
- #if SWIFT_CONDITION_SUPPORTS_CONSTEXPR
60
- static constexpr
61
- #else
62
- static
63
- #endif
64
- ConditionHandle
65
- staticInit () {
66
- return PTHREAD_COND_INITIALIZER;
67
- };
68
- static void init (ConditionHandle &condition);
69
- static void destroy (ConditionHandle &condition);
70
- static void notifyOne (ConditionHandle &condition);
71
- static void notifyAll (ConditionHandle &condition);
72
- static void wait (ConditionHandle &condition, ConditionMutexHandle &mutex);
73
- };
74
-
75
50
// / PThread low-level implementation that supports Mutex
76
51
// / found in Mutex.h
77
52
// /
78
53
// / See Mutex
79
54
struct MutexPlatformHelper {
80
- #if SWIFT_MUTEX_SUPPORTS_CONSTEXPR
81
- static constexpr
82
- #else
83
- static
84
- #endif
85
- ConditionMutexHandle
86
- conditionStaticInit () {
87
- return PTHREAD_MUTEX_INITIALIZER;
88
- };
89
55
#if SWIFT_MUTEX_SUPPORTS_CONSTEXPR
90
56
static constexpr
91
57
#else
@@ -99,35 +65,55 @@ struct MutexPlatformHelper {
99
65
return PTHREAD_MUTEX_INITIALIZER;
100
66
#endif
101
67
}
102
- static void init (ConditionMutexHandle &mutex, bool checked = false );
103
- static void destroy (ConditionMutexHandle &mutex);
104
- static void lock (ConditionMutexHandle &mutex);
105
- static void unlock (ConditionMutexHandle &mutex);
106
- static bool try_lock (ConditionMutexHandle &mutex);
107
-
108
- // The ConditionMutexHandle versions handle everything on-Apple platforms.
109
- #if HAS_OS_UNFAIR_LOCK
110
68
111
69
static void init (MutexHandle &mutex, bool checked = false );
112
70
static void destroy (MutexHandle &mutex);
113
71
static void lock (MutexHandle &mutex);
114
72
static void unlock (MutexHandle &mutex);
115
73
static bool try_lock (MutexHandle &mutex);
116
74
75
+ #if HAS_OS_UNFAIR_LOCK
117
76
// os_unfair_lock always checks for errors, so just call through.
118
- static void unsafeLock (MutexHandle &mutex) { lock (mutex); }
119
- static void unsafeUnlock (MutexHandle &mutex) { unlock (mutex); }
120
- #endif
121
-
122
- // The unsafe versions don't do error checking.
123
- static void unsafeLock (ConditionMutexHandle &mutex) {
77
+ static void unsafeLock (MutexHandle &mutex) {
78
+ lock (mutex);
79
+ }
80
+ static void unsafeUnlock (MutexHandle &mutex) {
81
+ unlock (mutex);
82
+ }
83
+ #else
84
+ // Skip error checking for the unsafe versions.
85
+ static void unsafeLock (MutexHandle &mutex) {
124
86
(void )pthread_mutex_lock (&mutex);
125
87
}
126
- static void unsafeUnlock (ConditionMutexHandle &mutex) {
88
+ static void unsafeUnlock (MutexHandle &mutex) {
127
89
(void )pthread_mutex_unlock (&mutex);
128
90
}
91
+ #endif
129
92
};
130
93
94
+ #if HAS_OS_UNFAIR_LOCK
95
+
96
+ inline void MutexPlatformHelper::init (os_unfair_lock &lock, bool checked) {
97
+ (void )checked; // Unfair locks are always checked.
98
+ lock = OS_UNFAIR_LOCK_INIT;
99
+ }
100
+
101
+ inline void MutexPlatformHelper::destroy (os_unfair_lock &lock) {}
102
+
103
+ inline void MutexPlatformHelper::lock (os_unfair_lock &lock) {
104
+ os_unfair_lock_lock (&lock);
105
+ }
106
+
107
+ inline void MutexPlatformHelper::unlock (os_unfair_lock &lock) {
108
+ os_unfair_lock_unlock (&lock);
109
+ }
110
+
111
+ inline bool MutexPlatformHelper::try_lock (os_unfair_lock &lock) {
112
+ return os_unfair_lock_trylock (&lock);
113
+ }
114
+
115
+ #endif
116
+
131
117
// / PThread low-level implementation that supports ReadWriteLock
132
118
// / found in Mutex.h
133
119
// /
0 commit comments