zenoh_sync/
mvar.rs

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Copyright (c) 2023 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use std::sync::atomic::{AtomicUsize, Ordering};

use tokio::sync::Mutex;
use zenoh_core::zasynclock;

use crate::Condition;

pub struct Mvar<T> {
    inner: Mutex<Option<T>>,
    cond_put: Condition,
    cond_take: Condition,
    wait_put: AtomicUsize,
    wait_take: AtomicUsize,
}

impl<T> Mvar<T> {
    pub fn new() -> Mvar<T> {
        Mvar {
            inner: Mutex::new(None),
            cond_put: Condition::new(),
            cond_take: Condition::new(),
            wait_put: AtomicUsize::new(0),
            wait_take: AtomicUsize::new(0),
        }
    }

    pub fn has_take_waiting(&self) -> bool {
        self.wait_take.load(Ordering::Acquire) > 0
    }

    pub fn has_put_waiting(&self) -> bool {
        self.wait_put.load(Ordering::Acquire) > 0
    }

    pub async fn try_take(&self) -> Option<T> {
        let mut guard = zasynclock!(self.inner);
        if let Some(inner) = guard.take() {
            drop(guard);
            self.cond_put.notify_one();
            return Some(inner);
        }
        None
    }

    pub async fn take(&self) -> T {
        loop {
            let mut guard = zasynclock!(self.inner);
            if let Some(inner) = guard.take() {
                self.wait_take.fetch_sub(1, Ordering::AcqRel);
                drop(guard);
                self.cond_put.notify_one();
                return inner;
            }
            self.wait_take.fetch_add(1, Ordering::AcqRel);
            self.cond_take.wait(guard).await;
        }
    }

    pub async fn put(&self, inner: T) {
        loop {
            let mut guard = zasynclock!(self.inner);
            if guard.is_some() {
                self.wait_put.fetch_add(1, Ordering::AcqRel);
                self.cond_put.wait(guard).await;
            } else {
                *guard = Some(inner);
                self.wait_put.fetch_sub(1, Ordering::AcqRel);
                drop(guard);
                self.cond_take.notify_one();
                return;
            }
        }
    }
}

impl<T> Default for Mvar<T> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use zenoh_result::ZResult;

    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn mvar() -> ZResult<()> {
        use std::{sync::Arc, time::Duration};

        use super::Mvar;

        const TIMEOUT: Duration = Duration::from_secs(60);

        let count: usize = 1_000;
        let mvar: Arc<Mvar<usize>> = Arc::new(Mvar::new());

        let c_mvar = mvar.clone();
        let ch = tokio::task::spawn(async move {
            for _ in 0..count {
                let n = c_mvar.take().await;
                print!("-{n} ");
            }
        });

        let ph = tokio::task::spawn(async move {
            for i in 0..count {
                mvar.put(i).await;
                print!("+{i} ");
            }
        });

        let _ = tokio::time::timeout(TIMEOUT, ph).await?;
        let _ = tokio::time::timeout(TIMEOUT, ch).await?;
        println!();
        Ok(())
    }
}