UMutex.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UMUTEX_H
00021 #define UMUTEX_H
00022 
00023 #include <errno.h>
00024 
00025 #ifdef WIN32
00026   #include "find_object/utilite/UWin32.h"
00027 #else
00028   #include <pthread.h>
00029 #endif
00030 
00031 
00054 class UMutex
00055 {
00056 
00057 public:
00058 
00062         UMutex()
00063         {
00064 #ifdef WIN32
00065                 InitializeCriticalSection(&C);
00066 #else
00067                 pthread_mutexattr_t attr;
00068                 pthread_mutexattr_init(&attr);
00069                 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
00070                 pthread_mutex_init(&M,&attr);
00071                 pthread_mutexattr_destroy(&attr);
00072 #endif
00073         }
00074 
00075         virtual ~UMutex()
00076         {
00077 #ifdef WIN32
00078                 DeleteCriticalSection(&C);
00079 #else
00080                 pthread_mutex_unlock(&M); pthread_mutex_destroy(&M);
00081 #endif
00082         }
00083 
00087         int lock() const
00088         {
00089 #ifdef WIN32
00090                 EnterCriticalSection(&C); return 0;
00091 #else
00092                 return pthread_mutex_lock(&M);
00093 #endif
00094         }
00095 
00096 #ifdef WIN32
00097         #if(_WIN32_WINNT >= 0x0400)
00098         int lockTry() const
00099         {
00100                 return (TryEnterCriticalSection(&C)?0:EBUSY);
00101         }
00102         #endif
00103 #else
00104         int lockTry() const
00105         {
00106                 return pthread_mutex_trylock(&M);
00107         }
00108 #endif
00109 
00113         int unlock() const
00114         {
00115 #ifdef WIN32
00116                 LeaveCriticalSection(&C); return 0;
00117 #else
00118                 return pthread_mutex_unlock(&M);
00119 #endif
00120         }
00121 
00122         private:
00123 #ifdef WIN32
00124                 mutable CRITICAL_SECTION C;
00125 #else
00126                 mutable pthread_mutex_t M;
00127 #endif
00128                 void operator=(UMutex &M) {}
00129                 UMutex( const UMutex &M ) {}
00130 };
00131 
00157 class UScopeMutex
00158 {
00159 public:
00160         UScopeMutex(const UMutex & mutex) :
00161                 mutex_(mutex)
00162         {
00163                 mutex_.lock();
00164         }
00165         // backward compatibility
00166         UScopeMutex(UMutex * mutex) :
00167                 mutex_(*mutex)
00168         {
00169                 mutex_.lock();
00170         }
00171         ~UScopeMutex()
00172         {
00173                 mutex_.unlock();
00174         }
00175 private:
00176         const UMutex & mutex_;
00177 };
00178 
00179 #endif // UMUTEX_H


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Aug 27 2015 13:00:33