mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin.
os_unfair_lock is much smaller than pthread_mutex_t (4 bytes versus 64) and a bit faster. However, it doesn't support condition variables. Most of our uses of Mutex don't use condition variables, but a few do. Introduce ConditionMutex and StaticConditionMutex, which allow condition variables and continue to use pthread_mutex_t. On all other platforms, we continue to use the same backing mutex type for both Mutex and ConditionMutex. rdar://problem/45412121
This commit is contained in:
@@ -114,6 +114,29 @@ bool MutexPlatformHelper::try_lock(pthread_mutex_t &mutex) {
|
||||
/* returnFalseOnEBUSY = */ true);
|
||||
}
|
||||
|
||||
#if HAS_OS_UNFAIR_LOCK
|
||||
|
||||
void MutexPlatformHelper::init(os_unfair_lock &lock, bool checked) {
|
||||
(void)checked; // Unfair locks are always checked.
|
||||
lock = OS_UNFAIR_LOCK_INIT;
|
||||
}
|
||||
|
||||
void MutexPlatformHelper::destroy(os_unfair_lock &lock) {}
|
||||
|
||||
void MutexPlatformHelper::lock(os_unfair_lock &lock) {
|
||||
os_unfair_lock_lock(&lock);
|
||||
}
|
||||
|
||||
void MutexPlatformHelper::unlock(os_unfair_lock &lock) {
|
||||
os_unfair_lock_unlock(&lock);
|
||||
}
|
||||
|
||||
bool MutexPlatformHelper::try_lock(os_unfair_lock &lock) {
|
||||
return os_unfair_lock_trylock(&lock);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void ReadWriteLockPlatformHelper::init(pthread_rwlock_t &rwlock) {
|
||||
reportError(pthread_rwlock_init(&rwlock, nullptr));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user