mirror of
https://github.com/bertptrs/tracing-mutex.git
synced 2025-12-25 20:50:32 +01:00
Compare commits
85 Commits
v0.1.2
...
9cfd407f16
| Author | SHA1 | Date | |
|---|---|---|---|
| 9cfd407f16 | |||
| 7a943ebba6 | |||
| 25ae542ade | |||
| 40f835afd2 | |||
| e60dba2c25 | |||
| 8b69ba7835 | |||
| 9ca5af2c82 | |||
| 74b4fe0bb1 | |||
|
|
6199598944 | ||
| fd75fc453b | |||
|
|
43df59ac1c | ||
| 1fe44d0a05 | |||
| c9083c8bc1 | |||
|
|
d8c559fd3f | ||
| a8e8af6351 | |||
| 068303d81d | |||
| 6be3e05cab | |||
| 909e934572 | |||
|
|
0ae544a07a | ||
| 4148d509bf | |||
| fc1593b76f | |||
| 8f19921e9e | |||
| 00420d6807 | |||
| 49b15bb6bd | |||
| 29c9daf53e | |||
|
|
8feedb09d2 | ||
| de9888a102 | |||
| 2d2e03eede | |||
| e9b577a0f5 | |||
| 5f6823394d | |||
|
|
61d19f866c | ||
| f78969ebf7 | |||
| 56b0604448 | |||
| 6e5516eaa7 | |||
| 764d3df454 | |||
|
|
e543860d8b | ||
|
|
ed04552af3 | ||
|
|
c5a506436c | ||
| 33cb6014a3 | |||
| 5232bac582 | |||
|
|
6472f4b807 | ||
| 6afe7b1c48 | |||
| 9238ef53ee | |||
|
|
c08addff7d | ||
|
|
c1ce9df8ad | ||
| 312eaa8649 | |||
|
|
1f7e6921aa | ||
| f7048f265f | |||
| 64e56fdb86 | |||
|
|
8e3278fdd2 | ||
| 9ea993e737 | |||
| 062850fc3e | |||
| 0d2622d5c6 | |||
| d1417e0b0c | |||
| fcc64e2cef | |||
| fd0d05307c | |||
| 2f6e214784 | |||
| 3ec7e83e00 | |||
| ea8e0208a0 | |||
| 8926af4e13 | |||
| 77676ea04d | |||
| 46c92cfbbf | |||
|
|
743cc83669 | ||
| 4faaae8d8f | |||
|
|
b78af9150d | ||
| 515930c0a2 | |||
|
|
b5a5ca16c3 | ||
| aef99d4f65 | |||
|
|
6073c6c78d | ||
| cdd44f74fa | |||
| 38b3b226cc | |||
| 3b9b908460 | |||
| ef421e20eb | |||
| 66576e5b0e | |||
| 308af218e1 | |||
| 79ed599a2f | |||
| 680e335ccf | |||
| 17761af5a8 | |||
| 4c70d999d6 | |||
| 618a11f940 | |||
| 77cd603363 | |||
| 73b4c8b1af | |||
| b21a63e74b | |||
| 6a3cb83d01 | |||
| 08cfb17234 |
52
.github/workflows/ci.yml
vendored
52
.github/workflows/ci.yml
vendored
@@ -2,12 +2,14 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- staging
|
||||
- trying
|
||||
pull_request:
|
||||
|
||||
name: Continuous integration
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
tests:
|
||||
name: Rust project
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -18,29 +20,45 @@ jobs:
|
||||
- nightly
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions-rs/toolchain@v1
|
||||
- uses: dtolnay/rust-toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: ${{ matrix.rust }}
|
||||
override: true
|
||||
components: rustfmt, clippy
|
||||
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: build
|
||||
# Make sure we test with recent deps
|
||||
- run: cargo update
|
||||
- run: cargo build --all-features --all-targets
|
||||
- run: cargo test --all-features
|
||||
- run: cargo fmt --all -- --check
|
||||
- run: cargo clippy --all-features --all-targets -- -D warnings
|
||||
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
msrv:
|
||||
name: MSRV
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions-rs/cargo@v1
|
||||
- uses: dtolnay/rust-toolchain@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
toolchain: "1.70"
|
||||
|
||||
- uses: actions-rs/cargo@v1
|
||||
- run: cargo test --all-features
|
||||
|
||||
docs:
|
||||
name: Documentation build
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: dtolnay/rust-toolchain@v1
|
||||
with:
|
||||
command: clippy
|
||||
args: -- -D warnings
|
||||
toolchain: nightly
|
||||
|
||||
- name: Build documentation
|
||||
env:
|
||||
# Build the docs like docs.rs builds it
|
||||
RUSTDOCFLAGS: --cfg docsrs
|
||||
run: cargo doc --all-features
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
|
||||
79
CHANGELOG.md
79
CHANGELOG.md
@@ -6,6 +6,74 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Changed
|
||||
|
||||
- Reworked CI to better test continued support for the minimum supported Rust version
|
||||
|
||||
## [0.3.0] - 2023-09-09
|
||||
|
||||
### Added
|
||||
|
||||
- The minimum supported Rust version is now defined as 1.70. Previously it was undefined.
|
||||
- Wrappers for `std::sync` primitives can now be `const` constructed.
|
||||
- Add support for `std::sync::OnceLock`
|
||||
- Added backtraces of mutex allocations to the cycle report. Capturing backtraces does incur some
|
||||
overhead, this can be mitigated by disabling the `backtraces` feature which is enabled by default.
|
||||
|
||||
### Breaking
|
||||
|
||||
- Update [`parking_lot`][parking_lot] dependency to `0.12`.
|
||||
- Restructured the crate to reduce typename verbosity. Wrapper names now match the name of the
|
||||
primitive they wrap. Specific always/debug tracing versions have now moved to separate modules.
|
||||
For example, `tracing_mutex::stdsync::TracingMutex` is now
|
||||
`tracing_mutex::stdsync::tracing::Mutex`, and `tracing_mutex::stdsync::DebugMutex` is now called
|
||||
`tracing_mutex::stdsync::Mutex`. This hopefully reduces the visual noise while reading code that
|
||||
uses this in practice. Unwrapped primitives are reexported under `tracing_mutex::stdsync::raw` for
|
||||
convenience.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Enforce that all internal mutex guards are `!Send`. They already should be according to other
|
||||
reasons, but this adds extra security through the type system.
|
||||
|
||||
## [0.2.1] - 2022-05-23
|
||||
|
||||
### Added
|
||||
|
||||
- Build [docs.rs] documentation with all features enabled for completeness.
|
||||
- Add support for `std::sync::Condvar`
|
||||
|
||||
### Fixed
|
||||
|
||||
- The `parkinglot` module is now correctly enabled by the `parkinglot` feature rather than the
|
||||
`lockapi` feature.
|
||||
|
||||
## [0.2.0] - 2022-05-07
|
||||
|
||||
### Added
|
||||
- Generic support for wrapping mutexes that implement the traits provided by the
|
||||
[`lock_api`][lock_api] crate. This can be used for creating support for other mutex providers that
|
||||
implement it.
|
||||
|
||||
- Support for [`parking_lot`][parking_lot] mutexes. Support includes type aliases for all
|
||||
provided mutex types as well as a dedicated `Once` wrapper.
|
||||
|
||||
- Simple benchmark to track the rough performance penalty incurred by dependency tracking.
|
||||
|
||||
### Breaking
|
||||
|
||||
- The library now requires edition 2021.
|
||||
|
||||
- The `Mutex`- and `RwLockGuards` now dereference to `T` rather than the lock guard they wrap. This
|
||||
is technically a bugfix but can theoretically break existing code.
|
||||
|
||||
- Self-cycles are no longer allowed for lock dependencies. They previously were because it usually
|
||||
isn't a problem, but it can create RWR deadlocks with `RwLocks`.
|
||||
|
||||
### Changed
|
||||
|
||||
- The project now targets edition 2021
|
||||
|
||||
## [0.1.2] - 2021-05-27
|
||||
|
||||
### Added
|
||||
@@ -29,7 +97,14 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
Initial release.
|
||||
|
||||
[Unreleased]: https://github.com/bertptrs/tracing-mutex/compare/v0.1.2...HEAD
|
||||
[0.1.2]: https://github.com/bertptrs/tracing-mutex/compare/v0.1.2...v0.1.2
|
||||
[Unreleased]: https://github.com/bertptrs/tracing-mutex/compare/v0.3.0...HEAD
|
||||
[0.3.0]: https://github.com/bertptrs/tracing-mutex/compare/v0.2.1...v0.3.0
|
||||
[0.2.1]: https://github.com/bertptrs/tracing-mutex/compare/v0.2.0...v0.2.1
|
||||
[0.2.0]: https://github.com/bertptrs/tracing-mutex/compare/v0.1.2...v0.2.0
|
||||
[0.1.2]: https://github.com/bertptrs/tracing-mutex/compare/v0.1.1...v0.1.2
|
||||
[0.1.1]: https://github.com/bertptrs/tracing-mutex/compare/v0.1.0...v0.1.1
|
||||
[0.1.0]: https://github.com/bertptrs/tracing-mutex/releases/tag/v0.1.0
|
||||
|
||||
[docs.rs]: https://docs.rs/tracing-mutex/latest/tracing_mutex/
|
||||
[lock_api]: https://docs.rs/lock_api/
|
||||
[parking_lot]: https://docs.rs/parking_lot/
|
||||
|
||||
810
Cargo.lock
generated
Normal file
810
Cargo.lock
generated
Normal file
@@ -0,0 +1,810 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15c4c2c83f81532e5845a733998b6971faca23490340a418e9b72a3ec9de12ea"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "effd91f6c78e5a4ace8a5d3c0b6bfaec9e2baaef55f3efc00e45fb2e477ee926"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdf919175532b369853f5d5e20b26b43112613fd6fe7aee757e35f7a44642656"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "defaa24ecc093c77630e6c15e17c51f5e187bf35ee514f4e2d67baaa96dae22b"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d5f1946157a96594eb2d2c10eb7ad9a2b27518cb3000209dec700c35df9197d"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78116e32a042dd73c2901f0dc30790d20ff3447f3e3472fad359e8c3d282bcd6"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961"
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
|
||||
dependencies = [
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"is-terminal",
|
||||
"itertools",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
"oorandom",
|
||||
"plotters",
|
||||
"rayon",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crossbeam-epoch",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-epoch"
|
||||
version = "0.9.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
"memoffset",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f"
|
||||
dependencies = [
|
||||
"errno-dragonfly",
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno-dragonfly"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "1.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b"
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"rustix",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.147"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
"plotters-backend",
|
||||
"plotters-svg",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plotters-backend"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e76628b4d3a7581389a35d5b6e2139607ad7c75b17aed325f210aa91f4a9609"
|
||||
|
||||
[[package]]
|
||||
name = "plotters-svg"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38f6d39893cca0701371e3c27294f09797214b86f1fb951b89ade8ec04e2abab"
|
||||
dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon"
|
||||
version = "1.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
|
||||
dependencies = [
|
||||
"either",
|
||||
"rayon-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rayon-core"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"crossbeam-deque",
|
||||
"crossbeam-utils",
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
|
||||
dependencies = [
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9bfe0f2582b4931a45d1fa608f8a8722e8b3c7ac54dd6d5f3b3212791fedef49"
|
||||
dependencies = [
|
||||
"bitflags 2.4.0",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.105"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-mutex"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"criterion",
|
||||
"lock_api",
|
||||
"parking_lot",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.87"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
29
Cargo.toml
29
Cargo.toml
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "tracing-mutex"
|
||||
version = "0.1.2"
|
||||
version = "0.3.0"
|
||||
authors = ["Bert Peters <bert@bertptrs.nl>"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
documentation = "https://docs.rs/tracing-mutex"
|
||||
categories = ["concurrency", "development-tools::debugging"]
|
||||
@@ -10,9 +10,32 @@ keywords = ["mutex", "rwlock", "once", "thread"]
|
||||
description = "Ensure deadlock-free mutexes by allocating in order, or else."
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/bertptrs/tracing-mutex"
|
||||
rust-version = "1.70"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
# Build docs for all features so the documentation is more complete
|
||||
all-features = true
|
||||
# Set custom cfg so we can enable docs.rs magic
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1"
|
||||
lock_api = { version = "0.4", optional = true }
|
||||
parking_lot = { version = "0.12", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.5"
|
||||
rand = "0.8"
|
||||
|
||||
[[bench]]
|
||||
name = "mutex"
|
||||
harness = false
|
||||
|
||||
[features]
|
||||
default = ["backtraces"]
|
||||
backtraces = []
|
||||
# Feature names do not match crate names pending namespaced features.
|
||||
lockapi = ["lock_api"]
|
||||
parkinglot = ["parking_lot", "lockapi"]
|
||||
|
||||
[build-dependencies]
|
||||
autocfg = "1.4.0"
|
||||
|
||||
@@ -186,7 +186,7 @@ APPENDIX: How to apply the Apache License to your work.
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
Copyright 2022 Bert Peters
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright © 2021 Bert Peters
|
||||
Copyright © 2022 Bert Peters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the “Software”), to deal in the Software without restriction,
|
||||
|
||||
36
README.md
36
README.md
@@ -23,10 +23,12 @@ tree out of it, and panics if your dependencies would create a cycle. It provide
|
||||
existing synchronization primitives with an identical API, and should be a drop-in replacement.
|
||||
|
||||
Inspired by [this blogpost][whileydave], which references a similar behaviour implemented by
|
||||
[Abseil][abseil-mutex] for their mutexes.
|
||||
[Abseil][abseil-mutex] for their mutexes. [This article goes into more depth on the exact
|
||||
implementation.][article]
|
||||
|
||||
[whileydave]: https://whileydave.com/2020/12/19/dynamic-cycle-detection-for-lock-ordering/
|
||||
[abseil-mutex]: https://abseil.io/docs/cpp/guides/synchronization
|
||||
[article]: https://bertptrs.nl/2022/06/23/deadlock-free-mutexes-and-directed-acyclic-graphs.html
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -34,7 +36,7 @@ Add this dependency to your `Cargo.lock` file like any other:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
tracing-mutex = "0.1"
|
||||
tracing-mutex = "0.2"
|
||||
```
|
||||
|
||||
Then use the locks provided by this library instead of the ones you would use otherwise.
|
||||
@@ -42,9 +44,9 @@ Replacements for the synchronization primitives in `std::sync` can be found in t
|
||||
Support for other synchronization primitives is planned.
|
||||
|
||||
```rust
|
||||
use tracing_mutex::stdsync::TracingMutex;
|
||||
use tracing_mutex::stdsync::Mutex;
|
||||
|
||||
let some_mutex = TracingMutex::new(42);
|
||||
let some_mutex = Mutex::new(42);
|
||||
*some_mutex.lock().unwrap() += 1;
|
||||
println!("{:?}", some_mutex);
|
||||
```
|
||||
@@ -54,17 +56,31 @@ introduce a cyclic dependency between your locks, the operation panics instead.
|
||||
immediately notice the cyclic dependency rather than be eventually surprised by it in production.
|
||||
|
||||
Mutex tracing is efficient, but it is not completely overhead-free. If you cannot spare the
|
||||
performance penalty in your production environment, this library also offers debug-only tracing.
|
||||
`DebugMutex`, also found in the `stdsync` module, is a type alias that evaluates to `TracingMutex`
|
||||
when debug assertions are enabled, and to `Mutex` when they are not. Similar helper types are
|
||||
available for other synchronization primitives.
|
||||
performance penalty in your production environment, this library also offers debug-only tracing. The
|
||||
type aliases in `tracing_mutex::stdsync` correspond to tracing primitives from
|
||||
`tracing_mutex::stdsync::tracing` when debug assertions are enabled, and to primitives from
|
||||
`std::sync::Mutex` when they are not. A similar structure exists for other
|
||||
|
||||
The minimum supported Rust version is 1.70. Increasing this is not considered a breaking change, but
|
||||
will be avoided within semver-compatible releases if possible.
|
||||
|
||||
### Features
|
||||
|
||||
- Dependency-tracking wrappers for all locking primitives
|
||||
- Optional opt-out for release mode code
|
||||
- Optional backtrace capture to aid with reproducing cyclic mutex chains
|
||||
- Support for primitives from:
|
||||
- `std::sync`
|
||||
- `parking_lot`
|
||||
- Any library that implements the `lock_api` traits
|
||||
|
||||
## Future improvements
|
||||
|
||||
- Improve performance in lock tracing
|
||||
- Optional logging to make debugging easier
|
||||
- Better and configurable error handling when detecting cyclic dependencies
|
||||
- Support for other locking libraries, such as `parking_lot`
|
||||
- Support for other locking libraries
|
||||
- Support for async locking libraries
|
||||
- Support for `Send` mutex guards
|
||||
|
||||
**Note:** `parking_lot` has already began work on its own deadlock detection mechanism, which works
|
||||
in a different way. Both can be complimentary.
|
||||
|
||||
82
benches/mutex.rs
Normal file
82
benches/mutex.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use criterion::criterion_group;
|
||||
use criterion::criterion_main;
|
||||
use criterion::BenchmarkId;
|
||||
use criterion::Criterion;
|
||||
use criterion::Throughput;
|
||||
use rand::prelude::*;
|
||||
use tracing_mutex::stdsync::tracing::Mutex as TracingMutex;
|
||||
|
||||
const SAMPLE_SIZES: [usize; 5] = [10, 30, 100, 300, 1000];
|
||||
|
||||
/// Reproducibly generate random combinations a, b where the index(a) < index(b)
|
||||
///
|
||||
/// All combinations are generated
|
||||
fn generate_combinations<T>(options: &[Arc<T>]) -> Vec<(Arc<T>, Arc<T>)> {
|
||||
let mut combinations = Vec::new();
|
||||
|
||||
for (i, first) in options.iter().enumerate() {
|
||||
for second in options.iter().skip(i + 1) {
|
||||
combinations.push((Arc::clone(first), Arc::clone(second)));
|
||||
}
|
||||
}
|
||||
|
||||
let mut rng = StdRng::seed_from_u64(42);
|
||||
|
||||
combinations.shuffle(&mut rng);
|
||||
|
||||
combinations
|
||||
}
|
||||
|
||||
/// Take two arbitrary mutexes, lock the first, lock the second while holding the first.
|
||||
fn benchmark_baseline(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("baseline");
|
||||
|
||||
for nodes in SAMPLE_SIZES {
|
||||
group.throughput(Throughput::Elements((nodes * (nodes - 1) / 2) as u64));
|
||||
group.bench_with_input(BenchmarkId::from_parameter(nodes), &nodes, |b, &s| {
|
||||
b.iter_batched(
|
||||
|| {
|
||||
let mutexes: Vec<_> = (0..s).map(|_| Arc::new(Mutex::new(()))).collect();
|
||||
generate_combinations(&mutexes)
|
||||
},
|
||||
|combinations| {
|
||||
for (first, second) in combinations {
|
||||
let _first = first.lock();
|
||||
let _second = second.lock();
|
||||
}
|
||||
},
|
||||
criterion::BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as [`benchmark_baseline`] but now while tracking dependencies.
|
||||
fn benchmark_tracing_mutex(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("tracing_mutex");
|
||||
|
||||
for nodes in SAMPLE_SIZES {
|
||||
group.throughput(Throughput::Elements((nodes * (nodes - 1) / 2) as u64));
|
||||
group.bench_with_input(BenchmarkId::from_parameter(nodes), &nodes, |b, &s| {
|
||||
b.iter_batched(
|
||||
|| {
|
||||
let mutexes: Vec<_> = (0..s).map(|_| Arc::new(TracingMutex::new(()))).collect();
|
||||
generate_combinations(&mutexes)
|
||||
},
|
||||
|combinations| {
|
||||
for (first, second) in combinations {
|
||||
let _first = first.lock();
|
||||
let _second = second.lock();
|
||||
}
|
||||
},
|
||||
criterion::BatchSize::SmallInput,
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
criterion_group!(benches, benchmark_baseline, benchmark_tracing_mutex);
|
||||
criterion_main!(benches);
|
||||
6
bors.toml
Normal file
6
bors.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
status = [
|
||||
'Rust project (1.70)',
|
||||
'Rust project (stable)',
|
||||
'Rust project (beta)',
|
||||
'Documentation build',
|
||||
]
|
||||
10
build.rs
Normal file
10
build.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use autocfg::AutoCfg;
|
||||
|
||||
fn main() {
|
||||
// To avoid bumping MSRV unnecessarily, we can sniff certain features. Reevaluate this on major
|
||||
// releases.
|
||||
let ac = AutoCfg::new().unwrap();
|
||||
ac.emit_has_path("std::sync::LazyLock");
|
||||
|
||||
autocfg::rerun_path("build.rs");
|
||||
}
|
||||
62
examples/mutex_cycle.rs
Normal file
62
examples/mutex_cycle.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
//! Show what a crash looks like
|
||||
//!
|
||||
//! This shows what a traceback of a cycle detection looks like. It is expected to crash when run in
|
||||
//! debug mode, because it might deadlock. In release mode, no tracing is used and the program may
|
||||
//! do any of the following:
|
||||
//!
|
||||
//! - Return a random valuation of `a`, `b`, and `c`. The implementation has a race-condition by
|
||||
//! design. I have observed (4, 3, 6), but also (6, 3, 5).
|
||||
//! - Deadlock forever.
|
||||
//!
|
||||
//! One can increase the SLEEP_TIME constant to increase the likelihood of a deadlock to occur. On
|
||||
//! my machine, 1ns of sleep time gives about a 50/50 chance of the program deadlocking.
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use tracing_mutex::stdsync::Mutex;
|
||||
|
||||
fn main() {
|
||||
let a = Mutex::new(1);
|
||||
let b = Mutex::new(2);
|
||||
let c = Mutex::new(3);
|
||||
|
||||
// Increase this time to increase the likelihood of a deadlock.
|
||||
const SLEEP_TIME: Duration = Duration::from_nanos(1);
|
||||
|
||||
// Depending on random CPU performance, this section may deadlock, or may return a result. With
|
||||
// tracing enabled, the potential deadlock is always detected and a backtrace should be
|
||||
// produced.
|
||||
thread::scope(|s| {
|
||||
// Create an edge from a to b
|
||||
s.spawn(|| {
|
||||
let a = a.lock().unwrap();
|
||||
thread::sleep(SLEEP_TIME);
|
||||
*b.lock().unwrap() += *a;
|
||||
});
|
||||
|
||||
// Create an edge from b to c
|
||||
s.spawn(|| {
|
||||
let b = b.lock().unwrap();
|
||||
thread::sleep(SLEEP_TIME);
|
||||
*c.lock().unwrap() += *b;
|
||||
});
|
||||
|
||||
// Create an edge from c to a
|
||||
//
|
||||
// N.B. the program can crash on any of the three edges, as there is no guarantee which
|
||||
// thread will execute first. Nevertheless, any one of them is guaranteed to panic with
|
||||
// tracing enabled.
|
||||
s.spawn(|| {
|
||||
let c = c.lock().unwrap();
|
||||
thread::sleep(SLEEP_TIME);
|
||||
*a.lock().unwrap() += *c;
|
||||
});
|
||||
});
|
||||
|
||||
println!(
|
||||
"{}, {}, {}",
|
||||
a.into_inner().unwrap(),
|
||||
b.into_inner().unwrap(),
|
||||
c.into_inner().unwrap()
|
||||
);
|
||||
}
|
||||
149
src/graph.rs
149
src/graph.rs
@@ -1,5 +1,5 @@
|
||||
use std::array::IntoIter;
|
||||
use std::cell::Cell;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::hash::Hash;
|
||||
@@ -20,23 +20,24 @@ type Order = usize;
|
||||
/// visibly changed.
|
||||
///
|
||||
/// [paper]: https://whileydave.com/publications/pk07_jea/
|
||||
#[derive(Default, Debug)]
|
||||
pub struct DiGraph<V>
|
||||
#[derive(Debug)]
|
||||
pub struct DiGraph<V, E>
|
||||
where
|
||||
V: Eq + Hash + Copy,
|
||||
{
|
||||
nodes: HashMap<V, Node<V>>,
|
||||
/// Next topological sort order
|
||||
next_ord: Order,
|
||||
nodes: HashMap<V, Node<V, E>>,
|
||||
// Instead of reordering the orders in the graph whenever a node is deleted, we maintain a list
|
||||
// of unused ids that can be handed out later again.
|
||||
unused_order: Vec<Order>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Node<V>
|
||||
struct Node<V, E>
|
||||
where
|
||||
V: Eq + Hash + Clone,
|
||||
{
|
||||
in_edges: HashSet<V>,
|
||||
out_edges: HashSet<V>,
|
||||
out_edges: HashMap<V, E>,
|
||||
// The "Ord" field is a Cell to ensure we can update it in an immutable context.
|
||||
// `std::collections::HashMap` doesn't let you have multiple mutable references to elements, but
|
||||
// this way we can use immutable references and still update `ord`. This saves quite a few
|
||||
@@ -44,7 +45,7 @@ where
|
||||
ord: Cell<Order>,
|
||||
}
|
||||
|
||||
impl<V> DiGraph<V>
|
||||
impl<V, E> DiGraph<V, E>
|
||||
where
|
||||
V: Eq + Hash + Copy,
|
||||
{
|
||||
@@ -55,12 +56,18 @@ where
|
||||
/// the node in the topological order.
|
||||
///
|
||||
/// New nodes are appended to the end of the topological order when added.
|
||||
fn add_node(&mut self, n: V) -> (&mut HashSet<V>, &mut HashSet<V>, Order) {
|
||||
let next_ord = &mut self.next_ord;
|
||||
fn add_node(&mut self, n: V) -> (&mut HashSet<V>, &mut HashMap<V, E>, Order) {
|
||||
// need to compute next id before the call to entry() to avoid duplicate borrow of nodes
|
||||
let fallback_id = self.nodes.len();
|
||||
|
||||
let node = self.nodes.entry(n).or_insert_with(|| {
|
||||
let order = *next_ord;
|
||||
*next_ord = next_ord.checked_add(1).expect("Topological order overflow");
|
||||
let order = if let Some(id) = self.unused_order.pop() {
|
||||
// Reuse discarded ordering entry
|
||||
id
|
||||
} else {
|
||||
// Allocate new order id
|
||||
fallback_id
|
||||
};
|
||||
|
||||
Node {
|
||||
ord: Cell::new(order),
|
||||
@@ -78,9 +85,12 @@ where
|
||||
Some(Node {
|
||||
out_edges,
|
||||
in_edges,
|
||||
..
|
||||
ord,
|
||||
}) => {
|
||||
out_edges.into_iter().for_each(|m| {
|
||||
// Return ordering to the pool of unused ones
|
||||
self.unused_order.push(ord.get());
|
||||
|
||||
out_edges.into_keys().for_each(|m| {
|
||||
self.nodes.get_mut(&m).unwrap().in_edges.remove(&n);
|
||||
});
|
||||
|
||||
@@ -97,18 +107,29 @@ where
|
||||
///
|
||||
/// Nodes, both from and to, are created as needed when creating new edges. If the new edge
|
||||
/// would introduce a cycle, the edge is rejected and `false` is returned.
|
||||
pub(crate) fn add_edge(&mut self, x: V, y: V) -> bool {
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// If the edge would introduce the cycle, the underlying graph is not modified and a list of
|
||||
/// all the edge data in the would-be cycle is returned instead.
|
||||
pub(crate) fn add_edge(&mut self, x: V, y: V, e: impl FnOnce() -> E) -> Result<(), Vec<E>>
|
||||
where
|
||||
E: Clone,
|
||||
{
|
||||
if x == y {
|
||||
// self-edges are not considered cycles
|
||||
return true;
|
||||
// self-edges are always considered cycles
|
||||
return Err(Vec::new());
|
||||
}
|
||||
|
||||
let (_, out_edges, ub) = self.add_node(x);
|
||||
|
||||
if !out_edges.insert(y) {
|
||||
// Edge already exists, nothing to be done
|
||||
return true;
|
||||
}
|
||||
match out_edges.entry(y) {
|
||||
Entry::Occupied(_) => {
|
||||
// Edge already exists, nothing to be done
|
||||
return Ok(());
|
||||
}
|
||||
Entry::Vacant(entry) => entry.insert(e()),
|
||||
};
|
||||
|
||||
let (in_edges, _, lb) = self.add_node(y);
|
||||
|
||||
@@ -116,11 +137,11 @@ where
|
||||
|
||||
if lb < ub {
|
||||
// This edge might introduce a cycle, need to recompute the topological sort
|
||||
let mut visited = IntoIter::new([x, y]).collect();
|
||||
let mut visited = [x, y].into_iter().collect();
|
||||
let mut delta_f = Vec::new();
|
||||
let mut delta_b = Vec::new();
|
||||
|
||||
if !self.dfs_f(&self.nodes[&y], ub, &mut visited, &mut delta_f) {
|
||||
if let Err(cycle) = self.dfs_f(&self.nodes[&y], ub, &mut visited, &mut delta_f) {
|
||||
// This edge introduces a cycle, so we want to reject it and remove it from the
|
||||
// graph again to keep the "does not contain cycles" invariant.
|
||||
|
||||
@@ -130,7 +151,7 @@ where
|
||||
self.nodes.get_mut(&x).map(|node| node.out_edges.remove(&y));
|
||||
|
||||
// No edge was added
|
||||
return false;
|
||||
return Err(cycle);
|
||||
}
|
||||
|
||||
// No need to check as we should've found the cycle on the forward pass
|
||||
@@ -142,44 +163,49 @@ where
|
||||
self.reorder(delta_f, delta_b);
|
||||
}
|
||||
|
||||
true
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Forwards depth-first-search
|
||||
fn dfs_f<'a>(
|
||||
&'a self,
|
||||
n: &'a Node<V>,
|
||||
n: &'a Node<V, E>,
|
||||
ub: Order,
|
||||
visited: &mut HashSet<V>,
|
||||
delta_f: &mut Vec<&'a Node<V>>,
|
||||
) -> bool {
|
||||
delta_f: &mut Vec<&'a Node<V, E>>,
|
||||
) -> Result<(), Vec<E>>
|
||||
where
|
||||
E: Clone,
|
||||
{
|
||||
delta_f.push(n);
|
||||
|
||||
n.out_edges.iter().all(|w| {
|
||||
for (w, e) in &n.out_edges {
|
||||
let node = &self.nodes[w];
|
||||
let ord = node.ord.get();
|
||||
|
||||
if ord == ub {
|
||||
// Found a cycle
|
||||
false
|
||||
return Err(vec![e.clone()]);
|
||||
} else if !visited.contains(w) && ord < ub {
|
||||
// Need to check recursively
|
||||
visited.insert(*w);
|
||||
self.dfs_f(node, ub, visited, delta_f)
|
||||
} else {
|
||||
// Already seen this one or not interesting
|
||||
true
|
||||
if let Err(mut chain) = self.dfs_f(node, ub, visited, delta_f) {
|
||||
chain.push(e.clone());
|
||||
return Err(chain);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Backwards depth-first-search
|
||||
fn dfs_b<'a>(
|
||||
&'a self,
|
||||
n: &'a Node<V>,
|
||||
n: &'a Node<V, E>,
|
||||
lb: Order,
|
||||
visited: &mut HashSet<V>,
|
||||
delta_b: &mut Vec<&'a Node<V>>,
|
||||
delta_b: &mut Vec<&'a Node<V, E>>,
|
||||
) {
|
||||
delta_b.push(n);
|
||||
|
||||
@@ -193,7 +219,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn reorder(&self, mut delta_f: Vec<&Node<V>>, mut delta_b: Vec<&Node<V>>) {
|
||||
fn reorder(&self, mut delta_f: Vec<&Node<V, E>>, mut delta_b: Vec<&Node<V, E>>) {
|
||||
self.sort(&mut delta_f);
|
||||
self.sort(&mut delta_b);
|
||||
|
||||
@@ -214,12 +240,25 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn sort(&self, ids: &mut [&Node<V>]) {
|
||||
fn sort(&self, ids: &mut [&Node<V, E>]) {
|
||||
// Can use unstable sort because mutex ids should not be equal
|
||||
ids.sort_unstable_by_key(|v| &v.ord);
|
||||
}
|
||||
}
|
||||
|
||||
// Manual `Default` impl as derive causes unnecessarily strong bounds.
|
||||
impl<V, E> Default for DiGraph<V, E>
|
||||
where
|
||||
V: Eq + Hash + Copy,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
nodes: Default::default(),
|
||||
unused_order: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rand::seq::SliceRandom;
|
||||
@@ -227,21 +266,31 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn nop() {}
|
||||
|
||||
#[test]
|
||||
fn test_no_self_cycle() {
|
||||
// Regression test for https://github.com/bertptrs/tracing-mutex/issues/7
|
||||
let mut graph = DiGraph::default();
|
||||
|
||||
assert!(graph.add_edge(1, 1, nop).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_digraph() {
|
||||
let mut graph = DiGraph::default();
|
||||
|
||||
// Add some safe edges
|
||||
assert!(graph.add_edge(0, 1));
|
||||
assert!(graph.add_edge(1, 2));
|
||||
assert!(graph.add_edge(2, 3));
|
||||
assert!(graph.add_edge(4, 2));
|
||||
assert!(graph.add_edge(0, 1, nop).is_ok());
|
||||
assert!(graph.add_edge(1, 2, nop).is_ok());
|
||||
assert!(graph.add_edge(2, 3, nop).is_ok());
|
||||
assert!(graph.add_edge(4, 2, nop).is_ok());
|
||||
|
||||
// Try to add an edge that introduces a cycle
|
||||
assert!(!graph.add_edge(3, 1));
|
||||
assert!(graph.add_edge(3, 1, nop).is_err());
|
||||
|
||||
// Add an edge that should reorder 0 to be after 4
|
||||
assert!(graph.add_edge(4, 0));
|
||||
assert!(graph.add_edge(4, 0, nop).is_ok());
|
||||
}
|
||||
|
||||
/// Fuzz the DiGraph implementation by adding a bunch of valid edges.
|
||||
@@ -249,7 +298,7 @@ mod tests {
|
||||
/// This test generates all possible forward edges in a 100-node graph consisting of natural
|
||||
/// numbers, shuffles them, then adds them to the graph. This will always be a valid directed,
|
||||
/// acyclic graph because there is a trivial order (the natural numbers) but because the edges
|
||||
/// are added in a random order the DiGraph will still occassionally need to reorder nodes.
|
||||
/// are added in a random order the DiGraph will still occasionally need to reorder nodes.
|
||||
#[test]
|
||||
fn fuzz_digraph() {
|
||||
// Note: this fuzzer is quadratic in the number of nodes, so this cannot be too large or it
|
||||
@@ -259,7 +308,9 @@ mod tests {
|
||||
|
||||
for i in 0..NUM_NODES {
|
||||
for j in i..NUM_NODES {
|
||||
edges.push((i, j));
|
||||
if i != j {
|
||||
edges.push((i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,7 +319,7 @@ mod tests {
|
||||
let mut graph = DiGraph::default();
|
||||
|
||||
for (x, y) in edges {
|
||||
assert!(graph.add_edge(x, y));
|
||||
assert!(graph.add_edge(x, y, nop).is_ok());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
216
src/lib.rs
216
src/lib.rs
@@ -18,8 +18,23 @@
|
||||
//! # Structure
|
||||
//!
|
||||
//! Each module in this crate exposes wrappers for a specific base-mutex with dependency trakcing
|
||||
//! added. For now, that is limited to [`stdsync`] which provides wrappers for the base locks in the
|
||||
//! standard library. More back-ends may be added as features in the future.
|
||||
//! added. This includes [`stdsync`] which provides wrappers for the base locks in the standard
|
||||
//! library, and more depending on enabled compile-time features. More back-ends may be added as
|
||||
//! features in the future.
|
||||
//!
|
||||
//! # Feature flags
|
||||
//!
|
||||
//! `tracing-mutex` uses feature flags to reduce the impact of this crate on both your compile time
|
||||
//! and runtime overhead. Below are the available flags. Modules are annotated with the features
|
||||
//! they require.
|
||||
//!
|
||||
//! - `backtraces`: Enables capturing backtraces of mutex dependencies, to make it easier to
|
||||
//! determine what sequence of events would trigger a deadlock. This is enabled by default, but if
|
||||
//! the performance overhead is unaccceptable, it can be disabled by disabling default features.
|
||||
//!
|
||||
//! - `lockapi`: Enables the wrapper lock for [`lock_api`][lock_api] locks
|
||||
//!
|
||||
//! - `parkinglot`: Enables wrapper types for [`parking_lot`][parking_lot] mutexes
|
||||
//!
|
||||
//! # Performance considerations
|
||||
//!
|
||||
@@ -41,46 +56,56 @@
|
||||
//!
|
||||
//! These operations have been reasonably optimized, but the performance penalty may yet be too much
|
||||
//! for production use. In those cases, it may be beneficial to instead use debug-only versions
|
||||
//! (such as [`stdsync::DebugMutex`]) which evaluate to a tracing mutex when debug assertions are
|
||||
//! (such as [`stdsync::Mutex`]) which evaluate to a tracing mutex when debug assertions are
|
||||
//! enabled, and to the underlying mutex when they're not.
|
||||
//!
|
||||
//! For ease of debugging, this crate will, by default, capture a backtrace when establishing a new
|
||||
//! dependency between two mutexes. This has an additional overhead of over 60%. If this additional
|
||||
//! debugging aid is not required, it can be disabled by disabling default features.
|
||||
//!
|
||||
//! [paper]: https://whileydave.com/publications/pk07_jea/
|
||||
//! [lock_api]: https://docs.rs/lock_api/0.4/lock_api/index.html
|
||||
//! [parking_lot]: https://docs.rs/parking_lot/0.12.1/parking_lot/
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
use std::cell::RefCell;
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
use std::ops::DerefMut;
|
||||
use std::ptr;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Once;
|
||||
use std::sync::MutexGuard;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::PoisonError;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
#[cfg(feature = "lockapi")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "lockapi")))]
|
||||
pub use lock_api;
|
||||
#[cfg(feature = "parkinglot")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parkinglot")))]
|
||||
pub use parking_lot;
|
||||
use reporting::Dep;
|
||||
use reporting::Reportable;
|
||||
|
||||
use crate::graph::DiGraph;
|
||||
|
||||
mod graph;
|
||||
#[cfg(feature = "lockapi")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "lockapi")))]
|
||||
pub mod lockapi;
|
||||
#[cfg(feature = "parkinglot")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parkinglot")))]
|
||||
pub mod parkinglot;
|
||||
mod reporting;
|
||||
pub mod stdsync;
|
||||
|
||||
/// Counter for Mutex IDs. Atomic avoids the need for locking.
|
||||
///
|
||||
/// Should be part of the `MutexID` impl but static items are not yet a thing.
|
||||
static ID_SEQUENCE: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
thread_local! {
|
||||
/// Stack to track which locks are held
|
||||
///
|
||||
/// Assuming that locks are roughly released in the reverse order in which they were acquired,
|
||||
/// a stack should be more efficient to keep track of the current state than a set would be.
|
||||
static HELD_LOCKS: RefCell<Vec<usize>> = RefCell::new(Vec::new());
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref DEPENDENCY_GRAPH: Mutex<DiGraph<usize>> = Default::default();
|
||||
static HELD_LOCKS: RefCell<Vec<usize>> = const { RefCell::new(Vec::new()) };
|
||||
}
|
||||
|
||||
/// Dedicated ID type for Mutexes
|
||||
@@ -101,6 +126,9 @@ impl MutexId {
|
||||
/// This function may panic when there are no more mutex IDs available. The number of mutex ids
|
||||
/// is `usize::MAX - 1` which should be plenty for most practical applications.
|
||||
pub fn new() -> Self {
|
||||
// Counter for Mutex IDs. Atomic avoids the need for locking.
|
||||
static ID_SEQUENCE: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
ID_SEQUENCE
|
||||
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |id| id.checked_add(1))
|
||||
.map(Self)
|
||||
@@ -120,23 +148,58 @@ impl MutexId {
|
||||
///
|
||||
/// This method panics if the new dependency would introduce a cycle.
|
||||
pub fn get_borrowed(&self) -> BorrowedMutex {
|
||||
let creates_cycle = HELD_LOCKS.with(|locks| {
|
||||
self.mark_held();
|
||||
BorrowedMutex {
|
||||
id: self,
|
||||
_not_send: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark this lock as held for the purposes of dependency tracking.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This method panics if the new dependency would introduce a cycle.
|
||||
pub fn mark_held(&self) {
|
||||
let opt_cycle = HELD_LOCKS.with(|locks| {
|
||||
if let Some(&previous) = locks.borrow().last() {
|
||||
let mut graph = get_dependency_graph();
|
||||
|
||||
!graph.add_edge(previous, self.value())
|
||||
graph.add_edge(previous, self.value(), Dep::capture).err()
|
||||
} else {
|
||||
false
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
if creates_cycle {
|
||||
// Panic without holding the lock to avoid needlessly poisoning it
|
||||
panic!("Mutex order graph should not have cycles");
|
||||
if let Some(cycle) = opt_cycle {
|
||||
panic!("{}", Dep::panic_message(&cycle))
|
||||
}
|
||||
|
||||
HELD_LOCKS.with(|locks| locks.borrow_mut().push(self.value()));
|
||||
BorrowedMutex(self)
|
||||
}
|
||||
|
||||
pub unsafe fn mark_released(&self) {
|
||||
HELD_LOCKS.with(|locks| {
|
||||
let mut locks = locks.borrow_mut();
|
||||
|
||||
for (i, &lock) in locks.iter().enumerate().rev() {
|
||||
if lock == self.value() {
|
||||
locks.remove(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Drop impls shouldn't panic but if this happens something is seriously broken.
|
||||
unreachable!("Tried to drop lock for mutex {:?} but it wasn't held", self)
|
||||
});
|
||||
}
|
||||
|
||||
/// Execute the given closure while the guard is held.
|
||||
pub fn with_held<T>(&self, f: impl FnOnce() -> T) -> T {
|
||||
// Note: we MUST construct the RAII guard, we cannot simply mark held + mark released, as
|
||||
// f() may panic and corrupt our state.
|
||||
let _guard = self.get_borrowed();
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,17 +229,13 @@ impl Drop for MutexId {
|
||||
///
|
||||
/// This type can be largely replaced once std::lazy gets stabilized.
|
||||
struct LazyMutexId {
|
||||
inner: UnsafeCell<MaybeUninit<MutexId>>,
|
||||
setter: Once,
|
||||
_marker: PhantomData<MutexId>,
|
||||
inner: OnceLock<MutexId>,
|
||||
}
|
||||
|
||||
impl LazyMutexId {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
inner: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
setter: Once::new(),
|
||||
_marker: PhantomData,
|
||||
inner: OnceLock::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,51 +246,36 @@ impl fmt::Debug for LazyMutexId {
|
||||
}
|
||||
}
|
||||
|
||||
/// Safety: the UnsafeCell is guaranteed to only be accessed mutably from a `Once`.
|
||||
unsafe impl Sync for LazyMutexId {}
|
||||
impl Default for LazyMutexId {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for LazyMutexId {
|
||||
type Target = MutexId;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.setter.call_once(|| {
|
||||
// Safety: this function is only called once, so only one mutable reference should exist
|
||||
// at a time.
|
||||
unsafe {
|
||||
*self.inner.get() = MaybeUninit::new(MutexId::new());
|
||||
}
|
||||
});
|
||||
|
||||
// Safety: after the above Once runs, there are no longer any mutable references, so we can
|
||||
// hand this out safely.
|
||||
//
|
||||
// Explanation of this monstrosity:
|
||||
//
|
||||
// - Get a pointer to the data from the UnsafeCell
|
||||
// - Dereference that to get a reference to the underlying MaybeUninit
|
||||
// - Use as_ptr on MaybeUninit to get a pointer to the initialized MutexID
|
||||
// - Dereference the pointer to turn in into a reference as intended.
|
||||
//
|
||||
// This should get slightly nicer once `maybe_uninit_extra` is stabilized.
|
||||
unsafe { &*((*self.inner.get()).as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LazyMutexId {
|
||||
fn drop(&mut self) {
|
||||
if self.setter.is_completed() {
|
||||
// We have a valid mutex ID and need to drop it
|
||||
|
||||
// Safety: we know that this pointer is valid because the initializer has successfully run.
|
||||
let mutex_id = unsafe { ptr::read((*self.inner.get()).as_ptr()) };
|
||||
|
||||
drop(mutex_id);
|
||||
}
|
||||
self.inner.get_or_init(MutexId::new)
|
||||
}
|
||||
}
|
||||
|
||||
/// Borrowed mutex ID
|
||||
///
|
||||
/// This type should be used as part of a mutex guard wrapper. It can be acquired through
|
||||
/// [`MutexId::get_borrowed`] and will automatically mark the mutex as not borrowed when it is
|
||||
/// dropped.
|
||||
///
|
||||
/// This type intentionally is [`!Send`](std::marker::Send) because the ownership tracking is based
|
||||
/// on a thread-local stack which doesn't work if a guard gets released in a different thread from
|
||||
/// where they're acquired.
|
||||
#[derive(Debug)]
|
||||
struct BorrowedMutex<'a>(&'a MutexId);
|
||||
struct BorrowedMutex<'a> {
|
||||
/// Reference to the mutex we're borrowing from
|
||||
id: &'a MutexId,
|
||||
/// This value serves no purpose but to make the type [`!Send`](std::marker::Send)
|
||||
_not_send: PhantomData<MutexGuard<'static, ()>>,
|
||||
}
|
||||
|
||||
/// Drop a lock held by the current thread.
|
||||
///
|
||||
@@ -239,29 +283,19 @@ struct BorrowedMutex<'a>(&'a MutexId);
|
||||
///
|
||||
/// This function panics if the lock did not appear to be handled by this thread. If that happens,
|
||||
/// that is an indication of a serious design flaw in this library.
|
||||
impl<'a> Drop for BorrowedMutex<'a> {
|
||||
impl Drop for BorrowedMutex<'_> {
|
||||
fn drop(&mut self) {
|
||||
let id = self.0;
|
||||
|
||||
HELD_LOCKS.with(|locks| {
|
||||
let mut locks = locks.borrow_mut();
|
||||
|
||||
for (i, &lock) in locks.iter().enumerate().rev() {
|
||||
if lock == id.value() {
|
||||
locks.remove(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Drop impls shouldn't panic but if this happens something is seriously broken.
|
||||
unreachable!("Tried to drop lock for mutex {:?} but it wasn't held", id)
|
||||
});
|
||||
// Safety: the only way to get a BorrowedMutex is by locking the mutex.
|
||||
unsafe { self.id.mark_released() };
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a reference to the current dependency graph
|
||||
fn get_dependency_graph() -> impl DerefMut<Target = DiGraph<usize>> {
|
||||
fn get_dependency_graph() -> impl DerefMut<Target = DiGraph<usize, Dep>> {
|
||||
static DEPENDENCY_GRAPH: OnceLock<Mutex<DiGraph<usize, Dep>>> = OnceLock::new();
|
||||
|
||||
DEPENDENCY_GRAPH
|
||||
.get_or_init(Default::default)
|
||||
.lock()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
@@ -289,11 +323,11 @@ mod tests {
|
||||
let c = LazyMutexId::new();
|
||||
|
||||
let mut graph = get_dependency_graph();
|
||||
assert!(graph.add_edge(a.value(), b.value()));
|
||||
assert!(graph.add_edge(b.value(), c.value()));
|
||||
assert!(graph.add_edge(a.value(), b.value(), Dep::capture).is_ok());
|
||||
assert!(graph.add_edge(b.value(), c.value(), Dep::capture).is_ok());
|
||||
|
||||
// Creating an edge c → a should fail as it introduces a cycle.
|
||||
assert!(!graph.add_edge(c.value(), a.value()));
|
||||
assert!(graph.add_edge(c.value(), a.value(), Dep::capture).is_err());
|
||||
|
||||
// Drop graph handle so we can drop vertices without deadlocking
|
||||
drop(graph);
|
||||
@@ -301,7 +335,9 @@ mod tests {
|
||||
drop(b);
|
||||
|
||||
// If b's destructor correctly ran correctly we can now add an edge from c to a.
|
||||
assert!(get_dependency_graph().add_edge(c.value(), a.value()));
|
||||
assert!(get_dependency_graph()
|
||||
.add_edge(c.value(), a.value(), Dep::capture)
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
/// Test creating a cycle, then panicking.
|
||||
@@ -331,7 +367,9 @@ mod tests {
|
||||
let mut edges = Vec::with_capacity(NUM_NODES * NUM_NODES);
|
||||
for i in 0..NUM_NODES {
|
||||
for j in i..NUM_NODES {
|
||||
edges.push((i, j));
|
||||
if i != j {
|
||||
edges.push((i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
352
src/lockapi.rs
Normal file
352
src/lockapi.rs
Normal file
@@ -0,0 +1,352 @@
|
||||
//! Wrapper implementations for [`lock_api`].
|
||||
//!
|
||||
//! This module does not provide any particular mutex implementation by itself, but rather can be
|
||||
//! used to add dependency tracking to mutexes that already exist. It implements all of the traits
|
||||
//! in `lock_api` based on the one it wraps. Crates such as `spin` and `parking_lot` provide base
|
||||
//! primitives that can be wrapped.
|
||||
//!
|
||||
//! Wrapped mutexes are at least one `usize` larger than the types they wrapped, and must be aligned
|
||||
//! to `usize` boundaries. As such, libraries with many mutexes may want to consider the additional
|
||||
//! required memory.
|
||||
use lock_api::GuardNoSend;
|
||||
use lock_api::RawMutex;
|
||||
use lock_api::RawMutexFair;
|
||||
use lock_api::RawMutexTimed;
|
||||
use lock_api::RawRwLock;
|
||||
use lock_api::RawRwLockDowngrade;
|
||||
use lock_api::RawRwLockFair;
|
||||
use lock_api::RawRwLockRecursive;
|
||||
use lock_api::RawRwLockRecursiveTimed;
|
||||
use lock_api::RawRwLockTimed;
|
||||
use lock_api::RawRwLockUpgrade;
|
||||
use lock_api::RawRwLockUpgradeDowngrade;
|
||||
use lock_api::RawRwLockUpgradeFair;
|
||||
use lock_api::RawRwLockUpgradeTimed;
|
||||
|
||||
use crate::LazyMutexId;
|
||||
|
||||
/// Tracing wrapper for all [`lock_api`] traits.
|
||||
///
|
||||
/// This wrapper implements any of the locking traits available, given that the wrapped type
|
||||
/// implements them. As such, this wrapper can be used both for normal mutexes and rwlocks.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct TracingWrapper<T> {
|
||||
inner: T,
|
||||
// Need to use a lazy mutex ID to intialize statically.
|
||||
id: LazyMutexId,
|
||||
}
|
||||
|
||||
impl<T> TracingWrapper<T> {
|
||||
/// Mark this lock as held in the dependency graph.
|
||||
fn mark_held(&self) {
|
||||
self.id.mark_held();
|
||||
}
|
||||
|
||||
/// Mark this lock as released in the dependency graph.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This function should only be called when the lock has been previously acquired by this
|
||||
/// thread.
|
||||
unsafe fn mark_released(&self) {
|
||||
self.id.mark_released();
|
||||
}
|
||||
|
||||
/// First mark ourselves as held, then call the locking function.
|
||||
fn lock(&self, f: impl FnOnce()) {
|
||||
self.mark_held();
|
||||
f();
|
||||
}
|
||||
|
||||
/// First call the unlocking function, then mark ourselves as realeased.
|
||||
unsafe fn unlock(&self, f: impl FnOnce()) {
|
||||
f();
|
||||
self.mark_released();
|
||||
}
|
||||
|
||||
/// Conditionally lock the mutex.
|
||||
///
|
||||
/// First acquires the lock, then runs the provided function. If that function returns true,
|
||||
/// then the lock is kept, otherwise the mutex is immediately marked as relased.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// The value returned from the callback.
|
||||
fn conditionally_lock(&self, f: impl FnOnce() -> bool) -> bool {
|
||||
// Mark as locked while we try to do the thing
|
||||
self.mark_held();
|
||||
|
||||
if f() {
|
||||
true
|
||||
} else {
|
||||
// Safety: we just locked it above.
|
||||
unsafe { self.mark_released() }
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawMutex for TracingWrapper<T>
|
||||
where
|
||||
T: RawMutex,
|
||||
{
|
||||
// Known issue with legacy initialisers, allow
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
const INIT: Self = Self {
|
||||
inner: T::INIT,
|
||||
id: LazyMutexId::new(),
|
||||
};
|
||||
|
||||
/// Always equal to [`GuardNoSend`], as an implementation detail in the tracking system requires
|
||||
/// this behaviour. May change in the future to reflect the actual guard type from the wrapped
|
||||
/// primitive.
|
||||
type GuardMarker = GuardNoSend;
|
||||
|
||||
fn lock(&self) {
|
||||
self.lock(|| self.inner.lock());
|
||||
}
|
||||
|
||||
fn try_lock(&self) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock())
|
||||
}
|
||||
|
||||
unsafe fn unlock(&self) {
|
||||
self.unlock(|| self.inner.unlock());
|
||||
}
|
||||
|
||||
fn is_locked(&self) -> bool {
|
||||
// Can't use the default implementation as the inner type might've overwritten it.
|
||||
self.inner.is_locked()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawMutexFair for TracingWrapper<T>
|
||||
where
|
||||
T: RawMutexFair,
|
||||
{
|
||||
unsafe fn unlock_fair(&self) {
|
||||
self.unlock(|| self.inner.unlock_fair())
|
||||
}
|
||||
|
||||
unsafe fn bump(&self) {
|
||||
// Bumping effectively doesn't change which locks are held, so we don't need to manage the
|
||||
// lock state.
|
||||
self.inner.bump();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawMutexTimed for TracingWrapper<T>
|
||||
where
|
||||
T: RawMutexTimed,
|
||||
{
|
||||
type Duration = T::Duration;
|
||||
|
||||
type Instant = T::Instant;
|
||||
|
||||
fn try_lock_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_for(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_until(timeout))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLock for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLock,
|
||||
{
|
||||
// Known issue with legacy initialisers, allow
|
||||
#[allow(clippy::declare_interior_mutable_const)]
|
||||
const INIT: Self = Self {
|
||||
inner: T::INIT,
|
||||
id: LazyMutexId::new(),
|
||||
};
|
||||
|
||||
/// Always equal to [`GuardNoSend`], as an implementation detail in the tracking system requires
|
||||
/// this behaviour. May change in the future to reflect the actual guard type from the wrapped
|
||||
/// primitive.
|
||||
type GuardMarker = GuardNoSend;
|
||||
|
||||
fn lock_shared(&self) {
|
||||
self.lock(|| self.inner.lock_shared());
|
||||
}
|
||||
|
||||
fn try_lock_shared(&self) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared())
|
||||
}
|
||||
|
||||
unsafe fn unlock_shared(&self) {
|
||||
self.unlock(|| self.inner.unlock_shared());
|
||||
}
|
||||
|
||||
fn lock_exclusive(&self) {
|
||||
self.lock(|| self.inner.lock_exclusive());
|
||||
}
|
||||
|
||||
fn try_lock_exclusive(&self) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_exclusive())
|
||||
}
|
||||
|
||||
unsafe fn unlock_exclusive(&self) {
|
||||
self.unlock(|| self.inner.unlock_exclusive());
|
||||
}
|
||||
|
||||
fn is_locked(&self) -> bool {
|
||||
self.inner.is_locked()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockDowngrade for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockDowngrade,
|
||||
{
|
||||
unsafe fn downgrade(&self) {
|
||||
// Downgrading does not require tracking
|
||||
self.inner.downgrade()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockUpgrade for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockUpgrade,
|
||||
{
|
||||
fn lock_upgradable(&self) {
|
||||
self.lock(|| self.inner.lock_upgradable());
|
||||
}
|
||||
|
||||
fn try_lock_upgradable(&self) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_upgradable())
|
||||
}
|
||||
|
||||
unsafe fn unlock_upgradable(&self) {
|
||||
self.unlock(|| self.inner.unlock_upgradable());
|
||||
}
|
||||
|
||||
unsafe fn upgrade(&self) {
|
||||
self.inner.upgrade();
|
||||
}
|
||||
|
||||
unsafe fn try_upgrade(&self) -> bool {
|
||||
self.inner.try_upgrade()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockFair for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockFair,
|
||||
{
|
||||
unsafe fn unlock_shared_fair(&self) {
|
||||
self.unlock(|| self.inner.unlock_shared_fair());
|
||||
}
|
||||
|
||||
unsafe fn unlock_exclusive_fair(&self) {
|
||||
self.unlock(|| self.inner.unlock_exclusive_fair());
|
||||
}
|
||||
|
||||
unsafe fn bump_shared(&self) {
|
||||
self.inner.bump_shared();
|
||||
}
|
||||
|
||||
unsafe fn bump_exclusive(&self) {
|
||||
self.inner.bump_exclusive();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockRecursive for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockRecursive,
|
||||
{
|
||||
fn lock_shared_recursive(&self) {
|
||||
self.lock(|| self.inner.lock_shared_recursive());
|
||||
}
|
||||
|
||||
fn try_lock_shared_recursive(&self) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared_recursive())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockRecursiveTimed for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockRecursiveTimed,
|
||||
{
|
||||
fn try_lock_shared_recursive_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared_recursive_for(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_shared_recursive_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared_recursive_until(timeout))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockTimed for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockTimed,
|
||||
{
|
||||
type Duration = T::Duration;
|
||||
|
||||
type Instant = T::Instant;
|
||||
|
||||
fn try_lock_shared_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared_for(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_shared_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_shared_until(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_exclusive_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_exclusive_for(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_exclusive_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_exclusive_until(timeout))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockUpgradeDowngrade for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockUpgradeDowngrade,
|
||||
{
|
||||
unsafe fn downgrade_upgradable(&self) {
|
||||
self.inner.downgrade_upgradable()
|
||||
}
|
||||
|
||||
unsafe fn downgrade_to_upgradable(&self) {
|
||||
self.inner.downgrade_to_upgradable()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockUpgradeFair for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockUpgradeFair,
|
||||
{
|
||||
unsafe fn unlock_upgradable_fair(&self) {
|
||||
self.unlock(|| self.inner.unlock_upgradable_fair())
|
||||
}
|
||||
|
||||
unsafe fn bump_upgradable(&self) {
|
||||
self.inner.bump_upgradable()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> RawRwLockUpgradeTimed for TracingWrapper<T>
|
||||
where
|
||||
T: RawRwLockUpgradeTimed,
|
||||
{
|
||||
fn try_lock_upgradable_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_upgradable_for(timeout))
|
||||
}
|
||||
|
||||
fn try_lock_upgradable_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.conditionally_lock(|| self.inner.try_lock_upgradable_until(timeout))
|
||||
}
|
||||
|
||||
unsafe fn try_upgrade_for(&self, timeout: Self::Duration) -> bool {
|
||||
self.inner.try_upgrade_for(timeout)
|
||||
}
|
||||
|
||||
unsafe fn try_upgrade_until(&self, timeout: Self::Instant) -> bool {
|
||||
self.inner.try_upgrade_until(timeout)
|
||||
}
|
||||
}
|
||||
234
src/parkinglot.rs
Normal file
234
src/parkinglot.rs
Normal file
@@ -0,0 +1,234 @@
|
||||
//! Wrapper types and type aliases for tracing [`parking_lot`] mutexes.
|
||||
//!
|
||||
//! This module provides type aliases that use the [`lockapi`][crate::lockapi] module to provide
|
||||
//! tracing variants of the `parking_lot` primitives. The [`tracing`] module contains type aliases
|
||||
//! that use dependency tracking, while the main `parking_lot` primitives are reexported as [`raw`].
|
||||
//!
|
||||
//! This main module imports from [`tracing`] when `debug_assertions` are enabled, and from [`raw`]
|
||||
//! when they're not. Note that primitives for which no tracing wrapper exists are not imported into
|
||||
//! the main module.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! ```
|
||||
//! # use std::sync::Arc;
|
||||
//! # use std::thread;
|
||||
//! use tracing_mutex::parkinglot::Mutex;
|
||||
//! let mutex = Arc::new(Mutex::new(0));
|
||||
//!
|
||||
//! let handles: Vec<_> = (0..10).map(|_| {
|
||||
//! let mutex = Arc::clone(&mutex);
|
||||
//! thread::spawn(move || *mutex.lock() += 1)
|
||||
//! }).collect();
|
||||
//!
|
||||
//! handles.into_iter().for_each(|handle| handle.join().unwrap());
|
||||
//!
|
||||
//! // All threads completed so the value should be 10.
|
||||
//! assert_eq!(10, *mutex.lock());
|
||||
//! ```
|
||||
//!
|
||||
//! # Limitations
|
||||
//!
|
||||
//! The main lock for the global state is still provided by `std::sync` and the tracing primitives
|
||||
//! are larger than the `parking_lot` primitives they wrap, so there can be a performance
|
||||
//! degradation between using this and using `parking_lot` directly. If this is of concern to you,
|
||||
//! try using the `DebugX`-structs, which provide cycle detection only when `debug_assertions` are
|
||||
//! enabled and have no overhead when they're not.
|
||||
//!
|
||||
//! In addition, the mutex guards returned by the tracing wrappers are `!Send`, regardless of
|
||||
//! whether `parking_lot` is configured to have `Send` mutex guards. This is a limitation of the
|
||||
//! current bookkeeping system.
|
||||
|
||||
pub use parking_lot as raw;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
pub use tracing::{
|
||||
FairMutex, FairMutexGuard, MappedFairMutexGuard, MappedMutexGuard, MappedReentrantMutexGuard,
|
||||
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, Once, OnceState,
|
||||
ReentrantMutex, ReentrantMutexGuard, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard,
|
||||
RwLockWriteGuard,
|
||||
};
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub use parking_lot::{
|
||||
FairMutex, FairMutexGuard, MappedFairMutexGuard, MappedMutexGuard, MappedReentrantMutexGuard,
|
||||
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, MutexGuard, Once, OnceState,
|
||||
ReentrantMutex, ReentrantMutexGuard, RwLock, RwLockReadGuard, RwLockUpgradableReadGuard,
|
||||
RwLockWriteGuard,
|
||||
};
|
||||
|
||||
/// Dependency tracing wrappers for [`parking_lot`].
|
||||
pub mod tracing {
|
||||
pub use parking_lot::OnceState;
|
||||
|
||||
use crate::lockapi::TracingWrapper;
|
||||
use crate::LazyMutexId;
|
||||
|
||||
type RawFairMutex = TracingWrapper<parking_lot::RawFairMutex>;
|
||||
type RawMutex = TracingWrapper<parking_lot::RawMutex>;
|
||||
type RawRwLock = TracingWrapper<parking_lot::RawRwLock>;
|
||||
|
||||
/// Dependency tracking fair mutex. See: [`parking_lot::FairMutex`].
|
||||
pub type FairMutex<T> = lock_api::Mutex<RawFairMutex, T>;
|
||||
/// Mutex guard for [`FairMutex`].
|
||||
pub type FairMutexGuard<'a, T> = lock_api::MutexGuard<'a, RawFairMutex, T>;
|
||||
/// RAII guard for [`FairMutexGuard::map`].
|
||||
pub type MappedFairMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawFairMutex, T>;
|
||||
|
||||
/// Dependency tracking mutex. See: [`parking_lot::Mutex`].
|
||||
pub type Mutex<T> = lock_api::Mutex<RawMutex, T>;
|
||||
/// Mutex guard for [`Mutex`].
|
||||
pub type MutexGuard<'a, T> = lock_api::MutexGuard<'a, RawMutex, T>;
|
||||
/// RAII guard for [`MutexGuard::map`].
|
||||
pub type MappedMutexGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawMutex, T>;
|
||||
|
||||
/// Dependency tracking reentrant mutex. See: [`parking_lot::ReentrantMutex`].
|
||||
///
|
||||
/// **Note:** due to the way dependencies are tracked, this mutex can only be acquired directly
|
||||
/// after itself. Acquiring any other mutex in between introduces a dependency cycle, and will
|
||||
/// therefore be rejected.
|
||||
pub type ReentrantMutex<T> = lock_api::ReentrantMutex<RawMutex, parking_lot::RawThreadId, T>;
|
||||
/// Mutex guard for [`ReentrantMutex`].
|
||||
pub type ReentrantMutexGuard<'a, T> =
|
||||
lock_api::ReentrantMutexGuard<'a, RawMutex, parking_lot::RawThreadId, T>;
|
||||
/// RAII guard for `ReentrantMutexGuard::map`.
|
||||
pub type MappedReentrantMutexGuard<'a, T> =
|
||||
lock_api::MappedReentrantMutexGuard<'a, RawMutex, parking_lot::RawThreadId, T>;
|
||||
|
||||
/// Dependency tracking RwLock. See: [`parking_lot::RwLock`].
|
||||
pub type RwLock<T> = lock_api::RwLock<RawRwLock, T>;
|
||||
/// Read guard for [`RwLock`].
|
||||
pub type RwLockReadGuard<'a, T> = lock_api::RwLockReadGuard<'a, RawRwLock, T>;
|
||||
/// Upgradable Read guard for [`RwLock`].
|
||||
pub type RwLockUpgradableReadGuard<'a, T> =
|
||||
lock_api::RwLockUpgradableReadGuard<'a, RawRwLock, T>;
|
||||
/// Write guard for [`RwLock`].
|
||||
pub type RwLockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawRwLock, T>;
|
||||
/// RAII guard for `RwLockReadGuard::map`.
|
||||
pub type MappedRwLockReadGuard<'a, T> = lock_api::MappedRwLockReadGuard<'a, RawRwLock, T>;
|
||||
/// RAII guard for `RwLockWriteGuard::map`.
|
||||
pub type MappedRwLockWriteGuard<'a, T> = lock_api::MappedRwLockWriteGuard<'a, RawRwLock, T>;
|
||||
|
||||
/// A dependency-tracking wrapper for [`parking_lot::Once`].
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Once {
|
||||
inner: parking_lot::Once,
|
||||
id: LazyMutexId,
|
||||
}
|
||||
|
||||
impl Once {
|
||||
/// Create a new `Once` value.
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
inner: parking_lot::Once::new(),
|
||||
id: LazyMutexId::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current state of this `Once`.
|
||||
pub fn state(&self) -> OnceState {
|
||||
self.inner.state()
|
||||
}
|
||||
|
||||
/// This call is considered as "locking this `Once`" and it participates in dependency
|
||||
/// tracking as such.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This method will panic if `f` panics, poisoning this `Once`. In addition, this function
|
||||
/// panics when the lock acquisition order is determined to be inconsistent.
|
||||
pub fn call_once(&self, f: impl FnOnce()) {
|
||||
self.id.with_held(|| self.inner.call_once(f));
|
||||
}
|
||||
|
||||
/// Performs the given initialization routine once and only once.
|
||||
///
|
||||
/// This method is identical to [`Once::call_once`] except it ignores poisoning.
|
||||
pub fn call_once_force(&self, f: impl FnOnce(OnceState)) {
|
||||
self.id.with_held(|| self.inner.call_once_force(f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
|
||||
use super::tracing;
|
||||
|
||||
#[test]
|
||||
fn test_mutex_usage() {
|
||||
let mutex = Arc::new(tracing::Mutex::new(()));
|
||||
let local_lock = mutex.lock();
|
||||
drop(local_lock);
|
||||
|
||||
thread::spawn(move || {
|
||||
let _remote_lock = mutex.lock();
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_mutex_conflict() {
|
||||
let mutexes = [
|
||||
tracing::Mutex::new(()),
|
||||
tracing::Mutex::new(()),
|
||||
tracing::Mutex::new(()),
|
||||
];
|
||||
|
||||
for i in 0..3 {
|
||||
let _first_lock = mutexes[i].lock();
|
||||
let _second_lock = mutexes[(i + 1) % 3].lock();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rwlock_usage() {
|
||||
let lock = Arc::new(tracing::RwLock::new(()));
|
||||
let lock2 = Arc::clone(&lock);
|
||||
|
||||
let _read_lock = lock.read();
|
||||
|
||||
// Should be able to acquire lock in the background
|
||||
thread::spawn(move || {
|
||||
let _read_lock = lock2.read();
|
||||
})
|
||||
.join()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rwlock_upgradable_read_usage() {
|
||||
let lock = tracing::RwLock::new(());
|
||||
|
||||
// Should be able to acquire an upgradable read lock.
|
||||
let upgradable_guard: tracing::RwLockUpgradableReadGuard<'_, _> = lock.upgradable_read();
|
||||
|
||||
// Should be able to upgrade the guard.
|
||||
let _write_guard: tracing::RwLockWriteGuard<'_, _> =
|
||||
tracing::RwLockUpgradableReadGuard::upgrade(upgradable_guard);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_once_usage() {
|
||||
let once = Arc::new(tracing::Once::new());
|
||||
let once_clone = once.clone();
|
||||
|
||||
assert!(!once_clone.state().done());
|
||||
|
||||
let handle = thread::spawn(move || {
|
||||
assert!(!once_clone.state().done());
|
||||
|
||||
once_clone.call_once(|| {});
|
||||
|
||||
assert!(once_clone.state().done());
|
||||
});
|
||||
|
||||
handle.join().unwrap();
|
||||
|
||||
assert!(once.state().done());
|
||||
}
|
||||
}
|
||||
64
src/reporting.rs
Normal file
64
src/reporting.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
//! Cycle reporting primitives
|
||||
//!
|
||||
//! This module exposes [`Dep`], which resolves to either something that tracks dependencies or to
|
||||
//! something that doesn't. It should only be assumed to implement the [`Reportable`] trait.
|
||||
use std::backtrace::Backtrace;
|
||||
use std::borrow::Cow;
|
||||
use std::fmt::Write;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "backtraces")]
|
||||
pub type Dep = MutexDep<Arc<Backtrace>>;
|
||||
#[cfg(not(feature = "backtraces"))]
|
||||
pub type Dep = MutexDep<()>;
|
||||
|
||||
// Base message to be reported when cycle is detected
|
||||
const BASE_MESSAGE: &str = "Found cycle in mutex dependency graph:";
|
||||
|
||||
pub trait Reportable: Clone {
|
||||
/// Capture the current state
|
||||
fn capture() -> Self;
|
||||
|
||||
/// Format a trace of state for human readable consumption.
|
||||
fn panic_message(trace: &[Self]) -> Cow<'static, str>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MutexDep<T>(T);
|
||||
|
||||
/// Use a unit as tracing data: no tracing.
|
||||
///
|
||||
/// This should have no runtime overhead for capturing traces and should therefore be cheap enough
|
||||
/// for most purposes.
|
||||
impl Reportable for MutexDep<()> {
|
||||
fn capture() -> Self {
|
||||
Self(())
|
||||
}
|
||||
|
||||
fn panic_message(_trace: &[Self]) -> Cow<'static, str> {
|
||||
Cow::Borrowed(BASE_MESSAGE)
|
||||
}
|
||||
}
|
||||
|
||||
/// Use a full backtrace as tracing data
|
||||
///
|
||||
/// Capture the entire backtrace which may be expensive. This implementation does not force capture
|
||||
/// in the event that backtraces are disabled at runtime, so the exact overhead can still be
|
||||
/// controlled a little.
|
||||
///
|
||||
/// N.B. the [`Backtrace`] needs to be wrapped in an Arc as backtraces are not [`Clone`].
|
||||
impl Reportable for MutexDep<Arc<Backtrace>> {
|
||||
fn capture() -> Self {
|
||||
Self(Arc::new(Backtrace::capture()))
|
||||
}
|
||||
|
||||
fn panic_message(trace: &[Self]) -> Cow<'static, str> {
|
||||
let mut message = format!("{BASE_MESSAGE}\n");
|
||||
|
||||
for entry in trace {
|
||||
let _ = writeln!(message, "{}", entry.0);
|
||||
}
|
||||
|
||||
message.into()
|
||||
}
|
||||
}
|
||||
1121
src/stdsync.rs
1121
src/stdsync.rs
File diff suppressed because it is too large
Load Diff
117
src/stdsync/tracing/lazy_lock.rs
Normal file
117
src/stdsync/tracing/lazy_lock.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
//! Wrapper implementation for LazyLock
|
||||
//!
|
||||
//! This lives in a separate module as LazyLock would otherwise raise our MSRV to 1.80. Reevaluate
|
||||
//! this in the future.
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::LazyMutexId;
|
||||
|
||||
/// Wrapper for [`std::sync::LazyLock`]
|
||||
///
|
||||
/// This wrapper participates in cycle detection like all other primitives in this crate. It should
|
||||
/// only be possible to encounter cycles when acquiring mutexes in the initialisation function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use tracing_mutex::stdsync::tracing::LazyLock;
|
||||
///
|
||||
/// static LOCK: LazyLock<i32> = LazyLock::new(|| {
|
||||
/// println!("Hello, world!");
|
||||
/// 42
|
||||
/// });
|
||||
///
|
||||
/// // This should print "Hello, world!"
|
||||
/// println!("{}", *LOCK);
|
||||
/// // This should not.
|
||||
/// println!("{}", *LOCK);
|
||||
/// ```
|
||||
pub struct LazyLock<T, F = fn() -> T> {
|
||||
// MSRV violation is fine, this is gated behind a cfg! check
|
||||
#[allow(clippy::incompatible_msrv)]
|
||||
inner: std::sync::LazyLock<T, F>,
|
||||
id: LazyMutexId,
|
||||
}
|
||||
|
||||
impl<T, F: FnOnce() -> T> LazyLock<T, F> {
|
||||
/// Creates a new lazy value with the given initializing function.
|
||||
pub const fn new(f: F) -> LazyLock<T, F> {
|
||||
Self {
|
||||
id: LazyMutexId::new(),
|
||||
// MSRV violation is fine, this is gated behind a cfg! check
|
||||
#[allow(clippy::incompatible_msrv)]
|
||||
inner: std::sync::LazyLock::new(f),
|
||||
}
|
||||
}
|
||||
|
||||
/// Force this lazy lock to be evaluated.
|
||||
///
|
||||
/// This is equivalent to dereferencing, but is more explicit.
|
||||
pub fn force(this: &LazyLock<T, F>) -> &T {
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F: FnOnce() -> T> Deref for LazyLock<T, F> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.id.with_held(|| &*self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> Default for LazyLock<T> {
|
||||
/// Return a `LazyLock` that is initialized through [`Default`].
|
||||
fn default() -> Self {
|
||||
Self::new(Default::default)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug, F> Debug for LazyLock<T, F> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
// Cannot implement this ourselves because the get() used is nightly, so delegate.
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::stdsync::Mutex;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_only_init_once() {
|
||||
let mut init_counter = 0;
|
||||
|
||||
let lock = LazyLock::new(|| {
|
||||
init_counter += 1;
|
||||
42
|
||||
});
|
||||
|
||||
assert_eq!(*lock, 42);
|
||||
LazyLock::force(&lock);
|
||||
|
||||
// Ensure we can access the init counter
|
||||
drop(lock);
|
||||
|
||||
assert_eq!(init_counter, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Found cycle")]
|
||||
fn test_panic_with_cycle() {
|
||||
let mutex = Mutex::new(());
|
||||
|
||||
let lock = LazyLock::new(|| *mutex.lock().unwrap());
|
||||
|
||||
// Establish the relation from lock to mutex
|
||||
LazyLock::force(&lock);
|
||||
|
||||
// Now do it the other way around, which should crash
|
||||
let _guard = mutex.lock().unwrap();
|
||||
LazyLock::force(&lock);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user