summaryrefslogtreecommitdiff
path: root/src/sys/eventfd.rs
blob: e8e2bd2352be4f3671e9098755f0f1889ba9b1de (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
use libc;
use fcntl::Fd;
use {Error, Result};

bitflags!(
    flags EventFdFlag: libc::c_int {
        const EFD_CLOEXEC   = 0o2000000, // Since Linux 2.6.27
        const EFD_NONBLOCK  = 0o0004000, // Since Linux 2.6.27
        const EFD_SEMAPHORE = 0o0000001, // Since Linux 2.6.30
    }
);

mod ffi {
    use libc;

    extern {
        pub fn eventfd(initval: libc::c_uint, flags: libc::c_int) -> libc::c_int;
    }
}

pub fn eventfd(initval: usize, flags: EventFdFlag) -> Result<Fd> {
    unsafe {
        let res = ffi::eventfd(initval as libc::c_uint, flags.bits());

        if res < 0 {
            return Err(Error::last());
        }

        Ok(res as Fd)
    }
}