code wiki / _hdl_build / nx_work_dispatcher.nx
nx_work_dispatcher.nx source
↩ module page · 47 lines · 2112 B
1// nx_work_dispatcher.nx -- the team picks items off the backlog and works them like a real team: respects
2// the governor's worker budget, tracks status, and ESCALATES blockers ("reaches out for help") instead of
3// spinning (operator: "acts like a real team picking up items from the backlogs and working them and
4// reaching out for help when they encounter blockers"). Supports a manual OVERRIDE pick (crash-the-gates).
5// Pure policy. license_tier: ORIGINAL Driven by nx_resource_governor + nx_sysload; priorities from the
6// PM decision matrix.
7
8import "nx_syscalls.nx"
9
10const WD_QUEUED: i64 = 0
11const WD_RUNNING: i64 = 1
12const WD_BLOCKED: i64 = 2
13const WD_DONE: i64 = 3
14const WD_HELP: i64 = 4 // escalated -- reached out for help
15
16// how many workers to START this beat = min(budget - already-running, queued).
17func wd_start_count(worker_budget: i64, running: i64, queued: i64) -> i64 {
18 var free: i64 = worker_budget - running
19 if free < 0 { free = 0 }
20 if queued < free { return queued }
21 return free
22}
23
24// next item to run: a manual OVERRIDE pick if set (>=0 and still queued), else highest-priority QUEUED.
25func wd_pick(states: *i64, prio: *i64, n: i64, override_idx: i64) -> i64 {
26 if override_idx >= 0 { if override_idx < n { if states[override_idx] == WD_QUEUED { return override_idx } } }
27 var best: i64 = 0 - 1; var bp: i64 = 0 - 1; var i: i64 = 0
28 while i < n { if states[i] == WD_QUEUED { if prio[i] > bp { bp = prio[i]; best = i } } i = i + 1 }
29 return best
30}
31
32// a BLOCKED item reaches out for help rather than retrying forever.
33func wd_should_escalate(state: i64) -> i64 { if state == WD_BLOCKED { return 1 } return 0 }
34
35func wd_count(states: *i64, n: i64, s: i64) -> i64 {
36 var c: i64 = 0; var i: i64 = 0
37 while i < n { if states[i] == s { c = c + 1 } i = i + 1 }
38 return c
39}
40
41func wd_state_label(s: i64) -> *u8 {
42 if s == WD_QUEUED { return "QUEUED" as *u8 }
43 if s == WD_RUNNING { return "RUNNING" as *u8 }
44 if s == WD_BLOCKED { return "BLOCKED" as *u8 }
45 if s == WD_DONE { return "DONE" as *u8 }
46 return "HELP-REQUESTED" as *u8
47}