1+ use rustix:: event:: { EventfdFlags , eventfd} ;
2+
13use super :: * ;
24
35#[ derive( Debug ) ]
@@ -8,40 +10,36 @@ pub(super) struct Notifier {
810impl Notifier {
911 /// Create a new notifier.
1012 pub fn new ( ) -> io:: Result < Self > {
11- let fd = syscall ! ( libc :: eventfd( 0 , libc :: EFD_CLOEXEC | libc :: EFD_NONBLOCK ) ) ?;
12- let fd = unsafe { OwnedFd :: from_raw_fd ( fd ) } ;
13+ let fd = eventfd ( 0 , EventfdFlags :: CLOEXEC | EventfdFlags :: NONBLOCK ) ?;
14+
1315 Ok ( Self {
1416 notify : Arc :: new ( Notify :: new ( fd) ) ,
1517 } )
1618 }
1719
1820 pub fn clear ( & self ) -> io:: Result < ( ) > {
19- loop {
20- let mut buffer = [ 0u64 ] ;
21- let res = syscall ! ( libc:: read(
22- self . as_raw_fd( ) ,
23- buffer. as_mut_ptr( ) . cast( ) ,
24- std:: mem:: size_of:: <u64 >( )
25- ) ) ;
26- match res {
27- Ok ( len) => {
28- debug_assert_eq ! ( len, std:: mem:: size_of:: <u64 >( ) as _) ;
29- break Ok ( ( ) ) ;
30- }
31- // Clear the next time:)
32- Err ( e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => break Ok ( ( ) ) ,
33- // Just like read_exact
34- Err ( e) if e. kind ( ) == io:: ErrorKind :: Interrupted => continue ,
35- Err ( e) => break Err ( e) ,
36- }
37- }
21+ const LEN : usize = std:: mem:: size_of :: < u64 > ( ) ;
22+
23+ let mut buffer = [ 0u8 ; LEN ] ;
24+
25+ let res = poll_io ( || rustix:: io:: read ( self , & mut buffer) ) ?;
26+
27+ debug_assert ! ( matches!( res, Poll :: Pending | Poll :: Ready ( LEN ) ) ) ;
28+
29+ Ok ( ( ) )
3830 }
3931
4032 pub fn waker ( & self ) -> Waker {
4133 Waker :: from ( self . notify . clone ( ) )
4234 }
4335}
4436
37+ impl AsFd for Notifier {
38+ fn as_fd ( & self ) -> BorrowedFd < ' _ > {
39+ self . notify . fd . as_fd ( )
40+ }
41+ }
42+
4543impl AsRawFd for Notifier {
4644 fn as_raw_fd ( & self ) -> RawFd {
4745 self . notify . fd . as_raw_fd ( )
@@ -61,12 +59,8 @@ impl Notify {
6159
6260 /// Notify the inner driver.
6361 pub fn notify ( & self ) -> io:: Result < ( ) > {
64- let data = 1u64 ;
65- syscall ! ( libc:: write(
66- self . fd. as_raw_fd( ) ,
67- & data as * const _ as * const _,
68- std:: mem:: size_of:: <u64 >( ) ,
69- ) ) ?;
62+ rustix:: io:: write ( & self . fd , & u64:: to_be_bytes ( 1 ) ) ?;
63+
7064 Ok ( ( ) )
7165 }
7266}
0 commit comments