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 |
#endif |
---|
162 |
|
---|
163 |
#endif |
---|
164 |
|
---|
165 |
static inline long INC_ATOMIC(volatile int32_t* val) |
---|
166 |
{ |
---|
167 |
int32_t actual; |
---|
168 |
do { |
---|
169 |
actual = *val; |
---|
170 |
} while (!CAS(actual, actual + 1, val)); |
---|
171 |
return actual; |
---|
172 |
} |
---|
173 |
|
---|
174 |
static inline long DEC_ATOMIC(volatile int32_t* val) |
---|
175 |
{ |
---|
176 |
int32_t actual; |
---|
177 |
do { |
---|
178 |
actual = *val; |
---|
179 |
} while (!CAS(actual, actual - 1, val)); |
---|
180 |
return actual; |
---|
181 |
} |
---|
182 |
|
---|
183 |
static inline long ADD_ATOMIC(volatile int32_t* val, int32_t addval) |
---|
184 |
{ |
---|
185 |
int32_t actual; |
---|
186 |
do { |
---|
187 |
actual = *val; |
---|
188 |
} while (!CAS(actual, actual + addval, val)); |
---|
189 |
return actual; |
---|
190 |
} |
---|
191 |
|
---|
192 |
static inline long SUBSTRACT_ATOMIC(volatile int32_t* val, int32_t addval) |
---|
193 |
{ |
---|
194 |
int32_t actual; |
---|
195 |
do { |
---|
196 |
actual = *val; |
---|
197 |
} while (!CAS(actual, actual - addval, val)); |
---|
198 |
return actual; |
---|
199 |
} |
---|
200 |
|
---|
201 |
static inline long ZERO_ATOMIC(volatile int32_t* val) |
---|
202 |
{ |
---|
203 |
int32_t actual; |
---|
204 |
do { |
---|
205 |
actual = *val; |
---|
206 |
} while (!CAS(actual, 0, val)); |
---|
207 |
return actual; |
---|
208 |
} |
---|
209 |
|
---|
210 |
#endif // __FFADO_ATOMIC__ |
---|
211 |
|
---|