Orocos Real-Time Toolkit  2.8.3
Atomic.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Dec 21 22:43:04 CET 2004 Atomic.hpp
3 
4  Atomic.hpp - description
5  -------------------
6  begin : Tue December 21 2004
7  copyright : (C) 2004 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 #ifndef OS_COMMON_ORO_ATOMIC_HPP
40 #define OS_COMMON_ORO_ATOMIC_HPP
41 
42 #include "oro_arch.h"
43 
44 namespace RTT
45 { namespace os {
50  {
51  oro_atomic_t _val;
52  public:
53  AtomicInt( int value = 0 )
54  {
55  ORO_ATOMIC_SETUP( &_val, value);
56  }
57 
58  AtomicInt(const AtomicInt& orig)
59  {
60  ORO_ATOMIC_SETUP( &_val, oro_atomic_read( &(orig._val) ) );
61  }
62 
64  {
65  ORO_ATOMIC_CLEANUP( &_val );
66  }
67 
68  const AtomicInt& operator=(const AtomicInt& orig)
69  {
70  oro_atomic_set( &_val, oro_atomic_read( &(orig._val)));
71  return *this;
72  }
73 
77  int read() const { return oro_atomic_read( &_val); }
78 
82  void set(int i) { oro_atomic_set( &_val, i ); }
83 
84  void add(int i) { oro_atomic_add( &_val, i ); }
85 
86  void sub(int i) { oro_atomic_sub( &_val, i ); }
87 
93  bool sub_and_test(int i) { return oro_atomic_sub_and_test( &_val, i) != 0; }
94 
95  void inc() { oro_atomic_inc( &_val ); }
96 
97  void dec() { oro_atomic_dec( &_val ); }
98 
104  bool dec_and_test() { return oro_atomic_dec_and_test( &_val ) != 0; }
105 
111  bool inc_and_test() { return oro_atomic_inc_and_test( &_val ) != 0; }
112  };
113 
114 }}
115 
116 #endif
AtomicInt(const AtomicInt &orig)
Definition: Atomic.hpp:58
C++ abstraction of atomic integer operations.
Definition: Atomic.hpp:49
bool sub_and_test(int i)
Subtract a value and test if the result is zero.
Definition: Atomic.hpp:93
int oro_atomic_inc_and_test(oro_atomic_t *a)
Increment a atomically and test for zero.
#define RTT_API
Definition: rtt-config.h:97
int oro_atomic_read(oro_atomic_t *a)
Returns the current counter value of the atomic structure a.
bool inc_and_test()
Increment and test if the result is zero.
Definition: Atomic.hpp:111
bool dec_and_test()
Decrement and test if the result is zero.
Definition: Atomic.hpp:104
void oro_atomic_inc(oro_atomic_t *a)
Increment a atomically.
void ORO_ATOMIC_SETUP(oro_atomic_t *a, int n)
Initializes the uninitialized atomic structure a with a counter value of 'n'.
AtomicInt(int value=0)
Definition: Atomic.hpp:53
const AtomicInt & operator=(const AtomicInt &orig)
Definition: Atomic.hpp:68
void ORO_ATOMIC_CLEANUP(oro_atomic_t *a)
Cleans up all resources allocated durint the setup of atomic structure a.
void oro_atomic_sub(int n, oro_atomic_t *a, int n)
Substract n from a.
int oro_atomic_dec_and_test(oro_atomic_t *a)
Decrement a atomically and test for zero.
int oro_atomic_sub_and_test(oro_atomic_t *a, int n)
Substract n from a and test for zero.
void sub(int i)
Definition: Atomic.hpp:86
void oro_atomic_set(oro_atomic_t *a, int n)
Sets the current counter value of the atomic structure a to n.
int read() const
Read the current value of the integer.
Definition: Atomic.hpp:77
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
void add(int i)
Definition: Atomic.hpp:84
void oro_atomic_dec(oro_atomic_t *a)
Decrement a atomically.
Structure that contains an int for atomic operations.
Definition: oro_arch.h:10
void oro_atomic_add(oro_atomic_t *a, int n)
Add n to a.