2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
40 typedef struct xatomic {
44 #define MAX64 (0xffffffffffffffffUL)
45 #define MAX128 (~(__uint128_t)0)
46 #define Z128 ((__uint128_t)0)
47 #define U128 ((__uint128_t)1)
49 #define declare_cas(at, var) \
50 uint64_t var, __xatomic_val_##var; \
51 __uint128_t __xatomic_serial_##var; \
52 xatomic *__xatomic_##var = (at)
54 #define cas_read(var) \
55 __xatomic_##var##_restart: \
56 __xatomic_serial_##var = ((__xatomic_##var)->value >> 64); \
57 var = __xatomic_val_##var = \
58 ((__xatomic_##var)->value & (0xffffffffffffffffUL)
60 #define cas_begin(at, var) \
61 uint64_t var, __xatomic_val_##var; \
62 xatomic *__xatomic_##var = (at); \
63 __xatomic_##var##_restart: \
64 var = __xatomic_val_##var = (__xatomic_##var)->value
66 #define cas_update(var) \
67 __sync_bool_compare_and_swap ( \
68 &(__xatomic_##var)->value, \
69 __xatomic_val_##var, \
73 #define cas_restart(var) \
74 goto __xatomic_##var##_restart
77 static inline uint64_t xatomic_read(xatomic *atomic)
79 return (uint64_t)atomic->value;
82 static inline void xatomic_write(xatomic *atomic, uint64_t newval)
84 cas_begin(atomic, val);
90 static inline uint64_t xatomic_inc(xatomic *atomic, uint64_t inc)
94 cas_begin(atomic, val);
103 static inline uint64_t xatomic_dec(xatomic *atomic, uint64_t dec)
107 cas_begin(atomic, val);
110 if (!cas_update(val))