Orocos Real-Time Toolkit  2.9.0
oro_arch.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 oro_atomic.h
3 
4  oro_atomic.h - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.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 
40 #ifndef __ARCH_NOASM_ORO_ATOMIC__
41 #define __ARCH_NOASM_ORO_ATOMIC__
42 
43 /*
44  * @file Atomic operations that C can't guarantee us. Useful for
45  * resource counting etc. We emulate it using mutexes.
46  */
47 
48 #include "../../rtt-config.h"
49 #include "../fosi.h"
50 
51 #ifndef __GNUC__
52 #define __inline__
53 #endif
54 
59 typedef struct {
61  int volatile cnter;
62 } oro_atomic_t;
63 
64 #define ORO_ATOMIC_SETUP(a_int,n) rtos_mutex_init(&((a_int)->m)); (a_int)->cnter = (n)
65 #define ORO_ATOMIC_CLEANUP(a_int) rtos_mutex_destroy(&((a_int)->m))
66 
67 #define oro_atomic_read(a_int) ((a_int)->cnter)
68 
69 #define oro_atomic_set(a_int,n) (((a_int)->cnter) = (n))
70 
71 static __inline__ void oro_atomic_add(oro_atomic_t *a_int, int n )
72 {
73  rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter += n; rtos_mutex_unlock(&((a_int)->m));
74 }
75 
76 static __inline__ int oro_atomic_add_return(oro_atomic_t *a_int, int n )
77 {
78  int new_value;
79  rtos_mutex_lock(&((a_int)->m)); new_value = ((a_int)->cnter += n); rtos_mutex_unlock(&((a_int)->m));
80  return new_value;
81 }
82 
83 static __inline__ void oro_atomic_sub(oro_atomic_t *a_int, int n )
84 {
85  rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; rtos_mutex_unlock(&((a_int)->m));
86 }
87 
88 static __inline__ int oro_atomic_sub_return(oro_atomic_t *a_int, int n )
89 {
90  int new_value;
91  rtos_mutex_lock(&((a_int)->m)); new_value = ((a_int)->cnter -= n); rtos_mutex_unlock(&((a_int)->m));
92  return new_value;
93 }
94 
95 static __inline__ int oro_atomic_add_and_test(oro_atomic_t *a_int, int n )
96 {
97  int ret = 0;
98  rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m));
99  return ret;
100 }
101 
102 static __inline__ int oro_atomic_sub_and_test(oro_atomic_t *a_int, int n )
103 {
104  int ret = 0;
105  rtos_mutex_lock(&((a_int)->m)); (a_int)->cnter -= n; ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m));
106  return ret;
107 }
108 
109 static __inline__ void oro_atomic_inc(oro_atomic_t *a_int)
110 {
111  rtos_mutex_lock(&((a_int)->m)); ++((a_int)->cnter) ; rtos_mutex_unlock(&((a_int)->m));
112 }
113 
115 {
116  int new_value;
117  rtos_mutex_lock(&((a_int)->m)); new_value = (++((a_int)->cnter)) ; rtos_mutex_unlock(&((a_int)->m));
118  return new_value;
119 }
120 
121 static __inline__ void oro_atomic_dec(oro_atomic_t *a_int)
122 {
123  rtos_mutex_lock(&((a_int)->m)); --((a_int)->cnter) ; rtos_mutex_unlock(&((a_int)->m));
124 }
125 
127 {
128  int new_value;
129  rtos_mutex_lock(&((a_int)->m)); new_value = (--((a_int)->cnter)) ; rtos_mutex_unlock(&((a_int)->m));
130  return new_value;
131 }
132 
134 {
135  int ret = 0;
136  rtos_mutex_lock(&((a_int)->m)); --((a_int)->cnter); ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m));
137  return ret;
138 }
139 
141 {
142  int ret = 0;
143  rtos_mutex_lock(&((a_int)->m)); ++((a_int)->cnter); ret = ((a_int)->cnter == 0); rtos_mutex_unlock(&((a_int)->m));
144  return ret;
145 }
146 
147 #endif
int rtos_mutex_unlock(rt_mutex_t *m)
int oro_atomic_inc_return(oro_atomic_t *a)
Increment a atomically and return the new value.
int oro_atomic_inc_and_test(oro_atomic_t *a)
Increment a atomically and test for zero.
int volatile cnter
Definition: oro_arch.h:61
int oro_atomic_sub_return(int n, oro_atomic_t *a, int n)
Subtract n from a and return the new value.
void oro_atomic_inc(oro_atomic_t *a)
Increment a atomically.
#define __inline__
Definition: oro_arch.h:52
volatile long oro_atomic_t
Definition: oro_arch.h:9
rt_mutex_t m
Definition: oro_arch.h:60
int rtos_mutex_lock(rt_mutex_t *m)
void oro_atomic_sub(int n, oro_atomic_t *a, int n)
Subtract 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)
Subtract n from a and test for zero.
int oro_atomic_dec_return(oro_atomic_t *a)
Decrement a atomically and return the new value.
void oro_atomic_dec(oro_atomic_t *a)
Decrement a atomically.
Structure that contains an int for atomic operations.
Definition: oro_arch.h:10
int oro_atomic_add_return(oro_atomic_t *a, int n)
Add n to a and return the new value.
cyg_mutex_t rt_mutex_t
Definition: fosi.h:185
void oro_atomic_add(oro_atomic_t *a, int n)
Add n to a.