Remove duplicate AO_spin and AO_pause definition in atomic_ops_stack
authorIvan Maidanski <ivmai@mail.ru>
Thu, 4 Oct 2012 12:49:03 +0000 (16:49 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Thu, 4 Oct 2012 12:53:48 +0000 (16:53 +0400)
* src/atomic_ops.c (AO_REQUIRE_CAS): Undefine and include atomic_ops.h
unconditionally.
* src/atomic_ops.c (AO_USE_WIN32_PTHREADS): Define macro (and include
windows.h) if Win32 (non-POSIX) to have Sleep-based implementation of
AO_pause.
* src/atomic_ops.c (dummy, AO_spin, AO_pause): Define unconditionally
(to be always available for atomic_ops_stack).
* src/atomic_ops.c (dummy): Rename to spin_dummy.
* src/atomic_ops_stack.c (dummy, AO_spin, AO_pause): Remove definition
(for Win32); remove FIXME.
* src/atomic_ops_stack.c (AO_pause): Declare if
AO_USE_ALMOST_LOCK_FREE defined.

src/atomic_ops.c
src/atomic_ops_stack.c

index 6842ac0..3017fc4 100644 (file)
 # define AO_USE_NO_SIGNALS
 #endif
 
+#undef AO_REQUIRE_CAS
+#include "atomic_ops.h" /* Without cas emulation! */
+
 #if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__BORLANDC__) \
     || defined(AO_USE_NO_SIGNALS)
 
-#undef AO_REQUIRE_CAS
-
 #include <pthread.h>
 
 #ifndef AO_USE_NO_SIGNALS
@@ -66,8 +67,6 @@
 # include <sys/select.h>
 #endif
 
-#include "atomic_ops.h"  /* Without cas emulation! */
-
 #ifndef AO_HAVE_double_t
 # include "atomic_ops/sysdeps/standard_ao_double_t.h"
 #endif
@@ -101,42 +100,7 @@ AO_TS_t AO_locks[AO_HASH_SIZE] = {
   AO_TS_INITIALIZER, AO_TS_INITIALIZER, AO_TS_INITIALIZER, AO_TS_INITIALIZER,
 };
 
-static AO_t dummy = 1;
-
-/* Spin for 2**n units. */
-static void AO_spin(int n)
-{
-  AO_t j = AO_load(&dummy);
-  int i = 2 << n;
-
-  while (i-- > 0)
-    j += (j - 1) << 2;
-  /* Given 'dummy' is initialized to 1, j is 1 after the loop.  */
-  AO_store(&dummy, j);
-}
-
-void AO_pause(int n)
-{
-  if (n < 12)
-    AO_spin(n);
-  else
-    {
-#     ifdef AO_USE_NANOSLEEP
-        struct timespec ts;
-        ts.tv_sec = 0;
-        ts.tv_nsec = (n > 28 ? 100000 * 1000 : 1 << (n - 2));
-        nanosleep(&ts, 0);
-#     elif defined(AO_USE_WIN32_PTHREADS)
-        Sleep(n > 28 ? 100 : n < 22 ? 1 : 1 << (n - 22)); /* in millis */
-#     else
-        struct timeval tv;
-        /* Short async-signal-safe sleep. */
-        tv.tv_sec = 0;
-        tv.tv_usec = n > 28 ? 100000 : 1 << (n - 12);
-        select(0, 0, 0, 0, &tv);
-#     endif
-    }
-}
+void AO_pause(int); /* defined below */
 
 static void lock_ool(volatile AO_TS_t *l)
 {
@@ -241,6 +205,48 @@ void AO_store_full_emulation(volatile AO_t *addr, AO_t val)
 
 #else /* Non-posix platform */
 
-extern int AO_non_posix_implementation_is_entirely_in_headers;
+# include <windows.h>
+
+# define AO_USE_WIN32_PTHREADS
+                /* define to use Sleep() */
+
+  extern int AO_non_posix_implementation_is_entirely_in_headers;
 
 #endif
+
+static AO_t spin_dummy = 1;
+
+/* Spin for 2**n units. */
+static void AO_spin(int n)
+{
+  AO_t j = AO_load(&spin_dummy);
+  int i = 2 << n;
+
+  while (i-- > 0)
+    j += (j - 1) << 2;
+  /* Given 'spin_dummy' is initialized to 1, j is 1 after the loop.     */
+  AO_store(&spin_dummy, j);
+}
+
+void AO_pause(int n)
+{
+  if (n < 12)
+    AO_spin(n);
+  else
+    {
+#     ifdef AO_USE_NANOSLEEP
+        struct timespec ts;
+        ts.tv_sec = 0;
+        ts.tv_nsec = (n > 28 ? 100000 * 1000 : 1 << (n - 2));
+        nanosleep(&ts, 0);
+#     elif defined(AO_USE_WIN32_PTHREADS)
+        Sleep(n > 28 ? 100 : n < 22 ? 1 : 1 << (n - 22)); /* in millis */
+#     else
+        struct timeval tv;
+        /* Short async-signal-safe sleep. */
+        tv.tv_sec = 0;
+        tv.tv_usec = n > 28 ? 100000 : 1 << (n - 12);
+        select(0, 0, 0, 0, &tv);
+#     endif
+    }
+}
index c19409e..97c8da6 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <assert.h>
+
 #define AO_REQUIRE_CAS
 #include "atomic_ops_stack.h"
 
-#if defined(_MSC_VER) \
-    || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
-  /* AO_pause not defined elsewhere */
-  /* FIXME: At least AO_spin should be factored out.    */
-#include <windows.h>
-
-static AO_t dummy = 1;
-
-/* Spin for 2**n units. */
-static void AO_spin(int n)
-{
-  AO_t j = AO_load(&dummy);
-  int i = 2 << n;
-
-  while (i-- > 0)
-    j += (j - 1) << 2;
-  /* Given 'dummy' is initialized to 1, j is 1 after the loop.  */
-  AO_store(&dummy, j);
-}
-
-void AO_pause(int n)
-{
-    if (n < 12)
-      AO_spin(n);
-    else
-      {
-        DWORD msecs;
-
-        /* Short async-signal-safe sleep. */
-        msecs = n > 28 ? 100 : n < 22 ? 1 : 1 << (n - 22); /* in millis */
-        Sleep(msecs);
-      }
-}
-
-#else
-
-/* AO_pause is available elsewhere */
-
-extern void AO_pause(int);
-
-#endif
-
 #ifdef AO_USE_ALMOST_LOCK_FREE
 
+  void AO_pause(int); /* defined in atomic_ops.c */
+
 /* LIFO linked lists based on compare-and-swap.  We need to avoid       */
 /* the case of a node deletion and reinsertion while I'm deleting       */
 /* it, since that may cause my CAS to succeed eventhough the next       */