2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005, 2006 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 |
|
#include "global_private.h" |
34 |
|
|
35 |
|
namespace LinuxSampler { |
36 |
|
|
37 |
|
/** @brief Mutual exclusive objects |
38 |
|
* |
39 |
|
* This class provides the classical thread / process synchronisation |
40 |
|
* technique called Mutex. It is used to protect critical sections, that is |
41 |
|
* resources (typically data structures) from being used at the same time by |
42 |
|
* different threads or processes which otherwise might turn into undefined |
43 |
|
* and of course undesired behavior. |
44 |
|
* |
45 |
|
* Note: as this technique might block the calling thread and also implies |
46 |
|
* a system call, this should not be used directly in realtime sensitive |
47 |
|
* threads! |
48 |
|
*/ |
49 |
class Mutex { |
class Mutex { |
50 |
public: |
public: |
51 |
|
/** |
52 |
|
* Constructor |
53 |
|
*/ |
54 |
Mutex(); |
Mutex(); |
55 |
~Mutex(); |
|
56 |
|
/** |
57 |
|
* Destructor |
58 |
|
*/ |
59 |
|
virtual ~Mutex(); |
60 |
|
|
61 |
|
/** @brief Lock this Mutex. |
62 |
|
* |
63 |
|
* If this Mutex object is currently be locked by another thread, |
64 |
|
* then the calling thread will be blocked until the other thread |
65 |
|
* unlocks this Mutex object. The calling thread though can safely |
66 |
|
* call this method several times without danger to be blocked |
67 |
|
* himself. |
68 |
|
* |
69 |
|
* The calling thread should call Unlock() as soon as the critical |
70 |
|
* section was left. |
71 |
|
*/ |
72 |
void Lock(); |
void Lock(); |
73 |
|
|
74 |
|
/** @brief Try to lock this Mutex. |
75 |
|
* |
76 |
|
* Same as Lock() except that this method won't block the calling |
77 |
|
* thread in case this Mutex object is currently locked by another |
78 |
|
* thread. So this call will always immediately return and the |
79 |
|
* return value has to be checked if the locking request was |
80 |
|
* successful or not. |
81 |
|
* |
82 |
|
* @returns true if the Mutex object could be locked, false if the |
83 |
|
* Mutex is currently locked by another thread |
84 |
|
*/ |
85 |
|
bool Trylock(); |
86 |
|
|
87 |
|
/** @brief Unlock this Mutex. |
88 |
|
* |
89 |
|
* If other threads are currently blocked and waiting due to a |
90 |
|
* Lock() call, one of them will be awaken. |
91 |
|
*/ |
92 |
void Unlock(); |
void Unlock(); |
93 |
|
|
94 |
protected: |
protected: |
95 |
|
#if defined(WIN32) |
96 |
|
HANDLE hMutex; |
97 |
|
#else |
98 |
pthread_mutex_t __posix_mutex; |
pthread_mutex_t __posix_mutex; |
99 |
pthread_mutexattr_t __posix_mutexattr; |
pthread_mutexattr_t __posix_mutexattr; |
100 |
|
#endif |
101 |
}; |
}; |
102 |
|
|
103 |
|
} // namespace LinuxSampler |
104 |
|
|
105 |
#endif // __MUTEX_H__ |
#endif // __MUTEX_H__ |