Mutex.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 10 16:16:57 CEST 2002  Mutex.hpp
00003 
00004                         Mutex.hpp -  description
00005                            -------------------
00006     begin                : Thu October 10 2002
00007     copyright            : (C) 2002 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
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 "Time.hpp"
00045 #ifdef ORO_OS_USE_BOOST_THREAD
00046 #include <boost/thread/mutex.hpp>
00047 #include <boost/thread/recursive_mutex.hpp>
00048 #include <boost/date_time/posix_time/posix_time_types.hpp>
00049 #endif
00050 
00051 namespace RTT
00052 { namespace OS {
00053     class MutexLock;
00054     class MutexTryLock;
00055     class MutexTimedLock;
00056 
00062     class RTT_API MutexInterface
00063     {
00064     public:
00065         virtual ~MutexInterface() {}
00066         virtual void lock() =0;
00067         virtual void unlock() =0;
00068         virtual bool trylock() = 0;
00069         virtual bool timedlock(Seconds s) = 0;
00070     };
00071 
00072 
00083     class RTT_API Mutex : public MutexInterface
00084     {
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
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3