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

Revision 445, 4.7 kB (checked in by pieterpalmers, 14 years ago)

* name change from FreeBoB to FFADO
* replaced tabs by 4 spaces
* got rid of end-of-line spaces
* made all license and copyrights conform

library becomes LGPL, apps become GPL
explicitly state LGPL v2.1 and GPL v2 (don't like v3 draft)

copyrights are 2005-2007 Daniel & Pieter
except for the MotU stuff (C) Jonathan, Pieter

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