crosvm/common/cros_async/src/queue.rs
Dennis Kempin 8a1c50d5cd Refactoring: Move various general purpose crates to common/
This change contains the results of running

./tools/contib/cargo_refactor.py

This will break the next uprev, and needs to be synchronizized
with the corresponding ebuild changes in https://crrev.com/c/3248925

BUG=b:195126527
TEST=./tools/run_tests

Change-Id: Ied15a1841887bb8f59fba65b912b81acf69beb73
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3248129
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2021-10-29 22:31:43 +00:00

66 lines
1.7 KiB
Rust

// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::collections::VecDeque;
use async_task::Runnable;
use sync::Mutex;
/// A queue of `Runnables`. Intended to be used by executors to keep track of futures that have been
/// scheduled to run.
pub struct RunnableQueue {
runnables: Mutex<VecDeque<Runnable>>,
}
impl RunnableQueue {
/// Create a new, empty `RunnableQueue`.
pub fn new() -> RunnableQueue {
RunnableQueue {
runnables: Mutex::new(VecDeque::new()),
}
}
/// Schedule `runnable` to run in the future by adding it to this `RunnableQueue`.
pub fn push_back(&self, runnable: Runnable) {
self.runnables.lock().push_back(runnable);
}
/// Remove and return the first `Runnable` in this `RunnableQueue` or `None` if it is empty.
pub fn pop_front(&self) -> Option<Runnable> {
self.runnables.lock().pop_front()
}
/// Create an iterator over this `RunnableQueue` that repeatedly calls `pop_front()` until it is
/// empty.
pub fn iter(&self) -> RunnableQueueIter {
self.into_iter()
}
}
impl Default for RunnableQueue {
fn default() -> Self {
Self::new()
}
}
impl<'q> IntoIterator for &'q RunnableQueue {
type Item = Runnable;
type IntoIter = RunnableQueueIter<'q>;
fn into_iter(self) -> Self::IntoIter {
RunnableQueueIter { queue: self }
}
}
/// An iterator over a `RunnableQueue`.
pub struct RunnableQueueIter<'q> {
queue: &'q RunnableQueue,
}
impl<'q> Iterator for RunnableQueueIter<'q> {
type Item = Runnable;
fn next(&mut self) -> Option<Self::Item> {
self.queue.pop_front()
}
}