27#include <seastar/core/semaphore.hh>
28#include <seastar/util/assert.hh>
29#include <seastar/util/modules.hh>
36template<
typename Clock>
39template<typename Clock = typename timer<>::clock>
40class rwlock_for_read {
43 return static_cast<basic_rwlock<Clock>*
>(
this)->read_lock();
46 static_cast<basic_rwlock<Clock>*
>(
this)->read_unlock();
48 friend class basic_rwlock<Clock>;
51template<typename Clock = typename timer<>::clock>
52class rwlock_for_write {
55 return static_cast<basic_rwlock<Clock>*
>(
this)->write_lock();
58 static_cast<basic_rwlock<Clock>*
>(
this)->write_unlock();
60 friend class basic_rwlock<Clock>;
75template<typename Clock = typename timer<>::clock>
76class basic_rwlock :
private rwlock_for_read<Clock>, rwlock_for_write<Clock> {
104 future<> read_lock(
typename semaphore_type::time_point timeout = semaphore_type::time_point::max()) {
105 return _sem.
wait(timeout);
109 return _sem.
wait(as);
116 SEASTAR_ASSERT(_sem.
current() < max_ops);
124 future<> write_lock(
typename semaphore_type::time_point timeout = semaphore_type::time_point::max()) {
125 return _sem.
wait(timeout, max_ops);
129 return _sem.
wait(as, max_ops);
136 SEASTAR_ASSERT(_sem.
current() == 0);
164 return get_units(_sem, 1, timeout);
168 return get_units(_sem, 1, as);
174 return try_get_units(_sem, 1);
189 return get_units(_sem, max_ops, timeout);
193 return get_units(_sem, max_ops, as);
199 return try_get_units(_sem, max_ops);
207 friend class rwlock_for_read<Clock>;
208 friend class rwlock_for_write<Clock>;
212using rwlock = basic_rwlock<>;
Definition: abort_source.hh:59
future read_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:104
std::optional< holder > try_hold_write_lock() noexcept
Definition: rwlock.hh:198
void read_unlock()
Definition: rwlock.hh:115
std::optional< holder > try_hold_read_lock() noexcept
Definition: rwlock.hh:173
future write_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:124
bool try_read_lock()
Tries to acquire the lock in read mode iff this can be done without waiting.
Definition: rwlock.hh:141
bool try_write_lock()
Tries to acquire the lock in write mode iff this can be done without waiting.
Definition: rwlock.hh:146
rwlock_for_write< Clock > & for_write()
Definition: rwlock.hh:97
rwlock_for_read< Clock > & for_read()
Definition: rwlock.hh:90
future< holder > hold_read_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:163
bool locked() const
Checks if any read or write locks are currently held.
Definition: rwlock.hh:203
future< holder > hold_write_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:188
void write_unlock()
Definition: rwlock.hh:135
Counted resource guard.
Definition: semaphore.hh:154
static constexpr size_t max_counter() noexcept
Returns the maximum number of units the semaphore counter can hold.
Definition: semaphore.hh:239
size_t current() const noexcept
Definition: semaphore.hh:460
future wait(size_t nr=1) noexcept
Definition: semaphore.hh:307
ssize_t available_units() const noexcept
Definition: semaphore.hh:466
bool try_wait(size_t nr=1) noexcept
Definition: semaphore.hh:448
void signal(size_t nr=1) noexcept
Definition: semaphore.hh:410
A representation of a possibly not-yet-computed value.
Definition: future.hh:1197
Definition: semaphore.hh:525
Seastar API namespace.
Definition: abort_on_ebadf.hh:26