Orocos Real-Time Toolkit  2.8.3
oro_arch.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 10 15:59:15 CET 2005 oro_atomic.h
3 
4  oro_atomic.h - description
5  -------------------
6  begin : Mon January 10 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 #include "../../rtt-config.h"
41 #ifndef __ORO_ARCH_x86_64__
42 #define __ORO_ARCH_x86_64__
43 
44 #ifndef CONFIG_FORCE_UP
45 #define ORO_LOCK "lock ; "
46 #else
47 #define ORO_LOCK ""
48 #endif
49 
50 typedef struct { volatile int counter; } oro_atomic_t;
51 
52 #define ORO_ATOMIC_SETUP oro_atomic_set
53 #define ORO_ATOMIC_CLEANUP(v)
54 
55 #define oro_atomic_read(v) ((v)->counter)
56 
57 #define oro_atomic_set(v,i) (((v)->counter) = (i))
58 
59 static __inline__ void oro_atomic_add(oro_atomic_t *v, int i)
60 {
61  __asm__ __volatile__(
62  ORO_LOCK "addl %1,%0"
63  :"=m" (v->counter)
64  :"ir" (i), "m" (v->counter));
65 }
66 
67 static __inline__ void oro_atomic_sub(oro_atomic_t *v, int i)
68 {
69  __asm__ __volatile__(
70  ORO_LOCK "subl %1,%0"
71  :"=m" (v->counter)
72  :"ir" (i), "m" (v->counter));
73 }
74 
76 {
77  unsigned char c;
78 
79  __asm__ __volatile__(
80  ORO_LOCK "subl %2,%0; sete %1"
81  :"=m" (v->counter), "=qm" (c)
82  :"ir" (i), "m" (v->counter) : "memory");
83  return c;
84 }
85 
87 {
88  __asm__ __volatile__(
89  ORO_LOCK "incl %0"
90  :"=m" (v->counter)
91  :"m" (v->counter));
92 }
93 
95 {
96  __asm__ __volatile__(
97  ORO_LOCK "decl %0"
98  :"=m" (v->counter)
99  :"m" (v->counter));
100 }
101 
103 {
104  unsigned char c;
105 
106  __asm__ __volatile__(
107  ORO_LOCK "decl %0; sete %1"
108  :"=m" (v->counter), "=qm" (c)
109  :"m" (v->counter) : "memory");
110  return c != 0;
111 }
112 
114 {
115  unsigned char c;
116 
117  __asm__ __volatile__(
118  ORO_LOCK "incl %0; sete %1"
119  :"=m" (v->counter), "=qm" (c)
120  :"m" (v->counter) : "memory");
121  return c != 0;
122 }
123 
124 static __inline__ int oro_atomic_add_negative(int i, oro_atomic_t *v)
125 {
126  unsigned char c;
127 
128  __asm__ __volatile__(
129  ORO_LOCK "addl %2,%0; sets %1"
130  :"=m" (v->counter), "=qm" (c)
131  :"ir" (i), "m" (v->counter) : "memory");
132  return c;
133 }
134 
135 #ifndef CONFIG_FORCE_UP
136 #define ORO_LOCK_PREFIX "lock ; "
137 #else
138 #define ORO_LOCK_PREFIX ""
139 #endif
140 
141 struct oro__xchg_dummy { unsigned long a[100]; };
142 #define oro__xg(x) ((struct oro__xchg_dummy *)(x))
143 
144 static inline unsigned long __oro_cmpxchg(volatile void *ptr, unsigned long old,
145  unsigned long _new, int size)
146 {
147  unsigned long prev;
148  switch (size) {
149  case 1:
150  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgb %b1,%2"
151  : "=a"(prev)
152  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
153  : "memory");
154  return prev;
155  case 2:
156  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgw %w1,%2"
157  : "=a"(prev)
158  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
159  : "memory");
160  return prev;
161  case 4:
162  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgl %k1,%2"
163  : "=a"(prev)
164  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
165  : "memory");
166  return prev;
167  case 8:
168  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgq %1,%2"
169  : "=a"(prev)
170  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
171  : "memory");
172  return prev;
173 
174  }
175  return old;
176 }
177 
178 #define oro_cmpxchg(ptr,o,n)\
179  ((__typeof__(*(ptr)))__oro_cmpxchg((ptr),(unsigned long)(o),\
180  (unsigned long)(n),sizeof(*(ptr))))
181 
182 #undef ORO_LOCK_PREFIX
183 #undef ORO_LOCK
184 #endif
#define oro__xg(x)
Definition: oro_arch.h:142
unsigned long a[100]
Definition: oro_arch.h:124
int oro_atomic_inc_and_test(oro_atomic_t *a)
Increment a atomically and test for zero.
volatile int counter
Definition: oro_arch.h:50
#define ORO_LOCK_PREFIX
Definition: oro_arch.h:136
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
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.
#define oro_atomic_add_negative(a, v)
Definition: oro_atomic.h:160
#define ORO_LOCK
Definition: oro_arch.h:45
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.