Qubit Lock
Lock-focused utilities for the Qubit Rust libraries. The crate provides synchronous and asynchronous lock wrappers plus monitor-style coordination.
Features
ArcMutex,ArcRwLock: parking_lot-based synchronous lock wrappers withArcbuilt in.ArcStdMutex,ArcStdRwLock: standard-library lock wrappers for callers that need poison semantics.ArcAsyncMutex,ArcAsyncRwLock: Tokio-based asynchronous lock wrappers enabled by the defaultasyncfeature.ParkingLotMonitor,ArcParkingLotMonitor,ParkingLotMonitorGuard: parking_lot-based condition coordination.StdMonitor,ArcStdMonitor,StdMonitorGuard: std-based condition coordination.MockMonitor,ArcMockMonitor: deterministic monitor testing with manually advanced timeout time.TokioMonitor,ArcTokioMonitor: async monitor coordination with Tokio.- Closure-based APIs that keep lock acquisition and release scoped to one call.
Arc*wrappers implementDerefandAsRef, so the native guard-based APIs of the wrapped primitive remain available when needed.
Installation
[]
= "0.8"
The async wrappers use Tokio synchronization primitives and are enabled by default. For sync-only users that want to avoid Tokio in the dependency graph:
[]
= { = "0.8", = false }
If your application creates a Tokio runtime, enable the appropriate Tokio
runtime features in your own Cargo.toml, such as rt or rt-multi-thread.
AsyncLock returns Send futures: ArcAsyncMutex<T> implements it for
T: Send, while ArcAsyncRwLock<T> implements it for T: Send + Sync.
Migration from 0.7
Version 0.8 contains intentional breaking API cleanup:
Monitoris now an aggregate trait for blocking monitor capabilities.- The concrete parking_lot monitor is now
ParkingLotMonitor; its cloneable handle isArcParkingLotMonitor. - Timeout condition methods are named
wait_until_forandwait_while_for. MockMonitorandArcMockMonitorprovide manually advanced timeout time for deterministic tests.- With the default
asyncfeature,TokioMonitorandArcTokioMonitorprovide async monitor operations. qubit_lock::lockandqubit_lock::monitorare no longer public modules. Import public types directly from the crate root.
Quick Start
Synchronous lock
use ;
Native lock APIs
Arc* wrappers can still use the native lock APIs of their wrapped
primitives through Deref or AsRef.
use ;
For ArcRwLock and ArcAsyncRwLock, the closure-based read and write
methods have the same names as the native guard-based methods. When Lock or
AsyncLock is in scope, use lock.as_ref().read() or explicit dereferencing
such as (*lock).read() to call the native guard API.
ParkingLotMonitor
use ArcParkingLotMonitor;
Project Layout
src/lock: lock traits and lock wrappers.src/monitor: monitor traits plus parking_lot, std, Tokio, and mock monitor implementations.tests/lock: lock behavior tests.tests/monitor: monitor behavior tests.tests/docs: README and doctest consistency tests.
Quality Checks
From a repository checkout:
License
Copyright (c) 2025 - 2026. Haixing Hu.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See LICENSE for the full license text.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Guidelines
- Follow Rust API Guidelines
- Keep comprehensive test coverage
- Document and provide examples for all public APIs
- Ensure all tests pass before submitting a PR
Author
Haixing Hu - Qubit Co. Ltd.
Related Projects
More Qubit Rust libraries are published under the qubit-ltd GitHub organization.
Repository: https://github.com/qubit-ltd/rs-lock