atomics: Move atomics/recounting support to separate files
This commit is contained in:
@@ -39,7 +39,6 @@
|
||||
#include <utils/debug.h>
|
||||
#include <utils/chunk.h>
|
||||
#include <collections/enumerator.h>
|
||||
#include <threading/spinlock.h>
|
||||
#include <threading/mutex.h>
|
||||
#include <threading/condvar.h>
|
||||
|
||||
@@ -724,78 +723,6 @@ void nop()
|
||||
{
|
||||
}
|
||||
|
||||
#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
|
||||
|
||||
/**
|
||||
* Spinlock for ref_get/put
|
||||
*/
|
||||
static spinlock_t *ref_lock;
|
||||
|
||||
/**
|
||||
* Increase refcount
|
||||
*/
|
||||
refcount_t ref_get(refcount_t *ref)
|
||||
{
|
||||
refcount_t current;
|
||||
|
||||
ref_lock->lock(ref_lock);
|
||||
current = ++(*ref);
|
||||
ref_lock->unlock(ref_lock);
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrease refcount
|
||||
*/
|
||||
bool ref_put(refcount_t *ref)
|
||||
{
|
||||
bool more_refs;
|
||||
|
||||
ref_lock->lock(ref_lock);
|
||||
more_refs = --(*ref) > 0;
|
||||
ref_lock->unlock(ref_lock);
|
||||
return !more_refs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current refcount
|
||||
*/
|
||||
refcount_t ref_cur(refcount_t *ref)
|
||||
{
|
||||
refcount_t current;
|
||||
|
||||
ref_lock->lock(ref_lock);
|
||||
current = *ref;
|
||||
ref_lock->unlock(ref_lock);
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spinlock for all compare and swap operations.
|
||||
*/
|
||||
static spinlock_t *cas_lock;
|
||||
|
||||
/**
|
||||
* Compare and swap if equal to old value
|
||||
*/
|
||||
#define _cas_impl(name, type) \
|
||||
bool cas_##name(type *ptr, type oldval, type newval) \
|
||||
{ \
|
||||
bool swapped; \
|
||||
cas_lock->lock(cas_lock); \
|
||||
if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
|
||||
cas_lock->unlock(cas_lock); \
|
||||
return swapped; \
|
||||
}
|
||||
|
||||
_cas_impl(bool, bool)
|
||||
_cas_impl(ptr, void*)
|
||||
|
||||
#endif /* !HAVE_GCC_ATOMIC_OPERATIONS && !HAVE_GCC_SYNC_OPERATIONS */
|
||||
|
||||
|
||||
#ifdef HAVE_FMEMOPEN_FALLBACK
|
||||
|
||||
static int fmemread(chunk_t *cookie, char *buf, int size)
|
||||
@@ -848,12 +775,7 @@ void utils_init()
|
||||
#ifdef WIN32
|
||||
windows_init();
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
|
||||
ref_lock = spinlock_create();
|
||||
cas_lock = spinlock_create();
|
||||
#endif
|
||||
|
||||
atomics_init();
|
||||
strerror_init();
|
||||
}
|
||||
|
||||
@@ -865,12 +787,7 @@ void utils_deinit()
|
||||
#ifdef WIN32
|
||||
windows_deinit();
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
|
||||
ref_lock->destroy(ref_lock);
|
||||
cas_lock->destroy(cas_lock);
|
||||
#endif
|
||||
|
||||
atomics_deinit();
|
||||
strerror_deinit();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user