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
6
namespace
rack
{
7
8
16
struct
SharedMutex
{
17
pthread_rwlock_t
rwlock
;
18
19
SharedMutex
() {
20
int
err = pthread_rwlock_init(&
rwlock
, NULL);
21
(void) err;
22
assert(!err);
23
}
24
~SharedMutex
() {
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
}
49
bool
try_lock_shared
() {
50
return
pthread_rwlock_tryrdlock(&
rwlock
) == 0;
51
}
52
void
unlock_shared
() {
53
unlock
();
54
}
55
};
56
57
58
template
<
class
TMutex>
59
struct
SharedLock
{
60
TMutex&
m
;
61
62
SharedLock
(TMutex&
m
) :
m
(
m
) {
63
m
.lock_shared();
64
}
65
~SharedLock
() {
66
m
.unlock_shared();
67
}
68
};
69
70
71
}
// namespace rack
common.hpp
rack
Root namespace for the Rack API.
Definition
AudioDisplay.hpp:9
rack::SharedLock
Definition
mutex.hpp:59
rack::SharedLock::SharedLock
SharedLock(TMutex &m)
Definition
mutex.hpp:62
rack::SharedLock::~SharedLock
~SharedLock()
Definition
mutex.hpp:65
rack::SharedLock::m
TMutex & m
Definition
mutex.hpp:60
rack::SharedMutex
Allows multiple "reader" threads to obtain a lock simultaneously, but only one "writer" thread.
Definition
mutex.hpp:16
rack::SharedMutex::try_lock_shared
bool try_lock_shared()
Returns whether the lock was acquired.
Definition
mutex.hpp:49
rack::SharedMutex::SharedMutex
SharedMutex()
Definition
mutex.hpp:19
rack::SharedMutex::unlock_shared
void unlock_shared()
Definition
mutex.hpp:52
rack::SharedMutex::lock
void lock()
Definition
mutex.hpp:28
rack::SharedMutex::~SharedMutex
~SharedMutex()
Definition
mutex.hpp:24
rack::SharedMutex::rwlock
pthread_rwlock_t rwlock
Definition
mutex.hpp:17
rack::SharedMutex::lock_shared
void lock_shared()
Definition
mutex.hpp:43
rack::SharedMutex::unlock
void unlock()
Definition
mutex.hpp:37
rack::SharedMutex::try_lock
bool try_lock()
Returns whether the lock was acquired.
Definition
mutex.hpp:34
include
mutex.hpp
Generated by
1.12.0