Skip to content

Commit

Permalink
Update codebase and remove winapi dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Mar 12, 2023
1 parent 6194df7 commit 8c8b08e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ version = "0.3"
optional = true
default-features = true

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["threadpoolapiset"]

[target.'cfg(any(target_os = "macos", target_os = "ios", unix))'.dependencies]
[target.'cfg(any(target_os = "macos", target_os = "ios", windows, unix))'.dependencies]
libc = { version = "0.2.60", default-features = false }
tokio_1 = { package = "tokio", version = "1.15", default-features = false, optional = true, features = ["net"] }

Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
#![warn(missing_docs)]

#![no_std]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style, clippy::needless_lifetimes))]

#[allow(unused_imports)]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
Expand Down
10 changes: 5 additions & 5 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//!State module

use core::{task};
use core::task;
use core::cell::UnsafeCell;
use core::sync::atomic::{AtomicBool, AtomicU8, Ordering};

Expand Down Expand Up @@ -68,7 +68,7 @@ impl AtomicWaker {

///This is the same function as `register` but working with owned version.
fn register_owned(&self, waker: task::Waker) {
match self.state.compare_and_swap(WAITING, REGISTERING, Ordering::Acquire) {
match self.state.compare_exchange(WAITING, REGISTERING, Ordering::Acquire, Ordering::Acquire).unwrap_or_else(|err| err) {
WAITING => {
unsafe {
*self.waker.get() = waker;
Expand All @@ -95,7 +95,7 @@ impl AtomicWaker {
}

fn register(&self, waker: &task::Waker) {
match self.state.compare_and_swap(WAITING, REGISTERING, Ordering::Acquire) {
match self.state.compare_exchange(WAITING, REGISTERING, Ordering::Acquire, Ordering::Acquire).unwrap_or_else(|err| err) {
WAITING => {
unsafe {
// Locked acquired, update the waker cell
Expand Down Expand Up @@ -231,7 +231,7 @@ impl TimerState {
///
///After that `Waker` is no longer registered with `TimerState`
pub(crate) fn wake(&self) {
if !self.woken.compare_and_swap(false, true, Ordering::SeqCst) {
if !self.woken.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).unwrap_or_else(|err| err) {
self.inner.wake();
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl Callback for task::Waker {
}
}

impl<'a> Callback for fn() {
impl Callback for fn() {
fn register(self, waker: &AtomicWaker) {
waker.register_owned(plain_fn::waker(self));
}
Expand Down
31 changes: 20 additions & 11 deletions src/timer/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@ use core::{task, time, ptr, mem};
use core::pin::Pin;
use core::future::Future;

use crate::state::{TimerState};
use crate::state::TimerState;
use crate::alloc::boxed::Box;

#[allow(non_snake_case, non_camel_case_types)]
mod ffi {
pub use winapi::shared::minwindef::{FILETIME};
pub use winapi::um::threadpoolapiset::{
CloseThreadpoolTimer,
CreateThreadpoolTimer,
SetThreadpoolTimerEx,
WaitForThreadpoolTimerCallbacks,
};
pub use core::ffi::c_void;
use libc::{c_ulong, c_int};

#[repr(C)]
pub struct FILETIME {
pub dwLowDateTime: c_ulong,
pub dwHighDateTime: c_ulong,
}
pub type PTP_TIMER = *mut c_void;

pub use winapi::ctypes::{c_ulong, c_void};
pub use winapi::um::winnt::{PTP_TIMER_CALLBACK, PTP_CALLBACK_INSTANCE, PTP_TIMER};
type PTP_TIMER_CALLBACK = Option<unsafe extern "system" fn(Instance: *mut c_void, Context: *mut c_void, Timer: *mut c_void)>;

extern "system" {
pub fn CloseThreadpoolTimer(pti: *mut c_void);
pub fn CreateThreadpoolTimer(pfnti: PTP_TIMER_CALLBACK, pv: *mut c_void, pcbe: *mut c_void) -> *mut c_void;
pub fn SetThreadpoolTimerEx(pti: *mut c_void, pftDueTime: *mut FILETIME, msPeriod: c_ulong, msWindowLength: c_ulong) -> c_int;
pub fn WaitForThreadpoolTimerCallbacks(pti: PTP_TIMER, fCancelPendingCallbacks: c_int);
}
}

unsafe extern "system" fn timer_callback(_: ffi::PTP_CALLBACK_INSTANCE, data: *mut ffi::c_void, _: ffi::PTP_TIMER) {
unsafe extern "system" fn timer_callback(_: *mut ffi::c_void, data: *mut ffi::c_void, _: *mut ffi::c_void) {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_ptr_alignment))]
let state = data as *mut TimerState;

Expand Down

0 comments on commit 8c8b08e

Please sign in to comment.