Cleaner builtin check for atomic sync
@@ -15,48 +15,42 @@ spinlock / sync for tasks
#define _GNU_SOURCE
#include <time.h>
#endif /* _GNU_SOURCE */
-
#include <stdlib.h>
-/** locks use a single byte */
-typedef volatile unsigned char spn_lock_i;
-
-/** The initail value of an unlocked spinlock. */
-#define SPN_LOCK_INIT 0
-
-/*********
- * manage the way threads "wait" for the lock to release
- */
+/* manage the way threads "wait" for the lock to release */
#if defined(__unix__) || defined(__APPLE__) || defined(__linux__)
/* nanosleep seems to be the most effective and efficient reschedule */
-#define reschedule_thread() \
- { \
- static const struct timespec tm = {.tv_nsec = 1}; \
- nanosleep(&tm, NULL); \
- }
-#define throttle_thread() \
+#define defer_nanosleep(length) \
{ \
- static const struct timespec tm = {.tv_nsec = 8388608UL}; \
+ static const struct timespec tm = {.tv_nsec = length}; \
nanosleep(&tm, NULL); \
}
+#define reschedule_thread() defer_nanosleep(1)
+#define throttle_thread() defer_nanosleep(8388608UL)
#else /* no effective rescheduling, just spin... */
#define reschedule_thread()
#define throttle_thread()
#endif
-#if !__clang__ && (__GNUC__ > 3)
-#define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
-#elif !defined(__has_builtin)
-#error Required feature check "__has_builtin" missing from this compiler.
-#else
+/** locks use a single byte */
+typedef volatile unsigned char spn_lock_i;
+/** The initail value of an unlocked spinlock. */
+#define SPN_LOCK_INIT 0
+
+/* Select the correct compiler builtin method. */
+#if defined(__has_builtin)
#if __has_builtin(__sync_swap)
#define SPN_LOCK_BUILTIN(...) __sync_swap(__VA_ARGS__)
#elif __has_builtin(__sync_fetch_and_or)
#define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
#else
#error Required builtin "__sync_swap" or "__sync_fetch_and_or" missing from compiler.
-#endif
+#endif /* defined(__has_builtin) */
+#elif __GNUC__ > 3
+#define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
+#else
+#error Required builtin "__sync_swap" or "__sync_fetch_and_or" not found.
#endif
/** returns 1 and 0 if the lock was successfully aquired (TRUE == FAIL). */
