24 #ifndef SEASTAR_MODULE
27 #include <seastar/core/semaphore.hh>
28 #include <seastar/util/modules.hh>
35 template<
typename Clock>
38 template<typename Clock = typename timer<>::clock>
39 class rwlock_for_read {
42 return static_cast<basic_rwlock<Clock>*
>(
this)->read_lock();
45 static_cast<basic_rwlock<Clock>*
>(
this)->read_unlock();
47 friend class basic_rwlock<Clock>;
50 template<typename Clock = typename timer<>::clock>
51 class rwlock_for_write {
54 return static_cast<basic_rwlock<Clock>*
>(
this)->write_lock();
57 static_cast<basic_rwlock<Clock>*
>(
this)->write_unlock();
59 friend class basic_rwlock<Clock>;
74 template<typename Clock = typename timer<>::clock>
75 class basic_rwlock :
private rwlock_for_read<Clock>, rwlock_for_write<Clock> {
103 future<> read_lock(
typename semaphore_type::time_point timeout = semaphore_type::time_point::max()) {
104 return _sem.
wait(timeout);
108 return _sem.
wait(as);
115 assert(_sem.
current() < max_ops);
123 future<> write_lock(
typename semaphore_type::time_point timeout = semaphore_type::time_point::max()) {
124 return _sem.
wait(timeout, max_ops);
128 return _sem.
wait(as, max_ops);
163 return get_units(_sem, 1, timeout);
167 return get_units(_sem, 1, as);
182 return get_units(_sem, max_ops, timeout);
186 return get_units(_sem, max_ops, as);
194 friend class rwlock_for_read<Clock>;
195 friend class rwlock_for_write<Clock>;
198 SEASTAR_MODULE_EXPORT
199 using 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:103
void read_unlock()
Definition: rwlock.hh:114
rwlock_for_read< Clock > & for_read()
Definition: rwlock.hh:89
future< holder > hold_write_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:181
future write_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:123
bool try_read_lock()
Tries to acquire the lock in read mode iff this can be done without waiting.
Definition: rwlock.hh:140
bool try_write_lock()
Tries to acquire the lock in write mode iff this can be done without waiting.
Definition: rwlock.hh:145
bool locked() const
Checks if any read or write locks are currently held.
Definition: rwlock.hh:190
future< holder > hold_read_lock(typename semaphore_type::time_point timeout=semaphore_type::time_point::max())
Definition: rwlock.hh:162
rwlock_for_write< Clock > & for_write()
Definition: rwlock.hh:96
void write_unlock()
Definition: rwlock.hh:134
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:231
size_t current() const noexcept
Definition: semaphore.hh:446
future wait(size_t nr=1) noexcept
Definition: semaphore.hh:299
ssize_t available_units() const noexcept
Definition: semaphore.hh:452
bool try_wait(size_t nr=1) noexcept
Definition: semaphore.hh:434
void signal(size_t nr=1) noexcept
Definition: semaphore.hh:396
Definition: semaphore.hh:511
Seastar API namespace.
Definition: abort_on_ebadf.hh:26