Skip to content

Commit f068e3e

Browse files
authored
feat: test for send & sync (#2)
1 parent 4518a88 commit f068e3e

9 files changed

Lines changed: 105 additions & 12 deletions

File tree

CONTRIBUTING.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Contributing to Synchrony
2+
3+
Thanks for your help improving the project! We are so happy to have you! :tada:
4+
5+
There are opportunities to contribute to Synchrony at any level. It doesn't matter if
6+
you are just getting started with Rust or are the most weathered expert, we can
7+
use your help. If you have any question about Synchrony, feel free to join [our group](https://t.me/compio_rs) in telegram.
8+
9+
This guide will walk you through the process of contributing to Synchrony on following topics:
10+
11+
- [General guidelines](#general-guidelines)
12+
- [Develop Guide](#develop-guide)
13+
- [Style Guide](#style-guide)
14+
- [Contribute with issue](#contribute-with-issue)
15+
- [Contribute with pull request](#contribute-with-pull-request)
16+
17+
## General guidelines
18+
19+
We adhere to [Rust Code of Conduct](https://www.rust-lang.org/policies/code-of-conduct). tl;dr: **be nice**. Before making any contribution, check existing issue and pull requests to avoid duplication of effort. Also, in case of bug, try updating to the latest version of Compio and/or rust might help.
20+
21+
### Develop Guide
22+
23+
Use nightly toolchain to develop and run `rustup update` regularly.
24+
25+
### Style Guide
26+
27+
- Use `cargo fmt --all` with nightly toolchain to format your code (for nightly `rustfmt` features, see detail in [`rustfmt.toml`]).
28+
- Use `cargo clippy --all` to check any style/code problem.
29+
- Use [Angular Convention](https://github.com/angular/angular/blob/main/CONTRIBUTING.md#-commit-message-format) when making commits
30+
31+
[`rustfmt.toml`]: https://github.com/compio-rs/synchrony/blob/master/rustfmt.toml
32+
33+
## Contribute with issue
34+
35+
If you find a bug or have a feature request, please [open an issue](https://github.com/compio-rs/synchrony/issues/new/choose) with detailed description. Issues that are lack of information or destructive will be requested for more information or closed.
36+
37+
It's also helpful if you can provide the following information:
38+
39+
- A minimal reproducible example
40+
- The version of Synchrony you are using.
41+
- The version of Rust you are using.
42+
- Your environment (OS, Platform, etc).
43+
44+
## Contribute with pull request
45+
46+
We welcome any code contributions. It's always welcome and recommended to open an issue to discuss on major changes before opening a PR. And pull requests should:
47+
48+
- follow the [Style Guide](#style-guide).
49+
- pass CI tests and style check.
50+
- be reviewed by at least one maintainer before getting merged.
51+
- have a description of what it does and why it is needed in PR body.

src/atomic/unsync.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ impl Debug for AtomicBool {
4343
}
4444
}
4545

46-
impl Display for AtomicBool {
47-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48-
Display::fmt(&self.v.get(), f)
49-
}
50-
}
51-
5246
impl AtomicBool {
5347
/// Creates a new [`AtomicBool`]
5448
pub const fn new(val: bool) -> Self {
@@ -204,12 +198,6 @@ macro_rules! atomic_int {
204198
}
205199
}
206200

207-
impl Display for $t {
208-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
209-
Display::fmt(&self.v.get(), f)
210-
}
211-
}
212-
213201
impl $t {
214202
#[doc = concat!("Creates a new [`", stringify!($t), "`]")]
215203
pub const fn new(val: $i) -> Self {

src/bilock/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/// Multithreaded BiLock
22
pub mod sync {
33
super::impl_bilock!(sync);
4+
5+
unsafe impl<T: Send> Send for Inner<T> {}
6+
unsafe impl<T: Send> Sync for Inner<T> {}
7+
8+
impl<T: Send> crate::AssertMt for BiLock<T> {}
9+
impl<T: Send> crate::AssertMt for BiLockAcquire<'_, T> {}
10+
impl<T: Send> crate::AssertMt for BiLockGuard<'_, T> {}
411
}
512

613
/// Singlethreaded BiLock

src/event.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/// Notify async tasks or threads.
22
pub mod sync {
33
pub use event_listener::{Event, EventListener};
4+
5+
impl crate::AssertMt for Event {}
6+
impl crate::AssertMt for EventListener {}
47
}
58
#[doc(inline)]
69
pub use local_event as unsync;

src/flag.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
/// Multithreaded boolean flag based on [`std::sync::atomic::AtomicBool`]
44
pub mod sync {
55
super::impl_flag!(sync);
6+
7+
impl crate::AssertMt for Flag {}
68
}
79

810
/// Singlethreaded boolean flag based on [`std::cell::Cell`]

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,7 @@ pub mod unsync {
9595
flag::unsync as flag, mutex_blocking::unsync as mutex_blocking, shared::unsync as shared,
9696
};
9797
}
98+
99+
/// A trait to assert that a type is `Send + Sync`.
100+
#[allow(dead_code)]
101+
trait AssertMt: Send + Sync {}

src/mutex/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,41 @@
55
66
/// Multithreaded async Mutex
77
pub mod sync {
8+
use crate::AssertMt;
9+
810
super::impl_mutex!(sync);
11+
12+
// Mutexes can be moved freely between threads and acquired on any thread so
13+
// long as the inner value can be safely sent between threads.
14+
unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
15+
unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
16+
17+
// It's safe to switch which thread the acquire is being attempted on so long as
18+
// `T` can be accessed on that thread.
19+
unsafe impl<T: ?Sized + Send> Send for MutexLockFuture<'_, T> {}
20+
21+
// doesn't have any interesting `&self` methods (only Debug)
22+
unsafe impl<T: ?Sized> Sync for MutexLockFuture<'_, T> {}
23+
24+
// It's safe to switch which thread the acquire is being attempted on so long as
25+
// `T` can be accessed on that thread.
26+
unsafe impl<T: ?Sized + Send> Send for OwnedMutexLockFuture<T> {}
27+
28+
// doesn't have any interesting `&self` methods (only Debug)
29+
unsafe impl<T: ?Sized> Sync for OwnedMutexLockFuture<T> {}
30+
31+
// Safe to send since we don't track any thread-specific details-- the inner
32+
// lock is essentially spinlock-equivalent (attempt to flip an atomic bool)
33+
unsafe impl<T: ?Sized + Send> Send for MutexGuard<'_, T> {}
34+
unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
35+
36+
unsafe impl<T: ?Sized + Send> Send for OwnedMutexGuard<T> {}
37+
unsafe impl<T: ?Sized + Sync> Sync for OwnedMutexGuard<T> {}
38+
39+
unsafe impl<T: ?Sized + Send, U: ?Sized + Send> Send for MappedMutexGuard<'_, T, U> {}
40+
unsafe impl<T: ?Sized + Sync, U: ?Sized + Sync> Sync for MappedMutexGuard<'_, T, U> {}
41+
42+
impl<T: Send> AssertMt for Mutex<T> {}
943
}
1044

1145
/// Singlethreaded async Mutex

src/mutex_blocking.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub mod sync {
6363
&mut *self.0
6464
}
6565
}
66+
67+
impl<T: Send> crate::AssertMt for Mutex<T> {}
6668
}
6769

6870
/// Singlethreaded blocking Mutex

src/waker_slot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
/// Multithreaded `WakerSlot` based on [`futures::task::AtomicWaker`].
77
pub mod sync {
88
pub use futures::task::AtomicWaker as WakerSlot;
9+
10+
impl crate::AssertMt for WakerSlot {}
911
}
1012

1113
/// Singlethreaded `WakerSlot`

0 commit comments

Comments
 (0)