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