Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

/home/pete/Work/TSE3.svn/tse3/src/tse3/Mutex.h

Go to the documentation of this file.
00001 /*
00002  * @(#)Mutex.h 3.00 2 October 2000
00003  *
00004  * Copyright (c) 2000 Pete Goodliffe (pete@cthree.org)
00005  *
00006  * This file is part of TSE3 - the Trax Sequencer Engine version 3.00.
00007  *
00008  * This library is modifiable/redistributable under the terms of the GNU
00009  * General Public License.
00010  *
00011  * You should have received a copy of the GNU General Public License along
00012  * with this program; see the file COPYING. If not, write to the Free Software
00013  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00014  *
00015  */
00016 
00017 #ifndef TSE3_MUTEX_H
00018 #define TSE3_MUTEX_H
00019 
00020 namespace TSE3
00021 {
00022     namespace Impl
00023     {
00045         class MutexImpl
00046         {
00047             public:
00048 
00049                 virtual ~MutexImpl() {}
00050 
00065                 virtual void lock() = 0;
00066 
00076                 virtual void unlock() = 0;
00077 
00084                 virtual bool locked() = 0;
00085         };
00086 
00099         class NullMutexImpl : public MutexImpl
00100         {
00101             public:
00102 
00103                 NullMutexImpl() : _locked(0) {}
00104 
00105                 virtual ~NullMutexImpl() {}
00106 
00110                 virtual void lock() { ++_locked; }
00111 
00115                 virtual void unlock() { if (_locked) --_locked; }
00116 
00120                 virtual bool locked() { return _locked; }
00121 
00122             private:
00123 
00124                 int _locked;
00125         };
00126 
00166         class Mutex
00167         {
00168             public:
00169 
00184                 Mutex(MutexImpl *i) : impl(i) {}
00185 
00186                 ~Mutex();
00187 
00210                 static void setImpl(MutexImpl *impl);
00211 
00220                 static Mutex *mutex();
00221 
00236                 void lock()
00237                 {
00238 #ifndef TSE3_WITHOUT_MUTEX
00239                     impl->lock();
00240 #endif
00241                 }
00242 
00251                 void unlock()
00252                 {
00253 #ifndef TSE3_WITHOUT_MUTEX
00254                     impl->unlock();
00255 #endif
00256                 }
00257 
00258             private:
00259 
00260                 MutexImpl        *impl;
00261 
00262                 static MutexImpl *globalImpl;
00263         };
00264 
00300         class CritSec
00301         {
00302             public:
00303 
00304                 CritSec()
00305                 {
00306 #ifndef TSE3_WITHOUT_MUTEX
00307                     Mutex::mutex()->lock();
00308 #endif
00309                 }
00310 
00311                 ~CritSec()
00312                 {
00313 #ifndef TSE3_WITHOUT_MUTEX
00314                     Mutex::mutex()->unlock();
00315 #endif
00316                 }
00317         };
00318     };
00319 }
00320 
00321 #endif

Generated on Wed May 25 14:45:09 2005 for TSE3 by doxygen 1.3.2