Skip to content

Commit d1b8188

Browse files
committed
Add wait/wake ptr funcs for Windows
1 parent c0a5ccd commit d1b8188

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std]
22
#![doc = include_str!("../README.md")]
33

4-
use core::sync::atomic::AtomicU32;
4+
use core::sync::atomic::{AtomicPtr, AtomicU32};
55

66
#[cfg(any(target_os = "linux", target_os = "android"))]
77
#[path = "linux.rs"]
@@ -28,6 +28,14 @@ pub fn wait(atomic: &AtomicU32, value: u32) {
2828
platform::wait(atomic, value)
2929
}
3030

31+
/// If the value is `value`, wait until woken up.
32+
///
33+
/// This function might also return spuriously,
34+
/// without a corresponding wake operation.
35+
pub fn wait_ptr<T>(atomic: *const AtomicPtr<T>, value: *mut T) {
36+
platform::wait_ptr(atomic, value)
37+
}
38+
3139
/// Wake one thread that is waiting on this atomic.
3240
///
3341
/// It's okay if the pointer dangles or is null.
@@ -36,10 +44,25 @@ pub fn wake_one(atomic: *const AtomicU32) {
3644
platform::wake_one(atomic);
3745
}
3846

47+
/// Wake one thread that is waiting on this atomic.
48+
///
49+
/// It's okay if the pointer dangles or is null.
50+
#[inline]
51+
pub fn wake_one_ptr<T>(atomic: *const AtomicPtr<T>) {
52+
platform::wake_one_ptr(atomic);
53+
}
54+
3955
/// Wake all threads that are waiting on this atomic.
4056
///
4157
/// It's okay if the pointer dangles or is null.
4258
#[inline]
4359
pub fn wake_all(atomic: *const AtomicU32) {
4460
platform::wake_all(atomic);
4561
}
62+
63+
/// Wake all threads that are waiting on this atomic.
64+
///
65+
/// It's okay if the pointer dangles or is null.
66+
pub fn wake_all_ptr<T>(atomic: *const AtomicPtr<T>) {
67+
platform::wake_all_ptr(atomic);
68+
}

src/windows.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::sync::atomic::AtomicU32;
1+
use core::sync::atomic::{AtomicPtr, AtomicU32};
22
use windows_sys::Win32::System::{
33
Threading::{WaitOnAddress, WakeByAddressAll, WakeByAddressSingle},
44
WindowsProgramming::INFINITE,
@@ -11,12 +11,27 @@ pub fn wait(a: &AtomicU32, expected: u32) {
1111
unsafe { WaitOnAddress(ptr.cast(), expected_ptr.cast(), 4, INFINITE) };
1212
}
1313

14+
pub fn wait_ptr<T>(a: *const AtomicPtr<T>, expected: *const T) {
15+
let expected_ptr: *const *const T = &expected as _;
16+
unsafe { WaitOnAddress(a.cast(), expected_ptr.cast(), 8, INFINITE )};
17+
}
18+
1419
#[inline]
1520
pub fn wake_one(ptr: *const AtomicU32) {
1621
unsafe { WakeByAddressSingle(ptr.cast()) };
1722
}
1823

24+
#[inline]
25+
pub fn wake_one_ptr<T>(ptr: *const AtomicPtr<T>) {
26+
unsafe { WakeByAddressSingle(ptr.cast()) };
27+
}
28+
1929
#[inline]
2030
pub fn wake_all(ptr: *const AtomicU32) {
2131
unsafe { WakeByAddressAll(ptr.cast()) };
2232
}
33+
34+
#[inline]
35+
pub fn wake_all_ptr<T>(ptr: *const AtomicPtr<T>) {
36+
unsafe { WakeByAddressAll(ptr.cast()) };
37+
}

0 commit comments

Comments
 (0)