Author: Armin Rigo <ar...@tunes.org>
Branch: nogil-unsafe-2
Changeset: r90427:52bbdf98f118
Date: 2017-02-28 16:15 +0100
http://bitbucket.org/pypy/pypy/changeset/52bbdf98f118/

Log:    (remi, arigo) port some locks from stmgc

diff --git a/rpython/translator/c/src/mem.c b/rpython/translator/c/src/mem.c
--- a/rpython/translator/c/src/mem.c
+++ b/rpython/translator/c/src/mem.c
@@ -15,15 +15,19 @@
 
 static struct pypy_debug_alloc_s *pypy_debug_alloc_list = NULL;
 
+static rpy_spinlock_t pypy_debug_alloc_lock = 0;
+
 RPY_EXTERN
 void pypy_debug_alloc_start(void *addr, const char *funcname)
 {
   struct pypy_debug_alloc_s *p = malloc(sizeof(struct pypy_debug_alloc_s));
   RPyAssert(p, "out of memory");
-  p->next = pypy_debug_alloc_list;
   p->addr = addr;
   p->funcname = funcname;
+  rpy_spinlock_acquire(&pypy_debug_alloc_lock);
+  p->next = pypy_debug_alloc_list;
   pypy_debug_alloc_list = p;
+  rpy_spinlock_release(&pypy_debug_alloc_lock);
 }
 
 RPY_EXTERN
@@ -32,12 +36,14 @@
   struct pypy_debug_alloc_s **p;
   if (!addr)
        return;
+  rpy_spinlock_acquire(&pypy_debug_alloc_lock);
   for (p = &pypy_debug_alloc_list; *p; p = &((*p)->next))
     if ((*p)->addr == addr)
       {
         struct pypy_debug_alloc_s *dying;
         dying = *p;
         *p = dying->next;
+        rpy_spinlock_release(&pypy_debug_alloc_lock);
         free(dying);
         return;
       }
@@ -49,6 +55,7 @@
 {
   long count = 0;
   struct pypy_debug_alloc_s *p;
+  rpy_spinlock_acquire(&pypy_debug_alloc_lock);
   for (p = pypy_debug_alloc_list; p; p = p->next)
     count++;
   if (count > 0)
@@ -64,6 +71,7 @@
       else
         fprintf(stderr, " (use PYPY_ALLOC=1 to see the list)\n");
     }
+  rpy_spinlock_release(&pypy_debug_alloc_lock);
 }
 
 #endif /* RPY_ASSERT */
diff --git a/rpython/translator/c/src/thread.h 
b/rpython/translator/c/src/thread.h
--- a/rpython/translator/c/src/thread.h
+++ b/rpython/translator/c/src/thread.h
@@ -58,4 +58,15 @@
 //    return &rpy_fastgil;
 }
 
+typedef unsigned char rpy_spinlock_t;
+static inline void rpy_spinlock_acquire(rpy_spinlock_t *p)
+{
+    while (pypy_lock_test_and_set(p, 1) != 0)
+        pypy_spin_loop();
+}
+static inline void rpy_spinlock_release(rpy_spinlock_t *p)
+{
+    pypy_lock_release(p);
+}
+
 #endif
diff --git a/rpython/translator/c/src/thread_pthread.h 
b/rpython/translator/c/src/thread_pthread.h
--- a/rpython/translator/c/src/thread_pthread.h
+++ b/rpython/translator/c/src/thread_pthread.h
@@ -84,3 +84,9 @@
 
 #define pypy_lock_test_and_set(ptr, value)  __sync_lock_test_and_set(ptr, 
value)
 #define pypy_lock_release(ptr)              __sync_lock_release(ptr)
+
+#if defined(__i386__) || defined(__amd64__)
+   static inline void pypy_spin_loop(void) { asm("pause" : : : "memory"); }
+#else
+#  define pypy_spin_loop()  /* nothing */
+#endif
diff --git a/rpython/translator/c/test/test_standalone.py 
b/rpython/translator/c/test/test_standalone.py
--- a/rpython/translator/c/test/test_standalone.py
+++ b/rpython/translator/c/test/test_standalone.py
@@ -1431,8 +1431,15 @@
         import time, gc
         from rpython.rlib import rthread, rposix
 
+        class X:
+            def __init__(self, prev):
+                self.prev = prev
+
         def bootstrap():
             rthread.gc_thread_start()
+            x = None
+            for i in range(1000):
+                x = X(x)
             os.write(1, "hi there\n")
             rthread.gc_thread_die()
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to