Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Mutex.h
Go to the documentation of this file.
1#pragma once
2
3#include <misc/gen/TimeSpec.h>
4#include <misc/global.h>
5#include <pthread.h>
6#include <string>
7
8namespace sf
9{
10
17{
18 public:
22 typedef ::pthread_mutex_t* handle_type;
23
29 explicit Mutex(const char* name = nullptr, bool recursive = true);
30
35
39 inline operator handle_type();
40
45 {
46 public:
50 explicit Lock(const Mutex& mutex, bool try_lock = false);
51
56
60 bool acquire(bool try_lock = false, const TimeSpec& timeout = {});
61
66 bool release();
67
72 explicit operator bool() const
73 {
74 return _locked;
75 }
76
77 private:
81 Lock(const Lock& lock) = delete;
82
86 Mutex& _mutexRef;
90 bool _locked{false};
91 };
92
93 friend class Condition;
94
95 private:
97 Mutex(const Mutex&) = delete;
98
100 const Mutex& operator=(const Mutex&) = delete;
101
103 bool isMutexDestroyed();
104
106 void clearMutex();
107
109 bool acquire(bool try_lock, const TimeSpec& timeout);
110
112 void release();
113
115 bool _recursive;
116
118 std::string _name;
119
121 ::pthread_mutex_t _mutex{};
122
124 pid_t _threadId{-1};
125};
126
130inline Mutex::operator handle_type()
131{
132 return &_mutex;
133}
134
135}// namespace sf
Locking class for easy locking and unlocking by destructor.
Definition Mutex.h:45
~Lock()
Destructor.
bool acquire(bool try_lock=false, const TimeSpec &timeout={})
Returns true when if it was acquired.
Lock(const Mutex &mutex, bool try_lock=false)
Constructor.
friend class Condition
Definition Mutex.h:93
Mutex(const char *name=nullptr, bool recursive=true)
Default constructor.
~Mutex()
Destructor.
::pthread_mutex_t * handle_type
Handle type.
Definition Mutex.h:22
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
Class wrapper for timespec structure to modify.
Definition TimeSpec.h:12