From a4302cadeb28b3531ba56e4020f043cdc6920c72 Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Wed, 4 May 2022 18:51:19 +0200 Subject: [PATCH 1/6] Use only non-send futures --- maplibre/Cargo.toml | 2 +- maplibre/src/io/scheduler.rs | 10 ---------- maplibre/src/io/source_client.rs | 6 +----- maplibre/src/platform/noweb/http_client.rs | 2 +- .../src/platform/noweb/schedule_method.rs | 20 ++++++++++++++----- web/Cargo.toml | 2 +- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/maplibre/Cargo.toml b/maplibre/Cargo.toml index d5578e485..dc321a2a5 100644 --- a/maplibre/Cargo.toml +++ b/maplibre/Cargo.toml @@ -12,7 +12,6 @@ readme = "../README.md" web-webgl = ["wgpu/webgl"] # Enable tracing using tracy on desktop/mobile and the chrome profiler on web enable-tracing = [ "tracing-subscriber", "tracing-tracy", "tracy-client"] -no-thread-safe-futures = [] [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3", features = [ @@ -23,6 +22,7 @@ wasm-bindgen-futures = "0.4" [target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android"))'.dependencies] tokio = { version = "1.17", features = ["macros", "rt", "rt-multi-thread", "sync", "time"] } +tokio-util = { version = "0.7.1", features = ["rt"] } env_logger = "0.9" reqwest = { version = "0.11", default-features = false, features = ["rustls-tls", "gzip"] } reqwest-middleware-cache = "0.1" # FIXME: Untrusted dependency diff --git a/maplibre/src/io/scheduler.rs b/maplibre/src/io/scheduler.rs index 972d62a38..13e4e31c5 100644 --- a/maplibre/src/io/scheduler.rs +++ b/maplibre/src/io/scheduler.rs @@ -25,16 +25,6 @@ where } pub trait ScheduleMethod: 'static { - #[cfg(not(feature = "no-thread-safe-futures"))] - fn schedule( - &self, - shared_thread_state: SharedThreadState, - future_factory: impl (FnOnce(SharedThreadState) -> T) + Send + 'static, - ) -> Result<(), Error> - where - T: Future + Send + 'static; - - #[cfg(feature = "no-thread-safe-futures")] fn schedule( &self, shared_thread_state: SharedThreadState, diff --git a/maplibre/src/io/source_client.rs b/maplibre/src/io/source_client.rs index ceaaf62af..3c10bf4e9 100644 --- a/maplibre/src/io/source_client.rs +++ b/maplibre/src/io/source_client.rs @@ -8,11 +8,7 @@ pub type HTTPClientFactory = dyn Fn() -> HC; // On the web platform futures are not thread-safe (i.e. not Send). This means we need to tell // async_trait that these bounds should not be placed on the async trait: // https://github.com/dtolnay/async-trait/blob/b70720c4c1cc0d810b7446efda44f81310ee7bf2/README.md#non-threadsafe-futures -// -// Users of this library can decide whether futures from the HTTPClient are thread-safe or not via -// the future "no-thread-safe-futures". Tokio futures are thread-safe. -#[cfg_attr(feature = "no-thread-safe-futures", async_trait(?Send))] -#[cfg_attr(not(feature = "no-thread-safe-futures"), async_trait)] +#[async_trait(?Send)] pub trait HTTPClient: Clone + Sync + Send + 'static { async fn fetch(&self, url: &str) -> Result, Error>; } diff --git a/maplibre/src/platform/noweb/http_client.rs b/maplibre/src/platform/noweb/http_client.rs index aa2d43c6e..818fe4a6b 100644 --- a/maplibre/src/platform/noweb/http_client.rs +++ b/maplibre/src/platform/noweb/http_client.rs @@ -40,7 +40,7 @@ impl ReqwestHttpClient { } } -#[async_trait] +#[async_trait(?Send)] impl HTTPClient for ReqwestHttpClient { async fn fetch(&self, url: &str) -> Result, Error> { let response = self.client.get(url).send().await?; diff --git a/maplibre/src/platform/noweb/schedule_method.rs b/maplibre/src/platform/noweb/schedule_method.rs index fd55cfa88..75a27c93c 100644 --- a/maplibre/src/platform/noweb/schedule_method.rs +++ b/maplibre/src/platform/noweb/schedule_method.rs @@ -2,12 +2,17 @@ use crate::error::Error; use crate::io::shared_thread_state::SharedThreadState; use crate::ScheduleMethod; use std::future::Future; - -pub struct TokioScheduleMethod; +use tokio::task; +use tokio_util::task::LocalPoolHandle; +pub struct TokioScheduleMethod { + pool: LocalPoolHandle, +} impl TokioScheduleMethod { pub fn new() -> Self { - Self {} + Self { + pool: LocalPoolHandle::new(4), + } } } @@ -18,9 +23,14 @@ impl ScheduleMethod for TokioScheduleMethod { future_factory: impl FnOnce(SharedThreadState) -> T + Send + 'static, ) -> Result<(), Error> where - T: Future + Send + 'static, + T: Future + 'static, { - tokio::task::spawn(future_factory(shared_thread_state)); + self.pool.spawn_pinned(|| { + let unsend_data = (future_factory)(shared_thread_state); + + async move { unsend_data.await } + }); + Ok(()) } } diff --git a/web/Cargo.toml b/web/Cargo.toml index 5d2417a01..201a2ff76 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -19,7 +19,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] async-trait = "0.1" -maplibre = { path = "../maplibre", features = ["no-thread-safe-futures"] } +maplibre = { path = "../maplibre", features = [] } winit = "*" log = "*" From f42c0b5e509f591a6a645c4f39039652b6373c47 Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Tue, 10 May 2022 09:19:49 +0200 Subject: [PATCH 2/6] Fix merge --- web/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/web/Cargo.toml b/web/Cargo.toml index 3c9cd7cb8..2ca59ddab 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -20,7 +20,6 @@ crate-type = ["cdylib", "rlib"] [dependencies] async-trait = "0.1" maplibre = { path = "../maplibre", features = [] } -maplibre = { path = "../maplibre", features = ["no-thread-safe-futures"] } maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" } log = "0.4" From 75c069e04eed03f092e7d96540522601eb858b1b Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Sun, 17 Jul 2022 21:20:38 +0200 Subject: [PATCH 3/6] Fix formatting --- maplibre/src/platform/noweb/schedule_method.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/maplibre/src/platform/noweb/schedule_method.rs b/maplibre/src/platform/noweb/schedule_method.rs index 9f91fae50..20672a5b4 100644 --- a/maplibre/src/platform/noweb/schedule_method.rs +++ b/maplibre/src/platform/noweb/schedule_method.rs @@ -1,4 +1,5 @@ use std::future::Future; + use tokio::task; use tokio_util::task::LocalPoolHandle; From 2a48027be11395dc6b75c1a5472eda58eb2a62ba Mon Sep 17 00:00:00 2001 From: Maximilian Ammann Date: Thu, 5 Oct 2023 20:41:00 +0100 Subject: [PATCH 4/6] Fixes after merge --- .idea/runConfigurations/Run_demo__debug_.xml | 2 +- android/Cargo.toml | 2 +- apple/Cargo.toml | 2 +- benchmarks/Cargo.toml | 2 +- maplibre-demo/Cargo.toml | 2 +- maplibre-winit/src/lib.rs | 6 +-- maplibre-winit/src/noweb.rs | 6 +-- maplibre/Cargo.toml | 3 +- maplibre/src/environment.rs | 4 +- maplibre/src/io/apc.rs | 12 ++---- maplibre/src/io/scheduler.rs | 2 +- maplibre/src/platform/noweb/scheduler.rs | 39 +++++++++----------- 12 files changed, 37 insertions(+), 45 deletions(-) diff --git a/.idea/runConfigurations/Run_demo__debug_.xml b/.idea/runConfigurations/Run_demo__debug_.xml index 8c5fa36e3..3ded76bf0 100644 --- a/.idea/runConfigurations/Run_demo__debug_.xml +++ b/.idea/runConfigurations/Run_demo__debug_.xml @@ -2,10 +2,10 @@