CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
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
23class COOPSYNC_TBB_EXPORT mutex {
24 public:
26 class scoped_lock;
27
29 mutex() = default;
30
32 mutex(const mutex&) = delete;
33
35 mutex& operator=(const mutex&) = delete;
37
38 mutex(mutex&&) = delete;
39
41 mutex& operator=(mutex&&) = delete;
42
48
51 COOPSYNC_TBB_NODISCARD bool try_lock() noexcept;
52
57 void lock();
58
61 void unlock();
62
64 static constexpr bool is_rw_mutex = false;
66 static constexpr bool is_recursive_mutex = false;
68 static constexpr bool is_fair_mutex = false;
69
70 private:
71 std::atomic<bool> m_locked = {false};
72 detail::wait_queue m_wait_queue;
73};
74
77class COOPSYNC_TBB_EXPORT COOPSYNC_TBB_NODISCARD mutex::scoped_lock {
78 public:
81
84 explicit scoped_lock(mutex& m);
85
88
90 scoped_lock(const scoped_lock&) = delete;
91
94
97
103 void acquire(mutex& m);
104
109 COOPSYNC_TBB_NODISCARD bool try_acquire(mutex& m);
110
113 void release();
114
115 private:
116 mutex* m_mutex;
117};
118
119} // namespace coopsync_tbb
RAII wrapper for mutex that acquires the mutex on construction and releases it on destruction.
Definition mutex.hpp:77
scoped_lock(const scoped_lock &)=delete
scoped_lock is not copy-constructible.
~scoped_lock()
Destroys the scoped_lock and releases the mutex.
void acquire(mutex &m)
Acquires the mutex.
scoped_lock()
Constructs a scoped_lock without acquiring a mutex.
scoped_lock(mutex &m)
Constructs a scoped_lock and acquires the given mutex.
scoped_lock & operator=(scoped_lock &&)=delete
scoped_lock is not move-assignable.
scoped_lock(scoped_lock &&)=delete
scoped_lock is not move-constructible.
bool try_acquire(mutex &m)
Attempts to acquire the mutex without blocking.
scoped_lock & operator=(const scoped_lock &)=delete
scoped_lock is not copy-assignable.
void release()
Releases the mutex. Does nothing if no mutex was previously acquired.
void unlock()
Releases the mutex. If there are tasks suspended on the mutex, exactly one of them is resumed and acq...
mutex(const mutex &)=delete
The mutex is not copy-constructible.
static constexpr bool is_fair_mutex
The mutex does not provide any fairness guarantees.
Definition mutex.hpp:68
static constexpr bool is_recursive_mutex
The mutex is not recursive.
Definition mutex.hpp:66
mutex()=default
Constructs a new mutex. The mutex is initially unlocked.
static constexpr bool is_rw_mutex
The mutex is not a reader-writer (shared) mutex.
Definition mutex.hpp:64
mutex & operator=(const mutex &)=delete
The mutex is not copy-assignable.
mutex(mutex &&)=delete
The mutex is not move-constructible.
void lock()
Acquires the mutex, suspending the calling task if necessary until the mutex becomes available.
bool try_lock() noexcept
Attempts to acquire the mutex without suspending.
mutex & operator=(mutex &&)=delete
Mutex is not move-assignable.
~mutex()
Destroys the mutex.
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11