1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
550 |
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
|
|
* 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 * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef __MUTEX_H__ |
25 |
|
|
#define __MUTEX_H__ |
26 |
|
|
|
27 |
|
|
#include <pthread.h> |
28 |
|
|
|
29 |
schoenebeck |
550 |
/** @brief Mutual exclusive objects |
30 |
|
|
* |
31 |
|
|
* This class provides the classical thread / process synchronisation |
32 |
|
|
* technique called Mutex. It is used to protect critical sections, that is |
33 |
|
|
* resources (typically data structures) from being used at the same time by |
34 |
|
|
* different threads or processes which otherwise might turn into undefined |
35 |
|
|
* and of course undesired behavior. |
36 |
|
|
* |
37 |
|
|
* Note: as this technique might block the calling thread and also implies |
38 |
|
|
* a system call, this should not be used directly in realtime sensitive |
39 |
|
|
* threads! |
40 |
|
|
*/ |
41 |
schoenebeck |
53 |
class Mutex { |
42 |
|
|
public: |
43 |
schoenebeck |
550 |
/** |
44 |
|
|
* Constructor |
45 |
|
|
*/ |
46 |
schoenebeck |
53 |
Mutex(); |
47 |
schoenebeck |
550 |
|
48 |
|
|
/** |
49 |
|
|
* Destructor |
50 |
|
|
*/ |
51 |
|
|
virtual ~Mutex(); |
52 |
|
|
|
53 |
|
|
/** @brief Lock this Mutex. |
54 |
|
|
* |
55 |
|
|
* If this Mutex object is currently be locked by another thread, |
56 |
|
|
* then the calling thread will be blocked until the other thread |
57 |
|
|
* unlocks this Mutex object. The calling thread though can safely |
58 |
|
|
* call this method several times without danger to be blocked |
59 |
|
|
* himself. |
60 |
|
|
* |
61 |
|
|
* The calling thread should call Unlock() as soon as the critical |
62 |
|
|
* section was left. |
63 |
|
|
*/ |
64 |
schoenebeck |
53 |
void Lock(); |
65 |
schoenebeck |
550 |
|
66 |
|
|
/** @brief Try to lock this Mutex. |
67 |
|
|
* |
68 |
|
|
* Same as Lock() except that this method won't block the calling |
69 |
|
|
* thread in case this Mutex object is currently locked by another |
70 |
|
|
* thread. So this call will always immediately return and the |
71 |
|
|
* return value has to be checked if the locking request was |
72 |
|
|
* successful or not. |
73 |
|
|
* |
74 |
|
|
* @returns true if the Mutex object could be locked, false if the |
75 |
|
|
* Mutex is currently locked by another thread |
76 |
|
|
*/ |
77 |
senkov |
165 |
bool Trylock(); |
78 |
schoenebeck |
550 |
|
79 |
|
|
/** @brief Unlock this Mutex. |
80 |
|
|
* |
81 |
|
|
* If other threads are currently blocked and waiting due to a |
82 |
|
|
* Lock() call, one of them will be awaken. |
83 |
|
|
*/ |
84 |
schoenebeck |
53 |
void Unlock(); |
85 |
schoenebeck |
550 |
|
86 |
schoenebeck |
53 |
protected: |
87 |
|
|
pthread_mutex_t __posix_mutex; |
88 |
|
|
pthread_mutexattr_t __posix_mutexattr; |
89 |
|
|
}; |
90 |
|
|
|
91 |
|
|
#endif // __MUTEX_H__ |