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