3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2017 Christian Schoenebeck * |
* Copyright (C) 2005 - 2020 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
24 |
#ifndef __MUTEX_H__ |
#ifndef __MUTEX_H__ |
25 |
#define __MUTEX_H__ |
#define __MUTEX_H__ |
26 |
|
|
27 |
|
// enable this for detecting mutex misusage and for debugging dead locks |
28 |
|
// (these features then still need to be enabled by Mutex::setDebugEnabled()) |
29 |
|
//#define DEBUG_MUTEX 1 |
30 |
|
|
31 |
#if defined(WIN32) |
#if defined(WIN32) |
32 |
#include <windows.h> |
#include <windows.h> |
33 |
#else |
#else |
125 |
*/ |
*/ |
126 |
void Unlock(); |
void Unlock(); |
127 |
|
|
128 |
|
#if DEBUG_MUTEX |
129 |
|
/** @brief Enable bug detection and debugging features. |
130 |
|
* |
131 |
|
* By passing @c true to this method, bug detection and debugging features |
132 |
|
* will be enabled for this Mutex object. For instance this will trigger an |
133 |
|
* assertion fault if a thread attempts to Unlock() a thread it does not own |
134 |
|
* a lock on, or when locking more than once while not using mutex type |
135 |
|
* @c RECURSIVE and much more. Additionally this will also record the name |
136 |
|
* of the thread currently holding a lock, and the backtrace of that |
137 |
|
* thread's lock. The latter information can then be used to debug |
138 |
|
* deadlocks. |
139 |
|
* |
140 |
|
* By default this is turned off and must be enabled for individual Mutex |
141 |
|
* objects, because otherwise it would cause a large number of false |
142 |
|
* positives (i.e. in certain edge cases like thread constructors / |
143 |
|
* destructors for instance). |
144 |
|
* |
145 |
|
* @param b - whether to enable bug detection / debugging features |
146 |
|
*/ |
147 |
|
void setDebugEnabled(bool b) { |
148 |
|
debugSelf = b; |
149 |
|
} |
150 |
|
#endif |
151 |
|
|
152 |
protected: |
protected: |
153 |
#if defined(WIN32) |
#if defined(WIN32) |
154 |
HANDLE hMutex; |
HANDLE hMutex; |
157 |
pthread_mutexattr_t __posix_mutexattr; |
pthread_mutexattr_t __posix_mutexattr; |
158 |
#endif |
#endif |
159 |
type_t type; |
type_t type; |
160 |
|
#if DEBUG_MUTEX |
161 |
|
std::string owner; ///< Name of the thread owning the current lock. |
162 |
|
long long int count; ///< How many times the owner currently acquired the lock recursively. |
163 |
|
std::string backtrace; ///< Call stack trace of (last) lock. |
164 |
|
bool debugSelf; ///< Whether bug detection and debugging features are enabled. |
165 |
|
#endif |
166 |
}; |
}; |
167 |
|
|
168 |
// Lock guard for exception safe locking |
// Lock guard for exception safe locking |