1 /*
2 * Copyright (c) 2002-2018 Balabit
3 * Copyright (c) 2018 Laszlo Budai <laszlo.budai@balabit.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * As an additional exemption you are allowed to compile & link against the
20 * OpenSSL libraries as published by the OpenSSL project. See the file
21 * COPYING for details.
22 *
23 */
24
25 #ifndef ATOMIC_GSSIZE_H_INCLUDED
26 #define ATOMIC_GSSIZE_H_INCLUDED
27
28 #include "syslog-ng.h"
29
30 G_STATIC_ASSERT(sizeof(gssize) == sizeof(gpointer));
31
32 typedef struct
33 {
34 gssize value;
35 } atomic_gssize;
36
37 static inline gssize
atomic_gssize_add(atomic_gssize * a,gssize add)38 atomic_gssize_add(atomic_gssize *a, gssize add)
39 {
40 return g_atomic_pointer_add(&a->value, add);
41 }
42
43 static inline gssize
atomic_gssize_sub(atomic_gssize * a,gssize sub)44 atomic_gssize_sub(atomic_gssize *a, gssize sub)
45 {
46 return g_atomic_pointer_add(&a->value, -1 * sub);
47 }
48
49 static inline gssize
atomic_gssize_inc(atomic_gssize * a)50 atomic_gssize_inc(atomic_gssize *a)
51 {
52 return g_atomic_pointer_add(&a->value, 1);
53 }
54
55 static inline gssize
atomic_gssize_dec(atomic_gssize * a)56 atomic_gssize_dec(atomic_gssize *a)
57 {
58 return g_atomic_pointer_add(&a->value, -1);
59 }
60
61 static inline gssize
atomic_gssize_get(atomic_gssize * a)62 atomic_gssize_get(atomic_gssize *a)
63 {
64 return (gssize)g_atomic_pointer_get(&a->value);
65 }
66
67 static inline void
atomic_gssize_set(atomic_gssize * a,gssize value)68 atomic_gssize_set(atomic_gssize *a, gssize value)
69 {
70 g_atomic_pointer_set(&a->value, value);
71 }
72
73 static inline gsize
atomic_gssize_get_unsigned(atomic_gssize * a)74 atomic_gssize_get_unsigned(atomic_gssize *a)
75 {
76 return (gsize)g_atomic_pointer_get(&a->value);
77 }
78
79 static inline gssize
atomic_gssize_racy_get(atomic_gssize * a)80 atomic_gssize_racy_get(atomic_gssize *a)
81 {
82 return a->value;
83 }
84
85 static inline gsize
atomic_gssize_racy_get_unsigned(atomic_gssize * a)86 atomic_gssize_racy_get_unsigned(atomic_gssize *a)
87 {
88 return (gsize)a->value;
89 }
90
91 static inline void
atomic_gssize_racy_set(atomic_gssize * a,gssize value)92 atomic_gssize_racy_set(atomic_gssize *a, gssize value)
93 {
94 a->value = value;
95 }
96
97 static inline gsize
atomic_gssize_or(atomic_gssize * a,gsize value)98 atomic_gssize_or(atomic_gssize *a, gsize value)
99 {
100 return g_atomic_pointer_or(&a->value, value);
101 }
102
103 static inline gsize
atomic_gssize_xor(atomic_gssize * a,gsize value)104 atomic_gssize_xor(atomic_gssize *a, gsize value)
105 {
106 return g_atomic_pointer_xor(&a->value, value);
107 }
108
109 static inline gsize
atomic_gssize_and(atomic_gssize * a,gsize value)110 atomic_gssize_and(atomic_gssize *a, gsize value)
111 {
112 return g_atomic_pointer_and(&a->value, value);
113 }
114
115 static inline gboolean
atomic_gssize_compare_and_exchange(atomic_gssize * a,gssize oldval,gssize newval)116 atomic_gssize_compare_and_exchange(atomic_gssize *a, gssize oldval, gssize newval)
117 {
118 return g_atomic_pointer_compare_and_exchange(&a->value, oldval, newval);
119 }
120
121 static inline gssize
atomic_gssize_set_and_get(atomic_gssize * a,gssize value)122 atomic_gssize_set_and_get(atomic_gssize *a, gssize value)
123 {
124 gssize oldval = atomic_gssize_get(a);
125
126 while (!atomic_gssize_compare_and_exchange(a,
127 oldval,
128 value))
129 {
130 oldval = atomic_gssize_get(a);
131 }
132
133 return oldval;
134 }
135 #endif
136