home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / SCHED.E < prev    next >
Text File  |  1993-01-12  |  2KB  |  88 lines

  1. -- sched.e
  2. -- Task Scheduler
  3.  
  4. global constant HUGE_TIME = 1e30
  5.  
  6. constant sample_interval = 1.0
  7. atom sample_count
  8.  
  9. type reasonable_delay(atom x)
  10.     return x > 0 and x < 30
  11. end type
  12.  
  13. global procedure init_delay()
  14. -- since time() may not have fine enough
  15. -- resolution for small delays, we see how many for-loop iterations
  16. -- we can complete over a small sample period
  17.  
  18.     atom t
  19.  
  20.     t = time() + sample_interval
  21.     for i = 1 to 999999999 do
  22.     if time() < t then
  23.     else
  24.         sample_count = i
  25.         exit
  26.     end if
  27.     end for
  28. end procedure
  29.  
  30. global procedure delay(reasonable_delay t)
  31. -- delay for t seconds
  32.     atom stop
  33.     if t > sample_interval then
  34.     -- time() should be precise enough
  35.     stop = time() + t
  36.     while time() < stop do
  37.     end while
  38.     else
  39.     -- loop a certain number of times
  40.     stop = time() + sample_interval
  41.     for i = 1 to floor(t / sample_interval * sample_count) do
  42.         if time() < stop then
  43.         else
  44.         end if
  45.     end for
  46.     end if
  47. end procedure
  48.  
  49.  
  50. global procedure sched(task t, positive_atom wait)
  51. -- schedule task to be reactivated in wait seconds
  52.  
  53.     if wait = 0 then
  54.     -- deactivate
  55.     tcb[t] = HUGE_TIME
  56.     else
  57.     -- activate in wait seconds from now
  58.     tcb[t] = time() + wait
  59.     end if
  60. end procedure
  61.  
  62.  
  63. global function next_task()
  64. -- choose the next task to be executed
  65.  
  66.     positive_atom mintime
  67.     task mintask
  68.  
  69.     -- find task with minimum time
  70.     mintime = HUGE_TIME
  71.     for i = 1 to NTASKS do
  72.     if tcb[i] < mintime then
  73.         mintask = i
  74.         mintime = tcb[i]
  75.     end if
  76.     end for
  77.  
  78.     -- subtract it's early-activation tolerance
  79.     tcb[mintask] = tcb[mintask] - eat[mintask]
  80.  
  81.     -- wait until it is time to activate it
  82.     while time() < tcb[mintask] do
  83.     end while
  84.  
  85.     return mintask
  86. end function
  87.  
  88.