Skip to content

No singletons, state_exec: the DSL's process-wide state per app, per object or gone - #1189

Closed
transfix wants to merge 4 commits into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-state-exec
Closed

transfix wants to merge 4 commits into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-state-exec

Conversation

@transfix

Copy link
Copy Markdown
Owner

No singletons, state_exec: the DSL's process-wide state per app, per object or gone. Stacked on #1176 (Ariadne part 3); a sibling of #1182, #1183, #1184 and #1187. Breaking (4.0).

What moved

  • Header statics:
    • nil_value, true_value and false_value are functions returning a fresh value_t (value_t cannot be constexpr). All 233 uses across the tree are updated.
    • trampoline_next()::h is gone: each sync_wait() owns a local slot, and every promise in the chain it drives points at it through promise_type::trampoline_.
  • Per app (app.slot):
    • frozen_builtins(cvc::app&). A group with no app freezes a default environment of its own.
    • exec_group's creation count, live-group count and the "manager is single" note. These were process-wide maps keyed by app address.
    • The threaded-host flag. pycvc no longer sets it for the whole process at import; it notes each app in make_app, app_from_capsule, at Exec construction and in set_lock_manager, and the embedded Python engine notes the host's app in attach().
  • Per object:
    • Scheduler serials and cycle-registry ids are 64 random bits.
    • Each async_host builds its reply channels from its own random tag plus a count.
    • CVC_EXEC_VERIFY is read when each scheduler is made.
    • The closure-body cache lives in the cycle registry, which removes t_shared_bodies and the registry-id counter.
    • The chain-pin scratch stack is intrinsics_context::pin_scratch.
    • The lock manager owns the CVC_EXEC_FUZZ state and the lock test hooks; the with-lock token is captured by the wait call the evaluator builds.
    • math.random with no group stream draws from its registry's own stream, under a mutex.
    • exec_pool's g_membership mutex is each group's own (group_pool_link::membership). pump_all takes it per group while it claims and releases the group, and purge_host_calls holds the group's own across its wait, so the call still cannot end while the purge waits on it. Pools of different apps no longer share a lock.
  • Removed or made constant:
    • stdlib_registry::standard() and shared_scope() are gone. make_scope() builds a new scope, and make_default_environment() links one of its own.
    • print takes stdout's own C library lock instead of a leaked process-wide mutex.
    • The special-forms list is a constexpr table, with a new is_special_form().
    • kSso, kFreshBuckets, kBucketFormulaHolds, has_cpu_clock and the CVC_STATE_EXEC_CHECK cache are computed where used, kept in the scope pool, or constexpr.

Cost: groups with no app, and every make_default_environment() call, now build their own stdlib scope instead of sharing one: a few hundred more allocations per default environment. Only state_exec_bench_smoke was run against it.

Public API changes

  • nil_value(), true_value(), false_value(); frozen_builtins(cvc::app&).
  • stdlib_registry::make_scope() replaces shared_scope(); standard() is removed, in pycvc too.
  • all_special_forms() returns by value; is_special_form() is new.
  • detail::live_groups(cvc::app&), detail::note_threaded_host(cvc::app&), threaded_host_noted(cvc::app&).
  • scheduler_base::verifies_pump_owner(), detail::new_scheduler_serial(), detail::pump_owner_verification().
  • On lock_manager: set_fuzz_seed, fuzz_seed, fuzz_hits, fuzz(), set_test_hooks, test_hooks. Removed: the process-wide set_lock_fuzz_seed, lock_fuzz_seed, lock_fuzz_hits, set_lock_test_hooks, lock_test_hooks_get, lock_enter_scope and lock_enter_token_get.
  • lock_port takes the fuzz state; lock_wait_call(ops, args, token) takes the token.

Allowlist: -79 lines (cvc 55, cvc-cli 6, cvcGL 6, pycvc 6, pycvc_gl 6)

The cvc-cli, cvcGL and pycvc lines are the header statics (nil_value and the rest) that every target compiled in.

Not removed: 31 lines (please review)

Apart from the exception classes and the scope-serial counter, these are per-thread, not process-wide: each is set and restored in scope by the thread doing the work, so no two apps share one. They stay because the code that reads them has no context to get one from: ~value_tag, shared_ptr deallocators, and native functions, whose type std::function<value_t(span<const value_t>)> takes no context argument (about 590 of them in 69 files).

  • types.cpp: the deadline and allocation guards, t_in_async_kernel, the iterative value-release queue (used from ~value_tag), and the scope pool (used from control-block deallocation).
  • Scope serials: g_next_serial_run plus two thread-locals. g_next_serial_run is the one process-wide counter left; it keeps scope serials unique across threads, and environment::extend has no app to hold it.
  • heap.cpp: the bound cycle registry and each thread's fallback registry, read when values are created.
  • stackless_evaluator.cpp: t_await, t_park_forbidden, t_step_depth (natives read them through can_await and request_await) and the per-frame vector recycling.
  • data_job.cpp t_scope, t_native_depth; stdlib.cpp t_stream; exec_group.cpp t_current (behind the public exec_group::current()); native_stack.cpp the thread's stack bound.
  • exceptions.cpp classes()::built: the 19 built-in exception classes. They are immutable and identified by address, so C++ errors raised anywhere map to the classes a program catches. Making them per app needs a design decision: the C++-to-DSL exception mapping, the read-only check and snapshot relinking would all have to take an app or environment.
  • Not on the allowlist (Windows only): types.cpp's measured TSC rate, cached once per process because measuring it takes a 5 ms sleep.

