| @@ -7,8 +7,11 @@ namespace rack { | |||||
| /** Allows multiple "reader" threads to obtain a lock simultaneously, but only one "writer" thread. | /** Allows multiple "reader" threads to obtain a lock simultaneously, but only one "writer" thread. | ||||
| This implementation is currently just a wrapper for pthreads, which works on Linux/Mac/. | This implementation is currently just a wrapper for pthreads, which works on Linux/Mac/. | ||||
| This is available in C++17 as std::shared_mutex, but unfortunately we're using C++11. | This is available in C++17 as std::shared_mutex, but unfortunately we're using C++11. | ||||
| Locking should be avoided in real-time audio threads. | |||||
| */ | */ | ||||
| struct SharedMutex { | struct SharedMutex { | ||||
| pthread_rwlock_t rwlock; | pthread_rwlock_t rwlock; | ||||
| @@ -22,12 +25,12 @@ struct SharedMutex { | |||||
| } | } | ||||
| void lock() { | void lock() { | ||||
| if (pthread_rwlock_rdlock(&rwlock)) | |||||
| throw Exception("pthread_rwlock_rdlock failed"); | |||||
| if (pthread_rwlock_wrlock(&rwlock)) | |||||
| throw Exception("pthread_rwlock_wrlock failed"); | |||||
| } | } | ||||
| /** Returns whether the lock was acquired. */ | /** Returns whether the lock was acquired. */ | ||||
| bool try_lock() { | bool try_lock() { | ||||
| return pthread_rwlock_tryrdlock(&rwlock) == 0; | |||||
| return pthread_rwlock_trywrlock(&rwlock) == 0; | |||||
| } | } | ||||
| void unlock() { | void unlock() { | ||||
| if (pthread_rwlock_unlock(&rwlock)) | if (pthread_rwlock_unlock(&rwlock)) | ||||
| @@ -35,16 +38,15 @@ struct SharedMutex { | |||||
| } | } | ||||
| void lock_shared() { | void lock_shared() { | ||||
| if (pthread_rwlock_wrlock(&rwlock)) | |||||
| throw Exception("pthread_rwlock_wrlock failed"); | |||||
| if (pthread_rwlock_rdlock(&rwlock)) | |||||
| throw Exception("pthread_rwlock_rdlock failed"); | |||||
| } | } | ||||
| /** Returns whether the lock was acquired. */ | /** Returns whether the lock was acquired. */ | ||||
| bool try_lock_shared() { | bool try_lock_shared() { | ||||
| return pthread_rwlock_trywrlock(&rwlock) == 0; | |||||
| return pthread_rwlock_tryrdlock(&rwlock) == 0; | |||||
| } | } | ||||
| void unlock_shared() { | void unlock_shared() { | ||||
| if (pthread_rwlock_unlock(&rwlock)) | |||||
| throw Exception("pthread_rwlock_unlock failed"); | |||||
| unlock(); | |||||
| } | } | ||||
| }; | }; | ||||