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
108# ifndef USEMUTEX_H
109# define USEMUTEX_H
110
111# include <pthread.h>
112# include <errno.h>
113# include <ahDefs.H>
114# include <ahUtils.H>
115
116# include <ah-errors.H>
117
119extern void init_mutex(pthread_mutex_t *);
120
122extern void init_mutex(pthread_mutex_t &);
123
125extern void destroy_mutex(pthread_mutex_t *);
126
128extern void destroy_mutex(pthread_mutex_t &);
129
130using namespace Aleph;
131
151{
154
155public:
156
163 void unlock()
164 {
165 ah_domain_error_if(mutex == nullptr)
166 << "unlock: nullptr pointer to mutex";
167
169 }
170
177 void lock()
178 {
179 ah_domain_error_if(mutex == nullptr)
180 << "lock: nullptr pointer to mutex";
181
183 }
184
195
206
208 void enter() { lock(); }
209
211 void leave() { unlock(); }
212
215 {
217 unlock();
218 }
219
222
225};
226
227
230# define CTOR_USE_MUTEX(name, mutex) name(mutex)
231
234# define CTOR_INH_USE_MUTEX(mutex) UseMutex(mutex)
235
238# define USE_MUTEX(name, mutex) UseMutex name(mutex)
239
242# define CRITICAL_SECTION(mutex) UseMutex critical_section(mutex)
243
244# endif // USEMUTEX_H
245
246
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.
Legacy RAII-style mutex lock guard.
Definition useMutex.H:151
void disallow_unlock()
Prevent automatic unlock on destruction.
Definition useMutex.H:221
void unlock()
Explicitly unlock the mutex.
Definition useMutex.H:163
UseMutex(pthread_mutex_t *m)
Construct and lock (pointer version)
Definition useMutex.H:191
void leave()
Alias for unlock()
Definition useMutex.H:211
pthread_mutex_t * mutex
Definition useMutex.H:152
void lock()
Explicitly lock the mutex.
Definition useMutex.H:177
bool unlock_when_destroy
Definition useMutex.H:153
UseMutex(pthread_mutex_t &m)
Construct and lock (reference version)
Definition useMutex.H:202
void allow_unlock()
Allow automatic unlock on destruction (default)
Definition useMutex.H:224
void enter()
Alias for lock()
Definition useMutex.H:208
~UseMutex()
Destructor - unlocks if allowed.
Definition useMutex.H:214
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
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