2010-01-28 17:32:07 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2008 Ryan Lortie
|
|
|
|
* Copyright © 2010 Codethink Limited
|
|
|
|
*
|
2022-05-18 10:15:38 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2010-01-28 17:32:07 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-01-28 17:32:07 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-01-28 17:32:07 +01:00
|
|
|
*
|
|
|
|
* Author: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __G_BITLOCK_H__
|
|
|
|
#define __G_BITLOCK_H__
|
|
|
|
|
|
|
|
#include <glib/gtypes.h>
|
|
|
|
|
2011-10-12 06:24:46 +02:00
|
|
|
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
|
2010-01-28 17:32:07 +01:00
|
|
|
#error "Only <glib.h> can be included directly."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
G_BEGIN_DECLS
|
|
|
|
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2010-01-30 18:13:50 +01:00
|
|
|
void g_bit_lock (volatile gint *address,
|
2010-01-28 17:32:07 +01:00
|
|
|
gint lock_bit);
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2010-01-30 18:13:50 +01:00
|
|
|
gboolean g_bit_trylock (volatile gint *address,
|
2010-01-28 17:32:07 +01:00
|
|
|
gint lock_bit);
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2010-01-30 18:13:50 +01:00
|
|
|
void g_bit_unlock (volatile gint *address,
|
2010-01-28 17:32:07 +01:00
|
|
|
gint lock_bit);
|
|
|
|
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2011-05-25 11:08:20 +02:00
|
|
|
void g_pointer_bit_lock (volatile void *address,
|
|
|
|
gint lock_bit);
|
2024-01-05 14:05:58 +01:00
|
|
|
|
|
|
|
GLIB_AVAILABLE_IN_2_80
|
|
|
|
void g_pointer_bit_lock_and_get (gpointer address,
|
|
|
|
guint lock_bit,
|
|
|
|
guintptr *out_ptr);
|
|
|
|
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2011-05-25 11:08:20 +02:00
|
|
|
gboolean g_pointer_bit_trylock (volatile void *address,
|
|
|
|
gint lock_bit);
|
2012-12-06 20:04:59 +01:00
|
|
|
GLIB_AVAILABLE_IN_ALL
|
2011-05-25 11:08:20 +02:00
|
|
|
void g_pointer_bit_unlock (volatile void *address,
|
|
|
|
gint lock_bit);
|
|
|
|
|
gbitlock: add g_pointer_bit_unlock_and_set() and g_pointer_bit_lock_mask_ptr()
The existing g_pointer_bit_lock() and g_pointer_bit_unlock() API
requires the user to understand/reimplement how bits of the pointer get
mangled. Add helper functions for that.
The useful thing to do with g_pointer_bit_lock() API is to get/set
pointers while having it locked. For example, to set the pointer a user
can do:
g_pointer_bit_lock (&lockptr, lock_bit);
ptr2 = set_bit_pointer_as_if_locked(ptr, lock_bit);
g_atomic_pointer_set (&lockptr, ptr2);
g_pointer_bit_unlock (&lockptr, lock_bit);
That has several problems:
- it requires one extra atomic operations (3 instead of 2, in the
non-contended case).
- the first g_atomic_pointer_set() already wakes blocked threads,
which find themselves still being locked and needs to go back to
sleep.
- the user needs to re-implement how bit-locking mangles the pointer so
that it looks as if it were locked.
- while the user tries to re-implement what glib does to mangle the
pointer for bitlocking, there is no immediate guarantee that they get
it right.
Now we can do instead:
g_pointer_bit_lock(&lockptr, lock_bit);
g_pointer_bit_unlock_and_set(&lockptr, lock_bit, ptr, 0);
This will also emit a critical if @ptr has the locked bit set.
g_pointer_bit_lock() really only works with pointers that have a certain
alignment, and the lowest bits unset. Otherwise, there is no space to
encode both the locking and all pointer values. The new assertion helps
to catch such bugs.
Also, g_pointer_bit_lock_mask_ptr() is here, so we can do:
g_pointer_bit_lock(&lockptr, lock_bit);
/* set a pointer separately, when g_pointer_bit_unlock_and_set() is unsuitable. */
g_atomic_pointer_set(&lockptr, g_pointer_bit_lock_mask_ptr(ptr, lock_bit, TRUE, 0, NULL));
...
g_pointer_bit_unlock(&lockptr, lock_bit);
and:
g_pointer_bit_lock(&lockptr, lock_bit);
/* read the real pointer after getting the lock. */
ptr = g_pointer_bit_lock_mask_ptr(lockptr, lock_bit, FALSE, 0, NULL));
...
g_pointer_bit_unlock(&lockptr, lock_bit);
2023-12-23 21:43:47 +01:00
|
|
|
GLIB_AVAILABLE_IN_2_80
|
|
|
|
gpointer g_pointer_bit_lock_mask_ptr (gpointer ptr,
|
|
|
|
guint lock_bit,
|
|
|
|
gboolean set,
|
|
|
|
guintptr preserve_mask,
|
|
|
|
gpointer preserve_ptr);
|
|
|
|
|
|
|
|
GLIB_AVAILABLE_IN_2_80
|
|
|
|
void g_pointer_bit_unlock_and_set (void *address,
|
|
|
|
guint lock_bit,
|
|
|
|
gpointer ptr,
|
|
|
|
guintptr preserve_mask);
|
|
|
|
|
2011-05-25 11:08:20 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
|
|
|
#define g_pointer_bit_lock(address, lock_bit) \
|
|
|
|
(G_GNUC_EXTENSION ({ \
|
|
|
|
G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
|
|
|
|
g_pointer_bit_lock ((address), (lock_bit)); \
|
|
|
|
}))
|
|
|
|
|
2024-01-05 14:05:58 +01:00
|
|
|
#define g_pointer_bit_lock_and_get(address, lock_bit, out_ptr) \
|
|
|
|
(G_GNUC_EXTENSION ({ \
|
|
|
|
G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
|
|
|
|
g_pointer_bit_lock_and_get ((address), (lock_bit), (out_ptr)); \
|
|
|
|
}))
|
|
|
|
|
2011-05-25 11:08:20 +02:00
|
|
|
#define g_pointer_bit_trylock(address, lock_bit) \
|
|
|
|
(G_GNUC_EXTENSION ({ \
|
|
|
|
G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
|
|
|
|
g_pointer_bit_trylock ((address), (lock_bit)); \
|
|
|
|
}))
|
|
|
|
|
|
|
|
#define g_pointer_bit_unlock(address, lock_bit) \
|
|
|
|
(G_GNUC_EXTENSION ({ \
|
|
|
|
G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
|
|
|
|
g_pointer_bit_unlock ((address), (lock_bit)); \
|
|
|
|
}))
|
|
|
|
|
gbitlock: add g_pointer_bit_unlock_and_set() and g_pointer_bit_lock_mask_ptr()
The existing g_pointer_bit_lock() and g_pointer_bit_unlock() API
requires the user to understand/reimplement how bits of the pointer get
mangled. Add helper functions for that.
The useful thing to do with g_pointer_bit_lock() API is to get/set
pointers while having it locked. For example, to set the pointer a user
can do:
g_pointer_bit_lock (&lockptr, lock_bit);
ptr2 = set_bit_pointer_as_if_locked(ptr, lock_bit);
g_atomic_pointer_set (&lockptr, ptr2);
g_pointer_bit_unlock (&lockptr, lock_bit);
That has several problems:
- it requires one extra atomic operations (3 instead of 2, in the
non-contended case).
- the first g_atomic_pointer_set() already wakes blocked threads,
which find themselves still being locked and needs to go back to
sleep.
- the user needs to re-implement how bit-locking mangles the pointer so
that it looks as if it were locked.
- while the user tries to re-implement what glib does to mangle the
pointer for bitlocking, there is no immediate guarantee that they get
it right.
Now we can do instead:
g_pointer_bit_lock(&lockptr, lock_bit);
g_pointer_bit_unlock_and_set(&lockptr, lock_bit, ptr, 0);
This will also emit a critical if @ptr has the locked bit set.
g_pointer_bit_lock() really only works with pointers that have a certain
alignment, and the lowest bits unset. Otherwise, there is no space to
encode both the locking and all pointer values. The new assertion helps
to catch such bugs.
Also, g_pointer_bit_lock_mask_ptr() is here, so we can do:
g_pointer_bit_lock(&lockptr, lock_bit);
/* set a pointer separately, when g_pointer_bit_unlock_and_set() is unsuitable. */
g_atomic_pointer_set(&lockptr, g_pointer_bit_lock_mask_ptr(ptr, lock_bit, TRUE, 0, NULL));
...
g_pointer_bit_unlock(&lockptr, lock_bit);
and:
g_pointer_bit_lock(&lockptr, lock_bit);
/* read the real pointer after getting the lock. */
ptr = g_pointer_bit_lock_mask_ptr(lockptr, lock_bit, FALSE, 0, NULL));
...
g_pointer_bit_unlock(&lockptr, lock_bit);
2023-12-23 21:43:47 +01:00
|
|
|
#define g_pointer_bit_unlock_and_set(address, lock_bit, ptr, preserve_mask) \
|
|
|
|
(G_GNUC_EXTENSION ({ \
|
|
|
|
G_STATIC_ASSERT (sizeof *(address) == sizeof (gpointer)); \
|
|
|
|
g_pointer_bit_unlock_and_set ((address), (lock_bit), (ptr), (preserve_mask)); \
|
|
|
|
}))
|
|
|
|
|
2011-05-25 11:08:20 +02:00
|
|
|
#endif
|
|
|
|
|
2010-01-28 17:32:07 +01:00
|
|
|
G_END_DECLS
|
|
|
|
|
|
|
|
#endif /* __G_BITLOCK_H_ */
|