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, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2014 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 |
|
#if defined(WIN32) |
28 |
|
#include <windows.h> |
29 |
|
#else |
30 |
#include <pthread.h> |
#include <pthread.h> |
31 |
|
#endif |
32 |
|
|
33 |
namespace LinuxSampler { |
namespace LinuxSampler { |
34 |
|
|
88 |
* Lock() call, one of them will be awaken. |
* Lock() call, one of them will be awaken. |
89 |
*/ |
*/ |
90 |
void Unlock(); |
void Unlock(); |
91 |
|
|
92 |
protected: |
protected: |
93 |
|
#if defined(WIN32) |
94 |
|
HANDLE hMutex; |
95 |
|
#else |
96 |
pthread_mutex_t __posix_mutex; |
pthread_mutex_t __posix_mutex; |
97 |
pthread_mutexattr_t __posix_mutexattr; |
pthread_mutexattr_t __posix_mutexattr; |
98 |
|
#endif |
99 |
|
}; |
100 |
|
|
101 |
|
// Lock guard for exception safe locking |
102 |
|
class LockGuard { |
103 |
|
public: |
104 |
|
LockGuard(Mutex& m) : pm(&m) { |
105 |
|
m.Lock(); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
109 |
|
* Empty LockGuard. This constructor can be used to implement conditional |
110 |
|
* mutex protection like: |
111 |
|
* @code |
112 |
|
* Mutex m; |
113 |
|
* LockGuard g; |
114 |
|
* if (requiresMutexProtection()) g = LockGuard(m); |
115 |
|
* @endcode |
116 |
|
*/ |
117 |
|
LockGuard() : pm(NULL) { |
118 |
|
} |
119 |
|
|
120 |
|
LockGuard(LockGuard& g) : pm(g.pm) { |
121 |
|
if (pm) pm->Lock(); |
122 |
|
} |
123 |
|
|
124 |
|
~LockGuard() { |
125 |
|
if (pm) pm->Unlock(); |
126 |
|
} |
127 |
|
private: |
128 |
|
Mutex* pm; |
129 |
}; |
}; |
130 |
|
|
131 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |