VCV Rack API v2
Loading...
Searching...
No Matches
mutex.hpp
Go to the documentation of this file.
1#pragma once
2#include <common.hpp>
3#include <pthread.h>
4
5
6namespace rack {
7
8
17 pthread_rwlock_t rwlock;
18
20 int err = pthread_rwlock_init(&rwlock, NULL);
21 assert(!err);
22 }
24 pthread_rwlock_destroy(&rwlock);
25 }
26
27 void lock() {
28 int err = pthread_rwlock_wrlock(&rwlock);
29 assert(!err);
30 }
32 bool try_lock() {
33 return pthread_rwlock_trywrlock(&rwlock) == 0;
34 }
35 void unlock() {
36 int err = pthread_rwlock_unlock(&rwlock);
37 assert(!err);
38 }
39
40 void lock_shared() {
41 int err = pthread_rwlock_rdlock(&rwlock);
42 assert(!err);
43 }
46 return pthread_rwlock_tryrdlock(&rwlock) == 0;
47 }
49 unlock();
50 }
51};
52
53
54template <class TMutex>
55struct SharedLock {
56 TMutex& m;
57
58 SharedLock(TMutex& m) : m(m) {
59 m.lock_shared();
60 }
62 m.unlock_shared();
63 }
64};
65
66
67} // namespace rack
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Definition mutex.hpp:55
SharedLock(TMutex &m)
Definition mutex.hpp:58
~SharedLock()
Definition mutex.hpp:61
TMutex & m
Definition mutex.hpp:56
Allows multiple "reader" threads to obtain a lock simultaneously, but only one "writer" thread.
Definition mutex.hpp:16
bool try_lock_shared()
Returns whether the lock was acquired.
Definition mutex.hpp:45
SharedMutex()
Definition mutex.hpp:19
void unlock_shared()
Definition mutex.hpp:48
void lock()
Definition mutex.hpp:27
~SharedMutex()
Definition mutex.hpp:23
pthread_rwlock_t rwlock
Definition mutex.hpp:17
void lock_shared()
Definition mutex.hpp:40
void unlock()
Definition mutex.hpp:35
bool try_lock()
Returns whether the lock was acquired.
Definition mutex.hpp:32