Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,24 @@ mutex consistent.
set this when the host process never installs a SIGUSR1 handler (i.e.
not under Wine); it measurably reduces lock-path CPU under contention
(~13% per-op in the multi-process benchmark).
A host that does install such a handler can avoid both syscalls with
`ntsync_set_lock_flag_hook` instead: the library sets a per-thread flag
the host owns while the thread holds the lock, the host's SIGUSR1 handler
marks the signal pending and returns while the flag is set, and the
library re-raises SIGUSR1 right after the unlock (protocol in
`include/ntsync_user.h`). Threads for which the hook returns NULL keep
the masking.
- `NTSYNC_WAKE_ALL=1` — every signal op wakes all of an object's waiters
instead of only as many wait-any waiters as it made units available. Kept
for comparison; the default is cheaper for the releasing thread once its
wakes are issued after the unlock (see `sim_unity_release_cost`).
- `NTSYNC_DEBUG=1` — log to stderr, stdout and `$TMPDIR/ntsync_debug.log`
(the file is the reliable channel for in-game diagnostics); dump
per-process stats every 10 s (waits, fast-path hits, lock acquisitions,
wake walks, signal→scheduled wake latency `wake_lat_cnt/us_sum/us_max`);
wake walks, signal→scheduled wake latency `wake_lat_cnt/us_sum/us_max`,
budgeted wakes that found nobody parked `wakes_unparked` and the second
walks that re-issued them `wake_rewalks`, SIGUSR1s held back by the
lock-flag hook `sigusr1_deferred`);
emit a "stuck wait" dump with per-object state (including creator pid
liveness) when a wait stalls.
- Object lifetime: an object is reaped only when its creator **and every
Expand Down
19 changes: 19 additions & 0 deletions include/ntsync_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ int32_t ntsync_init(const char *path);
* process exits. Returns the number of freed objects or a negative errno. */
int32_t ntsync_sweep_dead(void);

/* Optional SIGUSR1 cooperation, for hosts whose SIGUSR1 handler suspends the
* thread (Wine). A thread suspended while it holds the shared region lock
* would wedge every process on it, so by default the library blocks SIGUSR1
* around each lock acquisition: two sigprocmask syscalls per acquisition.
* A host can instead give each thread an int the library sets while the
* thread holds (or waits for) the lock:
* - get_flag returns the calling thread's int (NULL: fall back to masking
* for this acquisition). It is called in normal context only.
* - Before taking the lock the library stores NTSYNC_LOCK_FLAG_HELD; after
* releasing it, it atomically swaps the int to 0 and, if
* NTSYNC_LOCK_FLAG_SIGNAL_PENDING was set, re-raises SIGUSR1.
* - The host's SIGUSR1 handler must, before doing anything else, check the
* int: if nonzero, atomically OR in NTSYNC_LOCK_FLAG_SIGNAL_PENDING and
* return without suspending.
* Pass NULL to uninstall. Returns 0. */
#define NTSYNC_LOCK_FLAG_HELD 1
#define NTSYNC_LOCK_FLAG_SIGNAL_PENDING 2
int32_t ntsync_set_lock_flag_hook(int *(*get_flag)(void));

/* Create objects. Return 0 and store the handle, or a negative errno.
* Handles are never 0: slot 0 is permanently reserved because 0 is the
* "no alert" sentinel in ntsync_wait_args.alert. */
Expand Down
Loading
Loading