22.1.6 Termination Detection: The Watcher

The most difficult part of the implementation of the concurrent agenda model is to decide about when the concurrent computation is terminated. As mentioned before, termination is decided by a stateful agent that we call Watcher.

The duty of the Watcher is to count the number of tasks that have still to be performed some agent in the concurrent system. A task is given by an agent and an information item and means that the agent has to process the item.

The Watcher's state is implemented by a stateful Oz object with attributes for counting numbers of agents, infos, tasks in the system. Furthermore, there is an attribute initialize for memoizing whether the board has been initialized.

Suppose for instance that a new agent has been started. In this case, the number of agents has to be increased by one but also the number of tasks has to be increased by the tasks that the new agent has to perform. Hence, the number of task increases by the number of information items that are actually on the board.

<WatcherState>=
class WatcherState 
   attr 
      agents:0
      infos:0
      tasks:0
      initialized:false 
   meth init skip end 
   meth initialized 
      initialized<-true 
   end 
   meth infoProcessed 
      tasks<-@tasks-1
   end 
   meth newAgent 
      agents<-@agents+1
      tasks<-@tasks+@infos
   end 
   meth info(...)
      infos<-@infos+1
      tasks<-@tasks+@agents
   end 
   meth done($)
      {And @initialized @tasks==0}
   end 
end

<StartWatcher>=
local 
   
<WatcherState> 
in 
   proc{StartWatcher Board}
      local 
         State={New WatcherState init}
      in 
         proc{Process Item}
            {State Item}
            if {State done($)}
            then 
               {Board.post stop}
               raise done end 
            else 
               skip 
            end 
         end 
      end 
   in      
      thread 
         try {ForAll Board.items Process}
         catch done
         then skip 
         end 
      end 
   end 
end


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)