Add an 'atomic cas' wrapper for pointers.

Avoids having to repeat obfuscating castings everywhere...
This commit is contained in:
Bastien Montagne 2017-09-25 10:40:50 +02:00
parent 71e0894e0d
commit e4e2e3a15d
2 changed files with 14 additions and 0 deletions

View file

@ -107,6 +107,8 @@ ATOMIC_INLINE unsigned int atomic_fetch_and_add_u(unsigned int *p, unsigned int
ATOMIC_INLINE unsigned int atomic_fetch_and_sub_u(unsigned int *p, unsigned int x);
ATOMIC_INLINE unsigned int atomic_cas_u(unsigned int *v, unsigned int old, unsigned int _new);
ATOMIC_INLINE void *atomic_cas_ptr(void **v, void *old, void *_new);
/* WARNING! Float 'atomics' are really faked ones, those are actually closer to some kind of spinlock-sync'ed operation,
* which means they are only efficient if collisions are highly unlikely (i.e. if probability of two threads
* working on the same pointer at the same time is very low). */

View file

@ -168,6 +168,18 @@ ATOMIC_INLINE unsigned int atomic_cas_u(unsigned int *v, unsigned int old, unsig
#endif
}
/******************************************************************************/
/* Pointer operations. */
ATOMIC_INLINE void *atomic_cas_ptr(void **v, void *old, void *_new)
{
#if (LG_SIZEOF_PTR == 8)
return (void *)atomic_cas_uint64((uint64_t *)v, *(uint64_t *)&old, *(uint64_t *)&_new);
#elif (LG_SIZEOF_PTR == 4)
return (void *)atomic_cas_uint32((uint32_t *)v, *(uint32_t *)&old, *(uint32_t *)&_new);
#endif
}
/******************************************************************************/
/* float operations. */