Last active
April 17, 2021 13:42
-
-
Save Lessica/1a08d3ac392250d79f74f1623b02c91e to your computer and use it in GitHub Desktop.
ReadWriteLock: pthread_rwlock_t wrapper in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class ReadWriteLock { | |
private var rwlock: pthread_rwlock_t = { | |
var rwlock = pthread_rwlock_t() | |
pthread_rwlock_init(&rwlock, nil) | |
return rwlock | |
}() | |
func writeLock() { | |
pthread_rwlock_wrlock(&rwlock) | |
} | |
func readLock() { | |
pthread_rwlock_rdlock(&rwlock) | |
} | |
func tryReadLock() -> Bool { | |
return pthread_rwlock_tryrdlock(&rwlock) == 0 | |
} | |
func tryWriteLock() -> Bool { | |
return pthread_rwlock_trywrlock(&rwlock) == 0 | |
} | |
func unlock() { | |
pthread_rwlock_unlock(&rwlock) | |
} | |
deinit { | |
pthread_rwlock_destroy(&rwlock) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment