git subrepo pull --force deps/lightning
[pcsx_rearmed.git] / deps / lightning / check / catomic.c
index 04a2f89..ef09076 100644 (file)
@@ -4,6 +4,46 @@
 #include <unistd.h>
 #include <signal.h>
 
+#if DEBUG
+volatile
+#endif
+jit_word_t     lock;
+pthread_t      tids[4];
+
+#if DEBUG
+int debug_offset(void)
+{
+    int                i;
+    pthread_t  self = pthread_self();
+    for (i = 0; i < 4; ++i)
+       if (tids[i] == self)
+           return i;
+    return -1;
+}
+
+void debug_spin(void)
+{
+    printf("  spin %d : %ld\n", debug_offset(), lock);
+}
+
+void debug_lock(void)
+{
+    printf("  lock %d : %ld\n", debug_offset(), lock);
+}
+
+void debug_unlock(void)
+{
+    printf("unlock %d : %ld\n", debug_offset(), lock);
+}
+#define DEBUG_SPIN()   jit_calli(debug_spin)
+#define DEBUG_LOCK()   jit_calli(debug_lock)
+#define DEBUG_UNLOCK() jit_calli(debug_unlock)
+#else
+#define DEBUG_SPIN()   /**/
+#define DEBUG_LOCK()   /**/
+#define DEBUG_UNLOCK() /**/
+#endif
+
 void alarm_handler(int unused)
 {
     _exit(1);
@@ -17,8 +57,6 @@ main(int argc, char *argv[])
     jit_node_t          *jmpi_main, *label;
     jit_node_t          *func0, *func1, *func2, *func3;
     jit_node_t          *patch0, *patch1, *patch2, *patch3;
-    jit_word_t           lock;
-    pthread_t            tids[4];
 
     /* If there is any bug, do not hang in "make check" */
     signal(SIGALRM, alarm_handler);
@@ -35,31 +73,36 @@ main(int argc, char *argv[])
     name = jit_label();                                                \
      jit_prolog();                                             \
     jit_movi(JIT_V0, (jit_word_t)&lock);                       \
-    jit_movi(JIT_R1, 0);                                       \
-    jit_movi(JIT_R2, line);                                    \
+    jit_movi(JIT_V1, 0);                                       \
+    jit_movi(JIT_V2, line);                                    \
     /* spin until get the lock */                              \
+    DEBUG_SPIN();                                              \
     label = jit_label();                                       \
-    jit_casr(JIT_R0, JIT_V0, JIT_R1, JIT_R2);                  \
+    jit_casr(JIT_R0, JIT_V0, JIT_V1, JIT_V2);                  \
     jit_patch_at(jit_beqi(JIT_R0, 0), label);                  \
     /* lock acquired */                                                \
+    DEBUG_LOCK();                                              \
     jit_prepare();                                             \
-    /* pretend to be doing something useful for 0.01 usec
+    /* pretend to be doing something useful for 0.01 sec
      * while holding the lock */                               \
     jit_pushargi(10000);                                       \
     jit_finishi(usleep);                                       \
     /* release lock */                                         \
-    jit_movi(JIT_R1, 0);                                       \
-    jit_str(JIT_V0, JIT_R1);                                   \
+    DEBUG_UNLOCK();                                            \
+    jit_movi(JIT_V1, 0);                                       \
+    jit_str(JIT_V0, JIT_V1);                                   \
     /* Now test casi */                                                \
-    jit_movi(JIT_R1, 0);                                       \
-    jit_movi(JIT_R2, line);                                    \
+    jit_movi(JIT_V1, 0);                                       \
+    jit_movi(JIT_V2, line);                                    \
     /* spin until get the lock */                              \
+    DEBUG_SPIN();                                              \
     label = jit_label();                                       \
-    jit_casi(JIT_R0, (jit_word_t)&lock, JIT_R1, JIT_R2);       \
+    jit_casi(JIT_R0, (jit_word_t)&lock, JIT_V1, JIT_V2);       \
     jit_patch_at(jit_beqi(JIT_R0, 0), label);                  \
     /* lock acquired */                                                \
+    DEBUG_LOCK();                                              \
     jit_prepare();                                             \
-    /* pretend to be doing something useful for 0.01 usec
+    /* pretend to be doing something useful for 0.01 sec
      * while holding the lock */                               \
     jit_pushargi(10000);                                       \
     jit_finishi(usleep);                                       \
@@ -69,8 +112,9 @@ main(int argc, char *argv[])
     /*jit_pushargi((jit_word_t)#name);*/                       \
     jit_finishi(puts);                                         \
     /* release lock */                                         \
-    jit_movi(JIT_R1, 0);                                       \
-    jit_str(JIT_V0, JIT_R1);                                   \
+    DEBUG_UNLOCK();                                            \
+    jit_movi(JIT_V1, 0);                                       \
+    jit_str(JIT_V0, JIT_V1);                                   \
     jit_ret();                                                 \
     jit_epilog();
     defun(func0, __LINE__);
@@ -126,7 +170,7 @@ main(int argc, char *argv[])
 
     code = jit_emit();
 
-#if 1
+#if DEBUG
     jit_disassemble();
 #endif