base: Fix fallback atomics

This commit is contained in:
Nicolas Williams
2023-01-11 12:59:04 -06:00
parent 08c48c10c8
commit efdd6c589d
4 changed files with 59 additions and 50 deletions

View File

@@ -40,9 +40,11 @@
/* /*
* Atomic operations * Atomic operations
*
* (#define HEIM_BASE_ATOMICS_FALLBACK to test fallbacks.)
*/ */
#if defined(HAVE_STDATOMIC_H) #if !defined(HEIM_BASE_ATOMICS_FALLBACK) && defined(HAVE_STDATOMIC_H)
#include <stdatomic.h> #include <stdatomic.h>
@@ -93,7 +95,7 @@ heim_base_cas_64_(heim_base_atomic(uint64_t)*t, uint64_t e, uint64_t d)
#define heim_base_cas_32(t,e,d) heim_base_cas_32_((t), (e), (d)) #define heim_base_cas_32(t,e,d) heim_base_cas_32_((t), (e), (d))
#define heim_base_cas_64(t,e,d) heim_base_cas_64_((t), (e), (d)) #define heim_base_cas_64(t,e,d) heim_base_cas_64_((t), (e), (d))
#elif defined(__GNUC__) && defined(HAVE___SYNC_ADD_AND_FETCH) #elif !defined(HEIM_BASE_ATOMICS_FALLBACK) && defined(__GNUC__) && defined(HAVE___SYNC_ADD_AND_FETCH)
#define heim_base_atomic_barrier() __sync_synchronize() #define heim_base_atomic_barrier() __sync_synchronize()
@@ -120,7 +122,7 @@ heim_base_cas_64_(heim_base_atomic(uint64_t)*t, uint64_t e, uint64_t d)
#define heim_base_cas_32(t,e,d) __sync_val_compare_and_swap((t), (e), (d)) #define heim_base_cas_32(t,e,d) __sync_val_compare_and_swap((t), (e), (d))
#define heim_base_cas_64(t,e,d) __sync_val_compare_and_swap((t), (e), (d)) #define heim_base_cas_64(t,e,d) __sync_val_compare_and_swap((t), (e), (d))
#elif defined(__sun) #elif !defined(HEIM_BASE_ATOMICS_FALLBACK) && defined(__sun)
#include <sys/atomic.h> #include <sys/atomic.h>
#include <mbarrier.h> #include <mbarrier.h>
@@ -147,7 +149,7 @@ static inline void __heim_base_atomic_barrier(void)
#define heim_base_cas_32(t,e,d) atomic_cas_32((t), (e), (d)) #define heim_base_cas_32(t,e,d) atomic_cas_32((t), (e), (d))
#define heim_base_cas_64(t,e,d) atomic_cas_64((t), (e), (d)) #define heim_base_cas_64(t,e,d) atomic_cas_64((t), (e), (d))
#elif defined(_AIX) #elif !defined(HEIM_BASE_ATOMICS_FALLBACK) && defined(_AIX)
#include <sys/atomic_op.h> #include <sys/atomic_op.h>
@@ -213,7 +215,7 @@ heim_base_cas_64_(heim_base_atomic(uint64_t)*t, uint64_t e, uint64_t d)
#define heim_base_cas_32(t,e,d) heim_base_cas_32_((t), (e), (d)) #define heim_base_cas_32(t,e,d) heim_base_cas_32_((t), (e), (d))
#define heim_base_cas_64(t,e,d) heim_base_cas_64_((t), (e), (d)) #define heim_base_cas_64(t,e,d) heim_base_cas_64_((t), (e), (d))
#elif defined(_WIN32) #elif !defined(HEIM_BASE_ATOMICS_FALLBACK) && defined(_WIN32)
#define heim_base_atomic_barrier() MemoryBarrier() #define heim_base_atomic_barrier() MemoryBarrier()
@@ -232,116 +234,119 @@ heim_base_cas_64_(heim_base_atomic(uint64_t)*t, uint64_t e, uint64_t d)
#else #else
#define heim_base_atomic(T) volatile T
#define heim_base_atomic_barrier()
#define heim_base_atomic_load(x) (*(x))
#define heim_base_atomic_init(t, v) do { (*(t) = (v)); } while (0)
#define heim_base_atomic_store(t, v) do { (*(t) = (v)); } while (0)
#include <heim_threads.h> #include <heim_threads.h>
#define HEIM_BASE_NEED_ATOMIC_MUTEX 1 #define HEIM_BASE_NEED_ATOMIC_MUTEX 1
extern HEIMDAL_MUTEX _heim_base_mutex;
static inline uint32_t static inline uint32_t
heim_base_atomic_inc_32(uint32_t *x) heim_base_atomic_inc_32(heim_base_atomic(uint32_t) *x)
{ {
uint32_t t; uint32_t t;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
t = ++(*x); t = ++(*x);
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return t; return t;
} }
static inline uint32_t static inline uint32_t
heim_base_atomic_dec_32(uint32_t *x) heim_base_atomic_dec_32(heim_base_atomic(uint32_t) *x)
{ {
uint32_t t; uint32_t t;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
t = --(*x); t = --(*x);
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return t; return t;
} }
static inline uint64_t static inline uint64_t
heim_base_atomic_inc_64(uint64_t *x) heim_base_atomic_inc_64(heim_base_atomic(uint64_t) *x)
{ {
uint64_t t; uint64_t t;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
t = ++(*x); t = ++(*x);
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return t; return t;
} }
static inline uint64_t static inline uint64_t
heim_base_atomic_dec_64(uint64_t *x) heim_base_atomic_dec_64(heim_base_atomic(uint64_t) *x)
{ {
uint64_t t; uint64_t t;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
t = --(*x); t = --(*x);
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return t; return t;
} }
static inline void * static inline void *
heim_base_exchange_pointer(void *target, void *value) heim_base_exchange_pointer(heim_base_atomic(void *)target, void *value)
{ {
void *old; void *old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
old = *(void **)target; old = *(void **)target;
*(void **)target = value; *(void **)target = value;
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return old; return old;
} }
static inline uint32_t static inline uint32_t
heim_base_exchange_32(uint32_t *p, uint32_t newval) heim_base_exchange_32(heim_base_atomic(uint32_t) *target, uint32_t newval)
{ {
uint32_t old; uint32_t old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
old = *p; old = *target;
*p = newval; *target = newval;
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return old; return old;
} }
static inline uint64_t static inline uint64_t
heim_base_exchange_64(uint64_t *p, uint64_t newval) heim_base_exchange_64(heim_base_atomic(uint64_t) *target, uint64_t newval)
{ {
uint64_t old; uint64_t old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
old = *p; old = *target;
*p = newval; *target = newval;
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return old; return old;
} }
static inline void * static inline void *
heim_base_cas_pointer(void *target, void *expected, void *desired) heim_base_cas_pointer(heim_base_atomic(void *)target, void *expected, void *desired)
{ {
void *old; void *old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
if ((old = *(void **)target) == expected) if ((old = *(void **)target) == expected)
*(void **)target = desired; *(void **)target = desired;
HEIMDAL_MUTEX_unlock(&_heim_base_mutex); HEIMDAL_MUTEX_unlock(heim_base_mutex());
return old; return old;
} }
static inline uint32_t static inline uint32_t
heim_base_cas_32(uint32_t *p, uint32_t expected, uint32_t desired) heim_base_cas_32(heim_base_atomic(uint32_t) *target, uint32_t expected, uint32_t desired)
{ {
uint32_t old; uint32_t old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
if ((old = *(uint32_t *)p) == expected) if ((old = *(uint32_t *)target) == expected)
old = *p; *target = desired;
*p = desired; HEIMDAL_MUTEX_unlock(heim_base_mutex());
HEIMDAL_MUTEX_unlock(&_heim_base_mutex);
return old; return old;
} }
static inline uint64_t static inline uint64_t
heim_base_cas_64(uint64_t *p, uint64_t expected,uint64_t desired) heim_base_cas_64(heim_base_atomic(uint64_t) *target, uint64_t expected,uint64_t desired)
{ {
uint64_t old; uint64_t old;
HEIMDAL_MUTEX_lock(&_heim_base_mutex); HEIMDAL_MUTEX_lock(heim_base_mutex());
if ((old = *(uint64_t *)p) == expected) if ((old = *(uint64_t *)target) == expected)
old = *p; *target = desired;
*p = desired; HEIMDAL_MUTEX_unlock(heim_base_mutex());
HEIMDAL_MUTEX_unlock(&_heim_base_mutex);
return old; return old;
} }

