IO-Ppoll

 view release on metacpan or  search on metacpan

lib/IO/Ppoll.xs  view on Meta::CPAN

#  define PUSHmortal                     PUSHs(sv_newmortal())
#endif

#ifndef mPUSHi
#  define mPUSHi(i)                      sv_setiv_mg(PUSHmortal, (IV)(i))
#endif

MODULE = IO::Ppoll      PACKAGE = IO::Ppoll

BOOT:
{
  HV *stash;
  stash = gv_stashpvn("IO::Ppoll", 9, TRUE);
  newCONSTSUB(stash, "POLLIN",  newSViv(POLLIN));
  newCONSTSUB(stash, "POLLOUT", newSViv(POLLOUT));
  newCONSTSUB(stash, "POLLPRI", newSViv(POLLPRI));
  newCONSTSUB(stash, "POLLERR", newSViv(POLLERR));
  newCONSTSUB(stash, "POLLHUP", newSViv(POLLHUP));
  newCONSTSUB(stash, "POLLNVAL",newSViv(POLLNVAL));
}

SV *
get_events(fds, nfds, fd)
    SV *fds
    int nfds
    int fd
  INIT:
    int i;
    struct pollfd *fds_real;
  CODE:
    fds_real = (struct pollfd *)SvPV_nolen(fds);
    for(i = 0; i < nfds; i++) {
      if(fds_real[i].fd == fd)
        XSRETURN_IV(fds_real[i].events);
    }
    XSRETURN_NO;

int
get_revents(fds, nfds, fd)
    SV *fds
    int nfds
    int fd
  INIT:
    int i;
    struct pollfd *fds_real;
  CODE:
    fds_real = (struct pollfd *)SvPV_nolen(fds);
    for(i = 0; i < nfds; i++) {
      if(fds_real[i].fd == fd)
        XSRETURN_IV(fds_real[i].revents);
    }
    XSRETURN_NO;

void
get_fds(fds, nfds)
    SV *fds
    int nfds
  INIT:
    int i;
    struct pollfd *fds_real;
  PPCODE:
    fds_real = (struct pollfd *)SvPV_nolen(fds);
    EXTEND(SP, nfds);
    for(i = 0; i < nfds; i++) {
      int fd = fds_real[i].fd;
      mPUSHi(fd);
    }

void
get_fds_for(fds, nfds, events)
    SV *fds
    int nfds
    int events
  INIT:
    int i;
    struct pollfd *fds_real;
  PPCODE:
    fds_real = (struct pollfd *)SvPV_nolen(fds);
    EXTEND(SP, nfds);
    for(i = 0; i < nfds; i++) {
      int fd;
      if((fds_real[i].revents & events) == 0)
        continue;
      fd = fds_real[i].fd;
      mPUSHi(fd);
    }

void
mas_events(fds, nfds, fd, maskbits, setbits)
    SV *fds
    int &nfds
    int fd
    int maskbits
    int setbits
  CODE:
    struct pollfd *fds_real = (struct pollfd *)SvPV_nolen(fds);
    int i;
    for(i = 0; i < nfds; i++) {
      if(fds_real[i].fd == fd) {
        fds_real[i].events &= maskbits;
        fds_real[i].events |= setbits;
        break;
      }
    }
    if(i == nfds) {
      nfds++;
      SvGROW(fds, nfds * sizeof(struct pollfd));
      SvCUR_set(fds, nfds * sizeof(struct pollfd));
      SvPOK_only(fds);
      fds_real = (struct pollfd *)SvPV(fds, PL_na);
      fds_real[i].fd = fd;
      fds_real[i].events = setbits;
    }
  OUTPUT:
    nfds

void
del_events(fds, nfds, fd)
    SV *fds
    int &nfds
    int fd
  INIT:
    struct pollfd *fds_real;
  CODE:
    fds_real = (struct pollfd *)SvPV_nolen(fds);
    int i;
    for(i = 0; i < nfds; i++) {
      if(fds_real[i].fd == fd) {
        /* Since we don't care about the ordering here, just move the
         * top one down */
        fds_real[i] = fds_real[nfds-1];
        nfds--;
        SvCUR_set(fds, nfds * sizeof(struct pollfd));
        break;
      }
    }
  OUTPUT:



( run in 2.563 seconds using v1.01-cache-2.11-cpan-71847e10f99 )