code wiki / _hdl_build / nx_pub_q_gate.nx

nx_pub_q_gate.nx source

↩ module page · 54 lines · 3946 B

1// nx_pub_q_gate.nx -- R17 PROOF: request PRIORITY lanes + per-environment concurrency groups (LOCKGROUP). 2// 3// PRIORITY -- given queued requests at priorities 1/5/3, pick the HIGHEST (5), not the first or last (liar-kill). 4// LOCKGROUP -- different sites derive DIFFERENT lock resources (parallel-capable) and the same site the SAME one 5// (serialized via the proven flock); two different-site locks are held SIMULTANEOUSLY in-process. 6// File-based (no forks; cross-process serialization is already proven by nx_pub_loop_gate). license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_publisher.nx" 9import "nx_runpath.nx" 10 11func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 13func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" " as *u8); g_w(id); g_w(": " as *u8); if ok==1 { g_w("OK\n" as *u8); pass[0]=pass[0]+1 } else { g_w("FAIL\n" as *u8) } return 0 } 14func sv_write(path: *u8, content: *u8) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd<0 { return 0 } var n: i64=0; while content[n]!=(0 as u8){n=n+1} sys_write(fd, content, n); sys_close(fd); return 1 } 15func g_unlink(path: *u8) -> i64 { __syscall(87, path as i64, 0, 0, 0, 0, 0); return 0 } 16 17func main() -> i64 { 18 let pass: *i64 = sys_mmap(8) as *i64; pass[0]=0 19 g_w("=== NX-PUB-Q GATE (priority lanes + per-env concurrency lockgroups) ===\n" as *u8) 20 let wsid: *u8 = sys_mmap(64); rp_wsid(wsid); rp_ensure(wsid) 21 let D: *u8 = sys_mmap(512); rp_path(wsid, "pq" as *u8, D); __syscall(83, D as i64, 493, 0, 0, 0, 0) 22 let src: *u8 = sys_mmap(700); pub_join(D, "s.src" as *u8, src); sv_write(src, "QX\n" as *u8) 23 let qpath: *u8 = sys_mmap(700); pub_join(D, "pq.tsv" as *u8, qpath); g_unlink(qpath) 24 25 // ---- PRIORITY ---- 26 pub_submit_pri(qpath, src, "a.html" as *u8, "games" as *u8, "wsA" as *u8, "internal" as *u8, 1) 27 pub_submit_pri(qpath, src, "b.html" as *u8, "games" as *u8, "wsB" as *u8, "internal" as *u8, 5) 28 pub_submit_pri(qpath, src, "c.html" as *u8, "games" as *u8, "wsC" as *u8, "internal" as *u8, 3) 29 let pick_dest: *u8 = sys_mmap(256) 30 let best: i64 = pub_priority_pick(qpath, pick_dest) 31 let pri_ok: i64 = ((best==5) as i64) & (pub_streq(pick_dest, "b.html" as *u8)) 32 33 // ---- LOCKGROUP ---- 34 let rg: *u8 = sys_mmap(320); pub_lockgroup_res("games" as *u8, rg) 35 let rw: *u8 = sys_mmap(320); pub_lockgroup_res("wiki" as *u8, rw) 36 let rg2: *u8 = sys_mmap(320); pub_lockgroup_res("games" as *u8, rg2) 37 let distinct: i64 = (pub_streq(rg, rw)==0) as i64 38 let samesite: i64 = pub_streq(rg, rg2) 39 let fd1: i64 = pub_lockgroup_acquire("games" as *u8) 40 let fd2: i64 = pub_lockgroup_acquire("wiki" as *u8) 41 let both_held: i64 = ((fd1>=0) as i64) & ((fd2>=0) as i64) 42 pub_lockgroup_release(fd1); pub_lockgroup_release(fd2) 43 44 g_w(" priority: best=" as *u8); g_n(best); g_w(" dest='" as *u8); g_w(pick_dest); g_w("'\n" as *u8) 45 g_w(" lockgroup: distinct=" as *u8); g_n(distinct); g_w(" samesite=" as *u8); g_n(samesite); g_w(" fd1=" as *u8); g_n(fd1); g_w(" fd2=" as *u8); g_n(fd2); g_w("\n" as *u8) 46 47 g_row("PRIORITY: picks the HIGHEST-priority request (5 = b.html), not first(1)/last(3)" as *u8, pri_ok, pass) 48 g_row("LOCKGROUP: distinct resource per site (games != wiki), identical for same site" as *u8, distinct & samesite, pass) 49 g_row("LOCKGROUP: two different-site locks held SIMULTANEOUSLY (parallel-capable)" as *u8, both_held, pass) 50 51 g_w("NX-PUB-Q rows=3 pass=" as *u8); g_n(pass[0]) 52 if pass[0]==3 { g_w(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 53 g_w(" verdict=RED\n" as *u8); sys_exit(1); return 1 54}