[pypy-svn] r33061 - in pypy/dist/pypy/objspace: . cclp cclp/constraint

auc at codespeak.net auc at codespeak.net
Mon Oct 9 16:52:38 CEST 2006


Author: auc
Date: Mon Oct  9 16:52:33 2006
New Revision: 33061

Modified:
   pypy/dist/pypy/objspace/cclp/constraint/distributor.py
   pypy/dist/pypy/objspace/cclp/global_state.py
   pypy/dist/pypy/objspace/cclp/interp_var.py
   pypy/dist/pypy/objspace/cclp/scheduler.py
   pypy/dist/pypy/objspace/cclp/space.py
   pypy/dist/pypy/objspace/cclp/thread.py
   pypy/dist/pypy/objspace/cclp/thunk.py
   pypy/dist/pypy/objspace/cclp/variable.py
   pypy/dist/pypy/objspace/logic.py
Log:
replace global scheduler list by instance


Modified: pypy/dist/pypy/objspace/cclp/constraint/distributor.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/constraint/distributor.py	(original)
+++ pypy/dist/pypy/objspace/cclp/constraint/distributor.py	Mon Oct  9 16:52:33 2006
@@ -1,19 +1,14 @@
 import math
-from pypy.rpython.objectmodel import we_are_translated
 
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.baseobjspace import Wrappable
-from pypy.interpreter import baseobjspace, typedef, gateway
+from pypy.interpreter import baseobjspace, typedef
 from pypy.interpreter.gateway import interp2app
 
 from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.listobject import W_ListObject
 from pypy.objspace.std.stringobject import W_StringObject
 
-from pypy.objspace.cclp.types import W_AbstractDistributor, Solution, W_Var
-from pypy.objspace.cclp.misc import w, ClonableCoroutine, get_current_cspace
-from pypy.objspace.cclp.global_state import scheduler
-from pypy.objspace.cclp.interp_var import interp_free, interp_bind
+from pypy.objspace.cclp.types import W_AbstractDistributor
+from pypy.objspace.cclp.misc import w, get_current_cspace
 
 def distribute(space, w_strategy):
     assert isinstance(w_strategy, W_StringObject)

Modified: pypy/dist/pypy/objspace/cclp/global_state.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/global_state.py	(original)
+++ pypy/dist/pypy/objspace/cclp/global_state.py	Mon Oct  9 16:52:33 2006
@@ -1,3 +1,5 @@
 
-scheduler = []
+class State: pass
+
+sched = State()
 

Modified: pypy/dist/pypy/objspace/cclp/interp_var.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/interp_var.py	(original)
+++ pypy/dist/pypy/objspace/cclp/interp_var.py	Mon Oct  9 16:52:33 2006
@@ -1,6 +1,6 @@
 from pypy.objspace.cclp.variable import wait__Var, _assign_aliases, _entail
 from pypy.objspace.cclp.types import W_Root, W_Var, W_CVar
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 from pypy.objspace.cclp.misc import w
 
 
@@ -48,7 +48,7 @@
         assert isinstance(w_next, W_Var)
         _assign(w_curr, w_val)
         # notify the blocked threads
-        scheduler[0].unblock_on(w_curr)
+        sched.uler.unblock_on(w_curr)
         if w_next is w_var:
             break
         # switch to next

Modified: pypy/dist/pypy/objspace/cclp/scheduler.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/scheduler.py	(original)
+++ pypy/dist/pypy/objspace/cclp/scheduler.py	Mon Oct  9 16:52:33 2006
@@ -6,7 +6,7 @@
 from pypy.objspace.cclp.types import W_Var, W_FailedValue, aliases
 from pypy.objspace.cclp.misc import w, v, ClonableCoroutine
 from pypy.objspace.cclp.space import W_CSpace
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 
 #-- Singleton scheduler ------------------------------------------------
 
@@ -17,37 +17,22 @@
         self._main = ClonableCoroutine.w_getcurrent(space)
         assert isinstance(self._main, ClonableCoroutine)
         self._init_head(self._main)
