Condition.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 Condition.hpp
3 
4  Condition.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef OS_CONDITION_HPP
40 #define OS_CONDITION_HPP
41 
42 #include "fosi.h"
43 #include "../rtt-config.h"
44 #include "Time.hpp"
45 #include "Mutex.hpp"
46 #ifdef ORO_OS_USE_BOOST_THREAD
47 // BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG is defined in rtt-config.h
48 #include <boost/thread/condition.hpp>
49 #include <boost/date_time/posix_time/posix_time_types.hpp>
50 #endif
51 
52 namespace RTT
53 { namespace os {
54 
60  class RTT_API Condition
61  {
62 #ifndef ORO_OS_USE_BOOST_THREAD
63  protected:
65  public:
70  {
71  rtos_cond_init( &c);
72  }
73 
80  {
81  rtos_cond_destroy( &c );
82  }
83 
90  bool wait (Mutex& m)
91  {
92  return rtos_cond_wait( &c, &m.m ) == 0 ? true : false;
93  }
94 
103  template<class Predicate>
104  bool wait (Mutex& m, Predicate& p)
105  {
106  while( !p() )
107  if ( rtos_cond_wait( &c, &m.m ) != 0) return false;
108  return true;
109  }
113  void broadcast()
114  {
115  rtos_cond_broadcast( &c );
116  }
117 
129  bool wait_until(Mutex& m, nsecs abs_time)
130  {
131  if ( rtos_cond_timedwait( &c, &m.m, abs_time ) == 0 )
132  return true;
133  return false;
134  }
135 #else
136  protected:
137  boost::condition_variable_any c;
138  public:
142  Condition()
143  {
144  }
145 
151  ~Condition()
152  {
153  }
154 
161  bool wait (Mutex& m)
162  {
163  c.wait(m.m);
164  return true;
165  }
166 
175  template<class Predicate>
176  bool wait (Mutex& m, Predicate& p)
177  {
178  c.wait(m.m, p);
179  return true;
180  }
184  void broadcast()
185  {
186  c.notify_all();
187  }
188 
200  bool wait_until(Mutex& m, nsecs abs_time)
201  {
202  // abs_time is since epoch, so set p_time to epoch, then add our abs_time.
203  boost::posix_time::ptime p_time = boost::posix_time::from_time_t(0);
204 
205  // If the line below fails to compile, #define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG globally
206  // or #include <rtt/rtt-config.h> before any of your include headers.
207  boost::posix_time::nanosec abs_p_time = boost::posix_time::nanoseconds(abs_time);
208 
209  // wakeup time = epoch date + time since epoch
210  p_time = p_time + abs_p_time;
211  return c.timed_wait(m, p_time, &Condition::retfalse );
212  }
213  private:
214  static bool retfalse() { return false; }
215 
216 #endif
217 
218  };
219 }}
220 
221 #endif
rt_mutex_t m
Definition: Mutex.hpp:97
int rtos_cond_destroy(rt_cond_t *cond)
int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time)
An object oriented wrapper around a condition variable.
Definition: Condition.hpp:60
int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
bool wait(Mutex &m, Predicate &p)
Definition: Condition.hpp:104
bool wait_until(Mutex &m, nsecs abs_time)
Definition: Condition.hpp:129
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
long long nsecs
Definition: os/Time.hpp:69
int rtos_cond_init(rt_cond_t *cond)
int rtos_cond_broadcast(rt_cond_t *cond)
bool wait(Mutex &m)
Definition: Condition.hpp:90


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:22