cppthread 1.1.16
C++ Thread Library
guard.cpp
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
27// self
28//
29#include "cppthread/guard.h"
30
31#include "cppthread/exception.h"
32#include "cppthread/log.h"
33#include "cppthread/mutex.h"
34
35
36// last include
37//
38#include <snapdev/poison.h>
39
40
41
42namespace cppthread
43{
44
45
46
86 : f_mutex(&m)
87{
88 if(f_mutex == nullptr)
89 {
90 // mutex is mandatory
91 //
92 throw logic_error("mutex missing in guard() constructor");
93 }
94 f_mutex->lock();
95 f_locked = true;
96}
97
98
109{
110 try
111 {
112 unlock();
113 }
114 catch(std::exception const & e)
115 {
116 // a log was already printed, we do not absolutely need another one
117 log << log_level_t::fatal
118 << "mutex::unlock() threw an exception while in the ~guard() function."
119 << end;
120 std::terminate();
121 }
122}
123
124
142void guard::unlock(bool done)
143{
144 if(f_locked)
145 {
146 mutex * m(f_mutex);
147 f_locked = false;
148 if(done)
149 {
150 f_mutex = nullptr;
151 }
152 m->unlock();
153 }
154}
155
156
204{
205 if(f_mutex == nullptr)
206 {
207 return;
208 }
209
210 f_mutex->lock();
211
212 if(f_locked)
213 {
214 f_mutex->unlock();
215 }
216 else
217 {
218 f_locked = true;
219 }
220}
221
222
237{
238 bool result(f_mutex != nullptr);
239 if(result)
240 {
241 f_mutex->lock();
242 result = f_locked;
243 f_mutex->unlock();
244 }
245
246 return result;
247}
248
249
250
251
306} // namespace cppthread
307// vim: ts=4 sw=4 et
~guard()
Ensure that the mutex was unlocked.
Definition guard.cpp:108
bool is_locked() const
This function returns whether the guard is current locked.
Definition guard.cpp:236
void unlock(bool done=true)
Unlock this mutex.
Definition guard.cpp:142
void lock()
Relock this mutex.
Definition guard.cpp:203
mutex * f_mutex
The mutex used by the guard class.
Definition guard.h:56
bool f_locked
Whether the guard is currently in effect.
Definition guard.h:55
guard(mutex &m)
Lock a mutex.
Definition guard.cpp:85
A mutex object to ensures atomicity.
Definition mutex.h:55
void lock()
Lock a mutex.
Definition mutex.cpp:369
void unlock()
Unlock a mutex.
Definition mutex.cpp:443
Exceptions for the thread environment.
Thread Runner and Managers.
logger log
The logger object used to send logs out.
Definition log.cpp:90
Declaration of the log class used to send error messages.
logger & end(logger &l)
Close a log statement.
Definition log.h:90
Thread Runner and Managers.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.