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.LGPL **
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 <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Uncopyable.h>
00035 #include <pthread.h>
00036 
00037 namespace icl{
00038   namespace utils{
00039   
00041     class Lockable;
00044 
00045 
00054     class Mutex : public Uncopyable{
00055       public:
00056   
00058       enum MutexType {
00059       //#ifndef ICL_SYSTEM_WINDOWS
00061         mutexTypeNormal = PTHREAD_MUTEX_NORMAL,
00063         mutexTypeRecursive = PTHREAD_MUTEX_RECURSIVE
00064       //#else
00065   
00066       //#endif
00067       } type;
00068   
00070 
00073       Mutex(MutexType type = mutexTypeNormal):type(type){
00074           //#ifndef ICL_SYSTEM_WINDOWS
00075         pthread_mutexattr_init(&a);
00076         pthread_mutexattr_settype(&a, type);
00077         pthread_mutex_init(&m,&a);
00078           //#else
00079           
00080           //#endif
00081       }
00083       ~Mutex(){
00084           //#ifndef ICL_SYSTEM_WINDOWS
00085           pthread_mutex_destroy(&m);
00086           //#else
00087           
00088           //#endif
00089       }
00091       void lock(){
00092           //#ifndef ICL_SYSTEM_WINDOWS
00093         pthread_mutex_lock(&m);
00094           //#else
00095           
00096           //#endif
00097       }
00099 
00102       int trylock(){
00103         //#ifndef ICL_SYSTEM_WINDOWS
00104         return pthread_mutex_trylock(&m);
00105         //#else
00106   
00107         //#endif
00108       }
00109   
00111       void unlock(){
00112           //#ifndef ICL_SYSTEM_WINDOWS
00113         pthread_mutex_unlock(&m);
00114           //#else
00115           
00116           //#endif
00117       }
00118       
00120       class Locker : public Uncopyable{
00121         public:
00123         Locker(Mutex *m);
00124   
00126         Locker(Mutex &m);
00127   
00129         ICLUtils_API Locker(const Lockable *l);
00130   
00132         ICLUtils_API Locker(const Lockable &l);
00133   
00135         ~Locker();
00136         private:
00138         Mutex *m;
00139       };
00140       private:
00142       pthread_mutex_t m;
00144       pthread_mutexattr_t a;
00145     };
00146     
00147     
00149     inline Mutex::Locker::Locker(Mutex *m):m(m){
00150       m->lock();
00151     }
00152     inline Mutex::Locker::Locker(Mutex &m):m(&m){
00153       m.lock();
00154     }
00155     inline Mutex::Locker::~Locker(){
00156       m->unlock();
00157     }
00159   } // namespace utils
00160 }
00161 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines