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_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
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
00203 boost::posix_time::ptime p_time = boost::posix_time::from_time_t(0);
00204
00205
00206
00207 boost::posix_time::nanosec abs_p_time = boost::posix_time::nanoseconds(abs_time);
00208
00209
00210 p_time = p_time + abs_p_time;
00211 return c.timed_wait(m, p_time, &Condition::retfalse );
00212 }
00213 private:
00214 static bool retfalse() { return false; }
00215
00216 #endif
00217
00218 };
00219 }}
00220
00221 #endif