/[svn]/linuxsampler/trunk/src/common/Mutex.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/Mutex.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2501 - (hide annotations) (download) (as text)
Fri Jan 10 13:53:19 2014 UTC (10 years, 3 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 4634 byte(s)
- Fixed minor compilation error (certain GCC versions).

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 2500 * Copyright (C) 2005 - 2014 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 senoner 1481 #if defined(WIN32)
28     #include <windows.h>
29     #else
30 schoenebeck 53 #include <pthread.h>
31 senoner 1481 #endif
32 schoenebeck 53
33 schoenebeck 880 namespace LinuxSampler {
34    
35 schoenebeck 550 /** @brief Mutual exclusive objects
36     *
37     * This class provides the classical thread / process synchronisation
38     * technique called Mutex. It is used to protect critical sections, that is
39     * resources (typically data structures) from being used at the same time by
40     * different threads or processes which otherwise might turn into undefined
41     * and of course undesired behavior.
42     *
43     * Note: as this technique might block the calling thread and also implies
44     * a system call, this should not be used directly in realtime sensitive
45     * threads!
46     */
47 schoenebeck 53 class Mutex {
48     public:
49 schoenebeck 550 /**
50     * Constructor
51     */
52 schoenebeck 53 Mutex();
53 schoenebeck 550
54     /**
55     * Destructor
56     */
57     virtual ~Mutex();
58    
59     /** @brief Lock this Mutex.
60     *
61     * If this Mutex object is currently be locked by another thread,
62     * then the calling thread will be blocked until the other thread
63     * unlocks this Mutex object. The calling thread though can safely
64     * call this method several times without danger to be blocked
65     * himself.
66     *
67     * The calling thread should call Unlock() as soon as the critical
68     * section was left.
69     */
70 schoenebeck 53 void Lock();
71 schoenebeck 550
72     /** @brief Try to lock this Mutex.
73     *
74     * Same as Lock() except that this method won't block the calling
75     * thread in case this Mutex object is currently locked by another
76     * thread. So this call will always immediately return and the
77     * return value has to be checked if the locking request was
78     * successful or not.
79     *
80     * @returns true if the Mutex object could be locked, false if the
81     * Mutex is currently locked by another thread
82     */
83 senkov 165 bool Trylock();
84 schoenebeck 550
85     /** @brief Unlock this Mutex.
86     *
87     * If other threads are currently blocked and waiting due to a
88     * Lock() call, one of them will be awaken.
89     */
90 schoenebeck 53 void Unlock();
91 persson 2427
92 schoenebeck 53 protected:
93 senoner 1481 #if defined(WIN32)
94 persson 2427 HANDLE hMutex;
95 senoner 1481 #else
96 schoenebeck 53 pthread_mutex_t __posix_mutex;
97     pthread_mutexattr_t __posix_mutexattr;
98 senoner 1481 #endif
99 schoenebeck 53 };
100    
101 persson 2427 // Lock guard for exception safe locking
102     class LockGuard {
103     public:
104 schoenebeck 2500 LockGuard(Mutex& m) : pm(&m) {
105 persson 2427 m.Lock();
106     }
107 schoenebeck 2500
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 schoenebeck 2501 LockGuard(const LockGuard& g) : pm(g.pm) {
121 schoenebeck 2500 if (pm) pm->Lock();
122     }
123    
124 persson 2427 ~LockGuard() {
125 schoenebeck 2500 if (pm) pm->Unlock();
126 persson 2427 }
127     private:
128 schoenebeck 2500 Mutex* pm;
129 persson 2427 };
130    
131 schoenebeck 880 } // namespace LinuxSampler
132    
133 schoenebeck 53 #endif // __MUTEX_H__

  ViewVC Help
Powered by ViewVC