- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNSLock.swift
343 lines (288 loc) · 9.09 KB
/
NSLock.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
#if os(macOS) || os(iOS)
import Darwin
#elseif os(Linux) || CYGWIN
import Glibc
#endif
import CoreFoundation
publicprotocolNSLocking{
func lock()
func unlock()
}
#if CYGWIN
privatetypealias_PthreadMutexPointer=UnsafeMutablePointer<pthread_mutex_t?>
privatetypealias_PthreadCondPointer=UnsafeMutablePointer<pthread_cond_t?>
#else
privatetypealias_PthreadMutexPointer=UnsafeMutablePointer<pthread_mutex_t>
privatetypealias_PthreadCondPointer=UnsafeMutablePointer<pthread_cond_t>
#endif
openclassNSLock:NSObject,NSLocking{
internalvarmutex= _PthreadMutexPointer.allocate(capacity:1)
#if os(macOS) || os(iOS)
privatevartimeoutCond= _PthreadCondPointer.allocate(capacity:1)
privatevartimeoutMutex= _PthreadMutexPointer.allocate(capacity:1)
#endif
publicoverrideinit(){
pthread_mutex_init(mutex,nil)
#if os(macOS) || os(iOS)
pthread_cond_init(timeoutCond,nil)
pthread_mutex_init(timeoutMutex,nil)
#endif
}
deinit{
pthread_mutex_destroy(mutex)
mutex.deinitialize(count:1)
mutex.deallocate()
#if os(macOS) || os(iOS)
deallocateTimedLockData(cond: timeoutCond, mutex: timeoutMutex)
#endif
}
openfunc lock(){
pthread_mutex_lock(mutex)
}
openfunc unlock(){
pthread_mutex_unlock(mutex)
#if os(macOS) || os(iOS)
// Wakeup any threads waiting in lock(before:)
pthread_mutex_lock(timeoutMutex)
pthread_cond_broadcast(timeoutCond)
pthread_mutex_unlock(timeoutMutex)
#endif
}
openfunc `try`()->Bool{
returnpthread_mutex_trylock(mutex)==0
}
openfunc lock(before limit:Date)->Bool{
ifpthread_mutex_trylock(mutex)==0{
returntrue
}
#if os(macOS) || os(iOS)
returntimedLock(mutex: mutex, endTime: limit, using: timeoutCond, with: timeoutMutex)
#else
guardvar endTime =timeSpecFrom(date: limit)else{
returnfalse
}
returnpthread_mutex_timedlock(mutex,&endTime)==0
#endif
}
openvarname:String?
}
extensionNSLock{
internalfunc synchronized<T>(_ closure:()->T)->T{
self.lock()
defer{self.unlock()}
returnclosure()
}
}
openclassNSConditionLock:NSObject,NSLocking{
internalvar_cond=NSCondition()
internalvar_value:Int
internalvar_thread:pthread_t?
publicconvenienceoverrideinit(){
self.init(condition:0)
}
publicinit(condition:Int){
_value = condition
}
openfunc lock(){
let _ =lock(before:Date.distantFuture)
}
openfunc unlock(){
_cond.lock()
_thread =nil
_cond.broadcast()
_cond.unlock()
}
openvarcondition:Int{
return _value
}
openfunc lock(whenCondition condition:Int){
let _ =lock(whenCondition: condition, before:Date.distantFuture)
}
openfunc `try`()->Bool{
returnlock(before:Date.distantPast)
}
openfunc tryLock(whenCondition condition:Int)->Bool{
returnlock(whenCondition: condition, before:Date.distantPast)
}
openfunc unlock(withCondition condition:Int){
_cond.lock()
_thread =nil
_value = condition
_cond.broadcast()
_cond.unlock()
}
openfunc lock(before limit:Date)->Bool{
_cond.lock()
while _thread !=nil{
if !_cond.wait(until: limit){
_cond.unlock()
returnfalse
}
}
_thread =pthread_self()
_cond.unlock()
returntrue
}
openfunc lock(whenCondition condition:Int, before limit:Date)->Bool{
_cond.lock()
while _thread !=nil || _value != condition {
if !_cond.wait(until: limit){
_cond.unlock()
returnfalse
}
}
_thread =pthread_self()
_cond.unlock()
returntrue
}
openvarname:String?
}
openclassNSRecursiveLock:NSObject,NSLocking{
internalvarmutex= _PthreadMutexPointer.allocate(capacity:1)
#if os(macOS) || os(iOS)
privatevartimeoutCond= _PthreadCondPointer.allocate(capacity:1)
privatevartimeoutMutex= _PthreadMutexPointer.allocate(capacity:1)
#endif
publicoverrideinit(){
super.init()
#if CYGWIN
varattrib:pthread_mutexattr_t?=nil
#else
varattrib=pthread_mutexattr_t()
#endif
withUnsafeMutablePointer(to:&attrib){ attrs in
pthread_mutexattr_settype(attrs,Int32(PTHREAD_MUTEX_RECURSIVE))
pthread_mutex_init(mutex, attrs)
}
}
deinit{
pthread_mutex_destroy(mutex)
mutex.deinitialize(count:1)
mutex.deallocate()
#if os(macOS) || os(iOS)
deallocateTimedLockData(cond: timeoutCond, mutex: timeoutMutex)
#endif
}
openfunc lock(){
pthread_mutex_lock(mutex)
}
openfunc unlock(){
pthread_mutex_unlock(mutex)
#if os(macOS) || os(iOS)
// Wakeup any threads waiting in lock(before:)
pthread_mutex_lock(timeoutMutex)
pthread_cond_broadcast(timeoutCond)
pthread_mutex_unlock(timeoutMutex)
#endif
}
openfunc `try`()->Bool{
returnpthread_mutex_trylock(mutex)==0
}
openfunc lock(before limit:Date)->Bool{
ifpthread_mutex_trylock(mutex)==0{
returntrue
}
#if os(macOS) || os(iOS)
returntimedLock(mutex: mutex, endTime: limit, using: timeoutCond, with: timeoutMutex)
#else
guardvar endTime =timeSpecFrom(date: limit)else{
returnfalse
}
returnpthread_mutex_timedlock(mutex,&endTime)==0
#endif
}
openvarname:String?
}
openclassNSCondition:NSObject,NSLocking{
internalvarmutex= _PthreadMutexPointer.allocate(capacity:1)
internalvarcond= _PthreadCondPointer.allocate(capacity:1)
publicoverrideinit(){
pthread_mutex_init(mutex,nil)
pthread_cond_init(cond,nil)
}
deinit{
pthread_mutex_destroy(mutex)
pthread_cond_destroy(cond)
mutex.deinitialize(count:1)
cond.deinitialize(count:1)
mutex.deallocate()
cond.deallocate()
}
openfunc lock(){
pthread_mutex_lock(mutex)
}
openfunc unlock(){
pthread_mutex_unlock(mutex)
}
openfunc wait(){
pthread_cond_wait(cond, mutex)
}
openfunc wait(until limit:Date)->Bool{
guardvar timeout =timeSpecFrom(date: limit)else{
returnfalse
}
returnpthread_cond_timedwait(cond, mutex,&timeout)==0
}
openfunc signal(){
pthread_cond_signal(cond)
}
openfunc broadcast(){
pthread_cond_broadcast(cond)
}
openvarname:String?
}
privatefunc timeSpecFrom(date:Date)->timespec?{
guard date.timeIntervalSinceNow >0else{
returnnil
}
letnsecPerSec:Int64=1_000_000_000
letinterval= date.timeIntervalSince1970
letintervalNS=Int64(interval * Double(nsecPerSec))
returntimespec(tv_sec:Int(intervalNS / nsecPerSec),
tv_nsec:Int(intervalNS % nsecPerSec))
}
#if os(macOS) || os(iOS)
privatefunc deallocateTimedLockData(cond:_PthreadCondPointer, mutex:_PthreadMutexPointer){
pthread_cond_destroy(cond)
cond.deinitialize(count:1)
cond.deallocate()
pthread_mutex_destroy(mutex)
mutex.deinitialize(count:1)
mutex.deallocate()
}
// Emulate pthread_mutex_timedlock using pthread_cond_timedwait.
// lock(before:) passes a condition variable/mutex pair to use.
// unlock() will use pthread_cond_broadcast() to wake any waits in progress.
privatefunc timedLock(mutex:_PthreadMutexPointer, endTime:Date,
using timeoutCond:_PthreadCondPointer,
with timeoutMutex:_PthreadMutexPointer)->Bool{
vartimeSpec=timeSpecFrom(date: endTime)
whilevar ts = timeSpec {
letlockval=pthread_mutex_lock(timeoutMutex)
precondition(lockval ==0)
letwaitval=pthread_cond_timedwait(timeoutCond, timeoutMutex,&ts)
precondition(waitval ==0 || waitval == ETIMEDOUT)
letunlockval=pthread_mutex_unlock(timeoutMutex)
precondition(unlockval ==0)
if waitval == ETIMEDOUT {
returnfalse
}
lettryval=pthread_mutex_trylock(mutex)
precondition(tryval ==0 || tryval == EBUSY)
if tryval ==0{ // The lock was obtained.
returntrue
}
// pthread_cond_timedwait didnt timeout so wait some more.
timeSpec =timeSpecFrom(date: endTime)
}
returnfalse
}
#endif