From 5f6823394de027d39d3e1efb2ee8e2a31b4f9833 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 27 Aug 2022 10:26:42 +0200 Subject: [PATCH 1/4] Build and test with Rust 1.63 --- .github/workflows/ci.yml | 1 + Cargo.toml | 1 + bors.toml | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd4871a..3955601 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ jobs: strategy: matrix: rust: + - "1.63" # minimum stable rust version - stable - beta - nightly diff --git a/Cargo.toml b/Cargo.toml index aafd4b4..e63ccbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ 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.63" [package.metadata.docs.rs] # Build docs for all features so the documentation is more complete diff --git a/bors.toml b/bors.toml index 6f20720..452f44b 100644 --- a/bors.toml +++ b/bors.toml @@ -1,4 +1,5 @@ status = [ + 'Rust project (1.63)', 'Rust project (stable)', 'Rust project (beta)', 'Documentation build', From e9b577a0f52acff784445378d7ae4345b4adb683 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 27 Aug 2022 10:33:15 +0200 Subject: [PATCH 2/4] Make stdsync wrappers const-constructible --- src/stdsync.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/stdsync.rs b/src/stdsync.rs index c6e95f7..149caad 100644 --- a/src/stdsync.rs +++ b/src/stdsync.rs @@ -42,7 +42,6 @@ pub mod tracing { use crate::BorrowedMutex; use crate::LazyMutexId; - use crate::MutexId; /// Wrapper for [`std::sync::Mutex`]. /// @@ -51,7 +50,7 @@ pub mod tracing { #[derive(Debug, Default)] pub struct Mutex { inner: sync::Mutex, - id: MutexId, + id: LazyMutexId, } /// Wrapper for [`std::sync::MutexGuard`]. @@ -89,10 +88,10 @@ pub mod tracing { impl Mutex { /// Create a new tracing mutex with the provided value. - pub fn new(t: T) -> Self { + pub const fn new(t: T) -> Self { Self { inner: sync::Mutex::new(t), - id: MutexId::new(), + id: LazyMutexId::new(), } } @@ -220,8 +219,8 @@ pub mod tracing { impl Condvar { /// Creates a new condition variable which is ready to be waited on and notified. - pub fn new() -> Self { - Default::default() + pub const fn new() -> Self { + Self(sync::Condvar::new()) } /// Wrapper for [`std::sync::Condvar::wait`]. @@ -294,7 +293,7 @@ pub mod tracing { #[derive(Debug, Default)] pub struct RwLock { inner: sync::RwLock, - id: MutexId, + id: LazyMutexId, } /// Hybrid wrapper for both [`std::sync::RwLockReadGuard`] and [`std::sync::RwLockWriteGuard`]. @@ -312,10 +311,10 @@ pub mod tracing { pub type RwLockWriteGuard<'a, T> = TracingRwLockGuard<'a, sync::RwLockWriteGuard<'a, T>>; impl RwLock { - pub fn new(t: T) -> Self { + pub const fn new(t: T) -> Self { Self { inner: sync::RwLock::new(t), - id: MutexId::new(), + id: LazyMutexId::new(), } } From 2d2e03eede3c19b3e2260e4b572b71a643513264 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 29 Aug 2022 08:19:52 +0200 Subject: [PATCH 3/4] Simplify lazy mutex ID drop --- src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 39a1efc..4b54b4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,6 @@ 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; @@ -271,9 +270,7 @@ impl Drop for LazyMutexId { // 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); + unsafe { (*self.inner.get()).assume_init_drop() }; } } } From de9888a1020362939a446a78976819f243c59cac Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 29 Aug 2022 08:32:17 +0200 Subject: [PATCH 4/4] Update documentation with MSRV --- CHANGELOG.md | 5 +++++ README.md | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 538c4c8..55ef4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added + +- The minimum supported Rust version is now defined as 1.63. Previously it was undefined. +- Wrappers for `std::sync` primitives can now be `const` constructed. + ### Breaking - Update [`parking_lot`][parking_lot] dependency to `0.12`. diff --git a/README.md b/README.md index 99f2791..8210891 100644 --- a/README.md +++ b/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 @@ -59,6 +61,9 @@ performance penalty in your production environment, this library also offers deb when debug assertions are enabled, and to `Mutex` when they are not. Similar helper types are available for other synchronization primitives. +The minimum supported Rust version is 1.63. 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