use a critical section instead
This commit is contained in:
parent
c054710a8a
commit
a4abf68c31
|
@ -8,19 +8,46 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
#if os(Windows)
|
||||
import WinSDK
|
||||
#endif
|
||||
|
||||
final class Mutex: NSLocking {
|
||||
|
||||
private var semaphore: DispatchSemaphore
|
||||
#if os(Windows)
|
||||
private var mutex = CRITICAL_SECTION()
|
||||
|
||||
init() {
|
||||
semaphore = DispatchSemaphore(value: 1)
|
||||
InitializeCriticalSection(&mutex)
|
||||
}
|
||||
|
||||
deinit {
|
||||
DeleteCriticalSection(&mutex)
|
||||
}
|
||||
|
||||
func lock() {
|
||||
semaphore.wait()
|
||||
EnterCriticalSection(&mutex)
|
||||
}
|
||||
|
||||
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