1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::rt::object;
use crate::rt::{self, thread, Access, Mutex, VersionVec};

use std::collections::VecDeque;

use tracing::trace;

use super::Location;

#[derive(Debug, Copy, Clone)]
pub(crate) struct Condvar {
    state: object::Ref<State>,
}

#[derive(Debug)]
pub(super) struct State {
    /// Tracks access to the mutex
    last_access: Option<Access>,

    /// Threads waiting on the condvar
    waiters: VecDeque<thread::Id>,
}

impl Condvar {
    /// Create a new condition variable object
    pub(crate) fn new() -> Condvar {
        super::execution(|execution| {
            let state = execution.objects.insert(State {
                last_access: None,
                waiters: VecDeque::new(),
            });

            trace!(?state, "Condvar::new");

            Condvar { state }
        })
    }

    /// Blocks the current thread until this condition variable receives a notification.
    pub(crate) fn wait(&self, mutex: &Mutex, location: Location) {
        self.state.branch_opaque(location);

        rt::execution(|execution| {
            trace!(state = ?self.state, ?mutex, "Condvar::wait");

            let state = self.state.get_mut(&mut execution.objects);

            // Track the current thread as a waiter
            state.waiters.push_back(execution.threads.active_id());
        });

        // Release the lock
        mutex.release_lock();

        // Disable the current thread
        rt::park(location);

        // Acquire the lock again
        mutex.acquire_lock(location);
    }

    /// Wakes up one blocked thread on this condvar.
    pub(crate) fn notify_one(&self, location: Location) {
        self.state.branch_opaque(location);

        rt::execution(|execution| {
            let state = self.state.get_mut(&mut execution.objects);

            // Notify the first waiter
            let thread = state.waiters.pop_front();

            trace!(state = ?self.state, ?thread, "Condvar::notify_one");

            if let Some(thread) = thread {
                execution.threads.unpark(thread);
            }
        })
    }

    /// Wakes up all blocked threads on this condvar.
    pub(crate) fn notify_all(&self, location: Location) {
        self.state.branch_opaque(location);

        rt::execution(|execution| {
            let state = self.state.get_mut(&mut execution.objects);

            trace!(state = ?self.state, threads = ?state.waiters, "Condvar::notify_all");

            for thread in state.waiters.drain(..) {
                execution.threads.unpark(thread);
            }
        })
    }
}

impl State {
    pub(super) fn last_dependent_access(&self) -> Option<&Access> {
        self.last_access.as_ref()
    }

    pub(crate) fn set_last_access(&mut self, path_id: usize, version: &VersionVec) {
        Access::set_or_create(&mut self.last_access, path_id, version);
    }
}