Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only non-send futures #77

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/runConfigurations/Run_demo__debug_.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion android/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories.workspace = true
authors.workspace = true

[dependencies]
maplibre = { path = "../maplibre", features = ["thread-safe-futures"] }
maplibre = { path = "../maplibre", features = [] }
maplibre-winit = { path = "../maplibre-winit", version = "0.1.0" }
env_logger.workspace = true
log.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion apple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories.workspace = true
authors.workspace = true

[dependencies]
maplibre = { path = "../maplibre", features = ["thread-safe-futures"] }
maplibre = { path = "../maplibre", features = [] }
maplibre-winit = { path = "../maplibre-winit", version = "0.1.0" }

env_logger.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories.workspace = true
authors.workspace = true

[dependencies]
maplibre = { path = "../maplibre", features = ["headless", "embed-static-tiles", "thread-safe-futures"] }
maplibre = { path = "../maplibre", features = ["headless", "embed-static-tiles"] }

[dev-dependencies]
criterion.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion maplibre-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ headless = ["maplibre/headless"]

[dependencies]
env_logger.workspace = true
maplibre = { path = "../maplibre", version = "0.1.0", features = ["thread-safe-futures"] }
maplibre = { path = "../maplibre", version = "0.1.0", features = [] }
maplibre-winit = { path = "../maplibre-winit", version = "0.1.0" }
tile-grid.workspace = true
clap.workspace = true
6 changes: 3 additions & 3 deletions maplibre-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use instant::Instant;
use maplibre::{
environment::{Environment, OffscreenKernelEnvironment},
event_loop::{EventLoop, EventLoopProxy, SendEventError},
io::{apc::AsyncProcedureCall, scheduler::Scheduler, source_client::HttpClient},
io::{apc::AsyncProcedureCall, scheduler::ScheduleMethod, source_client::HttpClient},
map::Map,
window::{HeadedMapWindow, MapWindowConfig},
};
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<ET: 'static> EventLoopProxy<ET> for WinitEventLoopProxy<ET> {
}

pub struct WinitEnvironment<
S: Scheduler,
S: ScheduleMethod,
HC: HttpClient,
K: OffscreenKernelEnvironment,
APC: AsyncProcedureCall<K>,
Expand All @@ -205,7 +205,7 @@ pub struct WinitEnvironment<
}

impl<
S: Scheduler,
S: ScheduleMethod,
HC: HttpClient,
K: OffscreenKernelEnvironment,
APC: AsyncProcedureCall<K>,
Expand Down
6 changes: 3 additions & 3 deletions maplibre-winit/src/noweb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use maplibre::{
kernel::{Kernel, KernelBuilder},
map::Map,
platform::{
http_client::ReqwestHttpClient, run_multithreaded, scheduler::TokioScheduler,
http_client::ReqwestHttpClient, run_multithreaded, scheduler::TokioScheduleMethod,
ReqwestOffscreenKernelEnvironment,
},
raster::{DefaultRasterTransferables, RasterPlugin},
Expand Down Expand Up @@ -113,8 +113,8 @@ pub fn run_headed_map(
let kernel: Kernel<Environment<_, _, _>> = KernelBuilder::new()
.with_map_window_config(window_config)
.with_http_client(client.clone())
.with_apc(SchedulerAsyncProcedureCall::new(TokioScheduler::new()))
.with_scheduler(TokioScheduler::new())
.with_apc(SchedulerAsyncProcedureCall::new(TokioScheduleMethod::new()))
.with_scheduler(TokioScheduleMethod::new())
.build();

let renderer_builder = RendererBuilder::new().with_wgpu_settings(wgpu_settings);
Expand Down
1 change: 0 additions & 1 deletion maplibre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ default = ["raster"]
web-webgl = ["wgpu/webgl"]
# Enable tracing using tracy on desktop/mobile and the chrome profiler on web
trace = ["tracing-subscriber", "tracing-tracy"]
thread-safe-futures = []
embed-static-tiles = ["maplibre-build-tools/sqlite"]
headless = ["png"]
raster = ["image"]
Expand Down
4 changes: 2 additions & 2 deletions maplibre/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
io::{
apc::AsyncProcedureCall,
scheduler::Scheduler,
scheduler::ScheduleMethod,
source_client::{HttpClient, SourceClient},
},
window::MapWindowConfig,
Expand All @@ -20,7 +20,7 @@ pub trait Environment: 'static {

type AsyncProcedureCall: AsyncProcedureCall<Self::OffscreenKernelEnvironment>;

type Scheduler: Scheduler;
type Scheduler: ScheduleMethod;

type HttpClient: HttpClient;

Expand Down
4 changes: 2 additions & 2 deletions maplibre/src/headless/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
headless::window::HeadlessMapWindowConfig,
io::apc::SchedulerAsyncProcedureCall,
platform::{
http_client::ReqwestHttpClient, scheduler::TokioScheduler,
http_client::ReqwestHttpClient, scheduler::TokioScheduleMethod,
ReqwestOffscreenKernelEnvironment,
},
};
Expand All @@ -14,7 +14,7 @@ impl Environment for HeadlessEnvironment {
type MapWindowConfig = HeadlessMapWindowConfig;
type AsyncProcedureCall =
SchedulerAsyncProcedureCall<Self::OffscreenKernelEnvironment, Self::Scheduler>;
type Scheduler = TokioScheduler;
type Scheduler = TokioScheduleMethod;
type HttpClient = ReqwestHttpClient;
type OffscreenKernelEnvironment = ReqwestOffscreenKernelEnvironment;
}
6 changes: 3 additions & 3 deletions maplibre/src/headless/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
},
io::apc::SchedulerAsyncProcedureCall,
kernel::{Kernel, KernelBuilder},
platform::{http_client::ReqwestHttpClient, scheduler::TokioScheduler},
platform::{http_client::ReqwestHttpClient, scheduler::TokioScheduleMethod},
plugin::Plugin,
render::{
builder::RendererBuilder, graph::RenderGraph, tile_view_pattern::ViewTileSources,
Expand Down Expand Up @@ -37,8 +37,8 @@ pub async fn create_headless_renderer(
WindowSize::new(tile_size, tile_size).unwrap(),
))
.with_http_client(client.clone())
.with_apc(SchedulerAsyncProcedureCall::new(TokioScheduler::new()))
.with_scheduler(TokioScheduler::new())
.with_apc(SchedulerAsyncProcedureCall::new(TokioScheduleMethod::new()))
.with_scheduler(TokioScheduleMethod::new())
.build();

