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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 165 by senkov, Thu Jul 1 04:25:55 2004 UTC revision 2427 by persson, Sat Mar 2 07:03:04 2013 UTC
# Line 3  Line 3 
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 - 2013 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  *
# Line 23  Line 24 
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 {
34    
35    /** @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  class Mutex {  class Mutex {
48      public:      public:
49            /**
50             * Constructor
51             */    
52          Mutex();          Mutex();
53         ~Mutex();  
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          void Lock();          void Lock();
71    
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          bool Trylock();          bool Trylock();
84    
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          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        ~LockGuard() {
108            pm.Unlock();
109        }
110    private:
111        Mutex& pm;
112  };  };
113    
114    } // namespace LinuxSampler
115    
116  #endif // __MUTEX_H__  #endif // __MUTEX_H__

Legend:
Removed from v.165  
changed lines
  Added in v.2427

  ViewVC Help
Powered by ViewVC