summaryrefslogtreecommitdiff
path: root/src/sys/eventfd.rs
blob: b57f70315d6cf94babba8258cd920ef34a772e07 (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
use std::mem;
use libc::{c_int, c_uint};
use fcntl::Fd;
use errno::{SysResult, SysError};

bitflags!(
    #[deriving(Copy)]
    flags EventFdFlag: 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
    }
)

pub fn eventfd(initval: uint, flags: EventFdFlag) -> SysResult<Fd> {
    type F = unsafe extern "C" fn(initval: c_uint, flags: c_int) -> c_int;

    extern {
        #[linkage = "extern_weak"]
        static eventfd: *const ();
    }

    if eventfd.is_null() {
        panic!("eventfd unsupported on this platform");
    }

    let res = unsafe {
        mem::transmute::<*const (), F>(eventfd)(
            initval as c_uint, flags.bits())
    };

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

    Ok(res)
}