diff options
author | Stjepan Glavina <stjepang@gmail.com> | 2020-01-07 22:45:13 +0100 |
---|---|---|
committer | Stjepan Glavina <stjepang@gmail.com> | 2020-01-07 22:45:13 +0100 |
commit | 7e7d19cf17d3644556ee41fecd870b5d80a021dc (patch) | |
tree | bd11edf611572d82c7af6df5d543ad3f9a2d9ee3 | |
parent | af051a59a21e61ad97dbe5b209fac2a29e5f48fd (diff) | |
download | async-task-7e7d19cf17d3644556ee41fecd870b5d80a021dc.tar.gz |
Remove libc dependency on non-Unix platforms
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/raw.rs | 8 | ||||
-rw-r--r-- | src/task.rs | 1 | ||||
-rw-r--r-- | src/utils.rs | 18 |
4 files changed, 23 insertions, 6 deletions
@@ -12,7 +12,7 @@ keywords = ["futures", "task", "executor", "spawn"] categories = ["asynchronous", "concurrency"] readme = "README.md" -[dependencies] +[target.'cfg(unix)'.dependencies] libc = "0.2.66" [target.'cfg(windows)'.dependencies] @@ -10,7 +10,7 @@ use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; use crate::header::Header; use crate::state::*; -use crate::utils::{abort_on_panic, extend}; +use crate::utils::{abort, abort_on_panic, extend}; use crate::Task; /// The vtable for a task. @@ -111,7 +111,7 @@ where unsafe { // Allocate enough space for the entire task. let raw_task = match NonNull::new(alloc::alloc::alloc(task_layout.layout) as *mut ()) { - None => libc::abort(), + None => abort(), Some(p) => p, }; @@ -311,7 +311,7 @@ where if state & RUNNING == 0 { // If the reference count overflowed, abort. if state > isize::max_value() as usize { - libc::abort(); + abort(); } // Schedule the task. There is no need to call `Self::schedule(ptr)` @@ -343,7 +343,7 @@ where // If the reference count overflowed, abort. if state > isize::max_value() as usize { - libc::abort(); + abort(); } RawWaker::new(ptr, raw_waker_vtable) diff --git a/src/task.rs b/src/task.rs index b26c082..8ef209c 100644 --- a/src/task.rs +++ b/src/task.rs @@ -101,6 +101,7 @@ where /// // Create a task with the future and the schedule function. /// let (task, handle) = async_task::spawn_local(future, schedule, ()); /// ``` +#[cfg(any(unix, windows))] pub fn spawn_local<F, R, S, T>(future: F, schedule: S, tag: T) -> (Task<T>, JoinHandle<R, T>) where F: Future<Output = R> + 'static, diff --git a/src/utils.rs b/src/utils.rs index 7c71deb..cb9b65e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,22 @@ use core::alloc::Layout; use core::mem; +/// Aborts the process. +/// +/// To abort, this function simply panics while panicking. +pub(crate) fn abort() -> ! { + struct Panic; + + impl Drop for Panic { + fn drop(&mut self) { + panic!("aborting the process"); + } + } + + let _panic = Panic; + panic!("aborting the process"); +} + /// Calls a function and aborts if it panics. /// /// This is useful in unsafe code where we can't recover from panics. @@ -10,7 +26,7 @@ pub(crate) fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T { impl Drop for Bomb { fn drop(&mut self) { - unsafe { libc::abort() } + abort(); } } |