csutil/threading/mutex.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csutil/threading/mutex.h
Go to the documentation of this file.

    00001 /*
    00002   Copyright (C) 2006 by Marten Svanfeldt
    00003
    00004   This library is free software; you can redistribute it and/or
    00005   modify it under the terms of the GNU Lesser General Public
    00006   License as published by the Free Software Foundation; either
    00007   version 2 of the License, or (at your option) any later version.
    00008
    00009   This library is distributed in the hope that it will be useful,
    00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012   Library General Public License for more details.
    00013
    00014   You should have received a copy of the GNU Library General Public
    00015   License along with this library; if not, write to the Free
    00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_CSUTIL_THREADING_MUTEX_H__
    00020 #define __CS_CSUTIL_THREADING_MUTEX_H__
    00021
    00022 #include "csutil/noncopyable.h"
    00023
    00030 // Include implementation specific versions
    00031 #if defined(CS_PLATFORM_WIN32)
    00032 # include "csutil/threading/win32_mutex.h"
    00033 #elif defined(CS_PLATFORM_UNIX) || \
    00034       defined(CS_PLATFORM_MACOSX)
    00035 # include "csutil/threading/pthread_mutex.h"
    00036 #else
    00037 #error "No threading implementation for your platform"
    00038 #endif
    00039
    00040 namespace CS
    00041 {
    00042 namespace Threading
    00043 {
    00044
    00054   template<typename BaseMutex>
    00055   class MutexImpl : public BaseMutex,
    00056                     private CS::NonCopyable
    00057   {
    00058   public:
    00062     MutexImpl ()
    00063     {
    00064       BaseMutex::Initialize ();
    00065     }
    00066
    00070     ~MutexImpl ()
    00071     {
    00072       BaseMutex::Destroy ();
    00073     }
    00074
    00080     bool Lock ()
    00081     {
    00082       return BaseMutex::Lock ();
    00083     }
    00084
    00090     bool TryLock ()
    00091     {
    00092       return BaseMutex::TryLock ();
    00093     }
    00094
    00098     void Unlock ()
    00099     {
    00100       BaseMutex::Unlock ();
    00101     }
    00102
    00103   protected:
    00104     friend class ConditionBase;
    00105   };
    00106
    00110   typedef MutexImpl<Implementation::MutexBase> Mutex;
    00111
    00117   typedef MutexImpl<Implementation::RecursiveMutexBase> RecursiveMutex;
    00118
    00124   template<bool _Lock>
    00125   class OptionalMutex
    00126   {
    00127     Mutex theMutex;
    00128   public:
    00130     bool Lock () { return theMutex.Lock(); }
    00132     bool TryLock() { return theMutex.TryLock(); }
    00134     void Unlock() { theMutex.Unlock(); }
    00135   };
    00136
    00137   template<>
    00138   class OptionalMutex<false>
    00139   {
    00140   public:
    00141     bool Lock () { return true; }
    00142     bool TryLock() { return true; }
    00143     void Unlock() { }
    00144   };
    00145
    00146
    00162   template<typename T>
    00163   class ScopedLock
    00164   {
    00165   public:
    00166     ScopedLock (T& lockObj)
    00167       : lockObj (lockObj)
    00168     {
    00169       lockObj.Lock ();
    00170     }
    00171
    00172     ~ScopedLock ()
    00173     {
    00174       lockObj.Unlock ();
    00175     }
    00176
    00177   private:
    00178     T& lockObj;
    00179   };
    00180
    00181   // Standard lock
    00182   typedef ScopedLock<Mutex> MutexScopedLock;
    00183   typedef ScopedLock<RecursiveMutex> RecursiveMutexScopedLock;
    00184 }
    00185 }
    00186
    00187 #endif