[pypy-commit] stmgc hashtable: Add a test about hashtable conflicts

arigo noreply at buildbot.pypy.org
Sat Oct 18 17:56:54 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: hashtable
Changeset: r1482:40201225092d
Date: 2014-10-18 17:57 +0200
http://bitbucket.org/pypy/stmgc/changeset/40201225092d/

Log:	Add a test about hashtable conflicts

diff --git a/c7/test/support.py b/c7/test/support.py
--- a/c7/test/support.py
+++ b/c7/test/support.py
@@ -170,7 +170,9 @@
 stm_hashtable_t *stm_hashtable_create(void);
 void stm_hashtable_free(stm_hashtable_t *);
 object_t *stm_hashtable_read(stm_hashtable_t *, uintptr_t key);
-void stm_hashtable_write(stm_hashtable_t *, uintptr_t key, object_t *nvalue);
+bool _check_hashtable_read(stm_hashtable_t *, uintptr_t key);
+object_t *hashtable_read_result;
+bool _check_hashtable_write(stm_hashtable_t *, uintptr_t key, object_t *nvalue);
 uint32_t stm_hashtable_entry_userdata;
 """)
 
@@ -247,6 +249,18 @@
     CHECKED(stm_become_globally_unique_transaction(tl, "TESTGUT"));
 }
 
+object_t *hashtable_read_result;
+
+bool _check_hashtable_read(stm_hashtable_t *h, uintptr_t key)
+{
+    CHECKED(hashtable_read_result = stm_hashtable_read(h, key));
+}
+
+bool _check_hashtable_write(stm_hashtable_t *h, uintptr_t key, object_t *nvalue)
+{
+    CHECKED(stm_hashtable_write(h, key, nvalue));
+}
+
 #undef CHECKED
 
 
@@ -566,7 +580,6 @@
 
 
 
-
 SHADOWSTACK_LENGTH = 1000
 _keepalive = weakref.WeakKeyDictionary()
 
diff --git a/c7/test/test_hashtable.py b/c7/test/test_hashtable.py
--- a/c7/test/test_hashtable.py
+++ b/c7/test/test_hashtable.py
@@ -2,53 +2,86 @@
 import random
 import py
 
+
+class StmHashTable(object):
+    def __init__(self):
+        self.h = lib.stm_hashtable_create()
+
+    def free(self):
+        lib.stm_hashtable_free(self.h)
+
+    def __getitem__(self, key):
+        res = lib._check_hashtable_read(self.h, key)
+        if res:
+            raise Conflict
+        return lib.hashtable_read_result
+
+    def __setitem__(self, key, nvalue):
+        res = lib._check_hashtable_write(self.h, key, nvalue)
+        if res:
+            raise Conflict
+
+
 class TestHashtable(BaseTest):
 
     def test_empty(self):
         self.start_transaction()
-        h = lib.stm_hashtable_create()
+        h = StmHashTable()
         for i in range(100):
             index = random.randrange(0, 1<<64)
-            got = lib.stm_hashtable_read(h, index)
+            got = h[index]
             assert got == ffi.NULL
-        lib.stm_hashtable_free(h)
+        h.free()
 
     def test_set_value(self):
         self.start_transaction()
-        h = lib.stm_hashtable_create()
+        h = StmHashTable()
         lp1 = stm_allocate(16)
-        lib.stm_hashtable_write(h, 12345678901, lp1)
-        assert lib.stm_hashtable_read(h, 12345678901) == lp1
+        h[12345678901] = lp1
+        assert h[12345678901] == lp1
         for i in range(64):
             index = 12345678901 ^ (1 << i)
-            assert lib.stm_hashtable_read(h, index) == ffi.NULL
-        assert lib.stm_hashtable_read(h, 12345678901) == lp1
-        lib.stm_hashtable_free(h)
+            assert h[index] == ffi.NULL
+        assert h[12345678901] == lp1
+        h.free()
 
     def test_no_conflict(self):
-        h = lib.stm_hashtable_create()
+        h = StmHashTable()
         lp1 = stm_allocate_old(16)
         lp2 = stm_allocate_old(16)
         #
         self.start_transaction()
         stm_set_char(lp1, 'A')
-        lib.stm_hashtable_write(h, 1234, lp1)
+        h[1234] = lp1
         self.commit_transaction()
         #
         self.start_transaction()
         stm_set_char(lp2, 'B')
-        lib.stm_hashtable_write(h, 9991234, lp2)
+        h[9991234] = lp2
         #
         self.switch(1)
         self.start_transaction()
-        lp1b = lib.stm_hashtable_read(h, 1234)
+        lp1b = h[1234]
         assert stm_get_char(lp1b) == 'A'
         assert lp1b == lp1
         self.commit_transaction()
         #
         self.switch(0)
-        assert lib.stm_hashtable_read(h, 9991234) == lp2
+        assert h[9991234] == lp2
         assert stm_get_char(lp2) == 'B'
-        assert lib.stm_hashtable_read(h, 1234) == lp1
-        lib.stm_hashtable_write(h, 1234, ffi.NULL)
+        assert h[1234] == lp1
+        h[1234] = ffi.NULL
         self.commit_transaction()
+        h.free()
+
+    def test_conflict(self):
+        h = StmHashTable()
+        lp1 = stm_allocate_old(16)
+        lp2 = stm_allocate_old(16)
+        #
+        self.start_transaction()
+        h[1234] = lp1
+        #
+        self.switch(1)
+        self.start_transaction()
+        py.test.raises(Conflict, "h[1234] = lp2")


More information about the pypy-commit mailing list