Removing the rest needs a context argument on native functions or a redesign of how values are released: a follow-up.

Tests

  • multi_app_integration_test gains a state_exec part. Each app defines a function and an exception class, raises it through a call and catches it, runs a group of its own, and records the group's sequence and pid, whether its frozen builtins survived, and its threaded-host note.
  • ExecGroup.CreationOrderAndLiveCountArePerApp (both orders, apps on threads, a later app counts from 1).
  • SchedulerSerialTest.EachSchedulerDrawsItsOwn.
  • StdlibSharedScopeTest.EachAppHasItsOwnFrozenBuiltins and EachDefaultEnvironmentLinksItsOwnScope.
  • LockFuzz.TheSeedIsTheManagers.
  • StateExecTypesTest.ConstantsAreNewValues and LookupBucketFormulaHolds.
  • AppLockManager.AutoIsSharedForAThreadedHost runs in-process with two apps in both orders, instead of in a death-test child.
  • The frozen-builtins tests check one scope per app; the lock suites install fuzz seeds and hooks on their own managers.

Locally (Windows, MSVC Release; the full tree builds, including cvcGL, pycvc, pycvc_gl and the examples):

  • all 34 state_exec_* test executables, including state_exec_locks_test (215) and state_exec_locks_model_test (201);
  • ariadne_runtime_test (262), ariadne_registries_test, app_test (121), multi_app_integration_test, exec_pool_test (33);
  • 19 pycvc ctests (app, threading, async, the exec* and pycvc_state_exec* tests, both ariadne tests, pycvc_gl_ariadne_host, pycvc_gl_ariadne_gil);
  • state_exec_bench_smoke.

AsyncCalls.AS19 and AS24 failed once on wall-clock limits with the machine at 100% CPU, then passed three times on rerun. After the membership change: exec_pool_test, multi_app_integration_test, state_exec_groups_test, state_exec_async_calls_test and app_test pass.

CI

Full run: the push to ci/full/no-singletons-state-exec, https://github.com/transfix/libcvc/actions/runs/43214724015.

…object or gone

libcvc keeps no process-wide state in code we control (P0a). This moves the
state_exec objects that collide between apps onto the app or into the object
that owns them, and removes 77 lines from tools/singleton_scan/allowlist.txt.

