1/*
2 * Copyright © 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Author: Ryan Lortie <desrt@desrt.ca>
21 */
22
23#ifndef __G_BITLOCK_H__
24#define __G_BITLOCK_H__
25
26#include <glib/gtypes.h>
27
28#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29#error "Only <glib.h> can be included directly."
30#endif
31
32G_BEGIN_DECLS
33
34GLIB_AVAILABLE_IN_ALL
35void g_bit_lock (volatile gint *address,
36 gint lock_bit);
37GLIB_AVAILABLE_IN_2_86
38void g_bit_lock_and_get (gint *address,
39 guint lock_bit,
40 gint *out_val);
41
42GLIB_AVAILABLE_IN_ALL
43gboolean g_bit_trylock (volatile gint *address,
44 gint lock_bit);
45GLIB_AVAILABLE_IN_ALL
46void g_bit_unlock (volatile gint *address,
47 gint lock_bit);
48
49GLIB_AVAILABLE_IN_2_86
50void g_bit_unlock_and_set (gint *address,
51 guint lock_bit,
52 gint new_val,
53 gint preserve_mask);
54
55GLIB_AVAILABLE_IN_ALL
56void g_pointer_bit_lock (volatile void *address,
57 gint lock_bit);
58
59GLIB_AVAILABLE_IN_2_80
60void g_pointer_bit_lock_and_get (gpointer address,
61 guint lock_bit,
62 guintptr *out_ptr);
63
64GLIB_AVAILABLE_IN_ALL
65gboolean g_pointer_bit_trylock (volatile void *address,
66 gint lock_bit);
67GLIB_AVAILABLE_IN_ALL
68void g_pointer_bit_unlock (volatile void *address,
69 gint lock_bit);
70
71GLIB_AVAILABLE_IN_2_80
72gpointer g_pointer_bit_lock_mask_ptr (gpointer ptr,
73 guint lock_bit,
74 gboolean set,
75 guintptr preserve_mask,
76 gpointer preserve_ptr);
77
78GLIB_AVAILABLE_IN_2_80
79void g_pointer_bit_unlock_and_set (void *address,
80 guint lock_bit,
81 gpointer ptr,
82 guintptr preserve_mask);
83
84#ifdef __GNUC__
85
86#define g_pointer_bit_lock(address, lock_bit) \
87 (G_GNUC_EXTENSION ({ \
88 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
89 g_pointer_bit_lock ((address), (lock_bit)); \
90 }))
91
92#define g_pointer_bit_lock_and_get(address, lock_bit, out_ptr) \
93 (G_GNUC_EXTENSION ({ \
94 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
95 g_pointer_bit_lock_and_get ((address), (lock_bit), (out_ptr)); \
96 }))
97
98#define g_pointer_bit_trylock(address, lock_bit) \
99 (G_GNUC_EXTENSION ({ \
100 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
101 g_pointer_bit_trylock ((address), (lock_bit)); \
102 }))
103
104#define g_pointer_bit_unlock(address, lock_bit) \
105 (G_GNUC_EXTENSION ({ \
106 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
107 g_pointer_bit_unlock ((address), (lock_bit)); \
108 }))
109
110#define g_pointer_bit_unlock_and_set(address, lock_bit, ptr, preserve_mask) \
111 (G_GNUC_EXTENSION ({ \
112 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
113 g_pointer_bit_unlock_and_set ((address), (lock_bit), (ptr), (preserve_mask)); \
114 }))
115
116#endif
117
118G_END_DECLS
119
120#endif /* __G_BITLOCK_H_ */
121