use a critical section instead
This commit is contained in:
parent
c054710a8a
commit
a4abf68c31
|
@ -8,19 +8,46 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
#if os(Windows)
|
||||||
|
import WinSDK
|
||||||
|
#endif
|
||||||
|
|
||||||
final class Mutex: NSLocking {
|
final class Mutex: NSLocking {
|
||||||
|
#if os(Windows)
|
||||||
private var semaphore: DispatchSemaphore
|
private var mutex = CRITICAL_SECTION()
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
semaphore = DispatchSemaphore(value: 1)
|
InitializeCriticalSection(&mutex)
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
DeleteCriticalSection(&mutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
func lock() {
|
func lock() {
|
||||||
semaphore.wait()
|
EnterCriticalSection(&mutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
func unlock() {
|
func unlock() {
|
||||||
semaphore.signal()
|
LeaveCriticalSection(&mutex)
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
private var mutex = pthread_mutex_t()
|
||||||
|
|
||||||
|
init() {
|
||||||
|
pthread_mutex_init(&mutex, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
pthread_mutex_destroy(&mutex)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lock() {
|
||||||
|
pthread_mutex_lock(&mutex)
|
||||||
|
}
|
||||||
|
|
||||||
|
func unlock() {
|
||||||
|
pthread_mutex_unlock(&mutex)
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue