base: support for 64-bit atomic increment/decrement
This commit is contained in:
@@ -36,6 +36,8 @@
|
|||||||
#ifndef HEIM_BASE_ATOMICS_H
|
#ifndef HEIM_BASE_ATOMICS_H
|
||||||
#define HEIM_BASE_ATOMICS_H 1
|
#define HEIM_BASE_ATOMICS_H 1
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Atomic operations
|
* Atomic operations
|
||||||
*/
|
*/
|
||||||
@@ -50,10 +52,10 @@
|
|||||||
|
|
||||||
#define heim_base_atomic(T) _Atomic(T)
|
#define heim_base_atomic(T) _Atomic(T)
|
||||||
|
|
||||||
#define heim_base_atomic_inc(x) (atomic_fetch_add((x), 1) + 1)
|
#define heim_base_atomic_inc_32(x) (atomic_fetch_add((x), 1) + 1)
|
||||||
#define heim_base_atomic_dec(x) (atomic_fetch_sub((x), 1) - 1)
|
#define heim_base_atomic_dec_32(x) (atomic_fetch_sub((x), 1) - 1)
|
||||||
#define heim_base_atomic_integer_type heim_base_atomic(unsigned int)
|
#define heim_base_atomic_inc_64(x) (atomic_fetch_add((x), 1) + 1)
|
||||||
#define heim_base_atomic_integer_max UINT_MAX
|
#define heim_base_atomic_dec_64(x) (atomic_fetch_sub((x), 1) - 1)
|
||||||
|
|
||||||
#define heim_base_exchange_pointer(t,v) atomic_exchange((t), (v))
|
#define heim_base_exchange_pointer(t,v) atomic_exchange((t), (v))
|
||||||
#define heim_base_exchange_32(t,v) atomic_exchange((t), (v))
|
#define heim_base_exchange_32(t,v) atomic_exchange((t), (v))
|
||||||
@@ -63,10 +65,10 @@
|
|||||||
|
|
||||||
#define heim_base_atomic_barrier() __sync_synchronize()
|
#define heim_base_atomic_barrier() __sync_synchronize()
|
||||||
|
|
||||||
#define heim_base_atomic_inc(x) __sync_add_and_fetch((x), 1)
|
#define heim_base_atomic_inc_32(x) __sync_add_and_fetch((x), 1)
|
||||||
#define heim_base_atomic_dec(x) __sync_sub_and_fetch((x), 1)
|
#define heim_base_atomic_dec_32(x) __sync_sub_and_fetch((x), 1)
|
||||||
#define heim_base_atomic_integer_type unsigned int
|
#define heim_base_atomic_inc_64(x) __sync_add_and_fetch((x), 1)
|
||||||
#define heim_base_atomic_integer_max UINT_MAX
|
#define heim_base_atomic_dec_64(x) __sync_sub_and_fetch((x), 1)
|
||||||
|
|
||||||
#ifndef __has_builtin
|
#ifndef __has_builtin
|
||||||
#define __has_builtin(x) 0
|
#define __has_builtin(x) 0
|
||||||
@@ -94,14 +96,16 @@ static inline void __heim_base_atomic_barrier(void)
|
|||||||
|
|
||||||
#define heim_base_atomic_barrier() __heim_base_atomic_barrier()
|
#define heim_base_atomic_barrier() __heim_base_atomic_barrier()
|
||||||
|
|
||||||
#define heim_base_atomic_inc(x) atomic_inc_uint_nv((volatile uint_t *)(x))
|
#define heim_base_atomic(T) volatile T
|
||||||
#define heim_base_atomic_dec(x) atomic_dec_uint_nv((volatile uint_t *)(x))
|
|
||||||
#define heim_base_atomic_integer_type uint_t
|
|
||||||
#define heim_base_atomic_integer_max UINT_MAX
|
|
||||||
|
|
||||||
#define heim_base_exchange_pointer(t,v) atomic_swap_ptr((volatile void *)(t), (void *)(v))
|
#define heim_base_atomic_inc_32(x) atomic_inc_32_nv((x))
|
||||||
#define heim_base_exchange_32(t,v) atomic_swap_32((volatile uint32_t *)(t), (v))
|
#define heim_base_atomic_dec_32(x) atomic_dec_32_nv((x))
|
||||||
#define heim_base_exchange_64(t,v) atomic_swap_64((volatile uint64_t *)(t), (v))
|
#define heim_base_atomic_inc_64(x) atomic_inc_64_nv((x))
|
||||||
|
#define heim_base_atomic_dec_64(x) atomic_dec_64_nv((x))
|
||||||
|
|
||||||
|
#define heim_base_exchange_pointer(t,v) atomic_swap_ptr((t), (void *)(v))
|
||||||
|
#define heim_base_exchange_32(t,v) atomic_swap_32((t), (v))
|
||||||
|
#define heim_base_exchange_64(t,v) atomic_swap_64((t), (v))
|
||||||
|
|
||||||
#elif defined(_AIX)
|
#elif defined(_AIX)
|
||||||
|
|
||||||
@@ -109,10 +113,10 @@ static inline void __heim_base_atomic_barrier(void)
|
|||||||
|
|
||||||
#define heim_base_atomic_barrier() __isync()
|
#define heim_base_atomic_barrier() __isync()
|
||||||
|
|
||||||
#define heim_base_atomic_inc(x) (fetch_and_addlp((atomic_l)(x), 1) + 1)
|
#define heim_base_atomic_inc_32(x) (fetch_and_add((atomic_p)(x), 1) + 1)
|
||||||
#define heim_base_atomic_dec(x) (fetch_and_addlp((atomic_l)(x), -1) - 1)
|
#define heim_base_atomic_dec_32(x) (fetch_and_add((atomic_p)(x), -1) - 1)
|
||||||
#define heim_base_atomic_integer_type long
|
#define heim_base_atomic_inc_64(x) (fetch_and_addlp((atomic_l)(x), 1) + 1)
|
||||||
#define heim_base_atomic_integer_max LONG_MAX
|
#define heim_base_atomic_dec_64(x) (fetch_and_addlp((atomic_l)(x), -1) - 1)
|
||||||
|
|
||||||
static inline void *
|
static inline void *
|
||||||
heim_base_exchange_pointer(void *p, void *newval)
|
heim_base_exchange_pointer(void *p, void *newval)
|
||||||
@@ -151,10 +155,10 @@ heim_base_exchange_64(uint64_t *p, uint64_t newval)
|
|||||||
|
|
||||||
#define heim_base_atomic_barrier() MemoryBarrier()
|
#define heim_base_atomic_barrier() MemoryBarrier()
|
||||||
|
|
||||||
#define heim_base_atomic_inc(x) InterlockedIncrement(x)
|
#define heim_base_atomic_inc_32(x) InterlockedIncrement(x)
|
||||||
#define heim_base_atomic_dec(x) InterlockedDecrement(x)
|
#define heim_base_atomic_dec_32(x) InterlockedDecrement(x)
|
||||||
#define heim_base_atomic_integer_type LONG
|
#define heim_base_atomic_inc_64(x) InterlockedIncrement64(x)
|
||||||
#define heim_base_atomic_integer_max MAXLONG
|
#define heim_base_atomic_dec_64(x) InterlockedDecrement64(x)
|
||||||
|
|
||||||
#define heim_base_exchange_pointer(t,v) InterlockedExchangePointer((PVOID volatile *)(t), (PVOID)(v))
|
#define heim_base_exchange_pointer(t,v) InterlockedExchangePointer((PVOID volatile *)(t), (PVOID)(v))
|
||||||
#define heim_base_exchange_32(t,v) ((ULONG)InterlockedExchange((LONG volatile *)(t), (LONG)(v)))
|
#define heim_base_exchange_32(t,v) ((ULONG)InterlockedExchange((LONG volatile *)(t), (LONG)(v)))
|
||||||
@@ -167,23 +171,40 @@ heim_base_exchange_64(uint64_t *p, uint64_t newval)
|
|||||||
#define HEIM_BASE_NEED_ATOMIC_MUTEX 1
|
#define HEIM_BASE_NEED_ATOMIC_MUTEX 1
|
||||||
extern HEIMDAL_MUTEX _heim_base_mutex;
|
extern HEIMDAL_MUTEX _heim_base_mutex;
|
||||||
|
|
||||||
#define heim_base_atomic_integer_type unsigned int
|
static inline uint32_t
|
||||||
#define heim_base_atomic_integer_max UINT_MAX
|
heim_base_atomic_inc_32(uint32_t *x)
|
||||||
|
|
||||||
static inline heim_base_atomic_integer_type
|
|
||||||
heim_base_atomic_inc(heim_base_atomic_integer_type *x)
|
|
||||||
{
|
{
|
||||||
heim_base_atomic_integer_type 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 heim_base_atomic_integer_type
|
static inline uint32_t
|
||||||
heim_base_atomic_dec(heim_base_atomic_integer_type *x)
|
heim_base_atomic_dec_32(uint32_t *x)
|
||||||
{
|
{
|
||||||
heim_base_atomic_integer_type t;
|
uint32_t t;
|
||||||
|
HEIMDAL_MUTEX_lock(&_heim_base_mutex);
|
||||||
|
t = --(*x);
|
||||||
|
HEIMDAL_MUTEX_unlock(&_heim_base_mutex);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
heim_base_atomic_inc_64(uint64_t *x)
|
||||||
|
{
|
||||||
|
uint64_t t;
|
||||||
|
HEIMDAL_MUTEX_lock(&_heim_base_mutex);
|
||||||
|
t = ++(*x);
|
||||||
|
HEIMDAL_MUTEX_unlock(&_heim_base_mutex);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
heim_base_atomic_dec_64(uint64_t *x)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
@@ -37,11 +37,11 @@
|
|||||||
#include "heimbase-atomics.h"
|
#include "heimbase-atomics.h"
|
||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
|
|
||||||
static heim_base_atomic_integer_type tidglobal = HEIM_TID_USER;
|
static heim_base_atomic(uint32_t) tidglobal = HEIM_TID_USER;
|
||||||
|
|
||||||
struct heim_base {
|
struct heim_base {
|
||||||
heim_type_t isa;
|
heim_type_t isa;
|
||||||
heim_base_atomic_integer_type ref_cnt;
|
heim_base_atomic(uint32_t) ref_cnt;
|
||||||
HEIM_TAILQ_ENTRY(heim_base) autorel;
|
HEIM_TAILQ_ENTRY(heim_base) autorel;
|
||||||
heim_auto_release_t autorelpool;
|
heim_auto_release_t autorelpool;
|
||||||
uintptr_t isaextra[3];
|
uintptr_t isaextra[3];
|
||||||
@@ -50,7 +50,7 @@ struct heim_base {
|
|||||||
/* specialized version of base */
|
/* specialized version of base */
|
||||||
struct heim_base_mem {
|
struct heim_base_mem {
|
||||||
heim_type_t isa;
|
heim_type_t isa;
|
||||||
heim_base_atomic_integer_type ref_cnt;
|
heim_base_atomic(uint32_t) ref_cnt;
|
||||||
HEIM_TAILQ_ENTRY(heim_base) autorel;
|
HEIM_TAILQ_ENTRY(heim_base) autorel;
|
||||||
heim_auto_release_t autorelpool;
|
heim_auto_release_t autorelpool;
|
||||||
const char *name;
|
const char *name;
|
||||||
@@ -94,10 +94,10 @@ heim_retain(heim_object_t ptr)
|
|||||||
|
|
||||||
p = PTR2BASE(ptr);
|
p = PTR2BASE(ptr);
|
||||||
|
|
||||||
if (heim_base_atomic_load(&p->ref_cnt) == heim_base_atomic_integer_max)
|
if (heim_base_atomic_load(&p->ref_cnt) == UINT32_MAX)
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|
||||||
if ((heim_base_atomic_inc(&p->ref_cnt) - 1) == 0)
|
if ((heim_base_atomic_inc_32(&p->ref_cnt) - 1) == 0)
|
||||||
heim_abort("resurection");
|
heim_abort("resurection");
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ heim_retain(heim_object_t ptr)
|
|||||||
void
|
void
|
||||||
heim_release(void *ptr)
|
heim_release(void *ptr)
|
||||||
{
|
{
|
||||||
heim_base_atomic_integer_type old;
|
heim_base_atomic(uint32_t) old;
|
||||||
struct heim_base *p;
|
struct heim_base *p;
|
||||||
|
|
||||||
if (ptr == NULL || heim_base_is_tagged(ptr))
|
if (ptr == NULL || heim_base_is_tagged(ptr))
|
||||||
@@ -119,10 +119,10 @@ heim_release(void *ptr)
|
|||||||
|
|
||||||
p = PTR2BASE(ptr);
|
p = PTR2BASE(ptr);
|
||||||
|
|
||||||
if (heim_base_atomic_load(&p->ref_cnt) == heim_base_atomic_integer_max)
|
if (heim_base_atomic_load(&p->ref_cnt) == UINT32_MAX)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
old = heim_base_atomic_dec(&p->ref_cnt) + 1;
|
old = heim_base_atomic_dec_32(&p->ref_cnt) + 1;
|
||||||
|
|
||||||
if (old > 1)
|
if (old > 1)
|
||||||
return;
|
return;
|
||||||
@@ -161,7 +161,7 @@ void
|
|||||||
_heim_make_permanent(heim_object_t ptr)
|
_heim_make_permanent(heim_object_t ptr)
|
||||||
{
|
{
|
||||||
struct heim_base *p = PTR2BASE(ptr);
|
struct heim_base *p = PTR2BASE(ptr);
|
||||||
heim_base_atomic_store(&p->ref_cnt, heim_base_atomic_integer_max);
|
heim_base_atomic_store(&p->ref_cnt, UINT32_MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -322,7 +322,7 @@ _heim_create_type(const char *name,
|
|||||||
if (type == NULL)
|
if (type == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
type->tid = heim_base_atomic_inc(&tidglobal);
|
type->tid = heim_base_atomic_inc_32(&tidglobal);
|
||||||
type->name = name;
|
type->name = name;
|
||||||
type->init = init;
|
type->init = init;
|
||||||
type->dealloc = dealloc;
|
type->dealloc = dealloc;
|
||||||
|
@@ -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(size_t) refs;
|
volatile heim_base_atomic(uint32_t) refs;
|
||||||
size_t len;
|
size_t len;
|
||||||
struct heim_log_facility_internal *val;
|
struct heim_log_facility_internal *val;
|
||||||
};
|
};
|
||||||
@@ -157,7 +157,7 @@ heim_log_facility *
|
|||||||
heim_log_ref(heim_log_facility *fac)
|
heim_log_ref(heim_log_facility *fac)
|
||||||
{
|
{
|
||||||
if (fac)
|
if (fac)
|
||||||
(void) heim_base_atomic_inc(&fac->refs);
|
(void) heim_base_atomic_inc_32(&fac->refs);
|
||||||
return fac;
|
return fac;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,7 +467,7 @@ heim_closelog(heim_context context, heim_log_facility *fac)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!fac || heim_base_atomic_dec(&fac->refs))
|
if (!fac || heim_base_atomic_dec_32(&fac->refs))
|
||||||
return;
|
return;
|
||||||
for (i = 0; i < fac->len; i++)
|
for (i = 0; i < fac->len; i++)
|
||||||
(*fac->val[i].close_func)(fac->val[i].data);
|
(*fac->val[i].close_func)(fac->val[i].data);
|
||||||
|
Reference in New Issue
Block a user