lockguard.h
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#ifndef OSDEV_COMPONENTS_MQTT_LOCKGUARD_H
#define OSDEV_COMPONENTS_MQTT_LOCKGUARD_H
// std
#include <mutex>
// osdev::components::mqtt::measurement
#include "measure.h"
#include "utils.h"
/**
* @brief Enable the lock measurements when macro MEASURE_LOCKS is defined.
* If the macro is not defined the NOP versions of the measure macros are used.
*/
#ifdef MEASURE_LOCKS
#define OSDEV_COMPONENTS_MEASURELOCK OSDEV_COMPONENTS_MEASUREMENT_MEASURE
#define OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC OSDEV_COMPONENTS_MEASUREMENT_MEASURE_SPECIFIC
#else
#define OSDEV_COMPONENTS_MEASURELOCK OSDEV_COMPONENTS_MEASUREMENT_MEASURE_NOP
#define OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC OSDEV_COMPONENTS_MEASUREMENT_MEASURE_SPECIFIC_NOP
#endif
/**
* @brief Create a lockguard for a given mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns a mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_LOCKGUARD_OBTAINER(mutexVariableName, mutexObtainer) \
OSDEV_COMPONENTS_MEASURELOCK(LOCKGUARD, std::lock_guard<std::mutex> Lock__Guard__##mutexVariableName##__(mutexObtainer), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(Lock__Guard__##mutexVariableName##__);
/**
* @brief Create a lockguard for a given mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns a mutex.
* @param id The id that identifies this specific lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_LOCKGUARD_SPECIFIC_OBTAINER(mutexVariableName, mutexObtainer, id) \
OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC(id, LOCKGUARD, std::lock_guard<std::mutex> Lock__Guard__##mutexVariableName##__(mutexObtainer), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(Lock__Guard__##mutexVariableName##__);
/**
* @brief Create a lockguard for a given recursive mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns a mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_RECURSIVELOCKGUARD_OBTAINER(mutexVariableName, mutexObtainer) \
OSDEV_COMPONENTS_MEASURELOCK(RECURSIVELOCKGUARD, std::lock_guard<std::recursive_mutex> Lock__Guard__##mutexVariableName##__(mutexObtainer), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(Lock__Guard__##mutexVariableName##__);
/**
* @brief Create a lockguard for a given recursive mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns a mutex.
* @param id The id that identifies this specific lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_RECURSIVELOCKGUARD_SPECIFIC_OBTAINER(mutexVariableName, mutexObtainer, id) \
OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC(id, RECURSIVELOCKGUARD, std::lock_guard<std::recursive_mutex> Lock__Guard__##mutexVariableName##__(mutexVariableName), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(Lock__Guard__##mutexVariableName##__);
/**
* @brief Create a lockguard for a given bare mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_LOCKGUARD(mutexVariableName) \
OSDEV_COMPONENTS_LOCKGUARD_OBTAINER(mutexVariableName, mutexVariableName)
/**
* @brief Create a lockguard for a given bare mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param id The id that identifies this specific lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_LOCKGUARD_SPECIFIC(mutexVariableName, id) \
OSDEV_COMPONENTS_LOCKGUARD_SPECIFIC_OBTAINER(mutexVariableName, mutexVariableName, id)
/**
* @brief Create a lockguard for a given bare recursive mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_RECURSIVELOCKGUARD(mutexVariableName) \
OSDEV_COMPONENTS_RECURSIVELOCKGUARD_OBTAINER(mutexVariableName, mutexVariableName)
/**
* @brief Create a lockguard for a given bare recursive mutex in a specific context.
* @param mutexVariableName The name of the mutex.
* @param id The id that identifies this specific lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_RECURSIVELOCKGUARD_SPECIFIC(mutexVariableName, id) \
OSDEV_COMPONENTS_RECURSIVELOCKGUARD_SPECIFIC_OBTAINER(mutexVariableName, mutexVariableName, id)
/**
* @brief Defines the lock name that is used by the OSDEV_COMPONENTS_UNIQUELOCK_* macros
*/
#define OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName) Unique__Lock__##mutexVariableName##__
/**
* @brief Create a uniqeue lock for a given mutex.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns a mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_CREATE_OBTAINER(mutexVariableName, mutexObtainer) \
OSDEV_COMPONENTS_MEASURELOCK(UNIQUELOCK_CREATE, std::unique_lock<std::mutex> OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName)(mutexObtainer), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName));
/**
* @brief Create a uniqeue lock for a given mutex.
* @param mutexVariableName The name of the mutex.
* @param mutexObtainer The mutex to lock. This can also be a function that returns mutex.
* @param id The id that identifies this specific lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_CREATE_SPECIFIC_OBTAINER(mutexVariableName, mutexObtainer, id) \
OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC(id, UNIQUELOCK_CREATE, std::unique_lock<std::mutex> OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName)(mutexObtainer), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100) \
osdev::components::mqtt::apply_unused_parameters(OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName));
/**
* @brief Create a uniqeue lock for a given bare mutex.
* @param mutexVariableName The name of the mutex.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_CREATE(mutexVariableName) \
OSDEV_COMPONENTS_UNIQUELOCK_CREATE_OBTAINER(mutexVariableName, mutexVariableName)
/**
* @brief Create a uniqeue lock for a given bare mutex.
* @param mutexVariableName The name of the mutex.
* @param id The id that identifies this specific unique lock. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_CREATE_SPECIFIC(mutexVariableName, id) \
OSDEV_COMPONENTS_UNIQUELOCK_CREATE_SPECIFIC_OBTAINER(mutexVariableName, mutexVariableName, id)
/**
* @brief Lock a given uniqeue lock.
* @param mutexVariableName The name of the mutex from which the lockname is derived.
* If MEASURE_LOCKS is enabled this method uses a static histogram to record the timing of obtaining the lock for all instances.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_LOCK(mutexVariableName) \
OSDEV_COMPONENTS_MEASURELOCK(UNIQUELOCK_LOCK, OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName).lock(), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100)
/**
* @brief Lock a given uniqeue lock.
* @param mutexVariableName The name of the mutex from which the lockname is derived.
* @param id The id that identifies this specific unique lock guard. Used for measuring timings.
* If MEASURE_LOCKS is enabled this method uses a specific histogram instance to record the timing of obtaining this lock.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_LOCK_SPECIFIC(mutexVariableName, id) \
OSDEV_COMPONENTS_MEASURELOCK_SPECIFIC(id, UNIQUELOCK_LOCK, OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName).lock(), osdev::components::mqtt::measure_locking_tag, osdev::components::mqtt::MeasureLockingValue, 100)
/**
* @brief Unlock a given uniqeue lock.
* @param mutexVariableName The name of the mutex from which the lockname is derived.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_UNLOCK(mutexVariableName) \
OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName).unlock();
/**
* @brief Unlock a given uniqeue lock.
* @param mutexVariableName The name of the mutex from which the lockname is derived.
* @param id The id that identifies this specific unique lock guard. Can be used for measuring timings.
*/
#define OSDEV_COMPONENTS_UNIQUELOCK_UNLOCK_SPECIFIC(mutexVariableName, id) \
OSDEV_COMPONENTS_UNIQUELOCK(mutexVariableName).unlock();
namespace osdev {
namespace components {
namespace mqtt {
/**
* @brief Context tag type.
*/
struct measure_locking_tag
{
};
/**
* @brief Type for measuring lock timings
* The unit is microseconds and the values are expected between 0 and 100 microseconds.
* This type is used to construct a timing histogram.
*/
struct MeasureLockingValue
{
/**
* @brief The value type of the timing value.
*/
using value_type = std::chrono::microseconds;
const value_type minValue = value_type(0); ///< Constant mininum value.
const value_type maxValue = value_type(100); ///< Constant maximum value.
const char* unit = "us"; ///< The value unit.
};
} // End namespace mqtt
} // End namespace components
} // End namespace osdev
#endif // OSDEV_COMPONENTS_MQTT_LOCKGUARD_H