Skip to content

state_exec locks L0: state-incr, state-cas and state-swap - #860

Merged
transfix merged 13 commits into
masterfrom
perf/locks-l0
Oct 29, 2026
Merged

transfix merged 13 commits into
masterfrom
perf/locks-l0

Conversation

@transfix

Copy link
Copy Markdown
Owner

This is phase L0 of the state_exec locks plan: the atomic natives of docs/STATE_EXEC_LOCKS.md section 4.4.

Stacking. The base is perf/locks-base, which is master with #858 (C0, the sanitizer jobs) and #859 (B0, the bench's pair and gate rules) merged in. Once those two land, this retargets to master.

What it adds

  • cvc::state: compareAndSetValue, exchangeValue and updateValue.
    • Each compares, or computes, and assigns under one hold of the node's mutex.
    • Signals fire after the lock is released, and only on a change.
    • A writable transparent link writes its target, as value(v) does.
    • A transparent link that is not writable, or cannot be resolved, throws read_only_error instead of value(v)'s write on the link node (gap G-7). So a compare and its write always see the same node.
    • pycvc wraps the first two; updateValue takes a std::function and is %ignored.
  • The DSL natives.
    • (state-incr path [delta]) returns the new number. An absent or empty node is 0; non-numeric text is a ValueError and the node is left alone. int + int stays an int, and an overflow is an ArithmeticError. Floats are stored in their shortest round-trip form.
    • (state-cas path expected new) returns #t or #f. A nil expected matches an absent or empty node.
    • (state-swap path new) returns the old value, nil when absent or empty.
    • All three resolve paths under the chroot and through transparent links, as state-get does.
  • One reader of number text (detail::read_number_text), shared by the parser and state-incr (gap G-9).
  • A std::to_chars fallback for libc++ builds without floating-point to_chars. It writes to_chars's exact text, and a test checks it byte for byte on every platform, against a table and 20,000 values that include powers of two (gap G-10).
  • The bench scenario native.state_atomic: incr and cas against set, per call (gap G-8).
  • Docs: the developer guide, STATE_API.md, ARIADNE.md (the reactive lane does not bind them) and STATE_EXEC_BENCH.md.

Tests

  • state_exec_locks_test (new): suite Atomics, AT-1..AT-13 on both schedulers, plus argument errors and the DSL number rules.
    • AT-6: two threads × 10^6 updateValue calls; the count is exact. It is 10^4 under CVC_LOCKS_SANITIZER.
    • FormatShortestFallback.
    • The locks_world fixture: one intrinsics context per program, which L1 and later phases reuse.
  • state_test: StateAtomicMembers.*, 10 cases.
  • pycvc: test_pycvc_state.py gains three cases; test_pycvc_state_exec_locks.py is new.
  • Both the C++ and the pycvc lock tests join C0's TSan and ASan+UBSan jobs.

Run locally (Windows, MSVC, Release): every state_exec_* suite, state_test, ariadne_runtime_test and the 11 pycvc state/exec tests pass, unchanged except for the new cases. state_exec_bench --smoke passes.

Not done yet: the native.state_atomic gate (incr and cas at most 1.1x set), and the regression A/B at locks=none against the base. Both need a quiet machine. Results will be posted here.

… the locks deep workflow

The state_exec lock work (docs/STATE_EXEC_LOCKS.md) adds code that runs on
several threads, and no CI lane ran a race detector or an address checker. This
lands them before any lock code, so that the races and leaks already in the
tree get a baseline first.

- ci.yml `sanitizers` (tsan, asan-ubsan): gcc's -fsanitize=thread, or
  -fsanitize=address,undefined, over libcvc itself (the cvcpkg deps stay
  uninstrumented), RelWithDebInfo, building only the targets it runs. It runs
  the existing suites that use threads (state_exec_scheduler, _async,
  _integration and _coordinator tests, ariadne_runtime_test, thread_pool_test,
  async_lane_test) and pycvc's exec scripts, with the sanitizer runtime
  preloaded into the interpreter. Suppressions go in .github/sanitizers/, each
  with a comment naming what it hides. Each lock phase appends its suites.
- ci.yml's pull_request filter takes perf/** as well, so the lock phases'
  stacked drafts get the full set of jobs.
- locks-deep.yml (daily, and by hand on a branch): a Release Linux build of the
  lock test targets, running their deep sizes. A dispatchable workflow must be
  on the default branch, so it lands first; on a ref without the lock tests it
  stops after the checkout.
…s, gate, and policy/locks sweep keys

The state_exec lock phases decide their gates with the bench, and the design's
gates need things compare could not do: two points of one run against each
other, absolute bounds, a noise rule that refuses to judge a disturbed run,
and a rule under which a drop in steps_total fails.

tools/bench/state_exec_bench.py:
- key_of leaves out a param at its implicit default (policy=rr, locks=none),
  so records from binaries without those keys still match.
- compare --pair KEY=A:B compares the base's points at A with the
  candidate's at B, matched without KEY (both may be one run).
- compare --max-noise P: a point noisier than P% is INCONCLUSIVE (exit 2,
  "repeat in a quieter window", never a pass).
- compare --fail-on-change: any change beyond the noise fails, in either
  direction, for steps_total.
- gate RUN --scenario --metric (--max|--min) [--stat] [--where] [--max-noise]:
  absolute bounds on one run.

