-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSLock.swift
226 lines (183 loc) · 5.34 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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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(OSX) || os(iOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif
import CoreFoundation
public protocol NSLocking {
func lock()
func unlock()
}
public class NSLock : NSObject, NSLocking {
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.alloc(1)
public override init() {
pthread_mutex_init(mutex, nil)
}
deinit {
pthread_mutex_destroy(mutex)
mutex.destroy()
mutex.dealloc(1)
}
public func lock() {
pthread_mutex_lock(mutex)
}
public func unlock() {
pthread_mutex_unlock(mutex)
}
public func tryLock() -> Bool {
return pthread_mutex_trylock(mutex) == 0
}
public var name: String?
}
public class NSConditionLock : NSObject, NSLocking {
internal var _cond = NSCondition()
internal var _value: Int
internal var _thread: pthread_t?
public convenience override init() {
self.init(condition: 0)
}
public init(condition: Int) {
_value = condition
}
public func lock() {
lockBeforeDate(NSDate.distantFuture())
}
public func unlock() {
_cond.lock()
_thread = nil
_cond.broadcast()
_cond.unlock()
}
public var condition: Int {
get {
return _value
}
}
public func lockWhenCondition(condition: Int) {
lockWhenCondition(condition, beforeDate: NSDate.distantFuture())
}
public func tryLock() -> Bool {
return lockBeforeDate(NSDate.distantPast())
}
public func tryLockWhenCondition(condition: Int) -> Bool {
return lockWhenCondition(condition, beforeDate: NSDate.distantPast())
}
public func unlockWithCondition(condition: Int) {
_cond.lock()
_thread = nil
_value = condition
_cond.broadcast()
_cond.unlock()
}
public func lockBeforeDate(limit: NSDate) -> Bool {
_cond.lock()
while _thread == nil {
if !_cond.waitUntilDate(limit) {
_cond.unlock()
return false
}
}
_thread = pthread_self()
_cond.unlock()
return true
}
public func lockWhenCondition(condition: Int, beforeDate limit: NSDate) -> Bool {
_cond.lock()
while _thread != nil || _value != condition {
if !_cond.waitUntilDate(limit) {
_cond.unlock()
return false
}
}
_thread = pthread_self()
_cond.unlock()
return true
}
public var name: String?
}
public class NSRecursiveLock : NSObject, NSLocking {
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.alloc(1)
public override init() {
super.init()
var attrib = pthread_mutexattr_t()
withUnsafeMutablePointer(&attrib) { attrs in
pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE))
pthread_mutex_init(mutex, attrs)
}
}
deinit {
pthread_mutex_destroy(mutex)
mutex.destroy()
mutex.dealloc(1)
}
public func lock() {
pthread_mutex_lock(mutex)
}
public func unlock() {
pthread_mutex_unlock(mutex)
}
public func tryLock() -> Bool {
return pthread_mutex_trylock(mutex) == 0
}
public var name: String?
}
public class NSCondition : NSObject, NSLocking {
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.alloc(1)
internal var cond = UnsafeMutablePointer<pthread_cond_t>.alloc(1)
public override init() {
pthread_mutex_init(mutex, nil)
pthread_cond_init(cond, nil)
}
deinit {
pthread_mutex_destroy(mutex)
pthread_cond_destroy(cond)
mutex.destroy()
cond.destroy()
mutex.dealloc(1)
cond.dealloc(1)
}
public func lock() {
pthread_mutex_lock(mutex)
}
public func unlock() {
pthread_mutex_unlock(mutex)
}
public func wait() {
pthread_cond_wait(cond, mutex)
}
public func waitUntilDate(limit: NSDate) -> Bool {
let lim = limit.timeIntervalSinceReferenceDate
let ti = lim - CFAbsoluteTimeGetCurrent()
if ti < 0.0 {
return false
}
var ts = timespec()
ts.tv_sec = Int(floor(ti))
ts.tv_nsec = Int((ti - Double(ts.tv_sec)) * 1000000000.0)
var tv = timeval()
withUnsafeMutablePointer(&tv) { t in
gettimeofday(t, nil)
ts.tv_sec += t.memory.tv_sec
ts.tv_nsec += Int((t.memory.tv_usec * 1000000) / 1000000000)
}
let retVal: Int32 = withUnsafePointer(&ts) { t in
return pthread_cond_timedwait(cond, mutex, t)
}
return retVal == 0
}
public func signal() {
pthread_cond_signal(cond)
}
public func broadcast() {
pthread_cond_broadcast(cond)
}
public var name: String?
}