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_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__ 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  __asm__ __volatile__(
78  ORO_LOCK "incl %0"
79  :"=m" (v->counter)
80  :"m" (v->counter));
81 }
82 
84 {
85  __asm__ __volatile__(
86  ORO_LOCK "decl %0"
87  :"=m" (v->counter)
88  :"m" (v->counter));
89 }
90 
92 {
93  unsigned char c;
94 
95  __asm__ __volatile__(
96  ORO_LOCK "decl %0; sete %1"
97  :"=m" (v->counter), "=qm" (c)
98  :"m" (v->counter) : "memory");
99  return c != 0;
100 }
101 
103 {
104  unsigned char c;
105 
106  __asm__ __volatile__(
107  ORO_LOCK "incl %0; sete %1"
108  :"=m" (v->counter), "=qm" (c)
109  :"m" (v->counter) : "memory");
110  return c != 0;
111 }
112 
113 #define smp_mb__before_oro_atomic_dec() barrier()
114 #define smp_mb__after_oro_atomic_dec() barrier()
115 #define smp_mb__before_oro_atomic_inc() barrier()
116 #define smp_mb__after_oro_atomic_inc() barrier()
117 
118 #ifndef CONFIG_FORCE_UP
119 #define ORO_LOCK_PREFIX "lock ; "
120 #else
121 #define ORO_LOCK_PREFIX ""
122 #endif
123 
124 struct oro__xchg_dummy { unsigned long a[100]; };
125 #define oro__xg(x) ((struct oro__xchg_dummy *)(x))
126 
127 static inline unsigned long __oro_cmpxchg(volatile void *ptr, unsigned long old,
128  unsigned long _new, int size)
129 {
130  unsigned long prev;
131  switch (size) {
132  case 1:
133  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgb %b1,%2"
134  : "=a"(prev)
135  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
136  : "memory");
137  return prev;
138  case 2:
139  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgw %w1,%2"
140  : "=a"(prev)
141  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
142  : "memory");
143  return prev;
144  case 4:
145  __asm__ __volatile__(ORO_LOCK_PREFIX "cmpxchgl %1,%2"
146  : "=a"(prev)
147  : "q"(_new), "m"(*oro__xg(ptr)), "0"(old)
148  : "memory");
149  return prev;
150  }
151  return old;
152 }
153 
154 #define oro_cmpxchg(ptr,o,n)\
155  ((__typeof__(*(ptr)))__oro_cmpxchg((ptr),(unsigned long)(o),\
156  (unsigned long)(n),sizeof(*(ptr))))
157 
158 #undef ORO_LOCK
159 #undef ORO_LOCK_PREFIX
160 #endif
unsigned long a[100]
Definition: oro_arch.h:124
#define oro__xg(x)
Definition: oro_arch.h:125
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:119
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)
Substract 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
void oro_atomic_add(oro_atomic_t *a, int n)
Add n to a.