13 #ifndef __PROCESS_RWMUTEX_HPP__ 14 #define __PROCESS_RWMUTEX_HPP__ 48 synchronized (data->lock) {
49 if (!data->write_locked && data->read_locked == 0u) {
50 data->write_locked =
true;
53 future = w.promise.future();
54 data->waiters.push(std::move(w));
66 std::queue<Waiter> unblocked;
68 synchronized (data->lock) {
69 CHECK(data->write_locked);
70 CHECK_EQ(data->read_locked, 0u);
72 data->write_locked =
false;
74 if (!data->waiters.empty()) {
75 switch (data->waiters.front().type) {
78 while (!data->waiters.empty() &&
80 unblocked.push(std::move(data->waiters.front()));
84 data->read_locked = unblocked.size();
89 unblocked.push(std::move(data->waiters.front()));
91 data->write_locked =
true;
93 CHECK_EQ(data->read_locked, 0u);
100 while (!unblocked.empty()) {
101 unblocked.front().promise.set(
Nothing());
113 synchronized (data->lock) {
114 if (!data->write_locked && data->waiters.empty()) {
118 future = w.promise.future();
119 data->waiters.push(std::move(w));
133 synchronized (data->lock) {
134 CHECK(!data->write_locked);
135 CHECK_GT(data->read_locked, 0u);
139 if (data->read_locked == 0u && !data->waiters.empty()) {
142 waiter = std::move(data->waiters.front());
144 data->write_locked =
true;
149 waiter->promise.set(
Nothing());
156 enum { READ, WRITE }
type;
162 Data() : read_locked(0), write_locked(
false) {}
180 std::queue<Waiter> waiters;
184 std::atomic_flag lock = ATOMIC_FLAG_INIT;
187 std::shared_ptr<Data> data;
192 #endif // __PROCESS_RWMUTEX_HPP__ const short READ
A possible event while polling.
Definition: io.hpp:34
Future< Nothing > read_lock()
Definition: rwlock.hpp:109
Definition: nothing.hpp:16
Definition: option.hpp:29
void write_unlock()
Definition: rwlock.hpp:61
void read_unlock()
Definition: rwlock.hpp:126
bool isSome() const
Definition: option.hpp:116
Future< Nothing > write_lock()
Definition: rwlock.hpp:44
const short WRITE
A possible event while polling.
Definition: io.hpp:40
Protocol< PromiseRequest, PromiseResponse > promise
Definition: executor.hpp:48
ReadWriteLock is a lock that allows concurrent reads and exclusive writes.
Definition: rwlock.hpp:36
Try< uint32_t > type(const std::string &path)
ReadWriteLock()
Definition: rwlock.hpp:39