glib/glib/gbitlock.h

110 lines
4.3 KiB
C
Raw Normal View History

/*
* Copyright © 2008 Ryan Lortie
* Copyright © 2010 Codethink Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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
* version 2.1 of the License, or (at your option) any later version.
*
* 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/>.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#ifndef __G_BITLOCK_H__
#define __G_BITLOCK_H__
#include <glib/gtypes.h>
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
#error "Only <glib.h> can be included directly."
#endif
G_BEGIN_DECLS
GLIB_AVAILABLE_IN_ALL
2010-01-30 18:13:50 +01:00
void g_bit_lock (volatile gint *address,
gint lock_bit);
GLIB_AVAILABLE_IN_ALL
2010-01-30 18:13:50 +01:00
gboolean g_bit_trylock (volatile gint *address,
gint lock_bit);
GLIB_AVAILABLE_IN_ALL
2010-01-30 18:13:50 +01:00
void g_bit_unlock (volatile gint *address,
gint lock_bit);
GLIB_AVAILABLE_IN_ALL
void g_pointer_bit_lock (volatile void *address,
gint lock_bit);
GLIB_AVAILABLE_IN_2_80
void g_pointer_bit_lock_and_get (gpointer address,
guint lock_bit,
guintptr *out_ptr);
GLIB_AVAILABLE_IN_ALL
gboolean g_pointer_bit_trylock (volatile void *address,
gint lock_bit);
GLIB_AVAILABLE_IN_ALL
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);
#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)); \
}))
#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)); \
}))
#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)); \
}))
#endif
G_END_DECLS
#endif /* __G_BITLOCK_H_ */