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 
00045 namespace RTT
00046 { namespace OS {
00047     class MutexLock;
00048     class MutexTryLock;
00049 
00055     class MutexInterface 
00056     {
00057     public:
00058         virtual ~MutexInterface() {}
00059         virtual void lock() =0;
00060         virtual void unlock() =0;
00061         virtual bool trylock() = 0;
00062     };
00063 
00074     class Mutex : public MutexInterface
00075     {
00076         friend class OS::MutexLock;
00077         friend class OS::MutexTryLock;
00078     protected:
00079         rt_mutex_t m;
00080     public:
00084             Mutex()
00085             {
00086           rtos_mutex_init( &m);
00087             }
00088 
00095             virtual ~Mutex()
00096             {
00097                 lock();
00098                 unlock(); // remove this ? then we destroy a locked mutex.
00099                 // race condition here...
00100                 rtos_mutex_destroy( &m );
00101             }
00102 
00103             virtual void lock ()
00104             {
00105                 rtos_mutex_lock( &m );
00106             }
00107 
00108             virtual void unlock()
00109             {
00110                 rtos_mutex_unlock( &m );
00111             }
00112 
00118             virtual bool trylock()
00119             {
00120                 if ( rtos_mutex_trylock( &m ) == 0 )
00121                     return true;
00122                 return false;
00123             }
00124 
00125     };
00126 
00135     class MutexRecursive : public MutexInterface
00136     {
00137         public:
00141         MutexRecursive()
00142         {
00143     rtos_mutex_rec_init( &recm );
00144       }
00145 
00149       ~MutexRecursive()
00150       {
00151     lock();
00152     unlock();
00153     // Race condition here...
00154     rtos_mutex_rec_destroy( &recm);
00155       }
00156 
00157       void lock ()
00158       {
00159     rtos_mutex_rec_lock( &recm );
00160         }
00161 
00162       virtual void unlock()
00163       {
00164     rtos_mutex_rec_unlock( &recm );
00165       }
00166 
00172       virtual bool trylock()
00173       {
00174     if ( rtos_mutex_rec_trylock( &recm ) == 0 )
00175       return true;
00176     return false;
00177       }
00178 
00179     protected:
00180       rt_rec_mutex_t recm;
00181     };
00182 
00183 }}
00184 
00185 #endif

Generated on Tue Aug 25 14:17:22 2009 for Orocos Real-Time Toolkit by  doxygen 1.5.8