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 - 2019 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 * |
55 |
|
|
56 |
#include "Mutex.h" |
#include "Mutex.h" |
57 |
|
|
58 |
|
#if DEBUG_MUTEX |
59 |
|
# include "Thread.h" |
60 |
|
# include <assert.h> |
61 |
|
# include "global_private.h" |
62 |
|
#endif |
63 |
|
|
64 |
namespace LinuxSampler { |
namespace LinuxSampler { |
65 |
|
|
66 |
Mutex::Mutex(type_t type) { |
Mutex::Mutex(type_t type) { |
93 |
} |
} |
94 |
pthread_mutex_init(&__posix_mutex, &__posix_mutexattr); |
pthread_mutex_init(&__posix_mutex, &__posix_mutexattr); |
95 |
#endif |
#endif |
96 |
|
#if DEBUG_MUTEX |
97 |
|
debugSelf = false; |
98 |
|
count = 0; |
99 |
|
#endif |
100 |
} |
} |
101 |
|
|
102 |
Mutex::~Mutex() { |
Mutex::~Mutex() { |
114 |
#else |
#else |
115 |
pthread_mutex_lock(&__posix_mutex); |
pthread_mutex_lock(&__posix_mutex); |
116 |
#endif |
#endif |
117 |
|
#if DEBUG_MUTEX |
118 |
|
if (debugSelf) { |
119 |
|
std::string caller = Thread::nameOfCaller(); |
120 |
|
++count; |
121 |
|
assert(count > 0); |
122 |
|
if (type != RECURSIVE) |
123 |
|
assert(count == 1); |
124 |
|
if (!owner.empty()) |
125 |
|
assert(owner == caller); |
126 |
|
owner = caller; |
127 |
|
backtrace = backtraceAsString(); |
128 |
|
} |
129 |
|
#endif |
130 |
} |
} |
131 |
|
|
132 |
bool Mutex::Trylock() { |
bool Mutex::Trylock() { |
133 |
#if defined(WIN32) |
#if defined(WIN32) |
134 |
if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false; |
if( WaitForSingleObject(hMutex, 0) == WAIT_TIMEOUT) return false; |
|
return true; |
|
135 |
#else |
#else |
136 |
if (pthread_mutex_trylock(&__posix_mutex) == EBUSY) |
if (pthread_mutex_trylock(&__posix_mutex) == EBUSY) |
137 |
return false; |
return false; |
|
return true; |
|
138 |
#endif |
#endif |
139 |
|
#if DEBUG_MUTEX |
140 |
|
if (debugSelf) { |
141 |
|
std::string caller = Thread::nameOfCaller(); |
142 |
|
++count; |
143 |
|
assert(count > 0); |
144 |
|
if (type != RECURSIVE) |
145 |
|
assert(count == 1); |
146 |
|
if (!owner.empty()) |
147 |
|
assert(owner == caller); |
148 |
|
owner = caller; |
149 |
|
backtrace = backtraceAsString(); |
150 |
|
} |
151 |
|
#endif |
152 |
|
return true; |
153 |
} |
} |
154 |
|
|
155 |
void Mutex::Unlock() { |
void Mutex::Unlock() { |
156 |
|
#if DEBUG_MUTEX |
157 |
|
if (debugSelf) { |
158 |
|
std::string caller = Thread::nameOfCaller(); |
159 |
|
assert(count > 0); |
160 |
|
--count; |
161 |
|
assert(count >= 0); |
162 |
|
assert(owner == caller); |
163 |
|
if (!count) |
164 |
|
owner.clear(); |
165 |
|
} |
166 |
|
#endif |
167 |
#if defined(WIN32) |
#if defined(WIN32) |
168 |
ReleaseMutex(hMutex); |
ReleaseMutex(hMutex); |
169 |
#else |
#else |