CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
shared_mutex.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <atomic>
8
9#include "coopsync_tbb/detail/macros.hpp"
10#include "coopsync_tbb/detail/wait_queue.hpp"
11
12namespace coopsync_tbb {
13
24class COOPSYNC_TBB_EXPORT shared_mutex {
25 public:
27 class scoped_lock;
28
31 shared_mutex() = default;
32
34 shared_mutex(const shared_mutex&) = delete;
35
39
41
44
50
54 COOPSYNC_TBB_NODISCARD bool try_lock() noexcept;
55
61 void lock();
62
67 void unlock();
68
73 COOPSYNC_TBB_NODISCARD bool try_lock_shared() noexcept;
74
82
88
90 static constexpr bool is_rw_mutex = true;
92 static constexpr bool is_recursive_mutex = false;
94 static constexpr bool is_fair_mutex = false;
95
96 private:
97 // m_state meanings:
98 static constexpr int k_transition =
99 -2; // transition state during last-reader handoff
100 static constexpr int k_writer_locked = -1; // exclusive (writer) locked
101 // >=0 : number of shared (reader) owners
102 std::atomic<int> m_state = {0};
103
104 detail::wait_queue m_writer_waiters;
105 detail::wait_queue m_reader_waiters;
106};
107
110class COOPSYNC_TBB_EXPORT COOPSYNC_TBB_NODISCARD shared_mutex::scoped_lock {
111 public:
114
119 explicit scoped_lock(shared_mutex& m, bool write = true);
120
123
125 scoped_lock(const scoped_lock&) = delete;
126
129
132
135
141 void acquire(shared_mutex& m, bool write = true);
142
150 COOPSYNC_TBB_NODISCARD bool try_acquire(shared_mutex& m, bool write = true);
151
154 void release();
155
161
167
168 private:
169 shared_mutex* m_mutex;
170 bool m_is_writer_lock;
171};
172
176
177} // namespace coopsync_tbb
RAII wrapper for mutex that acquires the mutex on construction and releases it on destruction.
Definition shared_mutex.hpp:110
scoped_lock(const scoped_lock &)=delete
scoped_lock is not copy-constructible.
void release()
Releases the mutex. Does nothing if no mutex was previously acquired.
scoped_lock & operator=(const scoped_lock &)=delete
scoped_lock is not copy-assignable.
bool try_acquire(shared_mutex &m, bool write=true)
Attempts to acquire the mutex without blocking.
bool downgrade_to_reader()
Changes a reader lock to a writer lock.
~scoped_lock()
Destroys the scoped_lock and releases the mutex.
scoped_lock()
Constructs a scoped_lock without acquiring a mutex.
scoped_lock(scoped_lock &&)=delete
scoped_lock is not move-constructible.
scoped_lock & operator=(scoped_lock &&)=delete
scoped_lock is not move-assignable.
void acquire(shared_mutex &m, bool write=true)
Acquires the mutex.
scoped_lock(shared_mutex &m, bool write=true)
Constructs a scoped_lock and acquires the given mutex.
bool upgrade_to_writer()
Changes a writer lock to a reader lock.
A shared mutex that can be used to synchronize access to shared resources. The mutex can be acquired ...
Definition shared_mutex.hpp:24
shared_mutex & operator=(const shared_mutex &)=delete
The shared_mutex is not copy-assignable.
void lock_shared()
Acquires the shared_mutex in a shared mode, suspending the calling task if necessary until the shared...
shared_mutex(const shared_mutex &)=delete
The shared_mutex is not copy-constructible.
void unlock()
Releases the shared_mutex. If there are tasks suspended on the shared_mutex, either one of the exclus...
shared_mutex & operator=(shared_mutex &&)=delete
shared_mutex is not move-assignable.
bool try_lock_shared() noexcept
Attempts to acquire the shared_mutex in a shared mode without suspending.
static constexpr bool is_rw_mutex
The shared_mutex is a reader-writer (shared) mutex.
Definition shared_mutex.hpp:90
void unlock_shared()
Releases the shared_mutex from a shared mode. If there are tasks suspended on the shared_mutex,...
bool try_lock() noexcept
Attempts to acquire the shared_mutex without suspending.
static constexpr bool is_recursive_mutex
The shared_mutex is not recursive.
Definition shared_mutex.hpp:92
~shared_mutex()
Destroys the shared_mutex.
void lock()
Acquires the shared_mutex, suspending the calling task if necessary until the shared_mutex becomes av...
shared_mutex()=default
Constructs a new shared_mutex. The shared_mutex is initially unlocked.
shared_mutex(shared_mutex &&)=delete
The shared_mutex is not move-constructible.
static constexpr bool is_fair_mutex
The shared_mutex does not provide any fairness guarantees.
Definition shared_mutex.hpp:94
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11
shared_mutex rw_mutex
Alias for shared_mutex, following the TBB convention of naming reader-writer mutexes.
Definition shared_mutex.hpp:175