CLAM-Development  1.4.0
Mutex.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __MUTEX__
23 #define __MUTEX__
24 
25 #include <pthread.h>
26 #include "Lock.hxx"
27 #include <errno.h>
28 
29 // http://www.boost.org/libs/thread/doc/mutex_concept.html
30 
31 namespace CLAM
32 {
33 
34 struct xtime;
44 class Mutex
45 {
46 public:
47 
48  friend class Hidden::LockOps<Mutex>;
49  friend class Hidden::ScopedLock<Mutex>;
50 
52 
53  Mutex();
54 
55  ~Mutex();
56 
57 private:
58 
59  struct ConditionVar
60  {
61  pthread_mutex_t* pmutex;
62  };
63 
64  void DoLock();
65 
66  void DoUnlock();
67 
68  void DoLock( ConditionVar& state );
69 
70  void DoUnlock( ConditionVar& state );
71 
72  pthread_mutex_t mMutex;
73 
74 };
75 
76 class TryMutex
77 {
78 public:
79  friend class Hidden::LockOps<TryMutex>;
80 
83 
84  TryMutex();
85  ~TryMutex();
86 
87 private:
88  struct ConditionVar
89  {
90  pthread_mutex_t* pmutex;
91  };
92 
93  void DoLock();
94 
95  bool DoTryLock();
96 
97  void DoUnlock();
98 
99  void DoLock( ConditionVar& state );
100 
101  void DoUnlock( ConditionVar& state );
102 
103  pthread_mutex_t mMutex;
104 
105 };
106 
108 {
109 public:
111 
115 
116  TimedMutex();
117  ~TimedMutex();
118 
119 private:
120  struct ConditionVar
121  {
122  pthread_mutex_t* pmutex;
123  };
124 
125  void DoLock();
126 
127  bool DoTryLock();
128 
129  bool DoTimedLock( const xtime& xt );
130 
131  void DoUnlock();
132 
133  void DoLock( ConditionVar& state );
134 
135  void DoUnlock( ConditionVar& state );
136 
137  pthread_mutex_t mMutex;
138 
139  pthread_cond_t mCondition;
140 
141  bool mLocked;
142 
143 };
144 
145 } // end of namespace CLAM
146 
147 #endif // Mutex.hxx
148