WaitQueue

Struct WaitQueue 

Source
pub struct WaitQueue {
    tasks: TaskQueue,
    lock: RawSpinLock,
}
Expand description

等待队列结构体 用于管理等待某些事件的任务列表 提供将任务加入等待队列、从队列中唤醒任务等功能 内部使用任务队列和自旋锁来保证线程安全 使用示例:

let mut wait_queue = WaitQueue::new();
wait_queue.sleep(task); // 将任务加入等待队列并阻塞
wait_queue.wake_up(&task); // 唤醒指定任务
wait_queue.wake_up_one(); // 唤醒队首任务
wait_queue.wake_up_all(); // 唤醒所有任务

Fields§

§tasks: TaskQueue§lock: RawSpinLock

Implementations§

Source§

impl WaitQueue

Source

pub fn new() -> Self

创建一个新的等待队列实例

Source

pub fn sleep(&mut self, task: Arc<SpinLock<Task>>)

把任务加入等待队列,并调用 sleep_task(不会导致调度)

Source

pub fn wake_up(&mut self, task: &Arc<SpinLock<Task>>)

从等待队列中移除指定任务并在锁释放后唤醒

Source

pub fn wake_up_one(&mut self)

唤醒队首一个任务:在临界区内 pop,然后在临界区外唤醒

Source

pub fn wake_up_all(&mut self)

唤醒队列中所有任务:一次性把要唤醒的任务收集出来,释放锁后逐个唤醒

Source

pub fn add_task(&mut self, task: Arc<SpinLock<Task>>)

将任务加入等待队列(不阻塞)

Source

pub fn remove_task(&mut self, task: &Arc<SpinLock<Task>>)

从等待队列中移除指定任务(不唤醒)

Source

pub fn contains(&self, task: &Arc<SpinLock<Task>>) -> bool

检查任务是否在队列中

Source

pub fn is_empty(&self) -> bool

检查等待队列是否为空

Source

pub fn sleep_if<F>(&mut self, task: Arc<SpinLock<Task>>, check_fn: F) -> bool
where F: FnOnce() -> bool,

原子地检查条件并睡眠(用于防止 lost wakeup) check_fn 在持有锁时被调用,如果返回 true 则不睡眠

Trait Implementations§

Source§

impl Debug for WaitQueue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for WaitQueue

Source§

impl Sync for WaitQueue

Auto Trait Implementations§

§

impl !Freeze for WaitQueue

§

impl !RefUnwindSafe for WaitQueue

§

impl Unpin for WaitQueue

§

impl !UnwindSafe for WaitQueue

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.