RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
rt.h
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #ifndef RT_H
21 #define RT_H
22 
23 #include <fifo.h>
24 #include <mutex.h>
25 #include <pthread.h>
26 #include <semaphore.h>
27 #include <settings.h>
28 
30 
34 namespace RT
35 {
36 
37 namespace OS
38 {
39 
40 typedef void * Task;
41 
42 int initiate(void);
43 void shutdown(void);
44 
45 int createTask(Task *,void *(*)(void *),void *,int =0);
46 void deleteTask(Task);
47 
48 int setPeriod(Task,long long);
49 void sleepTimestep(Task);
50 
51 bool isRealtime(void);
52 
60 long long getTime(void);
61 
62 } // namespace OS
63 
70 class Event
71 {
72 
73  friend class System;
74 
75 public:
76 
77  Event(void);
78  virtual ~Event(void);
79 
85  virtual int callback(void)=0;
86 
87 private:
88 
89  void execute(void);
90  void wait(void);
91 
92  int retval;
93  sem_t signal;
94 
95 }; // class Event
96 
97 template<typename T>
98 class List
99 {
100 
101 public:
102 
103  class Node;
104 
105  class iterator
106  {
107 
108  public:
109 
110  iterator(void)
111  : current(0) {};
112  iterator(T *x)
113  : current(static_cast<Node *>(x)) {};
114  iterator(const iterator &x)
115  : current(x.current) {};
116 
117  bool operator==(const iterator &x) const
118  {
119  return current == x.current;
120  };
121  bool operator!=(const iterator &x) const
122  {
123  return current != x.current;
124  };
125 
126  T &operator*(void) const
127  {
128  return *static_cast<T *>(current);
129  };
130  T *operator->(void) const
131  {
132  return static_cast<T *>(current);
133  };
134 
136  {
137  current = current->next;
138  return *this;
139  };
141  {
142  typename RT::List<T>::iterator tmp = *this;
143  current = current->next;
144  return tmp;
145  };
147  {
148  current = current->prev;
149  return *this;
150  };
152  {
153  typename RT::List<T>::iterator tmp = *this;
154  current = current->prev;
155  return tmp;
156  };
157 
158 
159  private:
160 
161  Node *current;
162 
163  }; // class iterator
164 
166  {
167 
168  public:
169 
171  : current(0) {};
172  const_iterator(const T *x)
173  : current(static_cast<const Node *>(x)) {};
175  : current(x.current) {};
176 
177  bool operator==(const const_iterator &x) const
178  {
179  return current == x.current;
180  };
181  bool operator!=(const const_iterator &x) const
182  {
183  return current != x.current;
184  };
185 
186  const T &operator*(void) const
187  {
188  return *static_cast<const T *>(current);
189  };
190  const T *operator->(void) const
191  {
192  return static_cast<const T *>(current);
193  };
194 
196  {
197  current = current->next;
198  return *this;
199  };
201  {
202  typename RT::List<T>::const_iterator tmp = *this;
203  current = current->next;
204  return tmp;
205  };
207  {
208  current = current->prev;
209  return *this;
210  };
212  {
213  typename RT::List<T>::const_iterator tmp = *this;
214  current = current->prev;
215  return tmp;
216  };
217 
218 
219  private:
220 
221  const Node *current;
222 
223  }; // class const_iterator
224 
225  class Node
226  {
227 
228  friend class List<T>;
229  friend class List<T>::iterator;
230  friend class List<T>::const_iterator;
231 
232  public:
233 
234  Node(void)
235  : next(0), prev(0) {};
236  virtual ~Node(void) {};
237 
238  bool operator==(const Node &x) const
239  {
240  return next == x.next && prev == x.prev;
241  };
242 
243  private:
244 
245  Node *next, *prev;
246 
247  }; // class Node
248 
249  List(void)
250  : count(0), head(&tail), tail() {};
251  virtual ~List(void)
252  {
253 #ifdef DEBUG
254  if(tail.next)
255  ERROR_MSG("RT::List::~List : end of list overwritten\n");
256 #endif
257  };
258 
259  size_t size(void) const
260  {
261  return count;
262  };
263  bool empty(void) const
264  {
265  return count==0;
266  };
267 
268  iterator begin(void)
269  {
270  return iterator(static_cast<T *>(head));
271  };
272  iterator end(void)
273  {
274  return iterator(static_cast<T *>(&tail));
275  };
276 
277  const_iterator begin(void) const
278  {
279  return const_iterator(static_cast<const T *>(head));
280  };
281  const_iterator end(void) const
282  {
283  return const_iterator(static_cast<const T *>(&tail));
284  };
285 
286  void insert(iterator,T &);
287  void insertRT(iterator position,T &node)
288  {
289  Node *object = static_cast<Node *>(&node);
290 
291  object->next = &(*position);
292  object->prev = object->next->prev;
293  if(object->next == head)
294  head = object;
295  else
296  position->prev->next = object;
297  position->prev = object;
298  count++;
299  };
300 
301  void remove(T &);
302  void removeRT(T &node)
303  {
304  Node *object = static_cast<Node *>(&node);
305 
306  if(object == &tail)
307  return;
308  if(object == head)
309  head = object->next;
310  else if(object->prev)
311  object->prev->next = object->next;
312  object->next->prev = object->prev;
313  count--;
314  };
315 
316 private:
317 
319  {
320 
321  public:
322 
324  : list(l), iter(i), node(n) {};
325  int callback(void)
326  {
327  list->insertRT(iter,*node);
328  return 0;
329  };
330 
331  private:
332 
333  List<T> *list;
335  T *node;
336 
337  }; // class InsertListNodeEvent;
338 
340  {
341 
342  public:
343 
345  : list(l), node(n) {};
346  int callback(void)
347  {
348  list->removeRT(*node);
349  return 0;
350  };
351 
352  private:
353 
354  List<T> *list;
355  T *node;
356 
357  }; // class RemoveListNodeEvnet;
358 
359  size_t count;
360  Node *head, tail;
361 
362 }; // class List
363 
364 class Device;
365 class Thread;
366 
371 class System
372 {
373 
374  friend class Device;
375  friend class Thread;
376 
377 public:
378 
385  static System *getInstance(void);
386 
392  long long getPeriod(void) const
393  {
394  return period;
395  };
402  int setPeriod(long long period);
403 
413  void foreachDevice(void (*callback)(Device *,void *),void *param);
423  void foreachThread(void (*callback)(Thread *,void *),void *param);
424 
434  int postEvent(Event *event,bool blocking =true);
435 
436 private:
437 
438  /******************************************************************
439  * The constructors, destructor, and assignment operator are made *
440  * private to control instantiation of the class. *
441  ******************************************************************/
442 
443  System(void);
444  ~System(void);
445  System(const System &) : eventFifo(0) {};
447  {
448  return *getInstance();
449  };
450 
451  class SetPeriodEvent : public RT::Event
452  {
453 
454  public:
455 
456  SetPeriodEvent(long long);
457  ~SetPeriodEvent(void);
458 
459  int callback(void);
460 
461  private:
462 
463  long long period;
464 
465  }; // class SetPeriodEvent
466 
467 
468  static System *instance;
469 
471  void insertDevice(Device *);
472  void removeDevice(Device *);
473 
475  void insertThread(Thread *);
476  void removeThread(Thread *);
477 
478  static void *bounce(void *);
479  void execute(void);
480 
481  bool finished;
482  pthread_t thread;
484  long long period;
485 
488 
490 
491 }; // class System
492 
498 class Device : public List<Device>::Node
499 {
500 
501 public:
502 
503  Device(void);
504  virtual ~Device(void);
505 
517  /**********************************************************
518  * read & write must not be pure virtual because they can *
519  * be called during construction and destruction. *
520  **********************************************************/
521 
522  virtual void read(void) {};
523  virtual void write(void) {};
524 
525  inline bool getActive(void) const
526  {
527  return active;
528  };
529  void setActive(bool);
530 
531 private:
532 
533  bool active;
534 
535 }; // class Device
536 
542 class Thread : public List<Thread>::Node
543 {
544 
545 public:
546 
547  typedef unsigned long Priority;
548 
549  static const Priority MinimumPriority = 0;
550  static const Priority MaximumPriority = 100;
551  static const Priority DefaultPriority = MaximumPriority/2;
552 
553  Thread(Priority p =DefaultPriority);
554  virtual ~Thread(void);
555 
563  Priority getPriority(void) const
564  {
565  return priority;
566  };
567 
574  /**********************************************************
575  * execute must not be pure virtual because it can be *
576  * called during construction and destruction. *
577  **********************************************************/
578 
579  virtual void execute(void) {};
580 
581  inline bool getActive(void) const
582  {
583  return active;
584  };
585  void setActive(bool);
586 
587 private:
588 
589  bool active;
590  Priority priority;
591 
592 }; // class Thread
593 
594 template<typename T>
595 void List<T>::insert(iterator position,T &node)
596 {
597  InsertListNodeEvent event(this,position,&node);
599 }
600 
601 template<typename T>
602 void List<T>::remove(T &node)
603 {
604  RemoveListNodeEvent event(this,&node);
606 }
607 
608 } // namespace RT
609 
610 #endif // RT_H
virtual ~Event(void)
Definition: rt.cpp:103
size_t size(void) const
Definition: rt.h:259
iterator & operator--(void)
Definition: rt.h:146
int retval
Definition: rt.h:92
long long getPeriod(void) const
Definition: rt.h:392
int initiate(void)
Definition: rt_os-posix.cpp:39
virtual ~List(void)
Definition: rt.h:251
InsertListNodeEvent(List< T > *l, iterator i, T *n)
Definition: rt.h:323
bool operator!=(const const_iterator &x) const
Definition: rt.h:181
const T * operator->(void) const
Definition: rt.h:190
iterator(T *x)
Definition: rt.h:112
void execute(void)
Definition: rt.cpp:313
Device(void)
Definition: rt.cpp:119
Definition: rt.h:98
unsigned long Priority
Definition: rt.h:547
void deleteTask(Task)
iterator operator--(int)
Definition: rt.h:151
Fifo eventFifo
Definition: rt.h:489
void setActive(bool)
Definition: rt.cpp:130
int createTask(Task *, void *(*)(void *), void *, int=0)
SetPeriodEvent(long long)
Definition: rt.cpp:76
void removeRT(T &node)
Definition: rt.h:302
bool getActive(void) const
Definition: rt.h:581
~System(void)
Definition: rt.cpp:181
int callback(void)
Definition: rt.cpp:81
static System * instance
Definition: rt.h:468
const Node * current
Definition: rt.h:216
bool empty(void) const
Definition: rt.h:263
List< RT::Device > devices
Definition: rt.h:486
virtual void read(void)
Definition: rt.h:522
const_iterator operator++(int)
Definition: rt.h:200
bool isRealtime(void)
void insertThread(Thread *)
Definition: rt.cpp:263
void insertRT(iterator position, T &node)
Definition: rt.h:287
Priority getPriority(void) const
Definition: rt.h:563
void insertDevice(Device *)
Definition: rt.cpp:229
void remove(T &)
Definition: rt.h:602
System(void)
Definition: rt.cpp:163
void execute(void)
Definition: rt.cpp:108
bool operator!=(const iterator &x) const
Definition: rt.h:121
Definition: mutex.h:28
void removeThread(Thread *)
Definition: rt.cpp:287
void sleepTimestep(Task)
const T & operator*(void) const
Definition: rt.h:186
const_iterator & operator--(void)
Definition: rt.h:206
Definition: fifo.h:26
#define ERROR_MSG(fmt, args...)
Definition: debug.h:41
bool finished
Definition: rt.h:481
iterator end(void)
Definition: rt.h:272
RemoveListNodeEvent(List< T > *l, T *n)
Definition: rt.h:344
void * Task
Definition: rt.h:40
iterator & operator++(void)
Definition: rt.h:135
const_iterator(void)
Definition: rt.h:170
Priority priority
Definition: rt.h:590
T & operator*(void) const
Definition: rt.h:126
bool active
Definition: rt.h:533
void foreachThread(void(*callback)(Thread *, void *), void *param)
Definition: rt.cpp:211
long long getTime(void)
virtual int callback(void)=0
RT::OS::Task task
Definition: rt.h:483
Thread(Priority p=DefaultPriority)
Definition: rt.cpp:141
T * operator->(void) const
Definition: rt.h:130
System & operator=(const System &)
Definition: rt.h:446
const_iterator(const T *x)
Definition: rt.h:172
List< T >::iterator iter
Definition: rt.h:334
void insert(iterator, T &)
Definition: rt.h:595
Event(void)
Definition: rt.cpp:98
Definition: rt.h:371
Node * next
Definition: rt.h:241
void foreachDevice(void(*callback)(Device *, void *), void *param)
Definition: rt.cpp:204
bool operator==(const const_iterator &x) const
Definition: rt.h:177
Node * head
Definition: rt.h:360
const_iterator operator--(int)
Definition: rt.h:211
static void * bounce(void *)
Definition: rt.cpp:304
Node * prev
Definition: rt.h:241
void setActive(bool)
Definition: rt.cpp:152
Node * current
Definition: rt.h:156
Mutex deviceMutex
Definition: rt.h:470
static const Priority MinimumPriority
Definition: rt.h:549
bool active
Definition: rt.h:589
iterator begin(void)
Definition: rt.h:268
Definition: rt.h:70
Node tail
Definition: rt.h:360
bool getActive(void) const
Definition: rt.h:525
int setPeriod(long long period)
Definition: rt.cpp:188
void wait(void)
Definition: rt.cpp:114
iterator(void)
Definition: rt.h:110
virtual void write(void)
Definition: rt.h:523
iterator operator++(int)
Definition: rt.h:140
System(const System &)
Definition: rt.h:445
const_iterator end(void) const
Definition: rt.h:281
Realtime Oriented Classes.
Definition: rt.h:34
long long period
Definition: rt.h:484
const_iterator(const const_iterator &x)
Definition: rt.h:174
long long period
Definition: rt.h:463
Mutex threadMutex
Definition: rt.h:474
sem_t signal
Definition: rt.h:93
const_iterator begin(void) const
Definition: rt.h:277
virtual ~Node(void)
Definition: rt.h:236
List(void)
Definition: rt.h:249
virtual ~Device(void)
Definition: rt.cpp:125
static System * getInstance(void)
Definition: rt.cpp:361
bool operator==(const Node &x) const
Definition: rt.h:238
size_t count
Definition: rt.h:359
int setPeriod(Task, long long)
Event Oriented Classes.
Definition: event.h:35
Definition: rt.h:498
void shutdown(void)
Definition: rt_os-posix.cpp:63
List< RT::Thread > threadList
Definition: rt.h:487
pthread_t thread
Definition: rt.h:482
static const Priority MaximumPriority
Definition: rt.h:550
iterator(const iterator &x)
Definition: rt.h:114
virtual ~Thread(void)
Definition: rt.cpp:147
void removeDevice(Device *)
Definition: rt.cpp:246
const_iterator & operator++(void)
Definition: rt.h:195
static const Priority DefaultPriority
Definition: rt.h:551
int postEvent(Event *event, bool blocking=true)
Definition: rt.cpp:218
virtual void execute(void)
Definition: rt.h:579
bool operator==(const iterator &x) const
Definition: rt.h:117
Definition: rt.h:542