[beast: 23/57] SFI: provide Bse::Spinlock type which supports static construction



commit 5d3f0dbecb09cda2253aebe3f8e20e2329aa5d1d
Author: Tim Janik <timj gnu org>
Date:   Sun Jul 16 14:15:01 2017 +0200

    SFI: provide Bse::Spinlock type which supports static construction
    
    Signed-off-by: Tim Janik <timj gnu org>

 configure.ac       |   38 ++++++++++++++++++++++++++++++++++++++
 sfi/Makefile.am    |    1 +
 sfi/bcore.hh       |   19 +++++++++++++++++++
 sfi/sysconfig.h.in |    3 ++-
 4 files changed, 60 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 0cc91e9..2803c18 100644
--- a/configure.ac
+++ b/configure.ac
@@ -332,6 +332,44 @@ if test -r ebeast/package.json.in ; then
 fi
 AM_CONDITIONAL(WITH_NPM, [test "_$with_usable_npm" = "_yes"])
 
+# == pthread_spinlock_t initializer ==
+AC_LANG_PUSH([C])
+saveed_LDFLAGS="$LDFLAGS" ; LDFLAGS="-pthread $LDFLAGS"
+AC_TRY_RUN(
+  [
+    #include <stdio.h>
+    #include <string.h>
+    #include <assert.h>
+    #include <pthread.h>
+    struct Spin { pthread_spinlock_t dummy1, s1, dummy2, s2, dummy3; };
+    int
+    main (int argc, char *argv[])
+    {
+      struct Spin spin;
+      memset (&spin, 0xffffffff, sizeof (spin));
+      if (pthread_spin_init (&spin.s1, 0) == 0 &&
+          pthread_spin_init (&spin.s2, 0) == 0 &&
+         sizeof (pthread_spinlock_t) == 4 && // current implementation assumption
+         spin.s1 == spin.s2) // check for location-independent initialization
+        {
+          FILE *f = fopen ("conftest_spinlock_initializer", "w");
+          assert (f);
+          fprintf (f, "/*{*/ ");
+          fprintf (f, "0x%04x,", *(int*) &spin.s1);
+          fprintf (f, " /*}*/\n");
+          fclose (f);
+        }
+        return 0;
+    } ],
+  BSE_SPINLOCK_INITIALIZER=`cat conftest_spinlock_initializer` && rm -f conftest_spinlock_initializer ,
+  BSE_SPINLOCK_INITIALIZER='' )
+LDFLAGS="$saveed_LDFLAGS"
+AC_LANG_POP([C])
+test -z "$BSE_SPINLOCK_INITIALIZER" && {
+  AC_MSG_FAILURE([spinlock initializer check failed to execute])
+}
+AC_SUBST(BSE_SPINLOCK_INITIALIZER)
+
 dnl #
 dnl # Define package requirements.
 dnl #
diff --git a/sfi/Makefile.am b/sfi/Makefile.am
index b5bea60..686ef65 100644
--- a/sfi/Makefile.am
+++ b/sfi/Makefile.am
@@ -43,6 +43,7 @@ libsfi_@MAJOR@include_HEADERS = $(sfi_public_headers) sysconfig.h
 libsfi_@MAJOR@_la_SOURCES     = $(sfi_all_sources)
 libsfi_@MAJOR@_la_CXXFLAGS    = $(AM_CXXFLAGS) $(LIBBSE_CFLAGS) -DBSE_CONVENIENCE
 libsfi_@MAJOR@_la_LIBADD      = $(LIBBSE_LIBS)
+$(sfi_public_headers):         sysconfig.h
 
 #
 # programs to build
diff --git a/sfi/bcore.hh b/sfi/bcore.hh
index 930cf28..ab55e61 100644
--- a/sfi/bcore.hh
+++ b/sfi/bcore.hh
@@ -147,6 +147,25 @@ info (const char *format, const Args &...args)
 #define return_unless(cond, ...)        BSE_RETURN_UNLESS (cond, __VA_ARGS__)
 #endif // BSE_CONVENIENCE
 
+// == Threading ==
+/**
+ * The Spinlock uses low-latency busy spinning to acquire locks.
+ * This class is a thin wrapper around pthread_spin_lock() and related functions.
+ * This class supports static construction.
+ */
+class Spinlock {
+  pthread_spinlock_t spinlock_;
+public:
+  constexpr Spinlock    () : spinlock_ { BSE_SPINLOCK_INITIALIZER } {}
+  void      lock        ()      { pthread_spin_lock (&spinlock_); }
+  void      unlock      ()      { pthread_spin_unlock (&spinlock_); }
+  bool      try_lock    ()      { return 0 == pthread_spin_trylock (&spinlock_); }
+  typedef pthread_spinlock_t* native_handle_type;
+  native_handle_type native_handle() { return &spinlock_; }
+  /*ctor*/  Spinlock    (const Spinlock&) = delete;
+  Spinlock& operator=   (const Spinlock&) = delete;
+};
+
 } // Bse
 
 #endif // __BSE_BCORE_HH__
diff --git a/sfi/sysconfig.h.in b/sfi/sysconfig.h.in
index 5dc497f..2fb9b48 100644
--- a/sfi/sysconfig.h.in
+++ b/sfi/sysconfig.h.in
@@ -4,6 +4,7 @@
 #define __BSE_SYSCONFIG_H__
 
 // system specifics
-#define BSE_SIZEOF_SYS_TYPESH_UINT         @BSE_SIZEOF_SYS_TYPESH_UINT@
+#define BSE_SIZEOF_SYS_TYPESH_UINT      @BSE_SIZEOF_SYS_TYPESH_UINT@
+#define BSE_SPINLOCK_INITIALIZER        @BSE_SPINLOCK_INITIALIZER@
 
 #endif /* __BSE_SYSCONFIG_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]