skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

timed_flush.c (692B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <skalibs/error.h>
      5 #include <skalibs/iopause.h>
      6 #include <skalibs/unix-timed.h>
      7 
      8 int timed_flush (void *b, init_func_ref getfd, init_func_ref isnonempty, init_func_ref flush, tain const *deadline, tain *stamp)
      9 {
     10   iopause_fd x = { .fd = (*getfd)(b), .events = IOPAUSE_WRITE, .revents = 0 } ;
     11   while ((*isnonempty)(b))
     12   {
     13     int r = iopause_stamp(&x, 1, deadline, stamp) ;
     14     if (r < 0) return 0 ;
     15     else if (!r) return (errno = ETIMEDOUT, 0) ;
     16     else if (x.revents & IOPAUSE_WRITE)
     17     {
     18       if (!((*flush)(b)) && !error_isagain(errno)) return 0 ;
     19     }
     20     else if (x.revents & IOPAUSE_EXCEPT) return (*flush)(b) ;
     21   }
     22   return 1 ;
     23 }