1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-01 23:30:28 +02:00
guile/libguile/atomics-internal.h
Andy Wingo b0ce014801 Inline thread wakeup data into "struct scm_thread"
This way we don't allocate an untagged wake data, and we don't need a
type tag.  On the other hand we have to roll a more complicated seqlock,
but that's fine.

Also switch to require C11 atomics.

* libguile/atomics-internal.h: Remove fallback for when we don't have
C11 atomics.
(scm_atomic_ref_uint32, scm_atomic_swap_uint32, scm_atomic_set_uint32):
New helpers.
* libguile/threads-internal.h:
* libguile/async.h:
* libguile/async.c: Inline the thread wake data.  Happily, waking a
remote thread is still wait-free from both sides.
2025-06-25 16:00:07 +02:00

126 lines
3.5 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SCM_ATOMICS_INTERNAL_H
#define SCM_ATOMICS_INTERNAL_H
/* Copyright 2016,2018-2019,2025
Free Software Foundation, Inc.
This file is part of Guile.
Guile 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 3 of the License, or
(at your option) any later version.
Guile 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
License along with Guile. If not, see
<https://www.gnu.org/licenses/>. */
#include "scm.h"
#ifndef HAVE_STDATOMIC_H
#error Guile needs C11 stdatomic.h
#endif
#include <stdatomic.h>
static inline uint32_t
scm_atomic_ref_uint32 (uint32_t *loc)
{
atomic_uint_least32_t *a_loc = (atomic_uint_least32_t *) loc;
return atomic_load (a_loc);
}
static inline uint32_t
scm_atomic_swap_uint32 (uint32_t *loc, uint32_t val)
{
atomic_uint_least32_t *a_loc = (atomic_uint_least32_t *) loc;
return atomic_exchange (a_loc, val);
}
static inline void
scm_atomic_set_uint32 (uint32_t *loc, uint32_t val)
{
atomic_uint_least32_t *a_loc = (atomic_uint_least32_t *) loc;
atomic_store (a_loc, val);
}
static inline uint32_t
scm_atomic_subtract_uint32 (uint32_t *loc, uint32_t arg)
{
atomic_uint_least32_t *a_loc = (atomic_uint_least32_t *) loc;
return atomic_fetch_sub (a_loc, arg);
}
static inline _Bool
scm_atomic_compare_and_swap_uint32 (uint32_t *loc, uint32_t *expected,
uint32_t desired)
{
atomic_uint_least32_t *a_loc = (atomic_uint_least32_t *) loc;
return atomic_compare_exchange_weak (a_loc, expected, desired);
}
static inline size_t
scm_atomic_subtract_size (size_t *loc, size_t arg)
{
atomic_size_t *a_loc = (atomic_size_t *) loc;
return atomic_fetch_sub (a_loc, arg);
}
static inline void
scm_atomic_set_pointer (void **loc, void *val)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
atomic_store (a_loc, (uintptr_t) val);
}
static inline void *
scm_atomic_ref_pointer (void **loc)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
return (void *) atomic_load (a_loc);
}
static inline void *
scm_atomic_swap_pointer (void **loc, void *new_val)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
return (void *) atomic_exchange (a_loc, (uintptr_t) new_val);
}
static inline void
scm_atomic_set_bits (scm_t_bits *loc, scm_t_bits val)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
atomic_store (a_loc, val);
}
static inline void
scm_atomic_set_scm (SCM *loc, SCM val)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
atomic_store (a_loc, SCM_UNPACK (val));
}
static inline SCM
scm_atomic_ref_scm (SCM *loc)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
return SCM_PACK (atomic_load (a_loc));
}
static inline SCM
scm_atomic_swap_scm (SCM *loc, SCM val)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
return SCM_PACK (atomic_exchange (a_loc, SCM_UNPACK (val)));
}
static inline SCM
scm_atomic_compare_and_swap_scm (SCM *loc, SCM expected, SCM desired)
{
atomic_uintptr_t *a_loc = (atomic_uintptr_t *) loc;
SCM result = expected;
atomic_compare_exchange_strong (a_loc, (uintptr_t *) &result,
SCM_UNPACK (desired));
return result;
}
#endif /* SCM_ATOMICS_INTERNAL_H */