Skip to content

Commit bfe623f

Browse files
committed
Reduce unsafe and support older Rust versions
Use waker-fn to reduce unsafe and to support older Rust versions before `Wake` was stabilized.
1 parent 1ef040e commit bfe623f

5 files changed

Lines changed: 15 additions & 36 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- uses: actions-rs/toolchain@v1
2323
with:
2424
profile: minimal
25-
toolchain: "1.51.0"
25+
toolchain: "1.46.0"
2626
default: true
2727

2828
- run: cargo test

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ edition = "2018"
1515
crossbeam-channel = "0.5"
1616
num_cpus = "1"
1717
once_cell = "1"
18+
waker-fn = "1"
1819

1920
[dev-dependencies]
2021
criterion = "0.3"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ threadfin = "0.1"
5050

5151
### Minimum supported Rust version
5252

53-
The minimum supported Rust version (or MSRV) for Threadfin is stable Rust 1.51 or greater, meaning we only guarantee that Threadfin will compile if you use a rustc version of at least 1.51. It might compile with older versions but that could change at any time.
53+
The minimum supported Rust version (or MSRV) for Threadfin is stable Rust 1.46 or greater, meaning we only guarantee that Threadfin will compile if you use a rustc version of at least 1.46. It might compile with older versions but that could change at any time.
5454

5555
This version is explicitly tested in CI and may only be bumped in new minor versions. Any changes to the supported minimum version will be called out in the release notes.
5656

src/wakers.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
use std::{
2-
ptr,
3-
sync::Arc,
4-
task::{RawWaker, RawWakerVTable, Wake, Waker},
2+
task::Waker,
53
thread::{self, Thread},
64
};
75

6+
use once_cell::sync::Lazy;
7+
88
/// Creates a dummy waker that does nothing.
99
pub(crate) fn empty_waker() -> Waker {
10-
const RAW_WAKER: RawWaker = RawWaker::new(ptr::null(), &VTABLE);
11-
const VTABLE: RawWakerVTable = RawWakerVTable::new(|_| RAW_WAKER, |_| {}, |_| {}, |_| {});
10+
static WAKER: Lazy<Waker> = Lazy::new(|| waker_fn::waker_fn(move || {}));
1211

13-
unsafe { Waker::from_raw(RAW_WAKER) }
12+
WAKER.clone()
1413
}
1514

1615
/// Creates a waker that unparks the current thread.
@@ -20,17 +19,5 @@ pub(crate) fn current_thread_waker() -> Waker {
2019

2120
/// Creates a waker that unparks a thread.
2221
pub(crate) fn thread_waker(thread: Thread) -> Waker {
23-
struct ThreadWaker(Thread);
24-
25-
impl Wake for ThreadWaker {
26-
fn wake(self: Arc<Self>) {
27-
self.0.unpark();
28-
}
29-
30-
fn wake_by_ref(self: &Arc<Self>) {
31-
self.0.unpark();
32-
}
33-
}
34-
35-
Arc::new(ThreadWaker(thread)).into()
22+
waker_fn::waker_fn(move || thread.unpark())
3623
}

src/worker.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, sync::Arc, task::Wake, time::Duration};
1+
use std::{collections::HashMap, time::Duration};
22

33
use crossbeam_channel::{unbounded, Receiver, Select, Sender};
44

@@ -142,21 +142,12 @@ impl<L: Listener> Worker<L> {
142142
// If it is possible for this task to yield, we need to prepare a new
143143
// waker to receive notifications with.
144144
if coroutine.might_yield() {
145-
struct IdWaker(usize, Sender<usize>);
145+
let sender = self.wake_notifications.0.clone();
146+
let coroutine_addr = coroutine.addr();
146147

147-
impl Wake for IdWaker {
148-
fn wake(self: Arc<Self>) {
149-
self.wake_by_ref();
150-
}
151-
152-
fn wake_by_ref(self: &Arc<Self>) {
153-
let _ = self.1.send(self.0);
154-
}
155-
}
156-
157-
coroutine.set_waker(
158-
Arc::new(IdWaker(coroutine.addr(), self.wake_notifications.0.clone())).into(),
159-
);
148+
coroutine.set_waker(waker_fn::waker_fn(move || {
149+
let _ = sender.send(coroutine_addr);
150+
}));
160151
}
161152

162153
self.listener.on_task_started();

0 commit comments

Comments
 (0)