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 (void) err;
22 assert(!err);
23 }
25 pthread_rwlock_destroy(&rwlock);
26 }
27
28 void lock() {
29 int err = pthread_rwlock_wrlock(&rwlock);
30 (void) err;
31 assert(!err);
32 }
34 bool try_lock() {
35 return pthread_rwlock_trywrlock(&rwlock) == 0;
36 }
37 void unlock() {
38 int err = pthread_rwlock_unlock(&rwlock);
39 (void) err;
40 assert(!err);
41 }
42
43 void lock_shared() {
44 int err = pthread_rwlock_rdlock(&rwlock);
45 (void) err;
46 assert(!err);
47 }
50 return pthread_rwlock_tryrdlock(&rwlock) == 0;
51 }
53 unlock();
54 }
55};
56
57
58template <class TMutex>
59struct SharedLock {
60 TMutex& m;
61
62 SharedLock(TMutex& m) : m(m) {
63 m.lock_shared();
64 }
66 m.unlock_shared();
67 }
68};
69
70
71} // namespace rack
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Definition mutex.hpp:59
SharedLock(TMutex &m)
Definition mutex.hpp:62
~SharedLock()
Definition mutex.hpp:65
TMutex & m
Definition mutex.hpp:60
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:49
SharedMutex()
Definition mutex.hpp:19
void unlock_shared()
Definition mutex.hpp:52
void lock()
Definition mutex.hpp:28
~SharedMutex()
Definition mutex.hpp:24
pthread_rwlock_t rwlock
Definition mutex.hpp:17
void lock_shared()
Definition mutex.hpp:43
void unlock()
Definition mutex.hpp:37
bool try_lock()
Returns whether the lock was acquired.
Definition mutex.hpp:34