Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
useMutex.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
102# ifndef USEMUTEX_H
103# define USEMUTEX_H
104
105# include <pthread.h>
106# include <errno.h>
107# include <ahDefs.H>
108# include <ahUtils.H>
109
110# include <ah-errors.H>
111
113extern void init_mutex(pthread_mutex_t *);
114
116extern void init_mutex(pthread_mutex_t &);
117
119extern void destroy_mutex(pthread_mutex_t *);
120
122extern void destroy_mutex(pthread_mutex_t &);
123
124using namespace Aleph;
125
142{
145
146public:
147
154 void unlock()
155 {
156 ah_domain_error_if(mutex == nullptr)
157 << "unlock: nullptr pointer to mutex";
158
160 }
161
168 void lock()
169 {
170 ah_domain_error_if(mutex == nullptr)
171 << "lock: nullptr pointer to mutex";
172
174 }
175
186
197
199 void enter() { lock(); }
200
202 void leave() { unlock(); }
203
206 {
208 unlock();
209 }
210
213
216};
217
218
221# define CTOR_USE_MUTEX(name, mutex) name(mutex)
222
225# define CTOR_INH_USE_MUTEX(mutex) UseMutex(mutex)
226
229# define USE_MUTEX(name, mutex) UseMutex name(mutex)
230
233# define CRITICAL_SECTION(mutex) UseMutex critical_section(mutex)
234
235# endif // USEMUTEX_H
236
237
238
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
Core definitions, constants, and utility macros for Aleph-w.
General utility functions and helpers.
RAII-style mutex lock guard.
Definition useMutex.H:142
void disallow_unlock()
Prevent automatic unlock on destruction.
Definition useMutex.H:212
void unlock()
Explicitly unlock the mutex.
Definition useMutex.H:154
UseMutex(pthread_mutex_t *m)
Construct and lock (pointer version)
Definition useMutex.H:182
void leave()
Alias for unlock()
Definition useMutex.H:202
pthread_mutex_t * mutex
Definition useMutex.H:143
void lock()
Explicitly lock the mutex.
Definition useMutex.H:168
bool unlock_when_destroy
Definition useMutex.H:144
UseMutex(pthread_mutex_t &m)
Construct and lock (reference version)
Definition useMutex.H:193
void allow_unlock()
Allow automatic unlock on destruction (default)
Definition useMutex.H:215
void enter()
Alias for lock()
Definition useMutex.H:199
~UseMutex()
Destructor - unlocks if allowed.
Definition useMutex.H:205
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
void init_mutex(pthread_mutex_t *)
Initialize a pthread mutex (pointer version)
Definition useMutex.C:41
void destroy_mutex(pthread_mutex_t *)
Destroy a pthread mutex (pointer version)
Definition useMutex.C:54