Name all the tasks! (#6726)

* Remove any implementation of `Spawn` or `Executor` from our task executors

* Fix compilation

* Rename `SpawnBlockingExecutor`

* Update primitives/core/src/traits.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fix tests

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-07-26 14:56:17 +02:00
committed by GitHub
parent 2ec131142b
commit 9310f15ac2
43 changed files with 280 additions and 280 deletions
-2
View File
@@ -72,8 +72,6 @@ mod changes_trie;
pub mod traits;
pub mod testing;
#[cfg(feature = "std")]
pub mod tasks;
#[cfg(feature = "std")]
pub mod vrf;
pub use self::hash::{H160, H256, H512, convert_hash};
-57
View File
@@ -1,57 +0,0 @@
// This file is part of Substrate.
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Module for low-level asynchronous processing.
use crate::traits::CloneableSpawn;
use futures::{executor, task};
/// Simple task executor.
///
/// Uses single thread for scheduling tasks. Can be cloned and used in
/// runtime host (implements `CloneableSpawn`).
#[derive(Debug, Clone)]
pub struct Executor {
pool: executor::ThreadPool,
}
impl Executor {
fn new() -> Self {
Self {
pool: executor::ThreadPool::builder().pool_size(1).create()
.expect("Failed to create task executor")
}
}
}
impl task::Spawn for Executor {
fn spawn_obj(&self, future: task::FutureObj<'static, ()>)
-> Result<(), task::SpawnError> {
self.pool.spawn_obj(future)
}
}
impl CloneableSpawn for Executor {
fn clone(&self) -> Box<dyn CloneableSpawn> {
Box::new(Clone::clone(self))
}
}
/// Create tasks executor.
pub fn executor() -> Box<dyn CloneableSpawn> {
Box::new(Executor::new())
}
+4 -4
View File
@@ -359,16 +359,16 @@ macro_rules! wasm_export_functions {
};
}
/// An executor that supports spawning blocking futures in tests.
/// A task executor that can be used in tests.
///
/// Internally this just wraps a `ThreadPool` with a pool size of `8`. This
/// should ensure that we have enough threads in tests for spawning blocking futures.
#[cfg(feature = "std")]
#[derive(Clone)]
pub struct SpawnBlockingExecutor(futures::executor::ThreadPool);
pub struct TaskExecutor(futures::executor::ThreadPool);
#[cfg(feature = "std")]
impl SpawnBlockingExecutor {
impl TaskExecutor {
/// Create a new instance of `Self`.
pub fn new() -> Self {
let mut builder = futures::executor::ThreadPoolBuilder::new();
@@ -377,7 +377,7 @@ impl SpawnBlockingExecutor {
}
#[cfg(feature = "std")]
impl crate::traits::SpawnNamed for SpawnBlockingExecutor {
impl crate::traits::SpawnNamed for TaskExecutor {
fn spawn_blocking(&self, _: &'static str, future: futures::future::BoxFuture<'static, ()>) {
self.0.spawn_ok(future);
}
+16 -11
View File
@@ -352,26 +352,21 @@ impl CallInWasmExt {
}
}
/// Something that can spawn tasks and also can be cloned.
pub trait CloneableSpawn: futures::task::Spawn + Send + Sync {
/// Clone as heap-allocated handle.
fn clone(&self) -> Box<dyn CloneableSpawn>;
}
sp_externalities::decl_extension! {
/// Task executor extension.
pub struct TaskExecutorExt(Box<dyn CloneableSpawn>);
pub struct TaskExecutorExt(Box<dyn SpawnNamed>);
}
impl TaskExecutorExt {
/// New instance of task executor extension.
pub fn new(spawn_handle: Box<dyn CloneableSpawn>) -> Self {
Self(spawn_handle)
pub fn new(spawn_handle: impl SpawnNamed + Send + 'static) -> Self {
Self(Box::new(spawn_handle))
}
}
/// Something that can spawn futures (blocking and non-blocking) with am assigned name.
pub trait SpawnNamed {
/// Something that can spawn futures (blocking and non-blocking) with an assigned name.
#[dyn_clonable::clonable]
pub trait SpawnNamed: Clone + Send + Sync {
/// Spawn the given blocking future.
///
/// The given `name` is used to identify the future in tracing.
@@ -381,3 +376,13 @@ pub trait SpawnNamed {
/// The given `name` is used to identify the future in tracing.
fn spawn(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>);
}
impl SpawnNamed for Box<dyn SpawnNamed> {
fn spawn_blocking(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) {
(**self).spawn_blocking(name, future)
}
fn spawn(&self, name: &'static str, future: futures::future::BoxFuture<'static, ()>) {
(**self).spawn(name, future)
}
}