root/trunk/libffado/src/libutil/Atomic.h

Revision 1681, 5.8 kB (checked in by arnonym, 14 years ago)

Apply the atomic.patch of #197 to trunk.

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 /*
25  * Copied from the jackd/jackdmp sources
26  * function names changed in order to avoid naming problems when using this in
27  * a jackd backend.
28  */
29
30 /* Original license:
31  *
32  *  Copyright (C) 2004-2006 Grame
33  *
34  *  This program is free software; you can redistribute it and/or modify
35  *  it under the terms of the GNU General Public License as published by
36  *  the Free Software Foundation; either version 2 of the License, or
37  *  (at your option) version 3 of the License.
38  *
39  *  This program is distributed in the hope that it will be useful,
40  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
41  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42  *  GNU General Public License for more details.
43  *
44  *  You should have received a copy of the GNU General Public License
45  *  along with this program; if not, write to the Free Software
46  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
47  *
48  */
49
50 #ifndef __FFADOATOMIC__
51 #define __FFADOATOMIC__
52
53 #include <stdint.h>
54
55 #if defined(__APPLE__)
56
57 #if defined(__ppc__)
58
59 static inline int CAS(register uint32_t value, register uint32_t newvalue, register volatile void* addr)
60 {
61     register int result;
62     asm volatile (
63         "# CAS                    \n"
64         "    lwarx    r0, 0, %1    \n"         // creates a reservation on addr
65         "    cmpw    r0, %2        \n"            //  test value at addr
66         "    bne-    1f          \n"
67         "    sync                \n"         //  synchronize instructions
68         "    stwcx.    %3, 0, %1    \n"         //  if the reservation is not altered
69         //  stores the new value at addr
70         "    bne-    1f          \n"
71         "   li      %0, 1       \n"
72         "    b        2f          \n"
73         "1:                     \n"
74         "   li      %0, 0       \n"
75         "2:                     \n"
76         : "=r" (result)
77         : "r" (addr), "r" (value), "r" (newvalue)
78         : "r0"
79         );
80     return result;
81 }
82
83 #endif
84
85 #if defined(__i386__) || defined(__x86_64__)
86
87 #ifdef __SMP__
88 #    define LOCK "lock ; "
89 #else
90 #    define LOCK ""
91 #endif
92
93 static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile void* addr)
94 {
95     register char ret;
96     __asm__ __volatile__ (
97         "# CAS \n\t"
98         LOCK "cmpxchg %2, (%1) \n\t"
99         "sete %0               \n\t"
100         : "=a" (ret)
101         : "c" (addr), "d" (newvalue), "a" (value)
102         );
103     return ret;
104 }
105
106 #endif
107
108 #endif
109
110 #ifdef __linux__
111
112 #ifdef __PPC__
113
114 static inline int CAS(register uint32_t value, register uint32_t newvalue, register volatile void* addr)
115 {
116     register int result;
117     register uint32_t tmp;
118     asm volatile (
119         "# CAS                    \n"
120         "    lwarx    %4, 0, %1    \n"         // creates a reservation on addr
121         "    cmpw    %4, %2        \n"        //  test value at addr
122         "    bne-    1f          \n"
123         "    sync                \n"         //  synchronize instructions
124         "    stwcx.    %3, 0, %1    \n"         //  if the reservation is not altered
125         //  stores the new value at addr
126         "    bne-    1f          \n"
127         "   li      %0, 1       \n"
128         "    b        2f          \n"
129         "1:                     \n"
130         "   li      %0, 0       \n"
131         "2:                     \n"
132         : "=r" (result)
133         : "r" (addr), "r" (value), "r" (newvalue), "r" (tmp)
134         );
135     return result;
136 }
137
138 #endif
139
140 #if defined(__i386__) || defined(__x86_64__)
141
142 #ifdef __SMP__
143 #    define LOCK "lock ; "
144 #else
145 #    define LOCK ""
146 #endif
147
148 static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile void* addr)
149 {
150     register char ret;
151     __asm__ __volatile__ (
152         "# CAS \n\t"
153         LOCK "cmpxchg %2, (%1) \n\t"
154         "sete %0               \n\t"
155         : "=a" (ret)
156         : "c" (addr), "d" (newvalue), "a" (value)
157         );
158     return ret;
159 }
160
161 #else
162 #warning using builtin gcc (version >4.1) atomic
163
164 static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile int32_t* addr)
165 {
166     return __sync_bool_compare_and_swap (addr, value, newvalue);
167 }
168
169 #endif
170
171 #endif
172
173 static inline long INC_ATOMIC(volatile int32_t* val)
174 {
175     int32_t actual;
176     do {
177         actual = *val;
178     } while (!CAS(actual, actual + 1, val));
179     return actual;
180 }
181
182 static inline long DEC_ATOMIC(volatile int32_t* val)
183 {
184     int32_t actual;
185     do {
186         actual = *val;
187     } while (!CAS(actual, actual - 1, val));
188     return actual;
189 }
190
191 static inline long ADD_ATOMIC(volatile int32_t* val, int32_t addval)
192 {
193     int32_t actual;
194     do {
195         actual = *val;
196     } while (!CAS(actual, actual + addval, val));
197     return actual;
198 }
199
200 static inline long SUBSTRACT_ATOMIC(volatile int32_t* val, int32_t addval)
201 {
202     int32_t actual;
203     do {
204         actual = *val;
205     } while (!CAS(actual, actual - addval, val));
206     return actual;
207 }
208
209 static inline long ZERO_ATOMIC(volatile int32_t* val)
210 {
211     int32_t actual;
212     do {
213         actual = *val;
214     } while (!CAS(actual, 0, val));
215     return actual;
216 }
217
218 #endif // __FFADO_ATOMIC__
219
Note: See TracBrowser for help on using the browser.