use a critical section instead

This commit is contained in:
beerpiss 2022-07-09 13:47:37 +07:00
parent c054710a8a
commit a4abf68c31
1 changed files with 32 additions and 5 deletions

View File

@ -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
}