From ede7e53cc81003486945b3df258e68cbdcd7847c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 14 May 2026 06:22:33 +0300 Subject: [PATCH] builtin: prealloc_atomics.h --- vlib/builtin/prealloc_atomics.h | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 vlib/builtin/prealloc_atomics.h diff --git a/vlib/builtin/prealloc_atomics.h b/vlib/builtin/prealloc_atomics.h new file mode 100644 index 000000000..45768c3d4 --- /dev/null +++ b/vlib/builtin/prealloc_atomics.h @@ -0,0 +1,34 @@ +#ifndef V_PREALLOC_ATOMICS_H +#define V_PREALLOC_ATOMICS_H + +#if defined(_MSC_VER) +#include +static inline int v_prealloc_atomic_add_i32(int *ptr, int delta) { + return (int)_InterlockedExchangeAdd((volatile long*)ptr, (long)delta) + delta; +} +static inline int v_prealloc_atomic_load_i32(int *ptr) { + return (int)_InterlockedCompareExchange((volatile long*)ptr, 0, 0); +} +static inline int v_prealloc_atomic_store_i32(int *ptr, int val) { + _InterlockedExchange((volatile long*)ptr, (long)val); + return val; +} +static inline int v_prealloc_atomic_cas_i32(int *ptr, int expected, int desired) { + return _InterlockedCompareExchange((volatile long*)ptr, (long)desired, (long)expected) == expected; +} +#else +static inline int v_prealloc_atomic_add_i32(int *ptr, int delta) { + return __sync_add_and_fetch(ptr, delta); +} +static inline int v_prealloc_atomic_load_i32(int *ptr) { + return __sync_add_and_fetch(ptr, 0); +} +static inline int v_prealloc_atomic_store_i32(int *ptr, int val) { + return __sync_lock_test_and_set(ptr, val); +} +static inline int v_prealloc_atomic_cas_i32(int *ptr, int expected, int desired) { + return __sync_bool_compare_and_swap(ptr, expected, desired); +} +#endif + +#endif -- 2.39.5