Header statics (every target that includes types.h or task.h):
- nil_value, true_value and false_value were inline const value_t objects, one
  per process: they are functions now, nil_value() etc., returning a new
  value_t (value_t is not a literal type, so it can't be constexpr). Every use
  in the tree is updated; the two sites that took &nil_value use a local.
- task.h's thread_local trampoline slot (trampoline_next()::h) is gone:
  sync_wait() owns its slot (a local) and every promise of the chain it drives
  points at it (promise_type::trampoline_, passed on as a task is awaited). A
  nested sync_wait has a slot of its own; a coroutine with no slot transfers
  symmetrically, as MSVC always did.

Per app (app.slot<T>()):
- frozen_builtins() is frozen_builtins(cvc::app &): the frozen default
  environment an app's exec_groups and pooled pycvc Execs extend, built once
  per app. A group with no app freezes a default environment of its own.
- exec_group's creation count (pool_link().seq), its live-group map and its
  single-manager note were process-wide maps keyed by app address (an app at a
  reused address could inherit another's count or miss its note): each app
  keeps its own. live_groups() takes cvc::app &. A group with no app has seq 0.
- The threaded-host flag (g_threaded_host) is the app's:
  detail::note_threaded_host(cvc::app &) / threaded_host_noted(cvc::app &).
  pycvc no longer sets a process-wide flag at import; it notes each app it
  makes (make_app) or runs an Exec on, before that app's lock manager is made.
  So importing pycvc no longer turns every later app's lock manager shared.

Per object:
- A scheduler's serial (the foreign-future check) and a cycle registry's id
  are 64 random bits each (never 0), not drawn from a library-wide counter.
- An async_host's reply channels are "<name>.async#<host tag>-<n>": the
  host's random tag and its own count, not a process-wide sequence.
- CVC_EXEC_VERIFY is read as each scheduler is made
  (scheduler_base::verifies_pump_owner()); no_process() is a member.
- The closure-body cache (t_shared_bodies) lives in the cycle registry its
  closures are tracked in (a group's, or the thread's own), so two groups
  never share a body list, and the registry id that keyed it is not needed.
- The intrinsics' chain-pin scratch stack is the intrinsics_context's
  (pin_scratch), not a thread_local; a data job's deferred write uses a local.
- The lock manager owns the CVC_EXEC_FUZZ state (seed, hits, the yield
  stream: lock_manager::set_fuzz_seed / fuzz_seed / fuzz_hits, read from the
  environment as each manager is made) and the test hooks
  (lock_manager::set_test_hooks / test_hooks). The process-wide seed, hit and
  thread counters, the thread_local PRNG and the global hook pointer are gone.
- math.random with no group stream bound draws from its stdlib_registry's own
  stream (under a mutex), not a thread_local engine.

Gone or made constant:
- stdlib_registry::standard() (a process-wide registry) and shared_scope() (a
  process-wide frozen scope) are removed; stdlib_registry::make_scope() builds
  a new frozen scope, and make_default_environment() links one of its own.
- print takes stdout's own C library lock (flockfile / _lock_file) around its
  one write, instead of a leaked process-wide mutex.
- stackless_evaluator::all_special_forms() returns a new std::set from a
  constexpr table; is_special_form(name) answers without building it (Ariadne's
  two callers use it).
- kSso, kFreshBuckets, kBucketFormulaHolds, has_cpu_clock and the env flag
  CVC_STATE_EXEC_CHECK are computed where used, kept in their owner (the scope
  pool), or constexpr; detail::bucket_formula_holds() is checked by a test.

API changes (4.0): nil_value() / true_value() / false_value();
frozen_builtins(cvc::app &); stdlib_registry::make_scope() replaces
shared_scope(), standard() is gone (pycvc too); all_special_forms() returns by
value, is_special_form() added; detail::live_groups(cvc::app &);
detail::note_threaded_host(cvc::app &), threaded_host_noted(cvc::app &);
detail::new_scheduler_serial(), pump_owner_verification(),
scheduler_base::verifies_pump_owner(); lock_manager::set_fuzz_seed,
fuzz_seed, fuzz_hits, fuzz(), set_test_hooks, test_hooks; lock_port takes the
fuzz state; detail::lock_fuzz_point(lock_fuzz *, where),
lock_fuzz_seed_from_environment(); the process-wide set_lock_fuzz_seed,
lock_fuzz_seed, lock_fuzz_hits, set_lock_test_hooks and lock_test_hooks_get
are gone; intrinsics_context::pin_scratch; detail::bucket_formula_holds().

Tests: multi_app_integration_test's workload gains state_exec(): each app runs
a DSL program on its exec scheduler that defines a function and an exception
class of its own, raises through a call and catches it, then runs a group of
the app (its seq, its pid, the app's frozen builtins, the app's threaded-host
note); the transcripts match alone, at once on threads and in reverse order.
New: ExecGroup.CreationOrderAndLiveCountArePerApp (two apps in both orders,
apps on threads, a later app counts from 1), SchedulerSerialTest
.EachSchedulerDrawsItsOwn, StdlibSharedScopeTest.EachAppHasItsOwnFrozenBuiltins
and EachDefaultEnvironmentLinksItsOwnScope, LockFuzz.TheSeedIsTheManagers,
StateExecTypesTest.ConstantsAreNewValues and LookupBucketFormulaHolds;
AppLockManager.AutoIsSharedForAThreadedHost runs in process now (two apps,
both orders, a later app) instead of in a death-test child; the frozen-builtins
tests check one scope per app and another app's differs. The lock suites
install fuzz seeds and hooks on their managers.

Allowlist lines removed (77): cvc 53 (state_exec sources 45, <header> 8),
cvc-cli 6, cvcGL 6, pycvc 6, pycvc_gl 6.
…ument

The evaluator hands detail::lock_wait_call the lock-enter token of the one
%lock-wait call it applies (the wait native it builds captures it), instead of
arming it in a thread_local (t_lock_enter): a program's own %lock-wait call has
none, as before (LW-2). lock_enter_scope and lock_enter_token_get() are gone.
One more allowlist line removed (cvc: t_lock_enter).

Also: an exec_group keeps a pointer to its app's counts, so its destructor
looks nothing up; the integration workload's error message is built with
str-concat (str takes one argument).
…ine's include

pycvc declares stackless_evaluator::all_special_forms() as it now is, returning
a new set by value (its set out-typemap takes the value). python_engine.cpp
includes lock_manager.h outside the server-only block, where attach() notes the
threaded host on the app it is given.
exec_pool's process-wide g_membership mutex guarded every group's
pool_link().call. Each group_pool_link now has its own membership mutex:
pump_all takes it per group while it claims and releases the group, and a
group's purge_host_calls holds its own across the wait, so the call still
cannot end while the purge waits on it. Pools of different apps no longer
share a lock. Allowlist: -1.
transfix added a commit that referenced this pull request Nov 12, 2026
The trampoline's await_suspend is a member template (it reads the caller's
promise for its trampoline slot), and GCC and Clang reject a member template
in a local class. CVC_COROUTINE_TRAMPOLINE is 1 only off MSVC, so Windows
built and every Linux, macOS and wasm build failed in task.h (#1189's run:
package-linux debug, CGAL A/B, both wasm-mt lanes). task<T>::awaiter and
task<void>::awaiter are now members; operator co_await returns one.
@transfix

Copy link
Copy Markdown
Owner Author

Replaced by #1203, which lands the whole no-singletons stack as one PR. This branch's head is an ancestor of #1203's branch, so its commits are all there; the branch is kept.

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