#include <limits.h>
#include <dirent.h>
#include <time.h>
-#include <pthread.h>
-#include "utils.h"
-
-#include "collections/enumerator.h"
-#include "utils/debug.h"
-#include "utils/chunk.h"
+#include <library.h>
+#include <utils/debug.h>
+#include <utils/chunk.h>
+#include <collections/enumerator.h>
+#include <threading/spinlock.h>
ENUM(status_names, SUCCESS, NEED_MORE,
"SUCCESS",
"NEED_MORE",
);
-/**
- * See header
- */
-void utils_init()
-{
-#ifdef WIN32
- windows_init();
-#endif /* WIN32 */
- strerror_init();
-}
-
-/**
- * See header
- */
-void utils_deinit()
-{
-#ifdef WIN32
- windows_deinit();
-#endif /* WIN32 */
- strerror_deinit();
-}
-
/**
* Described in header.
*/
#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
/**
- * We use a single mutex for all refcount variables.
+ * Spinlock for ref_get/put
*/
-static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
+static spinlock_t *ref_lock;
/**
* Increase refcount
{
refcount_t current;
- pthread_mutex_lock(&ref_mutex);
+ ref_lock->lock(ref_lock);
current = ++(*ref);
- pthread_mutex_unlock(&ref_mutex);
+ ref_lock->unlock(ref_lock);
+
return current;
}
{
bool more_refs;
- pthread_mutex_lock(&ref_mutex);
+ ref_lock->lock(ref_lock);
more_refs = --(*ref) > 0;
- pthread_mutex_unlock(&ref_mutex);
+ ref_lock->unlock(ref_lock);
return !more_refs;
}
{
refcount_t current;
- pthread_mutex_lock(&ref_mutex);
+ ref_lock->lock(ref_lock);
current = *ref;
- pthread_mutex_unlock(&ref_mutex);
+ ref_lock->unlock(ref_lock);
+
return current;
}
/**
- * Single mutex for all compare and swap operations.
+ * Spinlock for all compare and swap operations.
*/
-static pthread_mutex_t cas_mutex = PTHREAD_MUTEX_INITIALIZER;
+static spinlock_t *cas_lock;
/**
* Compare and swap if equal to old value
bool cas_##name(type *ptr, type oldval, type newval) \
{ \
bool swapped; \
- pthread_mutex_lock(&cas_mutex); \
+ cas_lock->lock(cas_lock); \
if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
- pthread_mutex_unlock(&cas_mutex); \
+ cas_lock->unlock(cas_lock); \
return swapped; \
}
#endif /* FMEMOPEN fallback*/
+/**
+ * See header
+ */
+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
+
+ strerror_init();
+}
+
+/**
+ * See header
+ */
+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
+
+ strerror_deinit();
+}
+
/**
* Described in header.
*/