let mwc: &HeadlessMapWindowConfig = kernel.map_window_config();
Expand Down
12 changes: 4 additions & 8 deletions maplibre/src/io/apc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use thiserror::Error;

use crate::{
coords::WorldTileCoords, define_label, environment::OffscreenKernelEnvironment,
io::scheduler::Scheduler, style::Style,
io::scheduler::ScheduleMethod, style::Style,
};

define_label!(MessageTag);
Expand Down Expand Up @@ -99,10 +99,6 @@ pub enum ProcedureError {
Send(SendError),
}

#[cfg(feature = "thread-safe-futures")]
pub type AsyncProcedureFuture =
Pin<Box<(dyn Future<Output = Result<(), ProcedureError>> + Send + 'static)>>;
#[cfg(not(feature = "thread-safe-futures"))]
pub type AsyncProcedureFuture =
Pin<Box<(dyn Future<Output = Result<(), ProcedureError>> + 'static)>>;

Expand Down Expand Up @@ -194,14 +190,14 @@ impl Context for SchedulerContext {
}
}

pub struct SchedulerAsyncProcedureCall<K: OffscreenKernelEnvironment, S: Scheduler> {
pub struct SchedulerAsyncProcedureCall<K: OffscreenKernelEnvironment, S: ScheduleMethod> {
channel: (Sender<Message>, Receiver<Message>),
buffer: RefCell<Vec<Message>>,
scheduler: S,
phantom_k: PhantomData<K>,
}

impl<K: OffscreenKernelEnvironment, S: Scheduler> SchedulerAsyncProcedureCall<K, S> {
impl<K: OffscreenKernelEnvironment, S: ScheduleMethod> SchedulerAsyncProcedureCall<K, S> {
pub fn new(scheduler: S) -> Self {
Self {
channel: mpsc::channel(),
Expand All @@ -212,7 +208,7 @@ impl<K: OffscreenKernelEnvironment, S: Scheduler> SchedulerAsyncProcedureCall<K,
}
}

impl<K: OffscreenKernelEnvironment, S: Scheduler> AsyncProcedureCall<K>
impl<K: OffscreenKernelEnvironment, S: ScheduleMethod> AsyncProcedureCall<K>
for SchedulerAsyncProcedureCall<K, S>
{
type Context = SchedulerContext;
Expand Down
13 changes: 2 additions & 11 deletions maplibre/src/io/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@ pub enum ScheduleError {

/// Async/await scheduler.
/// Can schedule a task from a future factory and a shared state.
pub trait Scheduler: 'static {
#[cfg(feature = "thread-safe-futures")]
fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
) -> Result<(), ScheduleError>
where
T: Future<Output = ()> + Send + 'static;

#[cfg(not(feature = "thread-safe-futures"))]
pub trait ScheduleMethod: 'static {
fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
Expand All @@ -34,7 +25,7 @@ pub trait Scheduler: 'static {

pub struct NopScheduler;

impl Scheduler for NopScheduler {
impl ScheduleMethod for NopScheduler {
fn schedule<T>(
&self,
_future_factory: impl FnOnce() -> T + Send + 'static,
Expand Down
14 changes: 5 additions & 9 deletions maplibre/src/io/source_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ use thiserror::Error;
use crate::{coords::WorldTileCoords, io::source_type::SourceType};

/// A closure that returns a HTTP client.
pub type HTTPClientFactory<HC> = dyn Fn() -> HC;
pub type HttpClientFactory<HC> = 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](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 "thread-safe-futures". Tokio futures are thread-safe.
#[cfg_attr(not(feature = "thread-safe-futures"), async_trait(?Send))]
#[cfg_attr(feature = "thread-safe-futures", async_trait)]
// 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
#[async_trait(?Send)]
pub trait HttpClient: Clone + Sync + Send + 'static {
async fn fetch(&self, url: &str) -> Result<Vec<u8>, SourceFetchError>;
}
Expand Down
3 changes: 1 addition & 2 deletions maplibre/src/platform/noweb/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ impl ReqwestHttpClient {
}
}

#[cfg_attr(not(feature = "thread-safe-futures"), async_trait(?Send))]
#[cfg_attr(feature = "thread-safe-futures", async_trait)]
#[async_trait(?Send)]
impl HttpClient for ReqwestHttpClient {
async fn fetch(&self, url: &str) -> Result<Vec<u8>, SourceFetchError> {
let response = self.client.get(url).send().await?;
Expand Down
39 changes: 18 additions & 21 deletions maplibre/src/platform/noweb/scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
use std::future::Future;

use crate::io::scheduler::{ScheduleError, Scheduler};
use tokio_util::task::LocalPoolHandle;

use crate::io::scheduler::{ScheduleError, ScheduleMethod};

/// Multi-threading with Tokio.
pub struct TokioScheduler;
pub struct TokioScheduleMethod {
pool: LocalPoolHandle,
}

impl TokioScheduler {
impl TokioScheduleMethod {
pub fn new() -> Self {
Self {}
Self {
pool: LocalPoolHandle::new(4),
}
}
}

impl Scheduler for TokioScheduler {
#[cfg(feature = "thread-safe-futures")]
impl ScheduleMethod for TokioScheduleMethod {
fn schedule<T>(
&self,
future_factory: impl FnOnce() -> T + Send + 'static,
) -> Result<(), ScheduleError>
where
T: Future<Output = ()> + Send + 'static,
{
tokio::task::spawn((future_factory)());
Ok(())
}

// FIXME: Provide a working implementation
#[cfg(not(feature = "thread-safe-futures"))]
fn schedule<T>(
&self,
_future_factory: impl FnOnce() -> T + 'static,
future_factory: impl FnOnce() -> T + 'static + Send,
) -> Result<(), ScheduleError>
where
T: Future<Output = ()> + 'static,
{
self.pool.spawn_pinned(|| {
let unsend_data = (future_factory)();

async move { unsend_data.await }
});
Ok(())
}
}

impl Default for TokioScheduler {
impl Default for TokioScheduleMethod {
fn default() -> Self {
Self::new()
}
Expand Down
4 changes: 2 additions & 2 deletions web/src/platform/multithreaded/pool_scheduler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::Future;

use maplibre::{benchmarking::io::scheduler::ScheduleError, io::scheduler::Scheduler};
use maplibre::{benchmarking::io::scheduler::ScheduleError, io::scheduler::ScheduleMethod};
use wasm_bindgen::{prelude::*, JsCast};
use web_sys::Worker;

Expand All @@ -27,7 +27,7 @@ impl WebWorkerPoolScheduler {
}
}

impl Scheduler for WebWorkerPoolScheduler {
impl ScheduleMethod for WebWorkerPoolScheduler {
fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
Expand Down
Loading