Qubit DCL
A standalone crate for double-checked locking over generic lock handles. It packages the usual “test outside the lock, lock, test again, run the task” sequence into a reusable DoubleCheckedLockExecutor, with an optional prepare / rollback / commit pipeline and structured results.
The crate re-exports ArcMutex and the Lock trait from qubit-lock so a typical app can depend on qubit-dcl alone.
Features
DoubleCheckedLockExecutor: one builder-built executor, many invocations; integrates with thequbit-functionTesterand runnable traits.DoubleCheckedLock: one-shot convenience entry foron(...).when(...).call*style execution without keeping an executor variable.- Double-checked flow: first condition check without the lock, optional pre-lock prepare, write lock, second check, then task; after the lock is released, optional prepare commit or rollback.
- Execution API:
call/execute(no direct&mut Tin the closure) andcall_with/execute_with(mutable access to protected data). - Typed outcomes:
ExecutionContextandExecutionResultdistinguish success, “condition not met,” task failure, and prepare finalization failures (ExecutorError). - Logging hooks via
logand configurableExecutionLoggeron the builder for unmet conditions and prepare-step failures.
How it works
- The condition tester runs twice (outside the lock, then again under the write lock). Anything the first read relies on must remain safe without this executor’s lock (for example atomics with appropriate orderings).
- If the first test passes, an optional prepare runnable may run; then the lock is taken, the second test runs, and the user task runs with
&mut Tif applicable. - If prepare ran successfully, after releasing the lock the executor may run commit on full success, or rollback when the inner check or task did not succeed.
Installation
[]
= "0.1.0"
qubit-dcl already depends on qubit-lock and re-exports ArcMutex and Lock; add a direct qubit-lock dependency only if you use types beyond those re-exports.
Quick start
use ;
use ;
Side-effect–only run (finish)
For execute or call with no meaningful return value, you can use ExecutionContext::finish on ExecutionContext<(), E> to get a bool success:
use ;
let data = new;
let ok = builder
.on
.when
.build
.execute
.finish;
assert!;
One-shot convenience (DoubleCheckedLock)
When you do not need to keep a reusable executor, use DoubleCheckedLock for a shorter chain:
use ;
use ;
let data = new;
let skip = new;
let updated = on
.when
.call_with
.get_result;
assert!;
assert_eq!;
Example program
A runnable sample is under examples/double_checked_lock_executor_demo.rs:
Builder API (summary)
- Start with
DoubleCheckedLockExecutor::builder(). - Attach a lock:
.on(lock)whereL: Lock<T>. - Set the double-checked condition:
.when(tester). - Optionally:
.prepare,.rollback_prepare,.commit_preparefor the prepare pipeline; any of.log_unmet_condition,.log_prepare_failure,.log_prepare_commit_failure,.log_prepare_rollback_failurefor diagnostics. - Finish with
.build().
Project layout
src/double_checked: executor, builders,ExecutionContext,ExecutionResult, errors, and logging.src/lock: lock-related glue used with the executor.tests/double_checkedandtests/docs: unit and README consistency tests.
Quality checks
License
Apache-2.0