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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::Volatile;
use shared_memory::SharedMemCast;
use std::sync::atomic::AtomicU8;
use std::sync::atomic::Ordering;

// TODO: support enums
const UNOCCUPIED: u8 = 0;
const RESERVED: u8 = 1;
const OCCUPIED: u8 = 2;

/// Optional shared data
pub struct SharedOption<T: SharedMemCast> {
    data: Volatile<T>,
    occupied: AtomicU8,
}

impl<T: SharedMemCast> SharedOption<T> {
    pub fn none() -> SharedOption<T> {
        SharedOption {
            data: Volatile::zeroed(),
            occupied: AtomicU8::new(UNOCCUPIED),
        }
    }

    pub fn some(value: T) -> SharedOption<T> {
        SharedOption {
            data: Volatile::new(value),
            occupied: AtomicU8::new(OCCUPIED),
        }
    }

    pub fn volatile_peek(&self) -> Option<&Volatile<T>> {
        if self.occupied.load(Ordering::SeqCst) == OCCUPIED {
            Some(&self.data)
        } else {
            None
        }
    }

    pub fn put(&self, value: T) -> Result<(), T> {
        if self
            .occupied
            .compare_and_swap(UNOCCUPIED, RESERVED, Ordering::SeqCst)
            == UNOCCUPIED
        {
            self.data.write_volatile(value);
            self.occupied.store(OCCUPIED, Ordering::SeqCst);
            Ok(())
        } else {
            Err(value)
        }
    }

    pub fn take(&self) -> Option<T> {
        if self
            .occupied
            .compare_and_swap(OCCUPIED, RESERVED, Ordering::SeqCst)
            == OCCUPIED
        {
            let result = self.data.read_volatile();
            self.occupied.store(UNOCCUPIED, Ordering::SeqCst);
            Some(result)
        } else {
            None
        }
    }
}