next up previous contents index
Next: Data Structures Up: No Title Previous: Threads

Time

  The module Time contains procedures for real-time applications.  

{Sleep +TimeOutI X ?Y}
     
reduces to X = Y after TimeOutI milliseconds. Note that Sleep is I/O and therefore only allowed on top level The Definition of Kernel Oz [11] . For example
  {{Sleep 1000 O} close}
sends object O the message close after a delay of one second.
Time.repeat
   
is bound to an object inheriting from UrObject which allows to There are default values for any of the iteration parameters. These are set on creation of an object inheriting from Time.repeat. and can be changed by inheritance. The functionality is controlled by the methods explained in the following paragraph.
{Time ?T}
     
binds T to the number of seconds elapsed since January, 1st of the current year (uses Unix.localTime).

Methods of the Time.repeat object

setRepAll(action:    +ActionPRT  <= dummyRep
          final:     +FinalPRT   <= finalRep
          delay:     +DelayI     <= 1000    
          delayFun:  +DelayFunP  <= fun{$} 1000 end
          number:    +NumI       <= ~1      
          priority:  +PriI       <= Thread.default
          killAction:+KillPRT    <= close
          killDelay: +KillDelayI <= 0)
   
initializes the loop with the action ActionPRT to iterate (default: message dummyRep), the action FinalPRT to finalize a finite iteration (default: message finalRep), the delay DelayI between iterations (default: one second), the function DelayFunP yielding the delay between iterations (default: constant 1000), the maximal number NumI of iterations (default: infinitely many), the action KillPRT to be performed at kill (default: message close), and the delay KillDelayI between receipt of the kill message and the actual kill (default: 0). The method dummyRep shows 'dummy repeat action' in the Browser, the method finalRep action does nothing. When both delay and delayFun parameters are given, the value of DelayFunP takes priority over that one of DelayI. All default actions ActionPRT, FinalPRT, and KillPRT can be changed by inheritance. The loop is started in its own thread. Its priority is determined by the value of PriI (default: Thread.default for normal priority). For example, try the following:
create O from Time.repeat
   with [setRepAll(action:proc{$} {Unix.system 'fortune' _} end 
                   number:10) 
         go]
end
getRep(action:    ?ActionPRT  <= _
       final:     ?FinalPRT   <= _
       delay:     ?DelayI     <= _
       delayFun:  ?DelayFunP  <= _
       number:    ?LimitI     <= _
       actual:    ?NumI       <= _
       priority:  ?PriI       <= _
       killAction:?KillPRT    <= _
       killDelay: ?KillDelayI <= _)
   
returns the current loop parameters: LimitI returns the current limit of the iteration, and NumI the number of iterations left to be done. If the delay is specified via DelayFunP (which need not be constant), then DelayI returns the last delay used. If DelayI is requested before the start of the iteration, then 1 is returned. The other values correspond to the fields of the method setRepAll. For example try:
create Counter from Time.repeat
   attr a:0
   with [setRepAll(action:inc number:10) go]
   meth inc a<-@a+1 end
   meth get(?A) A=@a end
   meth finalRep a<-0 end
end
{Counter getRep(final:{Browse} action:{Browse} actual:{Browse})}
{Counter get({Browse})}
This will show the atoms 'finalRep' and 'inc' in the Browser as well as a number between 1 and 10. After termination of the loop, the value of @a will be reset to 0.
setRepAction(+ActionPRT   <= dummyRep)
setRepFinal(+FinalPRT     <= finalRep)
setRepKillAction(+KillPRT <= close)
           
allow to set the various action parameters of the iteration. ActionPRT, FinalPRT, and KillPRT may be nullary procedures resp. tuples or records, arguments of other types are ignored. If they are procedures they are called as is. If they are tuples or records, they are interpreted as messages to be sent to self, for instance proc{$ {self ActionPRT} end} may be performed instead of ActionPRT.
setRepDelay(+DelayI         <= 1000)
setRepNum(+NumI             <= ~1)
setRepKillDelay(+KillDelayI <= 0)
           
allow to set the numeric parameters of the iteration.

DelayI, NumI, and KillDelayI must be integers. Arguments of other types are ignored. The iteration limit NumI is stored and subsequent loop instances (triggered by go) also obey it, unless the limit is reset to 1.

setRepDelayFun(+DelayFunP <= fun{$} 1000 end)
   
allows to set the delay function. DelayFunP must be a unary procedure which yields an integer value on application. Non-procedural arguments or procedures with arity different from one are ignored.
setRepPriority(+PriI <= Thread.default)
   
allows to set the priority of the thread in which the loop will be executed. PriI must be an appropriate integer.
go
   
starts the loop. If the loop is already running, go is ignored.
stop
   
halts the loop without changing the iteration index. There may be a delay between the receipt of this message and the actual stopping of the loop. The loop may be restarted with go thereafter.
kill
   
kills the loop after delaying and performing the prespecified killAction. The loop cannot be restarted thereafter, i.e. the repeater object instance is closed.
For details and examples about the usage of the sleep and repeat functionality provided by module Time see Programming in Oz [3] .  



next up previous contents index
Next: Data Structures Up: No Title Previous: Threads



Sven Schmeier
Tue Oct 24 09:20:46 MET 1995