#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0-or-later # Copyright (C) 2026 Tsiry Sandratraina # Generates sys/wasm/include/atomic.h for the FreeBSD port (.agents/freebsd.md). import sys W = [8, 16, 32, 64] ALIAS = {"char": 8, "short": 16, "int": 32, "long": 64, "ptr": 64} ORD = {"": "__ATOMIC_SEQ_CST", "_acq": "__ATOMIC_ACQUIRE", "_rel": "__ATOMIC_RELEASE"} out = [] p = out.append p("""/* SPDX-License-Identifier: BSD-2-Clause */ /* * Copyright (c) 2026 Tsiry Sandratraina * * atomic(9) for FreeBSD/wasm on the compiler's __atomic builtins. Generated * by unkernel's kernel/tools/freebsd-atomic.py; edit that instead. * A UP kernel (no -matomics) lowers them to plain accesses, which is * enough: a CPU is one thread, interrupts are polled. An SMP kernel builds * with -matomics, where they are wasm atomics (sequentially consistent). */ #ifndef _MACHINE_ATOMIC_H_ #define _MACHINE_ATOMIC_H_ #include #define mb() __atomic_thread_fence(__ATOMIC_SEQ_CST) #define rmb() __atomic_thread_fence(__ATOMIC_SEQ_CST) #define wmb() __atomic_thread_fence(__ATOMIC_SEQ_CST) static __inline void atomic_thread_fence_acq(void) { __atomic_thread_fence(__ATOMIC_ACQUIRE); } static __inline void atomic_thread_fence_rel(void) { __atomic_thread_fence(__ATOMIC_RELEASE); } static __inline void atomic_thread_fence_acq_rel(void) { __atomic_thread_fence(__ATOMIC_ACQ_REL); } static __inline void atomic_thread_fence_seq_cst(void) { __atomic_thread_fence(__ATOMIC_SEQ_CST); } """) for w in W: t = f"uint{w}_t" p(f"/* {w}-bit */") for o, mo in ORD.items(): for name, builtin in (("add", "__atomic_fetch_add"), ("subtract", "__atomic_fetch_sub"), ("set", "__atomic_fetch_or"), ("clear", "__atomic_fetch_and")): v = "~v" if name == "clear" else "v" p(f"static __inline void atomic_{name}{o}_{w}(volatile {t} *p, {t} v) {{ (void){builtin}(p, {v}, {mo}); }}") p(f"static __inline int atomic_cmpset{o}_{w}(volatile {t} *p, {t} c, {t} n) {{ return (__atomic_compare_exchange_n(p, &c, n, 0, {mo}, __ATOMIC_RELAXED)); }}") p(f"static __inline int atomic_fcmpset{o}_{w}(volatile {t} *p, {t} *c, {t} n) {{ return (__atomic_compare_exchange_n(p, c, n, 0, {mo}, __ATOMIC_RELAXED)); }}") p(f"static __inline {t} atomic_fetchadd_{w}(volatile {t} *p, {t} v) {{ return (__atomic_fetch_add(p, v, __ATOMIC_SEQ_CST)); }}") p(f"static __inline {t} atomic_readandclear_{w}(volatile {t} *p) {{ return (__atomic_exchange_n(p, 0, __ATOMIC_SEQ_CST)); }}") p(f"static __inline {t} atomic_swap_{w}(volatile {t} *p, {t} v) {{ return (__atomic_exchange_n(p, v, __ATOMIC_SEQ_CST)); }}") p(f"static __inline int atomic_testandset_{w}(volatile {t} *p, u_int b) {{ {t} m = ({t})1 << (b % {w}); return ((__atomic_fetch_or(p, m, __ATOMIC_SEQ_CST) & m) != 0); }}") p(f"static __inline int atomic_testandset_acq_{w}(volatile {t} *p, u_int b) {{ return (atomic_testandset_{w}(p, b)); }}") p(f"static __inline int atomic_testandclear_{w}(volatile {t} *p, u_int b) {{ {t} m = ({t})1 << (b % {w}); return ((__atomic_fetch_and(p, ~m, __ATOMIC_SEQ_CST) & m) != 0); }}") p(f"static __inline {t} atomic_load_acq_{w}(const volatile {t} *p) {{ return (__atomic_load_n(p, __ATOMIC_ACQUIRE)); }}") p(f"static __inline void atomic_store_rel_{w}(volatile {t} *p, {t} v) {{ __atomic_store_n(p, v, __ATOMIC_RELEASE); }}") p("") ops = [] for o in ORD: for name in ("add", "subtract", "set", "clear", "cmpset", "fcmpset"): ops.append(f"{name}{o}") ops += ["fetchadd", "readandclear", "swap", "testandset", "testandset_acq", "testandclear", "load_acq", "store_rel"] for a, w in ALIAS.items(): p(f"/* {a}: {w}-bit */") for op in ops: p(f"#define\tatomic_{op}_{a}\tatomic_{op}_{w}") p("") p("#endif /* !_MACHINE_ATOMIC_H_ */") sys.stdout.write("\n".join(out) + "\n")