Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 13th, 2011  |  syntax: Lua  |  size: 2.29 KB  |  views: 229  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --
  2. -- TIMER.LUA
  3. --
  4. -- Sample program for Lua Lanes
  5. --
  6.  
  7. -- On MSYS, stderr is buffered. In this test it matters.
  8. io.stderr:setvbuf "no"
  9.  
  10.  
  11. require "lanes"
  12.  
  13. local linda= lanes.linda()
  14.  
  15. local function PRINT(str)
  16.     io.stderr:write(str.."\n")
  17. end
  18.  
  19. local T1= "1s"  -- these keys can be anything...
  20. local T2= "5s"
  21.  
  22. local step= {}
  23.  
  24. lanes.timer( linda, T1, 1.0, 1.0 )
  25. step[T1]= 1.0
  26.  
  27. PRINT( "\n*** Timers every second (not synced to wall clock) ***\n" )
  28.  
  29. local v_first
  30. local v_last= {}     -- { [channel]= num }
  31. local T2_first_round= true
  32.  
  33. local caught= {}     -- { [T1]= bool, [T2]= bool }
  34.  
  35. while true do
  36.     io.stderr:write("waiting...\t")
  37.     local v,channel= linda:receive( 6.0, T1,T2 )
  38.     assert( channel==T1 or channel==T2 )
  39.     caught[channel]= true
  40.  
  41.     io.stderr:write( ((channel==T1) and "" or "\t\t").. string.format("%.3f",v),"\n" )
  42.     assert( type(v)=="number" )
  43.  
  44.     if v_last[channel] then
  45.         if channel==T2 and T2_first_round then
  46.             -- do not make measurements, first round is not 5secs due to wall clock adjustment
  47.             T2_first_round= false
  48.         else
  49.             assert( math.abs(v-v_last[channel]- step[channel]) < 0.02 )
  50.         end
  51.     end
  52.    
  53.     if not v_first then
  54.         v_first= v
  55.     elseif v-v_first > 3.0 and (not step[T2]) then
  56.         PRINT( "\n*** Adding timers every 5 second (synced to wall clock) ***\n" )
  57.  
  58.         -- The first event can be in the past (just cut seconds down to 5s)
  59.         --
  60.         local date= os.date("*t")
  61.         date.sec = date.sec - date.sec%5
  62.  
  63.         lanes.timer( linda, T2, date, 5.0 )
  64.         step[T2]= 5.0
  65.  
  66.     elseif v-v_first > 10 then    -- exit condition
  67.         break
  68.     end
  69.     v_last[channel]= v
  70. end  
  71.  
  72. -- Windows version had a bug where T2 timers were not coming through, at all.
  73. -- AKa 24-Jan-2009
  74. --
  75. assert( caught[T1] )
  76. assert( caught[T2] )
  77.  
  78. PRINT( "\n*** Clearing timers ***\n" )
  79.  
  80. lanes.timer( linda, T1, 0 )    -- reset; no reoccuring ticks
  81. lanes.timer( linda, T2, 0 )
  82.  
  83. linda:receive( 0, T1 )    -- clear out; there could be one tick left
  84. linda:receive( 0, T2 )
  85.  
  86. assert( linda:get(T1) == nil )
  87. assert( linda:get(T2) == nil )
  88.  
  89. PRINT "...making sure no ticks are coming..."
  90.  
  91. local v= linda:receive( 1.5, T1,T2 )    -- should not get any
  92. assert(v==nil)
clone this paste RAW Paste Data