summaryrefslogtreecommitdiffstats
path: root/vendor/fd-lock/src/sys/unix/rw_lock.rs
blob: c33ce5c78967033fbe728097361fef8105bad3ec (plain)
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
use rustix::fd::BorrowedFd;
use rustix::fs::{flock, FlockOperation};
use std::io::{self, Error, ErrorKind};
use std::os::unix::io::AsRawFd;

use super::{RwLockReadGuard, RwLockWriteGuard};

#[derive(Debug)]
pub struct RwLock<T: AsRawFd> {
    pub(crate) inner: T,
}

impl<T: AsRawFd> RwLock<T> {
    #[inline]
    pub fn new(inner: T) -> Self {
        RwLock { inner }
    }

    #[inline]
    pub fn write(&mut self) -> io::Result<RwLockWriteGuard<'_, T>> {
        flock(&self.as_fd(), FlockOperation::LockExclusive)?;
        Ok(RwLockWriteGuard::new(self))
    }

    #[inline]
    pub fn try_write(&mut self) -> Result<RwLockWriteGuard<'_, T>, Error> {
        flock(&self.as_fd(), FlockOperation::NonBlockingLockExclusive).map_err(|err| match err
            .kind()
        {
            ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
            _ => Error::from(err),
        })?;
        Ok(RwLockWriteGuard::new(self))
    }

    #[inline]
    pub fn read(&self) -> io::Result<RwLockReadGuard<'_, T>> {
        flock(&self.as_fd(), FlockOperation::LockShared)?;
        Ok(RwLockReadGuard::new(self))
    }

    #[inline]
    pub fn try_read(&self) -> Result<RwLockReadGuard<'_, T>, Error> {
        flock(&self.as_fd(), FlockOperation::NonBlockingLockShared).map_err(|err| {
            match err.kind() {
                ErrorKind::AlreadyExists => ErrorKind::WouldBlock.into(),
                _ => Error::from(err),
            }
        })?;
        Ok(RwLockReadGuard::new(self))
    }

    #[inline]
    pub fn into_inner(self) -> T
    where
        T: Sized,
    {
        self.inner
    }

    #[inline]
    pub(crate) fn as_fd(&self) -> BorrowedFd<'_> {
        // Safety: We assume that `self.inner`'s file descriptor is valid for
        // at least the lifetime of `self`.
        //
        // Once I/O safety is stablized in std, we can switch the public API to
        // use `AsFd` instead of `AsRawFd` and eliminate this `unsafe` block.
        unsafe { BorrowedFd::borrow_raw(self.inner.as_raw_fd()) }
    }
}