View File

@@ -61,9 +61,12 @@ struct heim_base_mem {
#define PTR2BASE(ptr) (((struct heim_base *)ptr) - 1) #define PTR2BASE(ptr) (((struct heim_base *)ptr) - 1)
#define BASE2PTR(ptr) ((void *)(((struct heim_base *)ptr) + 1)) #define BASE2PTR(ptr) ((void *)(((struct heim_base *)ptr) + 1))
#ifdef HEIM_BASE_NEED_ATOMIC_MUTEX HEIMDAL_MUTEX * HEIM_CALLCONV
HEIMDAL_MUTEX _heim_base_mutex = HEIMDAL_MUTEX_INITIALIZER; heim_base_mutex(void)
#endif {
static HEIMDAL_MUTEX _heim_base_mutex = HEIMDAL_MUTEX_INITIALIZER;
return &_heim_base_mutex;
}
/* /*
* Auto release structure * Auto release structure

View File

@@ -53,7 +53,7 @@ struct heim_log_facility_internal {
struct heim_log_facility_s { struct heim_log_facility_s {
char *program; char *program;
volatile heim_base_atomic(uint32_t) refs; heim_base_atomic(uint32_t) refs;
size_t len; size_t len;
struct heim_log_facility_internal *val; struct heim_log_facility_internal *val;
}; };

View File

@@ -44,6 +44,7 @@ HEIMDAL_BASE_1.0 {
heim_auto_release_create; heim_auto_release_create;
heim_auto_release_drain; heim_auto_release_drain;
heim_base_once_f; heim_base_once_f;
heim_base_mutex;
heim_bool_create; heim_bool_create;
heim_bool_val; heim_bool_val;
heim_clear_error_message; heim_clear_error_message;