syslog-ng source
atomic-gssize.h
Go to the documentation of this file.
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;
36 
37 static inline gssize
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
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
50 atomic_gssize_inc(atomic_gssize *a)
51 {
52  return g_atomic_pointer_add(&a->value, 1);
53 }
54 
55 static inline gssize
56 atomic_gssize_dec(atomic_gssize *a)
57 {
58  return g_atomic_pointer_add(&a->value, -1);
59 }
60 
61 static inline gssize
62 atomic_gssize_get(atomic_gssize *a)
63 {
64  return (gssize)g_atomic_pointer_get(&a->value);
65 }
66 
67 static inline void
68 atomic_gssize_set(atomic_gssize *a, gssize value)
69 {
70  g_atomic_pointer_set(&a->value, value);
71 }
72 
73 static inline gsize
74 atomic_gssize_get_unsigned(atomic_gssize *a)
75 {
76  return (gsize)g_atomic_pointer_get(&a->value);
77 }
78 
79 static inline gssize
80 atomic_gssize_racy_get(atomic_gssize *a)
81 {
82  return a->value;
83 }
84 
85 static inline gsize
86 atomic_gssize_racy_get_unsigned(atomic_gssize *a)
87 {
88  return (gsize)a->value;
89 }
90 
91 static inline void
92 atomic_gssize_racy_set(atomic_gssize *a, gssize value)
93 {
94  a->value = value;
95 }
96 
97 static inline gsize
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
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
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
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
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, oldval, value))
127  {
128  oldval = atomic_gssize_get(a);
129  }
130 
131  return oldval;
132 }
133 #endif
G_STATIC_ASSERT(sizeof(gssize)==sizeof(gpointer))
Definition: atomic-gssize.h:33
gssize value
Definition: atomic-gssize.h:34
GString * value
Definition: test_decode.c:28