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_ALL
38gboolean g_bit_trylock (volatile gint *address,
39 gint lock_bit);
40GLIB_AVAILABLE_IN_ALL
41void g_bit_unlock (volatile gint *address,
42 gint lock_bit);
43
44GLIB_AVAILABLE_IN_ALL
45void g_pointer_bit_lock (volatile void *address,
46 gint lock_bit);
47
48GLIB_AVAILABLE_IN_2_80
49void g_pointer_bit_lock_and_get (gpointer address,
50 guint lock_bit,
51 guintptr *out_ptr);
52
53GLIB_AVAILABLE_IN_ALL
54gboolean g_pointer_bit_trylock (volatile void *address,
55 gint lock_bit);
56GLIB_AVAILABLE_IN_ALL
57void g_pointer_bit_unlock (volatile void *address,
58 gint lock_bit);
59
60GLIB_AVAILABLE_IN_2_80
61gpointer g_pointer_bit_lock_mask_ptr (gpointer ptr,
62 guint lock_bit,
63 gboolean set,
64 guintptr preserve_mask,
65 gpointer preserve_ptr);
66
67GLIB_AVAILABLE_IN_2_80
68void g_pointer_bit_unlock_and_set (void *address,
69 guint lock_bit,
70 gpointer ptr,
71 guintptr preserve_mask);
72
73#ifdef __GNUC__
74
75#define g_pointer_bit_lock(address, lock_bit) \
76 (G_GNUC_EXTENSION ({ \
77 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
78 g_pointer_bit_lock ((address), (lock_bit)); \
79 }))
80
81#define g_pointer_bit_lock_and_get(address, lock_bit, out_ptr) \
82 (G_GNUC_EXTENSION ({ \
83 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
84 g_pointer_bit_lock_and_get ((address), (lock_bit), (out_ptr)); \
85 }))
86
87#define g_pointer_bit_trylock(address, lock_bit) \
88 (G_GNUC_EXTENSION ({ \
89 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
90 g_pointer_bit_trylock ((address), (lock_bit)); \
91 }))
92
93#define g_pointer_bit_unlock(address, lock_bit) \
94 (G_GNUC_EXTENSION ({ \
95 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
96 g_pointer_bit_unlock ((address), (lock_bit)); \
97 }))
98
99#define g_pointer_bit_unlock_and_set(address, lock_bit, ptr, preserve_mask) \
100 (G_GNUC_EXTENSION ({ \
101 G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
102 g_pointer_bit_unlock_and_set ((address), (lock_bit), (ptr), (preserve_mask)); \
103 }))
104
105#endif
106
107G_END_DECLS
108
109#endif /* __G_BITLOCK_H_ */
110