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

Revision 1933, 2.9 kB (checked in by adi, 13 years ago)

Use gcc builtin atomic functions. Closes #280.

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
56 static inline char CAS(volatile uint32_t value, uint32_t newvalue, volatile int32_t* addr)
57 {
58     return __sync_bool_compare_and_swap (addr, value, newvalue);
59 }
60
61 static inline long INC_ATOMIC(volatile int32_t* val)
62 {
63     int32_t actual;
64     do {
65         actual = *val;
66     } while (!CAS(actual, actual + 1, val));
67     return actual;
68 }
69
70 static inline long DEC_ATOMIC(volatile int32_t* val)
71 {
72     int32_t actual;
73     do {
74         actual = *val;
75     } while (!CAS(actual, actual - 1, val));
76     return actual;
77 }
78
79 static inline long ADD_ATOMIC(volatile int32_t* val, int32_t addval)
80 {
81     int32_t actual;
82     do {
83         actual = *val;
84     } while (!CAS(actual, actual + addval, val));
85     return actual;
86 }
87
88 static inline long SUBSTRACT_ATOMIC(volatile int32_t* val, int32_t addval)
89 {
90     int32_t actual;
91     do {
92         actual = *val;
93     } while (!CAS(actual, actual - addval, val));
94     return actual;
95 }
96
97 static inline long ZERO_ATOMIC(volatile int32_t* val)
98 {
99     int32_t actual;
100     do {
101         actual = *val;
102     } while (!CAS(actual, 0, val));
103     return actual;
104 }
105
106 #endif // __FFADO_ATOMIC__
107
Note: See TracBrowser for help on using the browser.