Condition.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  Condition.hpp
00003 
00004                         Condition.hpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
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_CONDITION_HPP
00040 #define OS_CONDITION_HPP
00041 
00042 #include "fosi.h"
00043 #include "../rtt-config.h"
00044 #include "Time.hpp"
00045 #include "Mutex.hpp"
00046 #ifdef ORO_OS_USE_BOOST_THREAD
00047 // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is defined in rtt-config.h
00048 #include <boost/thread/condition.hpp>
00049 #include <boost/date_time/posix_time/posix_time_types.hpp>
00050 #endif
00051 
00052 namespace RTT
00053 { namespace os {
00054 
00060         class RTT_API Condition
00061     {
00062 #ifndef ORO_OS_USE_BOOST_THREAD
00063         protected:
00064             rt_cond_t c;
00065         public:
00069             Condition()
00070             {
00071                 rtos_cond_init( &c);
00072             }
00073 
00079             ~Condition()
00080             {
00081                 rtos_cond_destroy( &c );
00082             }
00083 
00090             bool wait (Mutex& m)
00091             {
00092                 return rtos_cond_wait( &c, &m.m ) == 0 ? true : false;
00093             }
00094 
00103             template<class Predicate>
00104         bool wait (Mutex& m, Predicate& p)
00105         {
00106                 while( !p() )
00107                     if ( rtos_cond_wait( &c, &m.m ) != 0) return false;
00108                 return true;
00109         }
00113             void broadcast()
00114             {
00115                 rtos_cond_broadcast( &c );
00116             }
00117 
00129         bool wait_until(Mutex& m, nsecs abs_time)
00130         {
00131             if ( rtos_cond_timedwait( &c, &m.m, abs_time ) == 0 )
00132                 return true;
00133             return false;
00134         }
00135 #else
00136     protected:
00137         boost::condition_variable_any c;
00138     public:
00142         Condition()
00143         {
00144         }
00145 
00151         ~Condition()
00152         {
00153         }
00154 
00161         bool wait (Mutex& m)
00162         {
00163             c.wait(m.m);
00164             return true;
00165         }
00166 
00175         template<class Predicate>
00176         bool wait (Mutex& m, Predicate& p)
00177         {
00178             c.wait(m.m, p);
00179             return true;
00180         }
00184         void broadcast()
00185         {
00186             c.notify_all();
00187         }
00188 
00200         bool wait_until(Mutex& m, nsecs abs_time)
00201         {
00202             // abs_time is since epoch, so set p_time to epoch, then add our abs_time.
00203             boost::posix_time::ptime p_time = boost::posix_time::from_time_t(0);
00204             boost::posix_time::nanosec abs_p_time = boost::posix_time::nanoseconds(abs_time);
00205             // wakeup time = epoch date + time since epoch
00206             p_time = p_time + abs_p_time;
00207             return c.timed_wait(m, p_time, &Condition::retfalse );
00208         }
00209     private:
00210         static bool retfalse() { return false; }
00211 
00212 #endif
00213 
00214     };
00215 }}
00216 
00217 #endif


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:46