Orocos Real-Time Toolkit  2.8.3
Semaphore.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Apr 5 16:53:24 CEST 2005 Semaphore.hpp
3 
4  Semaphore.hpp - description
5  -------------------
6  begin : Tue April 05 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
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 
40 
41 #ifndef RTT_OS_SEMAPHORE_HPP
42 #define RTT_OS_SEMAPHORE_HPP
43 
44 #include "fosi.h"
45 #include "../rtt-config.h"
46 #include "../Time.hpp"
47 
48 namespace RTT
49 { namespace os {
62  {
63  private:
64  rt_sem_t sem;
65  public:
70  Semaphore(int count)
71  {
72  rtos_sem_init( &sem, count );
73  }
74 
79  {
80  rtos_sem_destroy( &sem );
81  }
82 
87  void wait()
88  {
89  rtos_sem_wait( &sem );
90  }
91 
96  void signal()
97  {
98  rtos_sem_signal( &sem );
99  }
100 
106  bool trywait()
107  {
108  if ( rtos_sem_trywait( &sem ) == 0 )
109  return true;
110  return false;
111  }
112 
116 #ifndef OROPKG_OS_MACOSX
117  int value()
118  {
119  return rtos_sem_value( &sem );
120  }
121 #endif
122  };
123 }}
124 
125 #endif
void signal()
Raise this semaphore and signal one thread waiting on this semaphore.
Definition: Semaphore.hpp:96
int value()
Return the current count of this semaphore.
Definition: Semaphore.hpp:117
int rtos_sem_trywait(rt_sem_t *m)
int rtos_sem_signal(rt_sem_t *m)
An object oriented wrapper around a counting semaphore.
Definition: Semaphore.hpp:61
Semaphore(int count)
Initialize a Semaphore with an initial count.
Definition: Semaphore.hpp:70
~Semaphore()
Destroy a Semaphore.
Definition: Semaphore.hpp:78
#define RTT_API
Definition: rtt-config.h:97
void wait()
Lower this semaphore and return if value() is non zero.
Definition: Semaphore.hpp:87
int rtos_sem_destroy(rt_sem_t *m)
int rtos_sem_init(rt_sem_t *m, int value)
All these should return zero in case of succes.
int rtos_sem_value(rt_sem_t *m)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
bool trywait()
Try to wait on this semaphore.
Definition: Semaphore.hpp:106
int rtos_sem_wait(rt_sem_t *m)
cyg_sem_t rt_sem_t
Definition: fosi.h:131