Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Mutex.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/Mutex.h                          **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <pthread.h>
00034 #include <ICLUtils/Uncopyable.h>
00035 
00036 namespace icl{
00037   namespace utils{
00038   
00040     class Lockable;
00043 
00044 
00053     class Mutex : public Uncopyable{ 
00054       public:
00055   
00057       enum MutexType {
00058       #ifndef ICL_SYSTEM_WINDOWS
00059 
00060         mutexTypeNormal = PTHREAD_MUTEX_NORMAL,
00062         mutexTypeRecursive = PTHREAD_MUTEX_RECURSIVE
00063       #else
00064   
00065       #endif
00066       };
00067   
00069 
00072       Mutex(MutexType type = mutexTypeNormal){
00073           #ifndef ICL_SYSTEM_WINDOWS
00074         pthread_mutexattr_init(&a);
00075         pthread_mutexattr_settype(&a, type);
00076         pthread_mutex_init(&m,&a);
00077           #else
00078           
00079           #endif
00080       }
00082       ~Mutex(){
00083           #ifndef ICL_SYSTEM_WINDOWS
00084           pthread_mutex_destroy(&m);
00085           #else
00086           
00087           #endif
00088       }
00090       void lock(){
00091           #ifndef ICL_SYSTEM_WINDOWS
00092         pthread_mutex_lock(&m);
00093           #else
00094           
00095           #endif
00096       }
00098 
00101       int trylock(){
00102         #ifndef ICL_SYSTEM_WINDOWS
00103         return pthread_mutex_trylock(&m);
00104         #else
00105   
00106         #endif
00107       }
00108   
00110       void unlock(){
00111           #ifndef ICL_SYSTEM_WINDOWS
00112         pthread_mutex_unlock(&m);
00113           #else
00114           
00115           #endif
00116       }
00117       
00119       class Locker : public Uncopyable{
00120         public:
00122         Locker(Mutex *m);
00123   
00125         Locker(Mutex &m);
00126   
00128         Locker(const Lockable *l);
00129   
00131         Locker(const Lockable &l);
00132   
00134         ~Locker();
00135         private:
00137         Mutex *m;
00138       };
00139       private:
00141       pthread_mutex_t m;
00143       pthread_mutexattr_t a;
00144     };
00145     
00146     
00148     inline Mutex::Locker::Locker(Mutex *m):m(m){
00149       m->lock();
00150     }
00151     inline Mutex::Locker::Locker(Mutex &m):m(&m){
00152       m.lock();
00153     }
00154     inline Mutex::Locker::~Locker(){
00155       m->unlock();
00156     }
00158   } // namespace utils
00159 }
00160 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines