Orocos Real-Time Toolkit  2.9.0
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_I386__
42 #define __ORO_ARCH_I386__
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__ int oro_atomic_add_return( oro_atomic_t *v, int i)
68 {
69  /* Modern 486+ processor */
70  int __i = i;
71  __asm__ __volatile__(
72  ORO_LOCK "xaddl %0, %1"
73  : "+r" (i), "+m" (v->counter)
74  : : "memory");
75  return i + __i;
76 }
77 
78 static __inline__ void oro_atomic_sub( oro_atomic_t *v, int i)
79 {
80  __asm__ __volatile__(
81  ORO_LOCK "subl %1,%0"
82  :"=m" (v->counter)
83  :"ir" (i), "m" (v->counter));
84 }
85 
86 static __inline__ int oro_atomic_sub_return( oro_atomic_t *v, int i)
87 {
88  return oro_atomic_add_return( v, -i );
89 }
90 
92 {
93  __asm__ __volatile__(
94  ORO_LOCK "incl %0"
95  :"=m" (v->counter)
96  :"m" (v->counter));
97 }
98 
100 {
101  __asm__ __volatile__(
102  ORO_LOCK "decl %0"
103  :"=m" (v->counter)
104  :"m" (v->counter));
105 }
106 
107 #define oro_atomic_inc_return(v) (oro_atomic_add_return(v, 1))
108 #define oro_atomic_dec_return(v) (oro_atomic_sub_return(v, 1))
109 
111 {
112  unsigned char c;
113 
114  __asm__ __volatile__(
115  ORO_LOCK "decl %0; sete %1"
116  :"=m" (v->counter), "=qm" (c)
117  :"m" (v->counter) : "memory");
118  return c != 0;
119 }
120 
122 {
123  unsigned char c;
124 
125  __asm__ __volatile__(
126  ORO_LOCK "incl %0; sete %1"
127  :"=m" (v->counter), "=qm" (c)
128  :"m" (v->counter) : "memory");
129  return c != 0;
130 }
131 
132 #define smp_mb__before_oro_atomic_dec() barrier()
133 #define smp_mb__after_oro_atomic_dec() barrier()
134 #define smp_mb__before_oro_atomic_inc() barrier()
135 #define smp_mb__after_oro_atomic_inc() barrier()
136 
137 #ifndef CONFIG_FORCE_UP
138 #define ORO_LOCK_PREFIX "lock ; "
139 #else
140 #define ORO_LOCK_PREFIX ""
141 #endif
142 
143 struct oro__xchg_dummy { unsigned long a[100]; };
144 #define oro__xg(x) ((struct oro__xchg_dummy *)(x))
145 
146 static inline unsigned long __oro_cmpxchg(volatile void *ptr, unsigned long old,
147  unsigned long _new, int size)
148 {
149  unsigned long prev;
150  switch (size) {
151  case 1:
152  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgb %b1,%2"
153  : "=a"(prev)
154  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
155  : "memory");
156  return prev;
157  case 2:
158  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgw %w1,%2"
159  : "=a"(prev)
160  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
161  : "memory");
162  return prev;
163  case 4:
164  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgl %1,%2"
165  : "=a"(prev)
166  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
167  : "memory");
168  return prev;
169  }
170  return old;
171 }
172 
173 #define oro_cmpxchg(ptr,o,n)\
174  ((__typeof__(*(ptr)))__oro_cmpxchg((ptr),(unsigned long)(o),\
175  (unsigned long)(n),sizeof(*(ptr))))
176 
177 #undef ORO_LOCK
178 #undef ORO_LOCK_PREFIX
179 #endif
unsigned long a[100]
Definition: oro_arch.h:143
#define oro__xg(x)
Definition: oro_arch.h:144
int oro_atomic_inc_and_test(oro_atomic_t *a)
Increment a atomically and test for zero.
#define ORO_LOCK
Definition: oro_arch.h:45
#define ORO_LOCK_PREFIX
Definition: oro_arch.h:138
int oro_atomic_sub_return(int n, oro_atomic_t *a, int n)
Subtract n from a and return the new value.
volatile int counter
Definition: oro_arch.h:50
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)
Subtract n from a.
int oro_atomic_dec_and_test(oro_atomic_t *a)
Decrement a atomically and test for zero.
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.
void oro_atomic_add(oro_atomic_t *a, int n)
Add n to a.