nx_lamport_clock.nx source
↩ module page · 23 lines · 1160 B
1// nx_lamport_clock.nx -- Lamport logical clock (Leslie Lamport 1978): causal ordering across actors, the id source
2// for the sequence CRDT. Each actor holds (clock, actor_id); tick() bumps + returns a monotonic clock for a new op;
3// observe(remote) advances local to max(local,remote) so a happens-before edge always yields a strictly greater id.
4// Combined with the actor_id tie-break this gives a DETERMINISTIC TOTAL ORDER every replica computes identically =
5// the backbone of CRDT convergence. 100% integer. V-COLLAB rung. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8// state: [0]=clock [1]=actor
9func lc_new(actor: i64) -> *i64 {
10 let c: *i64 = sys_mmap(16) as *i64
11 c[0] = 0
12 c[1] = actor
13 return c
14}
15func lc_actor(c: *i64) -> i64 { return c[1] }
16func lc_value(c: *i64) -> i64 { return c[0] }
17// tick: increment local clock, return the new value (the id_clock for a freshly created op)
18func lc_tick(c: *i64) -> i64 { c[0] = c[0] + 1; return c[0] }
19// observe a remote op's clock so causal order holds: local = max(local, remote)
20func lc_observe(c: *i64, remote: i64) -> i64 {
21 if remote > c[0] { c[0] = remote }
22 return c[0]
23}