[x265] [PATCH] threading: add a slow-path for platforms with no system atomics (closes #85)

Steve Borho steve at borho.org
Tue Aug 18 06:30:06 CEST 2015


# HG changeset patch
# User Steve Borho <steve at borho.org>
# Date 1439871404 -19800
#      Tue Aug 18 09:46:44 2015 +0530
# Node ID bc50a4b091336274b6c18b8942171fcb54cf111d
# Parent  996ebce8c874fc511d495cee227d24413e99d0c1
threading: add a slow-path for platforms with no system atomics (closes #85)

diff -r 996ebce8c874 -r bc50a4b09133 source/CMakeLists.txt
--- a/source/CMakeLists.txt	Mon Aug 17 10:52:15 2015 +0530
+++ b/source/CMakeLists.txt	Tue Aug 18 09:46:44 2015 +0530
@@ -88,6 +88,10 @@
         endif()
     endif()
     mark_as_advanced(LIBRT NUMA_FOUND)
+    option(NO_ATOMICS "Use a slow mutex to replace atomics" OFF)
+    if(NO_ATOMICS)
+        add_definitions(-DNO_ATOMICS=1)
+    endif(NO_ATOMICS)
 endif(UNIX)
 
 if(X64 AND NOT WIN32)
diff -r 996ebce8c874 -r bc50a4b09133 source/common/threading.cpp
--- a/source/common/threading.cpp	Mon Aug 17 10:52:15 2015 +0530
+++ b/source/common/threading.cpp	Tue Aug 18 09:46:44 2015 +0530
@@ -35,6 +35,55 @@
 #define STACK_ALIGN(func, ...) func(__VA_ARGS__)
 #endif
 
+#if NO_ATOMICS
+pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+int no_atomic_or(int* ptr, int mask)
+{ 
+    pthread_mutex_lock(&g_mutex);
+    int ret = *ptr;
+    *ptr |= mask;
+    pthread_mutex_unlock(&g_mutex);
+    return ret;
+}
+
+int no_atomic_and(int* ptr, int mask)
+{
+    pthread_mutex_lock(&g_mutex);
+    int ret = *ptr;
+    *ptr &= mask;
+    pthread_mutex_unlock(&g_mutex);
+    return ret;
+}
+
+int no_atomic_inc(int* ptr)
+{
+    pthread_mutex_lock(&g_mutex);
+    *ptr += 1;
+    int ret = *ptr;
+    pthread_mutex_unlock(&g_mutex);
+    return ret;
+}
+
+int no_atomic_dec(int* ptr)
+{
+    pthread_mutex_lock(&g_mutex);
+    *ptr -= 1;
+    int ret = *ptr;
+    pthread_mutex_unlock(&g_mutex);
+    return ret;
+}
+
+int no_atomic_add(int* ptr, int val)
+{
+    pthread_mutex_lock(&g_mutex);
+    *ptr += val;
+    int ret = *ptr;
+    pthread_mutex_unlock(&g_mutex);
+    return ret;
+}
+#endif
+
 /* C shim for forced stack alignment */
 static void stackAlignMain(Thread *instance)
 {
diff -r 996ebce8c874 -r bc50a4b09133 source/common/threading.h
--- a/source/common/threading.h	Mon Aug 17 10:52:15 2015 +0530
+++ b/source/common/threading.h	Tue Aug 18 09:46:44 2015 +0530
@@ -42,7 +42,30 @@
 #include <sys/sysctl.h>
 #endif
 
-#ifdef __GNUC__               /* GCCs builtin atomics */
+#if NO_ATOMICS
+
+#include <sys/time.h>
+#include <unistd.h>
+
+namespace X265_NS {
+// x265 private namespace
+int no_atomic_or(int* ptr, int mask);
+int no_atomic_and(int* ptr, int mask);
+int no_atomic_inc(int* ptr);
+int no_atomic_dec(int* ptr);
+int no_atomic_add(int* ptr, int val);
+}
+
+#define CLZ(id, x)            id = (unsigned long)__builtin_clz(x) ^ 31
+#define CTZ(id, x)            id = (unsigned long)__builtin_ctz(x)
+#define ATOMIC_OR(ptr, mask)  no_atomic_or((int*)ptr, mask)
+#define ATOMIC_AND(ptr, mask) no_atomic_and((int*)ptr, mask)
+#define ATOMIC_INC(ptr)       no_atomic_inc((int*)ptr)
+#define ATOMIC_DEC(ptr)       no_atomic_dec((int*)ptr)
+#define ATOMIC_ADD(ptr, val)  no_atomic_add((int*)ptr, val)
+#define GIVE_UP_TIME()        usleep(0)
+
+#elif __GNUC__               /* GCCs builtin atomics */
 
 #include <sys/time.h>
 #include <unistd.h>
diff -r 996ebce8c874 -r bc50a4b09133 source/common/version.cpp
--- a/source/common/version.cpp	Mon Aug 17 10:52:15 2015 +0530
+++ b/source/common/version.cpp	Tue Aug 18 09:46:44 2015 +0530
@@ -82,6 +82,12 @@
 #define ASM     "[noasm]"
 #endif
  
+#if NO_ATOMICS
+#define ATOMICS "[no-atomics]"
+#else
+#define ATOMICS ""
+#endif
+
 #if CHECKED_BUILD
 #define CHECKED "[CHECKED] "
 #else
@@ -106,4 +112,4 @@
 #endif
 
 const char* PFX(version_str) = XSTR(X265_VERSION);
-const char* PFX(build_info_str) = ONOS COMPILEDBY BITS ASM CHECKED BITDEPTH;
+const char* PFX(build_info_str) = ONOS COMPILEDBY BITS ASM ATOMICS CHECKED BITDEPTH;


More information about the x265-devel mailing list