Skip to content

Commit 124e932

Browse files
committed
Fix thread waker
1 parent 9e0a5dd commit 124e932

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

tests/rt.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@ pub(crate) mod thread {
66
use std::thread::Thread;
77
use core::{task, mem};
88

9-
const VTABLE: task::RawWakerVTable = task::RawWakerVTable::new(clone, action, action, on_drop);
9+
const VTABLE: task::RawWakerVTable = task::RawWakerVTable::new(clone, wake, wake_by_ref, on_drop);
1010

1111
unsafe fn on_drop(thread: *const ()) {
12-
let thread: Thread = mem::transmute(thread);
12+
let thread = Box::from_raw(thread as *mut Thread);
1313
drop(thread);
1414
}
1515

1616
unsafe fn clone(thread: *const()) -> task::RawWaker {
17-
let thread: Thread = mem::transmute(thread);
18-
let new_ptr = mem::transmute(thread.clone());
17+
let thread = Box::from_raw(thread as *mut Thread);
18+
let new_ptr = thread.clone();
1919
mem::forget(thread);
20-
task::RawWaker::new(new_ptr, &VTABLE)
20+
task::RawWaker::new(Box::into_raw(new_ptr) as _, &VTABLE)
2121
}
2222

23-
unsafe fn action(thread: *const ()) {
24-
let thread: Thread = mem::transmute(thread);
23+
unsafe fn wake(thread: *const ()) {
24+
let thread = Box::from_raw(thread as *mut () as *mut Thread);
25+
thread.unpark();
26+
}
27+
28+
unsafe fn wake_by_ref(thread: *const ()) {
29+
let thread = &*(thread as *const Box<Thread>);
2530
thread.unpark();
2631
}
2732

2833
#[inline(always)]
2934
pub fn waker(thread: Thread) -> task::Waker {
35+
//double pointer is so dumb...
36+
let thread = Box::new(thread);
3037
unsafe {
31-
task::Waker::from_raw(task::RawWaker::new(mem::transmute(thread), &VTABLE))
38+
task::Waker::from_raw(task::RawWaker::new(Box::into_raw(thread) as _, &VTABLE))
3239
}
3340
}
3441
}

0 commit comments

Comments
 (0)