aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStjepan Glavina <stjepang@gmail.com>2020-04-15 18:36:47 +0200
committerStjepan Glavina <stjepang@gmail.com>2020-04-15 18:36:47 +0200
commit9b0453baa92b38fdaf7b452a87326203ebcb17db (patch)
treefd10b5f9c890dbf52026e82138e7d7ee17784587
parent2469ca38c1a2dfab5bc9038079e69da4b3089bc7 (diff)
downloadasync-task-9b0453baa92b38fdaf7b452a87326203ebcb17db.tar.gz
Use ThreadId for spawn_local
-rw-r--r--.travis.yml1
-rw-r--r--Cargo.toml8
-rw-r--r--src/lib.rs2
-rw-r--r--src/task.rs33
4 files changed, 25 insertions, 19 deletions
diff --git a/.travis.yml b/.travis.yml
index bd66105..fb309a2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -23,3 +23,4 @@ script:
- cargo fmt --all -- --check
- cargo check --benches --bins --examples --tests
- cargo test -- --test-threads=1
+ - cargo check --no-default-features
diff --git a/Cargo.toml b/Cargo.toml
index 4fa912e..f957ba0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,11 +12,9 @@ keywords = ["futures", "task", "executor", "spawn"]
categories = ["asynchronous", "concurrency", "no-std"]
readme = "README.md"
-[target.'cfg(unix)'.dependencies]
-libc = "0.2.68"
-
-[target.'cfg(windows)'.dependencies]
-winapi = { version = "0.3.8", features = ["processthreadsapi"] }
+[features]
+default = ["std"]
+std = []
[dev-dependencies]
crossbeam = "0.7.3"
diff --git a/src/lib.rs b/src/lib.rs
index c4dea41..f5b96ad 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -135,5 +135,5 @@ pub use crate::join_handle::JoinHandle;
pub use crate::task::{spawn, Task};
pub use crate::waker_fn::waker_fn;
-#[cfg(any(unix, windows))]
+#[cfg(feature = "std")]
pub use crate::task::spawn_local;
diff --git a/src/task.rs b/src/task.rs
index 0c57a85..4d60f07 100644
--- a/src/task.rs
+++ b/src/task.rs
@@ -1,11 +1,10 @@
use core::fmt;
use core::future::Future;
use core::marker::PhantomData;
-use core::mem::{self, ManuallyDrop};
-use core::pin::Pin;
+use core::mem;
use core::ptr::NonNull;
use core::sync::atomic::Ordering;
-use core::task::{Context, Poll, Waker};
+use core::task::Waker;
use crate::header::Header;
use crate::raw::RawTask;
@@ -88,6 +87,9 @@ where
/// Unlike [`spawn`], this function does not require the future to implement [`Send`]. If the
/// [`Task`] reference is run or dropped on a thread it was not created on, a panic will occur.
///
+/// **NOTE:** This function is only available when the `std` feature for this crate is enabled (it
+/// is by default).
+///
/// [`Task`]: struct.Task.html
/// [`JoinHandle`]: struct.JoinHandle.html
/// [`spawn`]: fn.spawn.html
@@ -110,7 +112,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))]
+#[cfg(feature = "std")]
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,
@@ -118,20 +120,25 @@ where
S: Fn(Task<T>) + Send + Sync + 'static,
T: Send + Sync + 'static,
{
- #[cfg(unix)]
- #[inline]
- fn thread_id() -> usize {
- unsafe { libc::pthread_self() as usize }
- }
+ extern crate std;
+
+ use std::mem::ManuallyDrop;
+ use std::pin::Pin;
+ use std::task::{Context, Poll};
+ use std::thread::{self, ThreadId};
+ use std::thread_local;
- #[cfg(windows)]
#[inline]
- fn thread_id() -> usize {
- unsafe { winapi::um::processthreadsapi::GetCurrentThreadId() as usize }
+ fn thread_id() -> ThreadId {
+ thread_local! {
+ static ID: ThreadId = thread::current().id();
+ }
+ ID.try_with(|id| *id)
+ .unwrap_or_else(|_| thread::current().id())
}
struct Checked<F> {
- id: usize,
+ id: ThreadId,
inner: ManuallyDrop<F>,
}