diff options
| author | bors <bors@rust-lang.org> | 2018-06-26 21:09:52 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-06-26 21:09:52 +0000 |
| commit | 84804c3874a15f55a905c0b53d820372003b0c24 (patch) | |
| tree | d154389d90ec40f29ea4633d6be6c1add3c9ee6c /src/libcore/task/executor.rs | |
| parent | 9cc3d44b935fb97f19fed4395861cbc8d6bba0e4 (diff) | |
| parent | b39ea1d18f19f9cee3ad271a802b3157f9827f51 (diff) | |
| download | rust-84804c3874a15f55a905c0b53d820372003b0c24.tar.gz rust-84804c3874a15f55a905c0b53d820372003b0c24.zip | |
Auto merge of #51814 - MajorBreakfast:local-task-obj, r=cramertj
Add `LocalTaskObj` to `core::task` - Splits `libcore/task.rs` into submodules - Adds `LocalTaskObj` and `SpawnLocalObjError` (-> [Commit for this](https://github.com/rust-lang/rust/commit/433e6b31a75eea5ce45493acc63eae462d740338)) Note: To make reviewing easy, both actions have their own commit r? @cramertj
Diffstat (limited to 'src/libcore/task/executor.rs')
| -rw-r--r-- | src/libcore/task/executor.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/libcore/task/executor.rs b/src/libcore/task/executor.rs new file mode 100644 index 00000000000..73bf80d2f99 --- /dev/null +++ b/src/libcore/task/executor.rs @@ -0,0 +1,90 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![unstable(feature = "futures_api", + reason = "futures in libcore are unstable", + issue = "50547")] + +use fmt; +use super::{TaskObj, LocalTaskObj}; + +/// A task executor. +/// +/// A *task* is a `()`-producing async value that runs at the top level, and will +/// be `poll`ed until completion. It's also the unit at which wake-up +/// notifications occur. Executors, such as thread pools, allow tasks to be +/// spawned and are responsible for putting tasks onto ready queues when +/// they are woken up, and polling them when they are ready. +pub trait Executor { + /// Spawn the given task, polling it until completion. + /// + /// # Errors + /// + /// The executor may be unable to spawn tasks, either because it has + /// been shut down or is resource-constrained. + fn spawn_obj(&mut self, task: TaskObj) -> Result<(), SpawnObjError>; + + /// Determine whether the executor is able to spawn new tasks. + /// + /// # Returns + /// + /// An `Ok` return means the executor is *likely* (but not guaranteed) + /// to accept a subsequent spawn attempt. Likewise, an `Err` return + /// means that `spawn` is likely, but not guaranteed, to yield an error. + #[inline] + fn status(&self) -> Result<(), SpawnErrorKind> { + Ok(()) + } +} + +/// Provides the reason that an executor was unable to spawn. +pub struct SpawnErrorKind { + _hidden: (), +} + +impl fmt::Debug for SpawnErrorKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("SpawnErrorKind") + .field(&"shutdown") + .finish() + } +} + +impl SpawnErrorKind { + /// Spawning is failing because the executor has been shut down. + pub fn shutdown() -> SpawnErrorKind { + SpawnErrorKind { _hidden: () } + } + + /// Check whether this error is the `shutdown` error. + pub fn is_shutdown(&self) -> bool { + true + } +} + +/// The result of a failed spawn +#[derive(Debug)] +pub struct SpawnObjError { + /// The kind of error + pub kind: SpawnErrorKind, + + /// The task for which spawning was attempted + pub task: TaskObj, +} + +/// The result of a failed spawn +#[derive(Debug)] +pub struct SpawnLocalObjError { + /// The kind of error + pub kind: SpawnErrorKind, + + /// The task for which spawning was attempted + pub task: LocalTaskObj, +} |
