Image Component Library (ICL)
Mutex.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/Mutex.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Uncopyable.h>
35 #include <pthread.h>
36 
37 namespace icl{
38  namespace utils{
39 
41  class Lockable;
44 
54  class Mutex : public Uncopyable{
55  public:
56 
58  enum MutexType {
59  //#ifndef ICL_SYSTEM_WINDOWS
61  mutexTypeNormal = PTHREAD_MUTEX_NORMAL,
63  mutexTypeRecursive = PTHREAD_MUTEX_RECURSIVE
64  //#else
65 
66  //#endif
67  } type;
68 
70 
74  //#ifndef ICL_SYSTEM_WINDOWS
75  pthread_mutexattr_init(&a);
76  pthread_mutexattr_settype(&a, type);
77  pthread_mutex_init(&m,&a);
78  //#else
79 
80  //#endif
81  }
83  ~Mutex(){
84  //#ifndef ICL_SYSTEM_WINDOWS
85  pthread_mutex_destroy(&m);
86  //#else
87 
88  //#endif
89  }
91  void lock(){
92  //#ifndef ICL_SYSTEM_WINDOWS
93  pthread_mutex_lock(&m);
94  //#else
95 
96  //#endif
97  }
99 
102  int trylock(){
103  //#ifndef ICL_SYSTEM_WINDOWS
104  return pthread_mutex_trylock(&m);
105  //#else
106 
107  //#endif
108  }
109 
111  void unlock(){
112  //#ifndef ICL_SYSTEM_WINDOWS
113  pthread_mutex_unlock(&m);
114  //#else
115 
116  //#endif
117  }
118 
120  class Locker : public Uncopyable{
121  public:
123  Locker(Mutex *m);
124 
126  Locker(Mutex &m);
127 
129  ICLUtils_API Locker(const Lockable *l);
130 
132  ICLUtils_API Locker(const Lockable &l);
133 
135  ~Locker();
136  private:
139  };
140  private:
142  pthread_mutex_t m;
144  pthread_mutexattr_t a;
145  };
146 
147 
149  inline Mutex::Locker::Locker(Mutex *m):m(m){
150  m->lock();
151  }
152  inline Mutex::Locker::Locker(Mutex &m):m(&m){
153  m.lock();
154  }
155  inline Mutex::Locker::~Locker(){
156  m->unlock();
157  }
159  } // namespace utils
160 }
161 
undocument this line if you encounter any issues!
Definition: Any.h:37
Class interface for un-copyable classes.
Definition: Uncopyable.h:64
void unlock()
unlocks the mutex
Definition: Mutex.h:111
~Locker()
unlocks the given mutex (automatically called for objects on the stack)
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
Mutex * m
wrapped mutex
Definition: Mutex.h:138
recursive mutex can be locked repeatedly by owner-thread. needs equal unlocks.
Definition: Mutex.h:63
Locker(Mutex *m)
Locks the given mutex until the section is leaved.
enum icl::utils::Mutex::MutexType type
pthread_mutexattr_t a
wrapped thread_mutexattr struct
Definition: Mutex.h:144
Mutex(MutexType type=mutexTypeNormal)
Create a mutex.
Definition: Mutex.h:73
pthread_mutex_t m
wrapped thread_mutex_t struct
Definition: Mutex.h:142
int trylock()
locks the mutex without blocking. returns immediately.
Definition: Mutex.h:102
Interface for objects, that can be locked using an internal mutex.
Definition: Lockable.h:41
normal mutex can not be locked by owner before unlocking
Definition: Mutex.h:61
Mutex class of the ICL.
Definition: Mutex.h:54
Locks a mutex on the stack (mutex is unlocked when the stack's section is released.
Definition: Mutex.h:120
~Mutex()
Destroys the mutex.
Definition: Mutex.h:83
void lock()
locks the mutex
Definition: Mutex.h:91
MutexType
This enum holds available mutex types.
Definition: Mutex.h:58