Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef OS_MUTEX_HPP
00040 #define OS_MUTEX_HPP
00041
00042 #include "fosi.h"
00043 #include "../rtt-config.h"
00044 #include "rtt-os-fwd.hpp"
00045 #include "Time.hpp"
00046 #ifdef ORO_OS_USE_BOOST_THREAD
00047
00048 #include <boost/thread/mutex.hpp>
00049 #include <boost/thread/recursive_mutex.hpp>
00050 #include <boost/date_time/posix_time/posix_time_types.hpp>
00051 #endif
00052
00053 namespace RTT
00054 { namespace os {
00055
00061 class RTT_API MutexInterface
00062 {
00063 public:
00064 virtual ~MutexInterface() {}
00065 virtual void lock() =0;
00066 virtual void unlock() =0;
00067 virtual bool trylock() = 0;
00068 virtual bool timedlock(Seconds s) = 0;
00069 };
00070
00071
00082 class RTT_API Mutex : public MutexInterface
00083 {
00084 friend class Condition;
00085 #ifndef ORO_OS_USE_BOOST_THREAD
00086 protected:
00087 rt_mutex_t m;
00088 public:
00092 Mutex()
00093 {
00094 rtos_mutex_init( &m);
00095 }
00096
00102 virtual ~Mutex()
00103 {
00104 if ( trylock() ) {
00105 unlock();
00106 rtos_mutex_destroy( &m );
00107 }
00108 }
00109
00110 virtual void lock ()
00111 {
00112 rtos_mutex_lock( &m );
00113 }
00114
00115 virtual void unlock()
00116 {
00117 rtos_mutex_unlock( &m );
00118 }
00119
00125 virtual bool trylock()
00126 {
00127 if ( rtos_mutex_trylock( &m ) == 0 )
00128 return true;
00129 return false;
00130 }
00131
00139 virtual bool timedlock(Seconds s)
00140 {
00141 if ( rtos_mutex_lock_until( &m, rtos_get_time_ns() + Seconds_to_nsecs(s) ) == 0 )
00142 return true;
00143 return false;
00144 }
00145 #else
00146 protected:
00147 boost::timed_mutex m;
00148 public:
00152 Mutex()
00153 {
00154 }
00155
00161 virtual ~Mutex()
00162 {
00163 }
00164
00165 virtual void lock ()
00166 {
00167 m.lock();
00168 }
00169
00170 virtual void unlock()
00171 {
00172 m.unlock();
00173 }
00174
00180 virtual bool trylock()
00181 {
00182 return m.try_lock();
00183 }
00184
00192 virtual bool timedlock(Seconds s)
00193 {
00194 return m.timed_lock( boost::posix_time::microseconds(Seconds_to_nsecs(s)/1000) );
00195 }
00196 #endif
00197
00198 };
00199
00208 class RTT_API MutexRecursive : public MutexInterface
00209 {
00210 #ifndef ORO_OS_USE_BOOST_THREAD
00211 protected:
00212 rt_rec_mutex_t recm;
00213 public:
00217 MutexRecursive()
00218 {
00219 rtos_mutex_rec_init( &recm );
00220 }
00221
00227 virtual ~MutexRecursive()
00228 {
00229 if ( trylock() ) {
00230 unlock();
00231 rtos_mutex_rec_destroy( &recm );
00232 }
00233 }
00234
00235 void lock ()
00236 {
00237 rtos_mutex_rec_lock( &recm );
00238 }
00239
00240 virtual void unlock()
00241 {
00242 rtos_mutex_rec_unlock( &recm );
00243 }
00244
00250 virtual bool trylock()
00251 {
00252 if ( rtos_mutex_rec_trylock( &recm ) == 0 )
00253 return true;
00254 return false;
00255 }
00256
00264 virtual bool timedlock(Seconds s)
00265 {
00266 if ( rtos_mutex_rec_lock_until( &recm, rtos_get_time_ns() + Seconds_to_nsecs(s) ) == 0 )
00267 return true;
00268 return false;
00269 }
00270 #else
00271 protected:
00272 boost::recursive_timed_mutex recm;
00273 public:
00277 MutexRecursive()
00278 {
00279 }
00280
00286 virtual ~MutexRecursive()
00287 {
00288 }
00289
00290 void lock ()
00291 {
00292 recm.lock();
00293 }
00294
00295 virtual void unlock()
00296 {
00297 recm.unlock();
00298 }
00299
00305 virtual bool trylock()
00306 {
00307 return recm.try_lock();
00308 }
00309
00317 virtual bool timedlock(Seconds s)
00318 {
00319 return recm.timed_lock( boost::posix_time::microseconds( Seconds_to_nsecs(s)/1000 ) );
00320 }
00321 #endif
00322 };
00323
00324 }}
00325
00326 #endif