IBR-DTNSuite  0.10
Mutex.h
Go to the documentation of this file.
1 /*
2  * Mutex.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef IBRCOMMON_MUTEX_H_
23 #define IBRCOMMON_MUTEX_H_
24 
25 #include <pthread.h>
26 #include "ibrcommon/Exceptions.h"
27 
28 namespace ibrcommon
29 {
31  {
32  public:
33  MutexException(string what = "An error occured during mutex operation.") throw() : ibrcommon::Exception(what)
34  {
35  };
36  };
37 
39  {
40  public:
41  virtual ~MutexInterface() = 0;
42 
43  virtual void trylock() throw (MutexException) = 0;
44  virtual void enter() throw (MutexException) = 0;
45  virtual void leave() throw (MutexException) = 0;
46  };
47 
48  class MutexMock : public MutexInterface
49  {
50  public:
51  MutexMock() {};
52  virtual ~MutexMock() {};
53 
54  virtual void trylock() throw (MutexException) {};
55  virtual void enter() throw (MutexException) {};
56  virtual void leave() throw (MutexException) {};
57  };
58 
59  class Mutex : public MutexInterface
60  {
61  public:
63  {
64  MUTEX_NORMAL = PTHREAD_MUTEX_NORMAL,
65  MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE,
66  MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK
67  };
68 
70  virtual ~Mutex();
71 
72  virtual void trylock() throw (MutexException);
73  virtual void enter() throw (MutexException);
74  virtual void leave() throw (MutexException);
75 
76  static MutexInterface& dummy();
77 
78  protected:
79  pthread_mutex_t m_mutex;
80  pthread_mutexattr_t m_attr;
81 
82  private:
83  static MutexMock _dummy;
84  };
85 }
86 
87 #endif /*MUTEX_H_*/