-        self._init_blocked()
-        self._switch_count = 0
-        # more accounting
-        self._per_space_live_threads = {} # space -> nb runnable threads
-        self._traced = {} # thread -> vars
-        w("MAIN THREAD = ", str(id(self._main)))
-
-    def _init_blocked(self):
         self._blocked = {} # thread set
         # variables suspension lists
         self._blocked_on = {} # var -> threads
         self._blocked_byneed = {} # var -> threads
         self._asking = {} # thread -> cspace
+        self._switch_count = 0
+        # more accounting
+        self._per_space_live_threads = {} # space -> nb runnable threads
+        self._traced = {} # thread -> vars
+        w("MAIN THREAD = ", str(id(self._main)))
 
     def _init_head(self, thread):
         assert isinstance(thread, ClonableCoroutine)
         self._head = thread
         # for the reset case
         self._head._next = self._head._prev = self._head
-
-    def _check_initial_conditions(self):
-        try:
-            assert self._head._next == self._head._prev == self._head
-            assert self._head not in self._blocked
-            assert self._head not in self._blocked_on
-            assert self._head not in self._blocked_byneed
-            assert self._head not in self._asking
-        except Exception:
-            #XXX give sched_info maybe
-            raise OperationError(self.space.w_RuntimeError,
-                                 self.space.wrap("scheduler is in an incoherent state"))
             
     def _chain_insert(self, thread):
         assert thread._next is thread
@@ -144,8 +129,8 @@
                     return current
                 w(str(sched_info(self.space)))
                 ## we RESET sched state so as to keep being usable beyond that
-                self._init_head(self._main)
-                self._init_blocked()
+                reset_scheduler(self.space)
+                sched.uler._main = sched.uler._head = self._head
                 w(".. SCHEDULER reinitialized")
                 raise OperationError(self.space.w_AllBlockedError,
                                      self.space.wrap("can't schedule, possible deadlock in sight"))
@@ -175,7 +160,7 @@
         r._prev = l
         self._head = r
         if r == thread: #XXX write a test for me !
-            if not we_are_translated(): 
+            if not we_are_translated():
                 import traceback
                 traceback.print_exc()
         thread._next = thread._prev = None
@@ -322,13 +307,11 @@
         
 #-- Misc --------------------------------------------------
 def reset_scheduler(space):
-    "garbage collection of threads might pose some problems"
-    scheduler[0] = Scheduler(space)
-    scheduler[0]._check_initial_conditions()
+    sched.uler = Scheduler(space)
 app_reset_scheduler = gateway.interp2app(reset_scheduler)
 
 def sched_info(space):
-    s = scheduler[0]
+    s = sched.uler
     si = space.setitem
     sw = space.wrap
     w_ret = space.newdict()
@@ -344,5 +327,5 @@
 
 def schedule(space):
     "useful til we get preemtive scheduling deep into the vm"
-    scheduler[0].schedule_or_pass()
+    sched.uler.schedule_or_pass()
 app_schedule = gateway.interp2app(schedule)

Modified: pypy/dist/pypy/objspace/cclp/space.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/space.py	(original)
+++ pypy/dist/pypy/objspace/cclp/space.py	Mon Oct  9 16:52:33 2006
@@ -1,5 +1,8 @@
 from pypy.rpython.objectmodel import we_are_translated
 from pypy.interpreter import baseobjspace, gateway, argument, typedef
+from pypy.interpreter.function import Function
+from pypy.interpreter.eval import Code
+
 from pypy.interpreter.error import OperationError
 
 from pypy.objspace.std.intobject import W_IntObject
@@ -7,19 +10,13 @@
 
 from pypy.objspace.cclp.misc import ClonableCoroutine, get_current_cspace, w
 from pypy.objspace.cclp.thunk import CSpaceThunk, PropagatorThunk
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 from pypy.objspace.cclp.variable import newvar
 from pypy.objspace.cclp.types import ConsistencyError, Solution, W_Var, \
      W_CVar, W_AbstractDomain
 from pypy.objspace.cclp.interp_var import interp_bind, interp_free
 
 
