Orocos Real-Time Toolkit  2.8.3
Mutex.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 10 16:16:57 CEST 2002 Mutex.hpp
3 
4  Mutex.hpp - description
5  -------------------
6  begin : Thu October 10 2002
7  copyright : (C) 2002 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef OS_MUTEX_HPP
40 #define OS_MUTEX_HPP
41 
42 #include "fosi.h"
43 #include "../rtt-config.h"
44 #include "rtt-os-fwd.hpp"
45 #include "Time.hpp"
46 #ifdef ORO_OS_USE_BOOST_THREAD
47 // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is defined in rtt-config.h
48 #include <boost/thread/mutex.hpp>
49 #include <boost/thread/recursive_mutex.hpp>
50 #include <boost/date_time/posix_time/posix_time_types.hpp>
51 #endif
52 
53 namespace RTT
54 { namespace os {
55 
62  {
63  public:
64  virtual ~MutexInterface() {}
65  virtual void lock() =0;
66  virtual void unlock() =0;
67  virtual bool trylock() = 0;
68  virtual bool timedlock(Seconds s) = 0;
69  };
70 
71 
82  class RTT_API Mutex : public MutexInterface
83  {
84  friend class Condition;
85 #ifndef ORO_OS_USE_BOOST_THREAD
86  protected:
88  public:
93  {
94  rtos_mutex_init( &m);
95  }
96 
102  virtual ~Mutex()
103  {
104  if ( trylock() ) {
105  unlock();
106  rtos_mutex_destroy( &m );
107  }
108  }
109 
110  virtual void lock ()
111  {
112  rtos_mutex_lock( &m );
113  }
114 
115  virtual void unlock()
116  {
117  rtos_mutex_unlock( &m );
118  }
119 
125  virtual bool trylock()
126  {
127  if ( rtos_mutex_trylock( &m ) == 0 )
128  return true;
129  return false;
130  }
131 
139  virtual bool timedlock(Seconds s)
140  {
141  if ( rtos_mutex_trylock_for( &m, Seconds_to_nsecs(s) ) == 0 )
142  return true;
143  return false;
144  }
145 #else
146  protected:
147  boost::timed_mutex m;
148  public:
152  Mutex()
153  {
154  }
155 
161  virtual ~Mutex()
162  {
163  }
164 
165  virtual void lock ()
166  {
167  m.lock();
168  }
169 
170  virtual void unlock()
171  {
172  m.unlock();
173  }
174 
180  virtual bool trylock()
181  {
182  return m.try_lock();
183  }
184 
192  virtual bool timedlock(Seconds s)
193  {
194  return m.timed_lock( boost::posix_time::microseconds(Seconds_to_nsecs(s)/1000) );
195  }
196 #endif
197 
198  };
199 
209  {
210 #ifndef ORO_OS_USE_BOOST_THREAD
211  protected:
213  public:
218  {
219  rtos_mutex_rec_init( &recm );
220  }
221 
227  virtual ~MutexRecursive()
228  {
229  if ( trylock() ) {
230  unlock();
231  rtos_mutex_rec_destroy( &recm );
232  }
233  }
234 
235  void lock ()
236  {
237  rtos_mutex_rec_lock( &recm );
238  }
239 
240  virtual void unlock()
241  {
242  rtos_mutex_rec_unlock( &recm );
243  }
244 
250  virtual bool trylock()
251  {
252  if ( rtos_mutex_rec_trylock( &recm ) == 0 )
253  return true;
254  return false;
255  }
256 
264  virtual bool timedlock(Seconds s)
265  {
266  if ( rtos_mutex_rec_trylock_for( &recm, Seconds_to_nsecs(s) ) == 0 )
267  return true;
268  return false;
269  }
270 #else
271  protected:
272  boost::recursive_timed_mutex recm;
273  public:
278  {
279  }
280 
286  virtual ~MutexRecursive()
287  {
288  }
289 
290  void lock ()
291  {
292  recm.lock();
293  }
294 
295  virtual void unlock()
296  {
297  recm.unlock();
298  }
299 
305  virtual bool trylock()
306  {
307  return recm.try_lock();
308  }
309 
317  virtual bool timedlock(Seconds s)
318  {
319  return recm.timed_lock( boost::posix_time::microseconds( Seconds_to_nsecs(s)/1000 ) );
320  }
321 #endif
322  };
323 
324 }}
325 
326 #endif
virtual bool timedlock(Seconds s)
Lock this mutex, but don&#39;t wait longer for the lock than the specified timeout.
Definition: Mutex.hpp:139
double Seconds
Seconds are stored as a double precision float.
Definition: Time.hpp:53
int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
virtual ~Mutex()
Destroy a Mutex.
Definition: Mutex.hpp:102
rt_mutex_t m
Definition: Mutex.hpp:87
int rtos_mutex_unlock(rt_mutex_t *m)
int rtos_mutex_rec_init(rt_rec_mutex_t *m)
virtual bool timedlock(Seconds s)
Lock this mutex, but don&#39;t wait longer for the lock than the specified timeout.
Definition: Mutex.hpp:264
int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
int rtos_mutex_rec_trylock_for(rt_rec_mutex_t *m, NANO_TIME relative_time)
virtual void unlock()
Definition: Mutex.hpp:240
virtual bool trylock()
Try to lock this mutex.
Definition: Mutex.hpp:125
#define RTT_API
Definition: rtt-config.h:97
An object oriented wrapper around a condition variable.
Definition: Condition.hpp:60
int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
virtual ~MutexRecursive()
Destroy a MutexRecursive.
Definition: Mutex.hpp:227
int rtos_mutex_lock(rt_mutex_t *m)
int rtos_mutex_init(rt_mutex_t *m)
virtual void lock()
Definition: Mutex.hpp:110
int rtos_mutex_destroy(rt_mutex_t *m)
rt_rec_mutex_t recm
Definition: Mutex.hpp:212
int rtos_mutex_trylock(rt_mutex_t *m)
nsecs Seconds_to_nsecs(const Seconds s)
Definition: Time.hpp:107
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:82
virtual void unlock()
Definition: Mutex.hpp:115
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
An object oriented wrapper around a recursive mutex.
Definition: Mutex.hpp:208
int rtos_mutex_trylock_for(rt_mutex_t *m, NANO_TIME relative_time)
Mutex()
Initialize a Mutex.
Definition: Mutex.hpp:92
virtual bool trylock()
Try to lock this mutex.
Definition: Mutex.hpp:250
MutexRecursive()
Initialize a recursive Mutex.
Definition: Mutex.hpp:217
virtual ~MutexInterface()
Definition: Mutex.hpp:64
An interface to a Mutex.
Definition: Mutex.hpp:61
int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)
cyg_mutex_t rt_mutex_t
Definition: fosi.h:185