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
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{
    mem,
    ops::{Deref, DerefMut},
    sync::Arc,
};

use crate::maps::Map;

pub(crate) struct MapLockError;

/* FIXME: write a full RwLock implementation that doesn't use borrowing guards
 * so that try_read() and try_write() don't have to use the ugly lifetime
 * extension hack */

#[derive(Debug)]
pub(crate) struct MapLock {
    inner: Arc<RwLock<Map>>,
}

impl MapLock {
    pub(crate) fn new(map: Map) -> MapLock {
        MapLock {
            inner: Arc::new(RwLock::new(map)),
        }
    }

    pub(crate) fn try_read(&self) -> Result<MapRef, MapLockError> {
        let lock: Option<RwLockReadGuard<'static, Map>> =
            unsafe { mem::transmute(self.inner.try_read()) };
        lock.map(|guard| MapRef {
            _lock: self.inner.clone(),
            guard,
        })
        .ok_or(MapLockError)
    }

    pub(crate) fn try_write(&self) -> Result<MapRefMut, MapLockError> {
        let lock: Option<RwLockWriteGuard<'static, Map>> =
            unsafe { mem::transmute(self.inner.try_write()) };
        lock.map(|guard| MapRefMut {
            _lock: self.inner.clone(),
            guard,
        })
        .ok_or(MapLockError)
    }
}

/// A borrowed reference to a BPF map.
pub struct MapRef {
    _lock: Arc<RwLock<Map>>,
    guard: RwLockReadGuard<'static, Map>,
}

/// A mutable borrowed reference to a BPF map.
pub struct MapRefMut {
    _lock: Arc<RwLock<Map>>,
    guard: RwLockWriteGuard<'static, Map>,
}

impl Deref for MapRef {
    type Target = Map;

    fn deref(&self) -> &Map {
        &*self.guard
    }
}

impl Deref for MapRefMut {
    type Target = Map;

    fn deref(&self) -> &Map {
        &*self.guard
    }
}

impl DerefMut for MapRefMut {
    fn deref_mut(&mut self) -> &mut Map {
        &mut *self.guard
    }
}