-class DummyCallable(baseobjspace.W_Root):
-    def __call__(self): pass
-
-dummy_function = DummyCallable()
-dummy_args = argument.Arguments([])
-
 def newspace(space, w_callable, __args__):
     args = __args__.normalize()
     # coro init
@@ -29,11 +26,11 @@
     w_coro.bind(thunk)
     if not we_are_translated():
         w("NEWSPACE, thread", str(id(w_coro)), "for", str(w_callable.name))
-    w_space = W_CSpace(space, w_coro, parent=w_coro._cspace)
+    w_space = W_CSpace(space)
     w_coro._cspace = w_space
 
-    scheduler[0].add_new_thread(w_coro)
-    scheduler[0].schedule()
+    sched.uler.add_new_thread(w_coro)
+    sched.uler.schedule()
 
     return w_space
 app_newspace = gateway.interp2app(newspace, unwrap_spec=[baseobjspace.ObjSpace,
@@ -67,14 +64,9 @@
 
 class W_CSpace(baseobjspace.Wrappable):
 
-    def __init__(self, space, thread, parent):
-        assert isinstance(thread, ClonableCoroutine)
-        assert (parent is None) or isinstance(parent, W_CSpace)
+    def __init__(self, space):
         self.space = space # the object space ;-)
-        self.parent = parent
-        self.dist_thread = thread
         self.distributor = None
-        self.distributor_is_logic_script = True
         # choice mgmt
         self._choice = newvar(space)
         self._committed = newvar(space)
@@ -89,43 +81,49 @@
         
     def register_var(self, cvar):
         self._store[cvar.name] = cvar
-        self.distributor_is_logic_script = False
 
     def clone(self):
         if not we_are_translated():
-            print "<-CLONE !! -----------------------------------------------------------------------"            
+            print "<-CLONE !! -----------------------------------------------------------------------"
             # build fresh cspace & distributor thread
             thread = ClonableCoroutine(self.space)
-            new_cspace = W_CSpace(self.space, thread, None)
+            new_cspace = W_CSpace(self.space)
             thread._cspace = new_cspace
-            # new distributor
-            old_dist = self.dist_thread.thunk.dist
+            # new distributor instance
+            old_dist = self.distributor
             new_dist = old_dist.__class__(self.space, old_dist._fanout)
             new_dist._cspace = new_cspace
             # new distributor thunk
-            thunk = CSpaceThunk(self.space, self.space.wrap(dummy_function), dummy_args, thread)
+            thunk = CSpaceThunk(self.space,
+                                Function(self.space, Code('nocode')),
+                                argument.Arguments([]),
+                                thread)
             thread.bind(thunk)
-            scheduler[0].add_new_thread(thread)
+            sched.uler.add_new_thread(thread)
             # copy the store
             for var in self._store.values():
                 new_cspace.register_var(var.copy(self.space))
-            assert self.dist_thread.thunk.dist.distributable()
             # rebuild propagators
-            for const in self._constraints:
-                new_cspace.tell(const)
-            # other attrs
+            new_cspace._last_choice = self._last_choice
             new_cspace._solution = newvar(self.space)
             self.space.unify(new_cspace._solution, self.space.newlist(self._store.values()))
-            if hasattr(self, '_last_choice'):
-                new_cspace._last_choice = self._last_choice
-            print "LOOOOK at that : ", new_cspace._committed, self._committed
+            try:
+                for const in self._constraints:
+                    new_cspace.tell(const)
+                # other attrs
+                #if hasattr(self, '_last_choice'):
+                
+            except Exception, e:
+                print "DUH ?"
+                import traceback
+                traceback.print_exc()
             return new_cspace
         else:
             raise NotImplementedError
 
 
     def w_ask(self):
-        scheduler[0].wait_stable(self)
+        sched.uler.wait_stable(self)
         self.space.wait(self._choice)
         choice = self._choice.w_bound_to
         self._choice = newvar(self.space)
@@ -135,7 +133,7 @@
 
     def choose(self, n):
         assert n > 1
-        scheduler[0].wait_stable(self)
+        sched.uler.wait_stable(self)
         if self._failed: #XXX set by any propagator
             raise ConsistencyError
         assert interp_free(self._choice)
@@ -163,8 +161,8 @@
         if not we_are_translated():
             w("PROPAGATOR in thread", str(id(w_coro)))
             self._constraints.append(w_constraint)
-        scheduler[0].add_new_thread(w_coro)
-        scheduler[0].schedule()
+        sched.uler.add_new_thread(w_coro)
+        sched.uler.schedule()
 
     def fail(self):
         self._failed = True

Modified: pypy/dist/pypy/objspace/cclp/thread.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/thread.py	(original)
+++ pypy/dist/pypy/objspace/cclp/thread.py	Mon Oct  9 16:52:33 2006
@@ -4,7 +4,7 @@
 from pypy.objspace.cclp.types import W_Var, W_Future, W_FailedValue
 from pypy.objspace.cclp.misc import w, v, ClonableCoroutine, get_current_cspace
 from pypy.objspace.cclp.thunk import FutureThunk, ProcedureThunk
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 
 
 #-- Future --------------------------------------------------
@@ -24,7 +24,7 @@
     coro._cspace = get_current_cspace(space)
     if not we_are_translated():
         w("FUTURE", str(id(coro)), "for", str(w_callable.name))
-    scheduler[0].add_new_thread(coro)
+    sched.uler.add_new_thread(coro)
     return w_Future
 app_future = gateway.interp2app(future, unwrap_spec=[baseobjspace.ObjSpace,
                                                      baseobjspace.W_Root,
@@ -44,8 +44,8 @@
     coro._cspace = get_current_cspace(space)
     if not we_are_translated():
         w("STACKLET", str(id(coro)), "for", str(w_callable.name))
-    scheduler[0].add_new_thread(coro)
-    scheduler[0].schedule()
+    sched.uler.add_new_thread(coro)
+    sched.uler.schedule()
     return coro
 app_stacklet = gateway.interp2app(stacklet, unwrap_spec=[baseobjspace.ObjSpace,
                                                          baseobjspace.W_Root,

Modified: pypy/dist/pypy/objspace/cclp/thunk.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/thunk.py	(original)
+++ pypy/dist/pypy/objspace/cclp/thunk.py	Mon Oct  9 16:52:33 2006
@@ -2,7 +2,7 @@
 from pypy.module._stackless.interp_coroutine import AbstractThunk
 
 from pypy.objspace.cclp.misc import w
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 from pypy.objspace.cclp.types import W_Var, W_CVar, W_Future, W_FailedValue, \
      ConsistencyError, Solution, W_AbstractDomain
 from pypy.objspace.cclp.interp_var import interp_wait, interp_entail, \
@@ -33,18 +33,18 @@
 
     def call(self):
         w(".! initial (returnless) thunk CALL in", str(id(self._coro)))
-        scheduler[0].trace_vars(self._coro, logic_args(self.args.unpack()))
+        sched.uler.trace_vars(self._coro, logic_args(self.args.unpack()))
         try:
             try:
                 _AppThunk.call(self)
             except Exception, exc:
                 w(".! exceptional EXIT of procedure", str(id(self._coro)), "with", str(exc))
-                scheduler[0].dirty_traced_vars(self._coro, W_FailedValue(exc))
+                sched.uler.dirty_traced_vars(self._coro, W_FailedValue(exc))
             else:
                 w(".! clean EXIT of procedure", str(id(self._coro)))
         finally:
-            scheduler[0].remove_thread(self._coro)
-            scheduler[0].schedule()
+            sched.uler.remove_thread(self._coro)
+            sched.uler.schedule()
 
 
 class FutureThunk(_AppThunk):
@@ -55,7 +55,7 @@
 
     def call(self):
         w(".! initial thunk CALL in", str(id(self._coro)))
-        scheduler[0].trace_vars(self._coro, logic_args(self.args.unpack()))
+        sched.uler.trace_vars(self._coro, logic_args(self.args.unpack()))
         try:
             try:
                 _AppThunk.call(self)
@@ -63,15 +63,15 @@
                 w(".! exceptional EXIT of future", str(id(self._coro)), "with", str(exc))
                 failed_val = W_FailedValue(exc)
                 self.space.bind(self.w_Result, failed_val)
-                scheduler[0].dirty_traced_vars(self._coro, failed_val)
+                sched.uler.dirty_traced_vars(self._coro, failed_val)
             else:
                 w(".! clean EXIT of future", str(id(self._coro)),
                   "-- setting future result", str(self.w_Result), "to",
                   str(self.costate.w_tempval))
                 self.space.unify(self.w_Result, self.costate.w_tempval)
         finally:
-            scheduler[0].remove_thread(self._coro)
-            scheduler[0].schedule()
+            sched.uler.remove_thread(self._coro)
+            sched.uler.schedule()
 
 class CSpaceThunk(_AppThunk):
     "for a constraint script/logic program"
@@ -81,18 +81,17 @@
 
     def call(self):
         w("-- initial thunk CALL in", str(id(self._coro)))
-        scheduler[0].trace_vars(self._coro, logic_args(self.args.unpack()))
+        sched.uler.trace_vars(self._coro, logic_args(self.args.unpack()))
         cspace = self._coro._cspace
         space = self.space
         try:
             try:
-                #if self.runs_a_logic_script():
                 _AppThunk.call(self)
             except Exception, exc:
                 # maybe app_level let something buble up ...
                 w("-- exceptional EXIT of DISTRIBUTOR", str(id(self._coro)), "with", str(exc))
                 failed_value = W_FailedValue(exc)
-                scheduler[0].dirty_traced_vars(self._coro, failed_value)
+                sched.uler.dirty_traced_vars(self._coro, failed_value)
                 interp_bind(cspace._solution, failed_value)
                 cspace.fail()
             else:
@@ -110,8 +109,8 @@
                 interp_bind(cspace._choice, self.space.newint(1))
         finally:
             interp_bind(cspace._finished, self.space.w_True)
-            scheduler[0].remove_thread(self._coro)
-            scheduler[0].schedule()
+            sched.uler.remove_thread(self._coro)
+            sched.uler.schedule()
 
 
 class PropagatorThunk(AbstractThunk):
@@ -149,6 +148,6 @@
                     import traceback
                     traceback.print_exc()
         finally:
-            scheduler[0].remove_thread(self.coro)
-            scheduler[0].schedule()
+            sched.uler.remove_thread(self.coro)
+            sched.uler.schedule()
 

Modified: pypy/dist/pypy/objspace/cclp/variable.py
==============================================================================
--- pypy/dist/pypy/objspace/cclp/variable.py	(original)
+++ pypy/dist/pypy/objspace/cclp/variable.py	Mon Oct  9 16:52:33 2006
@@ -6,7 +6,7 @@
 from pypy.objspace.std.stringobject import W_StringObject
 
 from pypy.objspace.cclp.misc import w, v, ClonableCoroutine
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 from pypy.objspace.cclp.types import deref, W_Var, W_CVar, W_Future, W_FailedValue
 
 from pypy.rpython.objectmodel import we_are_translated
@@ -27,11 +27,11 @@
     return w_obj
 
 def wait__Var(space, w_var):
-    #w(":wait", str(id(ClonableCoroutine.w_getcurrent(space))))
+    w("###:wait", str(id(ClonableCoroutine.w_getcurrent(space))))
     if space.is_true(space.is_free(w_var)):
-        scheduler[0].unblock_byneed_on(w_var)
-        scheduler[0].add_to_blocked_on(w_var, ClonableCoroutine.w_getcurrent(space))
-        scheduler[0].schedule()
+        sched.uler.unblock_byneed_on(w_var)
+        sched.uler.add_to_blocked_on(w_var, ClonableCoroutine.w_getcurrent(space))
+        sched.uler.schedule()
         assert space.is_true(space.is_bound(w_var))
     w_ret = w_var.w_bound_to
     if isinstance(w_ret, W_FailedValue):
@@ -57,8 +57,8 @@
     if space.is_true(space.is_free(w_var)):
         if w_var.needed:
             return
-        scheduler[0].add_to_blocked_byneed(w_var, ClonableCoroutine.w_getcurrent(space))
-        scheduler[0].schedule()
+        sched.uler.add_to_blocked_byneed(w_var, ClonableCoroutine.w_getcurrent(space))
+        sched.uler.schedule()
     else:
         raise OperationError(space.w_TypeError,
                              space.wrap("wait_needed only supported on unbound variables"))
@@ -119,8 +119,10 @@
 def alias_of(space, w_var1, w_var2):
     assert isinstance(w_var1, W_Var)
     assert isinstance(w_var2, W_Var)
-    assert space.is_true(space.is_free(w_var1))
-    assert space.is_true(space.is_free(w_var2))
+    if not (space.is_true(space.is_free(w_var1)) and \
+            space.is_true(space.is_free(w_var2))):
+        raise OperationError(space.w_LogicError,
+                             space.wrap("don't call alias_of on bound variables"))
     w_curr = w_var1
     while 1:
         w_next = w_curr.w_bound_to
@@ -267,7 +269,7 @@
         assert isinstance(w_next, W_Var)
         _assign(space, w_curr, w_val)
         # notify the blocked threads
-        scheduler[0].unblock_on(w_curr)
+        sched.uler.unblock_on(w_curr)
         if space.is_true(space.is_nb_(w_next, w_var)):
             break
         # switch to next

Modified: pypy/dist/pypy/objspace/logic.py
==============================================================================
--- pypy/dist/pypy/objspace/logic.py	(original)
+++ pypy/dist/pypy/objspace/logic.py	Mon Oct  9 16:52:33 2006
@@ -10,7 +10,7 @@
 
 #-- MISC ----------------------------------------------------
 
-from pypy.objspace.cclp.misc import app_interp_id, app_switch_debug_info, app_is_interpreted
+from pypy.objspace.cclp.misc import app_interp_id, app_switch_debug_info, app_is_interpreted, w
 
 #-- THREADING/COROUTINING -----------------------------------
 
@@ -19,7 +19,7 @@
 from pypy.objspace.cclp.scheduler import Scheduler,  app_sched_info, \
      app_schedule, app_reset_scheduler
 
-from pypy.objspace.cclp.global_state import scheduler
+from pypy.objspace.cclp.global_state import sched
 
 #-- COMP. SPACE --------------------------------------------
 
@@ -95,14 +95,15 @@
     def eq(w_obj1, w_obj2):
         assert isinstance(w_obj1, W_Root)
         assert isinstance(w_obj2, W_Root)
-        # check identity
+        w("#> check identity")
         if space.is_true(space.is_nb_(w_obj1, w_obj2)):
             return space.newbool(True)
-        # check aliasing
+        w("#> check aliasing")
         if space.is_true(space.is_free(w_obj1)):
             if space.is_true(space.is_free(w_obj2)):
                 if space.is_true(alias_of(space, w_obj1, w_obj2)):
                     return space.newbool(True) # and just go on ...
+        w("#> using parent eq")
         return parentfn(wait(space, w_obj1), wait(space, w_obj2))
     return eq
 
@@ -294,7 +295,7 @@
 
     # do the magic
     patch_space_in_place(space, 'logic', proxymaker)
-
     # instantiate singleton scheduler
-    scheduler.append(Scheduler(space))
+    sched.uler = Scheduler(space)
+    
     return space



More information about the Pypy-commit mailing list