state_exec_bench: sched.single.*, flood.small and msg.* take two sweep keys,
policy (rr, priority, priority_rr) and locks (none; the lock phases add
private and shared, and a build without them skips such a point). A default
run measures the same points as before, plus the two params at their
defaults.

tools/bench/test_state_exec_bench.py tests the rules on synthetic records;
CTest runs it as state_exec_bench_tool_test when a working Python 3 is found.
Atomic single-node updates for the DSL's state-cas, state-swap and
state-incr (docs/STATE_EXEC_LOCKS.md 4.4). Each compares, or computes, and
assigns under one hold of the written node's mutex, then fires valueChanged
and the parent's child signal with no lock held, only when the value changed.

They write the node a read through a transparent link sees: a writable
transparent link writes its target, as value(v) does. Unlike value(v), a
transparent link that is not writable, or cannot be resolved, throws
read_only_error rather than taking the write on the link node, so a compare
and its write are always on the same node (gap G-7).

Three non-virtual members and one private helper, so state.h stays source-
and layout-compatible. pycvc wraps compareAndSetValue and exchangeValue;
updateValue takes a std::function and is %ignore'd.
…-incr

detail::read_number_text reads a token as the DSL does: the whole token as an
int64, else as a double (strtod, for a token that may be one). The parser's
parse_number_or_symbol now calls it, unchanged in behaviour, and state-incr
will read a node's text with it, so both agree on what a number is (gap G-9).
The atomic natives of docs/STATE_EXEC_LOCKS.md 4.4, each one native call
over cvc::state's atomic members:

- (state-cas path expected new): #t when the value equalled `expected` and
  is now `new`. A nil expected matches an absent or "" node; an absent node
  is made only then.
- (state-swap path new): the value it replaced, nil when absent or "".
- (state-incr path [delta]): the new number. The node's text is read as a
  DSL number (detail::read_number_text); an absent or "" node is 0; anything
  else is a ValueError and the node is left as it was. int + int stays an int
  (an overflow is an ArithmeticError, gap G-11); otherwise the sum is a float,
  stored in its shortest round-trip form.

Each resolves its path under the chroot and through a transparent link as
state-get reads it; nil is "", a list or dict is a TypeError. A change fires
watches and valueChanged once; an equal write fires nothing.

Floats are written with std::to_chars where the library has it, else by
detail::format_number_shortest_fallback, which writes to_chars's exact text
(shortest round-trip digits, fixed or scientific, the exact digits of a whole
number in fixed form). It is built everywhere, and a test checks it against
to_chars byte for byte (gap G-10).

Tests: state_exec_locks_test (new; the Atomics suite AT-1..AT-13 on both
schedulers, AT-6's two-thread updateValue, the formatter against to_chars on
a table and 20000 values; the locks_world fixture the lock phases reuse),
and test_pycvc_state_exec_locks.py. Both join C0's sanitizer jobs.
state-incr and state-cas timed against state-set by the native.* method, on a
node 1 or 4 levels down that starts at 0 and must end at n: op=set is
(state-set P (+ i 1)), op=incr (state-incr P), op=cas (state-cas P i (+ i 1)),
which succeeds and writes every time (gap G-8). Its gate, a paired one: incr
and cas each at most 1.1x set in extra.ns_per_call (docs/STATE_EXEC_BENCH.md).
The developer guide's DSL state section gains state-incr, state-cas and
state-swap: paths and links, absent nodes, nil, number text, signals, and
which tool to use when. Ariadne's reactive lane does not bind them (they
write). STATE_EXEC_LOCKS.md's status: L0 built.
master now has C0 (#858) and B0 (#859), squashed, so this branch takes
their final versions: master's ci.yml (one sanitizers job) with L0's two
entries in its lists (state_exec_locks_test, and
test_pycvc_state_exec_locks.py), and master's sanitizer files.
@transfix
transfix changed the base branch from perf/locks-base to master October 29, 2026 02:40
…looks

ASan's leak check failed state_exec_locks_test on 189 indirect leaks:
default environments held by a closure bound in the scope it captured,
a cycle only collect_cycles() frees. The tests step the schedulers by
hand, so nothing ever collected. A gtest global environment now runs
collect_cycles() at the end, when every world is gone, so the leak
check sees only real leaks.
@transfix
transfix marked this pull request as ready for review October 29, 2026 19:00
@transfix
transfix merged commit 82634fb into master Oct 29, 2026
26 checks passed
@transfix
transfix deleted the perf/locks-l0 branch October 29, 2026 20:54
transfix added a commit that referenced this pull request Oct 29, 2026
The project version and the cvcpkg recipe family move to 3.7.4. Every recipe is at cvc_revision 1
already, so none restarts.

3.7.4 carries the whole plant ledger on every plant (#876): plant_sample's vehicle
contact and what each plant measures, the dynamic plants' building and vehicle contacts and
attitude, nav_stats' vehicle_hits, cvc/nav/plant_ledger.h, and publish_nav_stats writing every
item, n/a where a plant does not measure it. demo3's Ledger screen (CVC-DBG/cvcdbg#256) reads it.
It also carries SDL3 owning cvcGL's onscreen window (#829), state_exec's state-incr, state-cas and
state-swap (#860), and Ariadne's window fitting (#828) and room relay test counting (#867).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant