[pypy-svn] r23280 - pypy/dist/pypy/lib/logic/computation_space

auc at codespeak.net auc at codespeak.net
Mon Feb 13 15:42:45 CET 2006


Author: auc
Date: Mon Feb 13 15:42:38 2006
New Revision: 23280

Modified:
   pypy/dist/pypy/lib/logic/computation_space/test_variable.py
   pypy/dist/pypy/lib/logic/computation_space/variable.py
Log:
multiple readers list


Modified: pypy/dist/pypy/lib/logic/computation_space/test_variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/test_variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/test_variable.py	Mon Feb 13 15:42:38 2006
@@ -130,7 +130,6 @@
         assert reductor.result == 725760
 
     def test_daisychain_stream(self):
-        # chained stupidity
         sp = space.ComputationSpace(dummy_problem)
 
         def woman_in_chains(thread, S):
@@ -162,3 +161,43 @@
 
         assert woman.result == 6
                 
+    def test_multiple_readers_list(self):
+        sp = space.ComputationSpace(dummy_problem)
+        
+        def generate(thread, L, N):
+            n=N.get()
+            assert 0 < n < 32768
+            l = v.Pair(0, None)
+            L.bind(l)
+            for i in range(1,n):
+                l.set_rest(v.Pair(i, None))
+                l = l.rest()
+            l.set_rest(v.NoValue)
+
+        def reduc(thread, L, fun):
+            l=L.get()
+            thread.result = 0
+            while l != v.NoValue:
+                val = l.first()
+                thread.result = fun(thread.result, val)
+                l = l.rest()
+            
+        L = sp.var('L')
+        N = sp.var('N')
+
+        r1 = FunThread(reduc, L, operator.add)
+        r2 = FunThread(reduc, L, operator.add)
+        r3 = FunThread(reduc, L, operator.add)
+        generator = FunThread(generate, L, N)
+
+        r1.start()
+        r2.start()
+        r3.start()
+        generator.start()
+
+        N.bind(42)
+
+        generator.join()
+        for r in (r1, r2, r3):
+            r.join()
+            assert r.result == 861

Modified: pypy/dist/pypy/lib/logic/computation_space/variable.py
==============================================================================
--- pypy/dist/pypy/lib/logic/computation_space/variable.py	(original)
+++ pypy/dist/pypy/lib/logic/computation_space/variable.py	Mon Feb 13 15:42:38 2006
@@ -137,6 +137,9 @@
     def first(self):
         return self._car
 
+    def set_first(self, first):
+        self._cdr = first
+
     def rest(self):
         return self._cdr
 
@@ -203,6 +206,31 @@
         curr = curr.rest()
     return head
 
+class CList(Pair):
+    """A List supporting concurrent access"""
+
+    def __init__(self, *args):
+        Pair.__init__(*args)
+        self.last_condition = threading.Condition()
+
+    def set_rest(self, rest):
+        self.last_condition.acquire()
+        try:
+            self._cdr = rest
+            self.last_condition.notifyAll()
+        finally:
+            self.last_condition.release()
+
+    def rest(self):
+        self.last_condition.acquire()
+        try:
+            while self._cdr == None:
+                self.condition.wait()
+            return self._cdr
+        finally:
+            self.last_condition.release()
+        
+
 class Stream(object):
     """A FIFO stream"""
 



More information about the Pypy-commit mailing list