tg_rcore_tutorial_sync/
condvar.rs1use super::{Mutex, UPIntrFreeCell};
2use alloc::{collections::VecDeque, sync::Arc};
3use tg_task_manage::ThreadId;
4
5pub struct Condvar {
7 pub inner: UPIntrFreeCell<CondvarInner>,
9}
10
11pub struct CondvarInner {
13 pub wait_queue: VecDeque<ThreadId>,
15}
16
17impl Condvar {
18 pub fn new() -> Self {
20 Self {
21 inner: unsafe {
23 UPIntrFreeCell::new(CondvarInner {
24 wait_queue: VecDeque::new(),
25 })
26 },
27 }
28 }
29 pub fn signal(&self) -> Option<ThreadId> {
31 let mut inner = self.inner.exclusive_access();
32 inner.wait_queue.pop_front()
33 }
34
35 pub fn wait_no_sched(&self, tid: ThreadId) -> bool {
45 self.inner.exclusive_session(|inner| {
46 inner.wait_queue.push_back(tid);
47 });
48 false
49 }
50 pub fn wait_with_mutex(
55 &self,
56 tid: ThreadId,
57 mutex: Arc<dyn Mutex>,
58 ) -> (bool, Option<ThreadId>) {
59 let waking_tid = mutex.unlock().unwrap();
62 (mutex.lock(tid), Some(waking_tid))
63 }
64}