[pypy-commit] pypy stm-gc: Expose bool_cas() to the RPython level. Interface tentative so far.

arigo noreply at buildbot.pypy.org
Sat Mar 31 16:33:07 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: stm-gc
Changeset: r54108:b089f7f2a92e
Date: 2012-03-31 16:13 +0200
http://bitbucket.org/pypy/pypy/changeset/b089f7f2a92e/

Log:	Expose bool_cas() to the RPython level. Interface tentative so far.

diff --git a/pypy/rlib/atomic_ops.py b/pypy/rlib/atomic_ops.py
new file mode 100644
--- /dev/null
+++ b/pypy/rlib/atomic_ops.py
@@ -0,0 +1,23 @@
+import py
+from pypy.tool.autopath import pypydir
+from pypy.rpython.lltypesystem import lltype, llmemory, rffi
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+
+
+cdir = py.path.local(pypydir) / 'translator' / 'stm'
+cdir2 = py.path.local(pypydir) / 'translator' / 'c'
+
+eci = ExternalCompilationInfo(
+    include_dirs = [cdir, cdir2],
+    post_include_bits = ['''
+#include "src_stm/atomic_ops.h"
+#define pypy_bool_cas(ptr, old, _new)                \\
+           bool_cas((volatile unsigned long*)(ptr),  \\
+                    (unsigned long)(old),            \\
+                    (unsigned long)(_new))
+'''],
+)
+
+
+bool_cas = rffi.llexternal('pypy_bool_cas', [llmemory.Address]*3, lltype.Bool,
+                           compilation_info=eci, macro=True)
diff --git a/pypy/rlib/test/test_atomic_ops.py b/pypy/rlib/test/test_atomic_ops.py
new file mode 100644
--- /dev/null
+++ b/pypy/rlib/test/test_atomic_ops.py
@@ -0,0 +1,31 @@
+from pypy.rlib.atomic_ops import bool_cas
+from pypy.rpython.lltypesystem import lltype, llmemory, rffi
+
+
+ARRAY = rffi.CArray(llmemory.Address)
+
+def test_bool_cas():
+    a = lltype.malloc(ARRAY, 1, flavor='raw')
+    a[0] = rffi.cast(llmemory.Address, 42)
+    #
+    res = bool_cas(rffi.cast(llmemory.Address, a),
+                   rffi.cast(llmemory.Address, 42),
+                   rffi.cast(llmemory.Address, 43))
+    assert res == True
+    assert rffi.cast(lltype.Signed, a[0]) == 43
+    #
+    res = bool_cas(rffi.cast(llmemory.Address, a),
+                   rffi.cast(llmemory.Address, 42),
+                   rffi.cast(llmemory.Address, 44))
+    assert res == False
+    assert rffi.cast(lltype.Signed, a[0]) == 43
+    #
+    lltype.free(a, flavor='raw')
+    return 0
+
+def test_translate_bool_cas():
+    from pypy.translator.c.test.test_genc import compile
+
+    f = compile(test_bool_cas, [])
+    res = f()
+    assert res == 0
diff --git a/pypy/translator/c/src/g_prerequisite.h b/pypy/translator/c/src/g_prerequisite.h
--- a/pypy/translator/c/src/g_prerequisite.h
+++ b/pypy/translator/c/src/g_prerequisite.h
@@ -17,11 +17,4 @@
 #include <stddef.h>
 
 
-#ifdef __GNUC__       /* other platforms too, probably */
-typedef _Bool bool_t;
-#else
-typedef unsigned char bool_t;
-#endif
-
-
 #include "src/align.h"
diff --git a/pypy/translator/stm/src_stm/atomic_ops.h b/pypy/translator/stm/src_stm/atomic_ops.h
--- a/pypy/translator/stm/src_stm/atomic_ops.h
+++ b/pypy/translator/stm/src_stm/atomic_ops.h
@@ -1,3 +1,5 @@
+#ifndef _SRCSTM_ATOMIC_OPS_
+#define _SRCSTM_ATOMIC_OPS_
 
 
 /* "compiler fence" for preventing reordering of loads/stores to
@@ -45,3 +47,6 @@
      relevant data from memory after the spinloop */
   asm volatile ("pause":::"memory");
 }
+
+
+#endif  /* _SRCSTM_ATOMIC_OPS_ */
diff --git a/pypy/translator/tool/cbuild.py b/pypy/translator/tool/cbuild.py
--- a/pypy/translator/tool/cbuild.py
+++ b/pypy/translator/tool/cbuild.py
@@ -329,4 +329,9 @@
    typedef unsigned long Unsigned;
 #  define SIGNED_MIN LONG_MIN
 #endif
+#ifdef __GNUC__       /* other platforms too, probably */
+typedef _Bool bool_t;
+#else
+typedef unsigned char bool_t;
+#endif
 '''


More information about the pypy-commit mailing list