Merge branch 'hotfix-0.3.5'
[archipelago] / xseg / xtypes / xatomic.h
1 /*
2  * Copyright 2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
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.
15  *
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.
28  *
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.
33  */
34
35 #ifndef _XATOMIC_H
36 #define _XATOMIC_H
37
38 #include <stdint.h>
39
40 typedef struct xatomic {
41         __uint128_t value;
42 } xatomic;
43
44 #define MAX64   (0xffffffffffffffffUL)
45 #define MAX128  (~(__uint128_t)0)
46 #define Z128    ((__uint128_t)0)
47 #define U128    ((__uint128_t)1)
48
49 #define declare_cas(at, var)                                            \
50         uint64_t var, __xatomic_val_##var;                              \
51         __uint128_t __xatomic_serial_##var;                             \
52         xatomic *__xatomic_##var = (at)
53
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)
59
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
65
66 #define cas_update(var)                         \
67         __sync_bool_compare_and_swap    (       \
68                 &(__xatomic_##var)->value,      \
69                 __xatomic_val_##var,            \
70                 var                             \
71         )
72
73 #define cas_restart(var)        \
74         goto __xatomic_##var##_restart
75
76
77 static inline uint64_t xatomic_read(xatomic *atomic)
78 {
79         return (uint64_t)atomic->value;
80 }
81
82 static inline void xatomic_write(xatomic *atomic, uint64_t newval)
83 {
84         cas_begin(atomic, val);
85         val = newval;
86         if (!cas_update(val))
87                 cas_restart(val);
88 }
89
90 static inline uint64_t xatomic_inc(xatomic *atomic, uint64_t inc)
91 {
92         uint64_t retval;
93
94         cas_begin(atomic, val);
95         retval = val;
96         val += inc;
97         if (!cas_update(val))
98                 cas_restart(val);
99
100         return retval;
101 }
102
103 static inline uint64_t xatomic_dec(xatomic *atomic, uint64_t dec)
104 {
105         uint64_t retval;
106
107         cas_begin(atomic, val);
108         retval = val;
109         val -= dec;
110         if (!cas_update(val))
111                 cas_restart(val);
112
113         return retval;
114 }
115
116 #endif