scheduler.nv
· 5.4 KiB · Text
Surowy
Playground
|:#############: Scheduler input and config :#############: |
|| :cycle size: 10
||
:: Start from 0 and step by 1000 milliseconds
:schedule: Task1 for 1
:schedule: Task2 for 2
:schedule: Task3 for 3
:schedule: ThreeSecondTask for 3
:schedule: Task4 for 4
:schedule: FiveSecondTask for 5
:schedule: EightSecondTask for 8
:schedule: Task9 for 9
:schedule: Task10 for 10
:schedule: Task11 for 11
:schedule: Task15 for 15
:schedule: Task21 for 21
:schedule: Halt for 21
|:#############: Task definitions :#############: |
| :run: Halt
:clock: $t ?
|
:@stdio: println Goodbye!
:log: Halting from schedule at time $t
:: Halt
| :run: $task
:clock: $t ?
|
:@stdio: println $task
:log: [ Time $t ] running task - $task
|:#############: Helpers :#############: |
| :let: $variable = :@math: $result |
:: $variable = $result
|:#############: Debug helpers :#############: |
| :clock: $t ? :cycle: $c $i ? :debug: $d |
:@stdio: write [dbg]
:@stdio: write 32
:@stdio: write [
:@stdio: write clock
:@stdio: write 32
:@stdio: print $t
:@stdio: write ]
:@stdio: write 32
:@stdio: println $d
|:#############: Rules :#############: |
| :: Start from $start_time and step by $time_step $units
|
:clock t0: $start_time
:clock: 0
:time step: $time_step $units
:cycle: 0 0
:: Run process loop
| :: Run process loop
|
:: Check for new tasks
:: Run current tasks
:: Sleep
:: Update clock
:: Update future tasks
:: Run process loop
| :: Sleep
:time step: $time_step $unit ?
|
:: Wake
:@time: sleep $time_step $unit
:log: Sleeping for $time_step $unit
| :: Wake :@time: done |
:log: Woken from sleep
|:#############: Clock :#############: |
| :: Update clock
:clock t0: $t0 ?
:clock: $t
:cycle size: $N ?
:cycle: $c $i ?
|
:let: next_clock = :@math: add $t 1
:let: next_cycle_index = :@math: add $i 1
:log: Updating clock
| :: next_clock = $t |
:clock: $t
| :: next_cycle_index = $i
:cycle: $c $_
:cycle size: $N ?
|
:cycle: $c $i
:: check for cycle rollover :@math: compare $i $N
:log: Checking for cycle rollover
| :: check for cycle rollover :@math: equal | :: reset cycle :log: The cycle rolls over
| :: check for cycle rollover :@math: greater | :: reset cycle :log: The cycle rolls over
| :: check for cycle rollover :@math: less | :log: No cycle rollover
| :: reset cycle
:cycle: $c $i ?
|
:let: next_cycle = :@math: add $c 1
| :: next_cycle = $c
:cycle: $_1 $_2
|
:cycle: $c 0
:log: Next cycle is $c
|:#############: Tasks :#############: |
|
:: Check for new tasks ?
:schedule: $task for $time ?
:clock t0: $t0 ?
|
:log: Calculating cycle for $time
:let: time_diff = :@math: subtract $time $t0
| :: time_diff = $result
:cycle size: $N ?
|
:let: time_cycle = :@math: antimodulo $result $N
:let: time_index = :@math: modulo $result $N
|
:schedule: $task for $time ?
:: time_index = $index_result
:: time_cycle = $cycle_result
|
:: Time $time will occur at cycle $cycle_result and index $index_result
:log: Calculated time $time will occur at cycle $cycle_result and index $index_result
| :###: Case A; the new task is in the current cycle
|
| :: Time $time will occur at cycle $current_cycle and index $i
:schedule: $task for $time
:cycle: $current_cycle $_ ?
|
:: Push $task to timestack $i
| :###: Case B; the new task is in a future
:###: assume that new tasks will not be in the past, so this is a default case
|
|
:: Time $time will occur at cycle $c and index $i
:schedule: $task for $time
|
:future: $time $c $i $task
| :: Check for new tasks |
|:###: Pulling tasks from the future to the present
:###: the future stack is currently unsorted, so this could be optimized
|
| :: Update future tasks ?
:future: $time $c $i $task
:cycle: $c 0 ?
|
:: Push $task to timestack $i
| :: Update future tasks ?
:future: $time $c $i $task
|
:next future: $time $c $i $task
| :: Update future tasks | :log: No future-era tasks to update
| :###: NB this rule has gotta come after the prior rules |
| :next future: $time $c $i $task
|
:future: #time $c $i $task
|:#############: pushy push :#############: |
| :: Push $task to timestack 0 | :timestack 0: $task
| :: Push $task to timestack 1 | :timestack 1: $task
| :: Push $task to timestack 2 | :timestack 2: $task
| :: Push $task to timestack 3 | :timestack 3: $task
| :: Push $task to timestack 4 | :timestack 4: $task
| :: Push $task to timestack 5 | :timestack 5: $task
| :: Push $task to timestack 6 | :timestack 6: $task
| :: Push $task to timestack 7 | :timestack 7: $task
| :: Push $task to timestack 8 | :timestack 8: $task
| :: Push $task to timestack 9 | :timestack 9: $task
|:#############: Task running :#############: |
| :: Run current tasks ? :cycle: $_ 0 ? :timestack 0: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 1 ? :timestack 1: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 2 ? :timestack 2: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 3 ? :timestack 3: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 4 ? :timestack 4: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 5 ? :timestack 5: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 6 ? :timestack 6: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 7 ? :timestack 7: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 8 ? :timestack 8: $task | :run: $task
| :: Run current tasks ? :cycle: $_ 9 ? :timestack 9: $task | :run: $task
| :: Run current tasks |
| 1 | |:#############: Scheduler input and config :#############: | |
| 2 | |
| 3 | || :cycle size: 10 |
| 4 | |
| 5 | || |
| 6 | :: Start from 0 and step by 1000 milliseconds |
| 7 | :schedule: Task1 for 1 |
| 8 | :schedule: Task2 for 2 |
| 9 | :schedule: Task3 for 3 |
| 10 | :schedule: ThreeSecondTask for 3 |
| 11 | :schedule: Task4 for 4 |
| 12 | :schedule: FiveSecondTask for 5 |
| 13 | :schedule: EightSecondTask for 8 |
| 14 | :schedule: Task9 for 9 |
| 15 | :schedule: Task10 for 10 |
| 16 | :schedule: Task11 for 11 |
| 17 | :schedule: Task15 for 15 |
| 18 | :schedule: Task21 for 21 |
| 19 | :schedule: Halt for 21 |
| 20 | |
| 21 | |:#############: Task definitions :#############: | |
| 22 | |
| 23 | | :run: Halt |
| 24 | :clock: $t ? |
| 25 | | |
| 26 | :@stdio: println Goodbye! |
| 27 | :log: Halting from schedule at time $t |
| 28 | :: Halt |
| 29 | |
| 30 | | :run: $task |
| 31 | :clock: $t ? |
| 32 | | |
| 33 | :@stdio: println $task |
| 34 | :log: [ Time $t ] running task - $task |
| 35 | |
| 36 | |
| 37 | |:#############: Helpers :#############: | |
| 38 | |
| 39 | | :let: $variable = :@math: $result | |
| 40 | :: $variable = $result |
| 41 | |
| 42 | |:#############: Debug helpers :#############: | |
| 43 | | :clock: $t ? :cycle: $c $i ? :debug: $d | |
| 44 | :@stdio: write [dbg] |
| 45 | :@stdio: write 32 |
| 46 | :@stdio: write [ |
| 47 | :@stdio: write clock |
| 48 | :@stdio: write 32 |
| 49 | :@stdio: print $t |
| 50 | :@stdio: write ] |
| 51 | :@stdio: write 32 |
| 52 | :@stdio: println $d |
| 53 | |
| 54 | |:#############: Rules :#############: | |
| 55 | |
| 56 | | :: Start from $start_time and step by $time_step $units |
| 57 | | |
| 58 | :clock t0: $start_time |
| 59 | :clock: 0 |
| 60 | :time step: $time_step $units |
| 61 | :cycle: 0 0 |
| 62 | :: Run process loop |
| 63 | |
| 64 | | :: Run process loop |
| 65 | | |
| 66 | :: Check for new tasks |
| 67 | :: Run current tasks |
| 68 | :: Sleep |
| 69 | :: Update clock |
| 70 | :: Update future tasks |
| 71 | :: Run process loop |
| 72 | |
| 73 | | :: Sleep |
| 74 | :time step: $time_step $unit ? |
| 75 | | |
| 76 | :: Wake |
| 77 | :@time: sleep $time_step $unit |
| 78 | :log: Sleeping for $time_step $unit |
| 79 | |
| 80 | | :: Wake :@time: done | |
| 81 | :log: Woken from sleep |
| 82 | |
| 83 | |
| 84 | |:#############: Clock :#############: | |
| 85 | |
| 86 | | :: Update clock |
| 87 | :clock t0: $t0 ? |
| 88 | :clock: $t |
| 89 | :cycle size: $N ? |
| 90 | :cycle: $c $i ? |
| 91 | | |
| 92 | :let: next_clock = :@math: add $t 1 |
| 93 | :let: next_cycle_index = :@math: add $i 1 |
| 94 | :log: Updating clock |
| 95 | |
| 96 | | :: next_clock = $t | |
| 97 | :clock: $t |
| 98 | |
| 99 | | :: next_cycle_index = $i |
| 100 | :cycle: $c $_ |
| 101 | :cycle size: $N ? |
| 102 | | |
| 103 | :cycle: $c $i |
| 104 | :: check for cycle rollover :@math: compare $i $N |
| 105 | :log: Checking for cycle rollover |
| 106 | |
| 107 | | :: check for cycle rollover :@math: equal | :: reset cycle :log: The cycle rolls over |
| 108 | | :: check for cycle rollover :@math: greater | :: reset cycle :log: The cycle rolls over |
| 109 | | :: check for cycle rollover :@math: less | :log: No cycle rollover |
| 110 | |
| 111 | | :: reset cycle |
| 112 | :cycle: $c $i ? |
| 113 | | |
| 114 | :let: next_cycle = :@math: add $c 1 |
| 115 | |
| 116 | | :: next_cycle = $c |
| 117 | :cycle: $_1 $_2 |
| 118 | | |
| 119 | :cycle: $c 0 |
| 120 | :log: Next cycle is $c |
| 121 | |
| 122 | |
| 123 | |:#############: Tasks :#############: | |
| 124 | |
| 125 | | |
| 126 | :: Check for new tasks ? |
| 127 | :schedule: $task for $time ? |
| 128 | :clock t0: $t0 ? |
| 129 | | |
| 130 | :log: Calculating cycle for $time |
| 131 | :let: time_diff = :@math: subtract $time $t0 |
| 132 | |
| 133 | | :: time_diff = $result |
| 134 | :cycle size: $N ? |
| 135 | | |
| 136 | :let: time_cycle = :@math: antimodulo $result $N |
| 137 | :let: time_index = :@math: modulo $result $N |
| 138 | |
| 139 | | |
| 140 | :schedule: $task for $time ? |
| 141 | :: time_index = $index_result |
| 142 | :: time_cycle = $cycle_result |
| 143 | | |
| 144 | :: Time $time will occur at cycle $cycle_result and index $index_result |
| 145 | :log: Calculated time $time will occur at cycle $cycle_result and index $index_result |
| 146 | |
| 147 | | :###: Case A; the new task is in the current cycle |
| 148 | | |
| 149 | | :: Time $time will occur at cycle $current_cycle and index $i |
| 150 | :schedule: $task for $time |
| 151 | :cycle: $current_cycle $_ ? |
| 152 | | |
| 153 | :: Push $task to timestack $i |
| 154 | |
| 155 | | :###: Case B; the new task is in a future |
| 156 | :###: assume that new tasks will not be in the past, so this is a default case |
| 157 | | |
| 158 | | |
| 159 | :: Time $time will occur at cycle $c and index $i |
| 160 | :schedule: $task for $time |
| 161 | | |
| 162 | :future: $time $c $i $task |
| 163 | |
| 164 | | :: Check for new tasks | |
| 165 | |
| 166 | |:###: Pulling tasks from the future to the present |
| 167 | :###: the future stack is currently unsorted, so this could be optimized |
| 168 | | |
| 169 | | :: Update future tasks ? |
| 170 | :future: $time $c $i $task |
| 171 | :cycle: $c 0 ? |
| 172 | | |
| 173 | :: Push $task to timestack $i |
| 174 | |
| 175 | | :: Update future tasks ? |
| 176 | :future: $time $c $i $task |
| 177 | | |
| 178 | :next future: $time $c $i $task |
| 179 | |
| 180 | | :: Update future tasks | :log: No future-era tasks to update |
| 181 | |
| 182 | | :###: NB this rule has gotta come after the prior rules | |
| 183 | | :next future: $time $c $i $task |
| 184 | | |
| 185 | :future: #time $c $i $task |
| 186 | |
| 187 | |
| 188 | |:#############: pushy push :#############: | |
| 189 | | :: Push $task to timestack 0 | :timestack 0: $task |
| 190 | | :: Push $task to timestack 1 | :timestack 1: $task |
| 191 | | :: Push $task to timestack 2 | :timestack 2: $task |
| 192 | | :: Push $task to timestack 3 | :timestack 3: $task |
| 193 | | :: Push $task to timestack 4 | :timestack 4: $task |
| 194 | | :: Push $task to timestack 5 | :timestack 5: $task |
| 195 | | :: Push $task to timestack 6 | :timestack 6: $task |
| 196 | | :: Push $task to timestack 7 | :timestack 7: $task |
| 197 | | :: Push $task to timestack 8 | :timestack 8: $task |
| 198 | | :: Push $task to timestack 9 | :timestack 9: $task |
| 199 | |
| 200 | |
| 201 | |:#############: Task running :#############: | |
| 202 | | :: Run current tasks ? :cycle: $_ 0 ? :timestack 0: $task | :run: $task |
| 203 | | :: Run current tasks ? :cycle: $_ 1 ? :timestack 1: $task | :run: $task |
| 204 | | :: Run current tasks ? :cycle: $_ 2 ? :timestack 2: $task | :run: $task |
| 205 | | :: Run current tasks ? :cycle: $_ 3 ? :timestack 3: $task | :run: $task |
| 206 | | :: Run current tasks ? :cycle: $_ 4 ? :timestack 4: $task | :run: $task |
| 207 | | :: Run current tasks ? :cycle: $_ 5 ? :timestack 5: $task | :run: $task |
| 208 | | :: Run current tasks ? :cycle: $_ 6 ? :timestack 6: $task | :run: $task |
| 209 | | :: Run current tasks ? :cycle: $_ 7 ? :timestack 7: $task | :run: $task |
| 210 | | :: Run current tasks ? :cycle: $_ 8 ? :timestack 8: $task | :run: $task |
| 211 | | :: Run current tasks ? :cycle: $_ 9 ? :timestack 9: $task | :run: $task |
| 212 | | :: Run current tasks | |
| 213 |