From b297bfdd47b0a39831d8cc2541aeb910f9af0bca Mon Sep 17 00:00:00 2001 From: Rickard Green Date: Fri, 3 Dec 2010 10:57:26 +0100 Subject: Introduce ethr_sint_t and use it for atomics The atomic memory operations interface used the 'long' type and assumed that it was of the same size as 'void *'. This is true on most platforms, however, not on Windows 64. --- erts/include/internal/ethr_mutex.h | 28 ++--- erts/include/internal/ethread.h | 141 ++++++++++++---------- erts/include/internal/ethread_header_config.h.in | 9 ++ erts/include/internal/gcc/ethr_atomic.h | 5 +- erts/include/internal/i386/atomic.h | 4 + erts/include/internal/libatomic_ops/ethr_atomic.h | 97 ++++++++------- erts/include/internal/ppc32/atomic.h | 4 + erts/include/internal/pthread/ethr_event.h | 16 +-- erts/include/internal/sparc32/atomic.h | 4 + erts/include/internal/tile/atomic.h | 4 + erts/include/internal/win/ethr_atomic.h | 1 - 11 files changed, 182 insertions(+), 131 deletions(-) (limited to 'erts/include') diff --git a/erts/include/internal/ethr_mutex.h b/erts/include/internal/ethr_mutex.h index 01855864e3..ebff845a54 100644 --- a/erts/include/internal/ethr_mutex.h +++ b/erts/include/internal/ethr_mutex.h @@ -78,13 +78,13 @@ # error Need a qlock implementation #endif -#define ETHR_RWMTX_W_FLG__ (((long) 1) << 31) -#define ETHR_RWMTX_W_WAIT_FLG__ (((long) 1) << 30) -#define ETHR_RWMTX_R_WAIT_FLG__ (((long) 1) << 29) +#define ETHR_RWMTX_W_FLG__ (((ethr_sint_t) 1) << 31) +#define ETHR_RWMTX_W_WAIT_FLG__ (((ethr_sint_t) 1) << 30) +#define ETHR_RWMTX_R_WAIT_FLG__ (((ethr_sint_t) 1) << 29) /* frequent read kind */ -#define ETHR_RWMTX_R_FLG__ (((long) 1) << 28) -#define ETHR_RWMTX_R_ABRT_UNLCK_FLG__ (((long) 1) << 27) +#define ETHR_RWMTX_R_FLG__ (((ethr_sint_t) 1) << 28) +#define ETHR_RWMTX_R_ABRT_UNLCK_FLG__ (((ethr_sint_t) 1) << 27) #define ETHR_RWMTX_R_PEND_UNLCK_MASK__ (ETHR_RWMTX_R_ABRT_UNLCK_FLG__ - 1) /* normal kind */ @@ -302,7 +302,7 @@ do { \ } while (0) # define ETHR_MTX_HARD_DEBUG_LFS_RLOCK(MTXB) \ do { \ - long val__; \ + ethr_sint_t val__; \ ETHR_COMPILER_BARRIER; \ val__ = ethr_atomic_inc_read(&(MTXB)->hdbg_lfs); \ ETHR_MTX_HARD_ASSERT(val__ > 0); \ @@ -317,13 +317,13 @@ do { \ } while (0) # define ETHR_MTX_HARD_DEBUG_LFS_RUNLOCK(MTXB) \ do { \ - long val__ = ethr_atomic_dec_read(&(MTXB)->hdbg_lfs); \ + ethr_sint_t val__ = ethr_atomic_dec_read(&(MTXB)->hdbg_lfs); \ ETHR_MTX_HARD_ASSERT(val__ >= 0); \ ETHR_COMPILER_BARRIER; \ } while (0) # define ETHR_MTX_HARD_DEBUG_LFS_RWLOCK(MTXB) \ do { \ - long val__; \ + ethr_sint_t val__; \ ETHR_COMPILER_BARRIER; \ val__ = ethr_atomic_dec_read(&(MTXB)->hdbg_lfs); \ ETHR_MTX_HARD_ASSERT(val__ == -1); \ @@ -338,7 +338,7 @@ do { \ } while (0) # define ETHR_MTX_HARD_DEBUG_LFS_RWUNLOCK(MTXB) \ do { \ - long val__ = ethr_atomic_inctest(&(MTXB)->hdbg_lfs); \ + ethr_sint_t val__ = ethr_atomic_inctest(&(MTXB)->hdbg_lfs); \ ETHR_MTX_HARD_ASSERT(val__ == 0); \ ETHR_COMPILER_BARRIER; \ } while (0) @@ -501,13 +501,13 @@ do { \ #if defined(ETHR_TRY_INLINE_FUNCS) || defined(ETHR_MUTEX_IMPL__) -void ethr_mutex_lock_wait__(ethr_mutex *, long); -void ethr_mutex_unlock_wake__(ethr_mutex *, long); +void ethr_mutex_lock_wait__(ethr_mutex *, ethr_sint_t); +void ethr_mutex_unlock_wake__(ethr_mutex *, ethr_sint_t); static ETHR_INLINE int ETHR_INLINE_FUNC_NAME_(ethr_mutex_trylock)(ethr_mutex *mtx) { - long act; + ethr_sint_t act; int res; ETHR_MTX_HARD_DEBUG_FENCE_CHK(mtx); ETHR_MTX_DBG_CHK_UNUSED_FLG_BITS(mtx); @@ -531,7 +531,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_mutex_trylock)(ethr_mutex *mtx) static ETHR_INLINE void ETHR_INLINE_FUNC_NAME_(ethr_mutex_lock)(ethr_mutex *mtx) { - long act; + ethr_sint_t act; ETHR_MTX_HARD_DEBUG_FENCE_CHK(mtx); ETHR_MTX_DBG_CHK_UNUSED_FLG_BITS(mtx); @@ -551,7 +551,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_mutex_lock)(ethr_mutex *mtx) static ETHR_INLINE void ETHR_INLINE_FUNC_NAME_(ethr_mutex_unlock)(ethr_mutex *mtx) { - long act; + ethr_sint_t act; ETHR_COMPILER_BARRIER; ETHR_MTX_HARD_DEBUG_FENCE_CHK(mtx); ETHR_MTX_HARD_DEBUG_LFS_RWUNLOCK(&mtx->mtxb); diff --git a/erts/include/internal/ethread.h b/erts/include/internal/ethread.h index 53fa1acdc2..30b7f03889 100644 --- a/erts/include/internal/ethread.h +++ b/erts/include/internal/ethread.h @@ -73,7 +73,7 @@ typedef struct { #endif /* Assume 64-byte cache line size */ -#define ETHR_CACHE_LINE_SIZE 64L +#define ETHR_CACHE_LINE_SIZE ((ethr_uint_t) 64) #define ETHR_CACHE_LINE_MASK (ETHR_CACHE_LINE_SIZE - 1) #define ETHR_CACHE_LINE_ALIGN_SIZE(SZ) \ @@ -171,6 +171,15 @@ typedef pthread_key_t ethr_tsd_key; # undef WIN32_LEAN_AND_MEAN #endif +#if defined(_MSC_VER) +#if ETHR_SIZEOF_PTR == 4 +#define ETHR_HAVE_INT_T 1 +typedef LONG ethr_sint_t; +typedef ULONG ethr_uint_t; +#else +#error "Only 32-bit windows is supported" +#endif +#endif struct ethr_join_data_; /* Types */ @@ -198,9 +207,19 @@ typedef DWORD ethr_tsd_key; #endif -#ifdef SIZEOF_LONG -#if SIZEOF_LONG < ETHR_SIZEOF_PTR -#error size of long currently needs to be at least the same as size of void * +#ifndef ETHR_HAVE_INT_T +#define ETHR_HAVE_INT_T 1 +#if ETHR_SIZEOF_INT == ETHR_SIZEOF_PTR +typedef int ethr_sint_t; +typedef unsigned int ethr_uint_t; +#elif ETHR_SIZEOF_LONG == ETHR_SIZEOF_PTR +typedef long ethr_sint_t; +typedef unsigned long ethr_uint_t; +#elif ETHR_SIZEOF_LONG_LONG == ETHR_SIZEOF_PTR +typedef long long ethr_sint_t; +typedef unsigned long long ethr_uint_t; +#else +#error "No integer type of the same size as pointers found" #endif #endif @@ -508,30 +527,30 @@ ETHR_INLINE_FUNC_NAME_(ethr_spin_lock)(ethr_spinlock_t *lock) */ typedef ethr_native_atomic_t ethr_atomic_t; #else -typedef long ethr_atomic_t; +typedef ethr_sint_t ethr_atomic_t; #endif #ifdef ETHR_NEED_ATOMIC_PROTOTYPES__ -void ethr_atomic_init(ethr_atomic_t *, long); -void ethr_atomic_set(ethr_atomic_t *, long); -long ethr_atomic_read(ethr_atomic_t *); -long ethr_atomic_inc_read(ethr_atomic_t *); -long ethr_atomic_dec_read(ethr_atomic_t *); +void ethr_atomic_init(ethr_atomic_t *, ethr_sint_t); +void ethr_atomic_set(ethr_atomic_t *, ethr_sint_t); +ethr_sint_t ethr_atomic_read(ethr_atomic_t *); +ethr_sint_t ethr_atomic_inc_read(ethr_atomic_t *); +ethr_sint_t ethr_atomic_dec_read(ethr_atomic_t *); void ethr_atomic_inc(ethr_atomic_t *); void ethr_atomic_dec(ethr_atomic_t *); -long ethr_atomic_add_read(ethr_atomic_t *, long); -void ethr_atomic_add(ethr_atomic_t *, long); -long ethr_atomic_read_band(ethr_atomic_t *, long); -long ethr_atomic_read_bor(ethr_atomic_t *, long); -long ethr_atomic_xchg(ethr_atomic_t *, long); -long ethr_atomic_cmpxchg(ethr_atomic_t *, long, long); -long ethr_atomic_read_acqb(ethr_atomic_t *); -long ethr_atomic_inc_read_acqb(ethr_atomic_t *); -void ethr_atomic_set_relb(ethr_atomic_t *, long); +ethr_sint_t ethr_atomic_add_read(ethr_atomic_t *, ethr_sint_t); +void ethr_atomic_add(ethr_atomic_t *, ethr_sint_t); +ethr_sint_t ethr_atomic_read_band(ethr_atomic_t *, ethr_sint_t); +ethr_sint_t ethr_atomic_read_bor(ethr_atomic_t *, ethr_sint_t); +ethr_sint_t ethr_atomic_xchg(ethr_atomic_t *, ethr_sint_t); +ethr_sint_t ethr_atomic_cmpxchg(ethr_atomic_t *, ethr_sint_t, ethr_sint_t); +ethr_sint_t ethr_atomic_read_acqb(ethr_atomic_t *); +ethr_sint_t ethr_atomic_inc_read_acqb(ethr_atomic_t *); +void ethr_atomic_set_relb(ethr_atomic_t *, ethr_sint_t); void ethr_atomic_dec_relb(ethr_atomic_t *); -long ethr_atomic_dec_read_relb(ethr_atomic_t *); -long ethr_atomic_cmpxchg_acqb(ethr_atomic_t *, long, long); -long ethr_atomic_cmpxchg_relb(ethr_atomic_t *, long, long); +ethr_sint_t ethr_atomic_dec_read_relb(ethr_atomic_t *); +ethr_sint_t ethr_atomic_cmpxchg_acqb(ethr_atomic_t *, ethr_sint_t, ethr_sint_t); +ethr_sint_t ethr_atomic_cmpxchg_relb(ethr_atomic_t *, ethr_sint_t, ethr_sint_t); #endif #if defined(ETHR_TRY_INLINE_FUNCS) || defined(ETHR_AUX_IMPL__) @@ -569,7 +588,7 @@ do { \ #endif static ETHR_INLINE void -ETHR_INLINE_FUNC_NAME_(ethr_atomic_init)(ethr_atomic_t *var, long i) +ETHR_INLINE_FUNC_NAME_(ethr_atomic_init)(ethr_atomic_t *var, ethr_sint_t i) { #ifdef ETHR_HAVE_NATIVE_ATOMICS ethr_native_atomic_init(var, i); @@ -579,7 +598,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_init)(ethr_atomic_t *var, long i) } static ETHR_INLINE void -ETHR_INLINE_FUNC_NAME_(ethr_atomic_set)(ethr_atomic_t *var, long i) +ETHR_INLINE_FUNC_NAME_(ethr_atomic_set)(ethr_atomic_t *var, ethr_sint_t i) { #ifdef ETHR_HAVE_NATIVE_ATOMICS ethr_native_atomic_set(var, i); @@ -588,20 +607,20 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_set)(ethr_atomic_t *var, long i) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_read)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_read(var); #else - long res; - ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = (long) *var); + ethr_sint_t res; + ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = *var); return res; #endif } static ETHR_INLINE void -ETHR_INLINE_FUNC_NAME_(ethr_atomic_add)(ethr_atomic_t *var, long incr) +ETHR_INLINE_FUNC_NAME_(ethr_atomic_add)(ethr_atomic_t *var, ethr_sint_t incr) { #ifdef ETHR_HAVE_NATIVE_ATOMICS ethr_native_atomic_add(var, incr); @@ -610,13 +629,13 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_add)(ethr_atomic_t *var, long incr) #endif } -static ETHR_INLINE long -ETHR_INLINE_FUNC_NAME_(ethr_atomic_add_read)(ethr_atomic_t *var, long i) +static ETHR_INLINE ethr_sint_t +ETHR_INLINE_FUNC_NAME_(ethr_atomic_add_read)(ethr_atomic_t *var, ethr_sint_t i) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_add_return(var, i); #else - long res; + ethr_sint_t res; ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *var += i; res = *var); return res; #endif @@ -642,78 +661,77 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_dec)(ethr_atomic_t *var) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_inc_read)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_inc_return(var); #else - long res; - ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = (long) ++(*var)); + ethr_sint_t res; + ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = ++(*var)); return res; #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_dec_read)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_dec_return(var); #else - long res; - ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = (long) --(*var)); + ethr_sint_t res; + ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = --(*var)); return res; #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_read_band)(ethr_atomic_t *var, - long mask) + ethr_sint_t mask) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_and_retold(var, mask); #else - long res; + ethr_sint_t res; ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = *var; *var &= mask); return res; #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_read_bor)(ethr_atomic_t *var, - long mask) + ethr_sint_t mask) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_or_retold(var, mask); #else - long res; + ethr_sint_t res; ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = *var; *var |= mask); return res; #endif } -static ETHR_INLINE long -ETHR_INLINE_FUNC_NAME_(ethr_atomic_xchg)(ethr_atomic_t *var, - long new) +static ETHR_INLINE ethr_sint_t +ETHR_INLINE_FUNC_NAME_(ethr_atomic_xchg)(ethr_atomic_t *var, ethr_sint_t new) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_xchg(var, new); #else - long res; + ethr_sint_t res; ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, res = *var; *var = new); return res; #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_cmpxchg)(ethr_atomic_t *var, - long new, - long exp) + ethr_sint_t new, + ethr_sint_t exp) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_cmpxchg(var, new, exp); #else - long res; + ethr_sint_t res; ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, { res = *var; @@ -733,7 +751,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_cmpxchg)(ethr_atomic_t *var, * _relb = release barrier */ -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_read_acqb)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS @@ -743,7 +761,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_read_acqb)(ethr_atomic_t *var) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_inc_read_acqb)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS @@ -754,7 +772,8 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_inc_read_acqb)(ethr_atomic_t *var) } static ETHR_INLINE void -ETHR_INLINE_FUNC_NAME_(ethr_atomic_set_relb)(ethr_atomic_t *var, long val) +ETHR_INLINE_FUNC_NAME_(ethr_atomic_set_relb)(ethr_atomic_t *var, + ethr_sint_t val) { #ifdef ETHR_HAVE_NATIVE_ATOMICS ethr_native_atomic_set_relb(var, val); @@ -773,7 +792,7 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_dec_relb)(ethr_atomic_t *var) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_dec_read_relb)(ethr_atomic_t *var) { #ifdef ETHR_HAVE_NATIVE_ATOMICS @@ -783,10 +802,10 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_dec_read_relb)(ethr_atomic_t *var) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_cmpxchg_acqb)(ethr_atomic_t *var, - long new, - long exp) + ethr_sint_t new, + ethr_sint_t exp) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_cmpxchg_acqb(var, new, exp); @@ -795,10 +814,10 @@ ETHR_INLINE_FUNC_NAME_(ethr_atomic_cmpxchg_acqb)(ethr_atomic_t *var, #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ETHR_INLINE_FUNC_NAME_(ethr_atomic_cmpxchg_relb)(ethr_atomic_t *var, - long new, - long exp) + ethr_sint_t new, + ethr_sint_t exp) { #ifdef ETHR_HAVE_NATIVE_ATOMICS return ethr_native_atomic_cmpxchg_relb(var, new, exp); diff --git a/erts/include/internal/ethread_header_config.h.in b/erts/include/internal/ethread_header_config.h.in index 5debb44756..8c6fbc1fdd 100644 --- a/erts/include/internal/ethread_header_config.h.in +++ b/erts/include/internal/ethread_header_config.h.in @@ -20,6 +20,15 @@ /* Define to the size of pointers */ #undef ETHR_SIZEOF_PTR +/* Define to the size of int */ +#undef ETHR_SIZEOF_INT + +/* Define to the size of long */ +#undef ETHR_SIZEOF_LONG + +/* Define to the size of long long */ +#undef ETHR_SIZEOF_LONG_LONG + /* Define if you want to disable native ethread implementations */ #undef ETHR_DISABLE_NATIVE_IMPLS diff --git a/erts/include/internal/gcc/ethr_atomic.h b/erts/include/internal/gcc/ethr_atomic.h index e8e529dd48..49a8ceeab1 100644 --- a/erts/include/internal/gcc/ethr_atomic.h +++ b/erts/include/internal/gcc/ethr_atomic.h @@ -36,11 +36,14 @@ # define ETHR_IMMED_ATOMIC_SET_GET_SAFE__ 1 #endif +#if ETHR_SIZEOF_LONG != ETHR_SIZEOF_PTR +# error "Incompatible size of 'long'" +#endif + typedef struct { volatile long counter; } ethr_native_atomic_t; - /* * According to the documentation this is what we want: * #define ETHR_MEMORY_BARRIER __sync_synchronize() diff --git a/erts/include/internal/i386/atomic.h b/erts/include/internal/i386/atomic.h index 52d01aab32..709a6a17fc 100644 --- a/erts/include/internal/i386/atomic.h +++ b/erts/include/internal/i386/atomic.h @@ -26,6 +26,10 @@ #ifndef ETHREAD_I386_ATOMIC_H #define ETHREAD_I386_ATOMIC_H +#if ETHR_SIZEOF_LONG != ETHR_SIZEOF_PTR +# error "Incompatible size of 'long'" +#endif + /* An atomic is an aligned long accessed via locked operations. */ typedef struct { diff --git a/erts/include/internal/libatomic_ops/ethr_atomic.h b/erts/include/internal/libatomic_ops/ethr_atomic.h index a6eb43a0bd..7def51ebd1 100644 --- a/erts/include/internal/libatomic_ops/ethr_atomic.h +++ b/erts/include/internal/libatomic_ops/ethr_atomic.h @@ -46,8 +46,7 @@ * - AO_store() * - AO_compare_and_swap() * - * The `AO_t' type also have to be at least as large as - * `void *' and `long' types. + * The `AO_t' type also have to be at least as large as the `void *' type. */ #if ETHR_SIZEOF_AO_T < ETHR_SIZEOF_PTR @@ -78,49 +77,49 @@ typedef struct { #if defined(ETHR_TRY_INLINE_FUNCS) || defined(ETHR_AUX_IMPL__) static ETHR_INLINE void -ethr_native_atomic_set(ethr_native_atomic_t *var, long value) +ethr_native_atomic_set(ethr_native_atomic_t *var, ethr_sint_t value) { AO_store(&var->counter, (AO_t) value); } static ETHR_INLINE void -ethr_native_atomic_init(ethr_native_atomic_t *var, long value) +ethr_native_atomic_init(ethr_native_atomic_t *var, ethr_sint_t value) { ethr_native_atomic_set(var, value); } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_read(ethr_native_atomic_t *var) { - return (long) AO_load(&var->counter); + return (ethr_sint_t) AO_load(&var->counter); } -static ETHR_INLINE long -ethr_native_atomic_add_return(ethr_native_atomic_t *var, long incr) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_add_return(ethr_native_atomic_t *var, ethr_sint_t incr) { #ifdef AO_HAVE_fetch_and_add - return ((long) AO_fetch_and_add(&var->counter, (AO_t) incr)) + incr; + return ((ethr_sint_t) AO_fetch_and_add(&var->counter, (AO_t) incr)) + incr; #else while (1) { AO_t exp = AO_load(&var->counter); AO_t new = exp + (AO_t) incr; if (AO_compare_and_swap(&var->counter, exp, new)) - return (long) new; + return (ethr_sint_t) new; } #endif } static ETHR_INLINE void -ethr_native_atomic_add(ethr_native_atomic_t *var, long incr) +ethr_native_atomic_add(ethr_native_atomic_t *var, ethr_sint_t incr) { (void) ethr_native_atomic_add_return(var, incr); } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_inc_return(ethr_native_atomic_t *var) { #ifdef AO_HAVE_fetch_and_add1 - return ((long) AO_fetch_and_add1(&var->counter)) + 1; + return ((ethr_sint_t) AO_fetch_and_add1(&var->counter)) + 1; #else return ethr_native_atomic_add_return(var, 1); #endif @@ -132,11 +131,11 @@ ethr_native_atomic_inc(ethr_native_atomic_t *var) (void) ethr_native_atomic_inc_return(var); } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_dec_return(ethr_native_atomic_t *var) { #ifdef AO_HAVE_fetch_and_sub1 - return ((long) AO_fetch_and_sub1(&var->counter)) - 1; + return ((ethr_sint_t) AO_fetch_and_sub1(&var->counter)) - 1; #else return ethr_native_atomic_add_return(var, -1); #endif @@ -148,47 +147,49 @@ ethr_native_atomic_dec(ethr_native_atomic_t *var) (void) ethr_native_atomic_dec_return(var); } -static ETHR_INLINE long -ethr_native_atomic_and_retold(ethr_native_atomic_t *var, long mask) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_and_retold(ethr_native_atomic_t *var, ethr_sint_t mask) { while (1) { AO_t exp = AO_load(&var->counter); AO_t new = exp & ((AO_t) mask); if (AO_compare_and_swap(&var->counter, exp, new)) - return (long) exp; + return (ethr_sint_t) exp; } } -static ETHR_INLINE long -ethr_native_atomic_or_retold(ethr_native_atomic_t *var, long mask) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_or_retold(ethr_native_atomic_t *var, ethr_sint_t mask) { while (1) { AO_t exp = AO_load(&var->counter); AO_t new = exp | ((AO_t) mask); if (AO_compare_and_swap(&var->counter, exp, new)) - return (long) exp; + return (ethr_sint_t) exp; } } -static ETHR_INLINE long -ethr_native_atomic_cmpxchg(ethr_native_atomic_t *var, long new, long exp) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_cmpxchg(ethr_native_atomic_t *var, + ethr_sint_t new, + ethr_sint_t exp) { - long act; + ethr_sint_t act; do { if (AO_compare_and_swap(&var->counter, (AO_t) exp, (AO_t) new)) return exp; - act = (long) AO_load(&var->counter); + act = (ethr_sint_t) AO_load(&var->counter); } while (act == exp); return act; } -static ETHR_INLINE long -ethr_native_atomic_xchg(ethr_native_atomic_t *var, long new) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_xchg(ethr_native_atomic_t *var, ethr_sint_t new) { while (1) { AO_t exp = AO_load(&var->counter); if (AO_compare_and_swap(&var->counter, exp, (AO_t) new)) - return (long) exp; + return (ethr_sint_t) exp; } } @@ -196,32 +197,32 @@ ethr_native_atomic_xchg(ethr_native_atomic_t *var, long new) * Atomic ops with at least specified barriers. */ -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_read_acqb(ethr_native_atomic_t *var) { #ifdef AO_HAVE_load_acquire - return (long) AO_load_acquire(&var->counter); + return (ethr_sint_t) AO_load_acquire(&var->counter); #else - long res = ethr_native_atomic_read(var); + ethr_sint_t res = ethr_native_atomic_read(var); ETHR_MEMORY_BARRIER; return res; #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_inc_return_acqb(ethr_native_atomic_t *var) { #ifdef AO_HAVE_fetch_and_add1_acquire - return ((long) AO_fetch_and_add1_acquire(&var->counter)) + 1; + return ((ethr_sint_t) AO_fetch_and_add1_acquire(&var->counter)) + 1; #else - long res = ethr_native_atomic_add_return(var, 1); + ethr_sint_t res = ethr_native_atomic_add_return(var, 1); ETHR_MEMORY_BARRIER; return res; #endif } static ETHR_INLINE void -ethr_native_atomic_set_relb(ethr_native_atomic_t *var, long value) +ethr_native_atomic_set_relb(ethr_native_atomic_t *var, ethr_sint_t value) { #ifdef AO_HAVE_store_release AO_store_release(&var->counter, (AO_t) value); @@ -231,11 +232,11 @@ ethr_native_atomic_set_relb(ethr_native_atomic_t *var, long value) #endif } -static ETHR_INLINE long +static ETHR_INLINE ethr_sint_t ethr_native_atomic_dec_return_relb(ethr_native_atomic_t *var) { #ifdef AO_HAVE_fetch_and_sub1_release - return ((long) AO_fetch_and_sub1_release(&var->counter)) - 1; + return ((ethr_sint_t) AO_fetch_and_sub1_release(&var->counter)) - 1; #else ETHR_MEMORY_BARRIER; return ethr_native_atomic_dec_return(var); @@ -248,34 +249,38 @@ ethr_native_atomic_dec_relb(ethr_native_atomic_t *var) (void) ethr_native_atomic_dec_return_relb(var); } -static ETHR_INLINE long -ethr_native_atomic_cmpxchg_acqb(ethr_native_atomic_t *var, long new, long exp) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_cmpxchg_acqb(ethr_native_atomic_t *var, + ethr_sint_t new, + ethr_sint_t exp) { #ifdef AO_HAVE_compare_and_swap_acquire - long act; + ethr_sint_t act; do { if (AO_compare_and_swap_acquire(&var->counter, (AO_t) exp, (AO_t) new)) return exp; - act = (long) AO_load(&var->counter); + act = (ethr_sint_t) AO_load(&var->counter); } while (act == exp); AO_nop_full(); return act; #else - long act = ethr_native_atomic_cmpxchg(var, new, exp); + ethr_sint_t act = ethr_native_atomic_cmpxchg(var, new, exp); ETHR_MEMORY_BARRIER; return act; #endif } -static ETHR_INLINE long -ethr_native_atomic_cmpxchg_relb(ethr_native_atomic_t *var, long new, long exp) +static ETHR_INLINE ethr_sint_t +ethr_native_atomic_cmpxchg_relb(ethr_native_atomic_t *var, + ethr_sint_t new, + ethr_sint_t exp) { #ifdef AO_HAVE_compare_and_swap_release - long act; + ethr_sint_t act; do { if (AO_compare_and_swap_release(&var->counter, (AO_t) exp, (AO_t) new)) return exp; - act = (long) AO_load(&var->counter); + act = (ethr_sint_t) AO_load(&var->counter); } while (act == exp); return act; #else diff --git a/erts/include/internal/ppc32/atomic.h b/erts/include/internal/ppc32/atomic.h index f21f7c9588..daa0de88ab 100644 --- a/erts/include/internal/ppc32/atomic.h +++ b/erts/include/internal/ppc32/atomic.h @@ -28,6 +28,10 @@ #ifndef ETHREAD_PPC_ATOMIC_H #define ETHREAD_PPC_ATOMIC_H +#if ETHR_SIZEOF_LONG != ETHR_SIZEOF_PTR +# error "Incompatible size of 'int'" +#endif + typedef struct { volatile int counter; } ethr_native_atomic_t; diff --git a/erts/include/internal/pthread/ethr_event.h b/erts/include/internal/pthread/ethr_event.h index 104ec287e0..4cf87406d7 100644 --- a/erts/include/internal/pthread/ethr_event.h +++ b/erts/include/internal/pthread/ethr_event.h @@ -40,15 +40,15 @@ #if ETHR_SIZEOF_PTR == 8 -#define ETHR_EVENT_OFF_WAITER__ 0xffffffffffffffffL -#define ETHR_EVENT_OFF__ 0x7777777777777777L -#define ETHR_EVENT_ON__ 0L +#define ETHR_EVENT_OFF_WAITER__ ((ethr_sint_t) 0xffffffffffffffffL) +#define ETHR_EVENT_OFF__ ((ethr_sint_t) 0x7777777777777777L) +#define ETHR_EVENT_ON__ ((ethr_sint_t) 0L) #elif ETHR_SIZEOF_PTR == 4 -#define ETHR_EVENT_OFF_WAITER__ 0xffffffffL -#define ETHR_EVENT_OFF__ 0x77777777L -#define ETHR_EVENT_ON__ 0L +#define ETHR_EVENT_OFF_WAITER__ ((ethr_sint_t) 0xffffffffL) +#define ETHR_EVENT_OFF__ ((ethr_sint_t) 0x77777777L) +#define ETHR_EVENT_ON__ ((ethr_sint_t) 0L) #else @@ -77,7 +77,7 @@ typedef struct { static void ETHR_INLINE ETHR_INLINE_FUNC_NAME_(ethr_event_set)(ethr_event *e) { - long val; + ethr_sint_t val; ETHR_WRITE_MEMORY_BARRIER; val = ethr_atomic_xchg(&e->futex, ETHR_EVENT_ON__); if (val == ETHR_EVENT_OFF_WAITER__) { @@ -114,7 +114,7 @@ typedef struct { static void ETHR_INLINE ETHR_INLINE_FUNC_NAME_(ethr_event_set)(ethr_event *e) { - long val; + ethr_sint_t val; ETHR_WRITE_MEMORY_BARRIER; val = ethr_atomic_xchg(&e->state, ETHR_EVENT_ON__); if (val == ETHR_EVENT_OFF_WAITER__) { diff --git a/erts/include/internal/sparc32/atomic.h b/erts/include/internal/sparc32/atomic.h index 2da6472393..8d94fa939f 100644 --- a/erts/include/internal/sparc32/atomic.h +++ b/erts/include/internal/sparc32/atomic.h @@ -24,6 +24,10 @@ #ifndef ETHR_SPARC32_ATOMIC_H #define ETHR_SPARC32_ATOMIC_H +#if ETHR_SIZEOF_LONG != ETHR_SIZEOF_PTR +# error "Incompatible size of 'long'" +#endif + typedef struct { volatile long counter; } ethr_native_atomic_t; diff --git a/erts/include/internal/tile/atomic.h b/erts/include/internal/tile/atomic.h index 69569d82d1..05246a2aa9 100644 --- a/erts/include/internal/tile/atomic.h +++ b/erts/include/internal/tile/atomic.h @@ -26,6 +26,10 @@ #include +#if ETHR_SIZEOF_LONG != ETHR_SIZEOF_PTR +# error "Incompatible size of 'long'" +#endif + /* An atomic is an aligned int accessed via locked operations. */ typedef struct { diff --git a/erts/include/internal/win/ethr_atomic.h b/erts/include/internal/win/ethr_atomic.h index 500459dd6c..47068927a6 100644 --- a/erts/include/internal/win/ethr_atomic.h +++ b/erts/include/internal/win/ethr_atomic.h @@ -66,7 +66,6 @@ #pragma intrinsic(_InterlockedCompareExchange_rel) #endif - typedef struct { volatile LONG value; } ethr_native_atomic_t; -- cgit v1.2.3