TryLock
Source:
TryLock.h
LockGuard¶
src/core/util/TryLock.h:36A scope guard releasing on exit and doing nothing when the latch was busy, the standard try-to-lock shape.
Public Methods¶
inline explicit LockGuard(TryLock & l)
: Tries the latch once; test the guard to learn whether it took.
inline ~LockGuard()
: Releases only what it took.
inline explicit operator bool() const
: Whether this guard holds the latch.
LockGuard(const LockGuard &) = delete
: Non-copyable: two guards would release one latch twice.
TryLock¶
src/core/util/TryLock.h:24A non-blocking latch guarding a resource two threads reach, in this codebase the sender one core drains while another streams into it.
Public Methods¶
inline bool tryAcquire()
: Take the latch when it is free, false when another thread holds it.
inline void release()
: Give it back.
More info¶
Only try-acquire, never a blocking lock¶
The hot-path rule forbids a render or encode thread blocking on a peer, so a caller that loses the race skips its slot rather than waiting. That single constraint is what lets this be a test-and-set on the one type the standard guarantees lock-free, rather than an operating-system mutex. With no waiting there is nothing to sleep on, nothing to wake, and no priority to inherit. So it costs one atomic read-modify-write with no system call, and needs no lifecycle.
It is not recursive: a thread already holding it must not re-acquire, which the test would refuse.
Why it lives in core rather than the platform layer¶
It is portable code with no hardware backing, so it fails that layer's charter, which is the one place hardware interfaces are allowed. Domain-neutral and reusable, so any module coordinating two threads over one resource can take it.