changeset 1251:2bbab8b5597f

Centralize the program counter changes.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 28 Sep 2009 15:59:57 +0200
parents 77f74e53f796
children c8e3e05b8be9
files viff/active.py viff/passive.py viff/runtime.py
diffstat 3 files changed, 16 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/viff/active.py	Sat Sep 19 15:34:01 2009 +0200
+++ b/viff/active.py	Mon Sep 28 15:59:57 2009 +0200
@@ -47,7 +47,7 @@
         Principles of Distributed Computing, 1984, pages 154-162.
         """
         # We need a unique program counter for each call.
-        self.program_counter[-1] += 1
+        self.increment_pc()
 
         result = Deferred()
         pc = tuple(self.program_counter)
--- a/viff/passive.py	Sat Sep 19 15:34:01 2009 +0200
+++ b/viff/passive.py	Mon Sep 28 15:59:57 2009 +0200
@@ -250,7 +250,7 @@
         starting program counter. This ensures that consequetive calls
         to PRSS-related methods will use unique program counters.
         """
-        self.program_counter[-1] += 1
+        self.increment_pc()
         return tuple(self.program_counter)
 
     def prss_share(self, inputters, field, element=None):
--- a/viff/runtime.py	Sat Sep 19 15:34:01 2009 +0200
+++ b/viff/runtime.py	Mon Sep 28 15:59:57 2009 +0200
@@ -604,6 +604,18 @@
         dl = DeferredList(vars)
         self.schedule_callback(dl, lambda _: self.shutdown())
 
+    def increment_pc(self):
+        """Increment the program counter."""
+        self.program_counter[-1] += 1
+
+    def fork_pc(self):
+        """Fork the program counter."""
+        self.program_counter.append(0)
+
+    def unfork_pc(self):
+        """Leave a fork of the program counter."""
+        self.program_counter.pop()
+
     def schedule_callback(self, deferred, func, *args, **kwargs):
         """Schedule a callback on a deferred with the correct program
         counter.
@@ -617,9 +629,8 @@
         Any extra arguments are passed to the callback as with
         :meth:`addCallback`.
         """
-        self.program_counter[-1] += 1
+        self.increment_pc()
         saved_pc = self.program_counter[:]
-        saved_pc.append(0)
 
         @wrapper(func)
         def callback_wrapper(*args, **kwargs):
@@ -627,6 +638,7 @@
             try:
                 current_pc = self.program_counter[:]
                 self.program_counter[:] = saved_pc
+                self.fork_pc()
                 return func(*args, **kwargs)
             finally:
                 self.program_counter[:] = current_pc