Exemplo n.º 1
0
ssize_t
syncsocket_read_absolute(SyncSocket* ssocket,
                         void* buf,
                         size_t size,
                         int64_t deadline)
{
    int ret;

    if (ssocket == NULL || ssocket->fd < 0 || ssocket->iolooper == NULL) {
        errno = EINVAL;
        return -1;
    }

    ret = iolooper_wait_absolute(ssocket->iolooper, deadline);
    if (ret > 0) {
        if (!iolooper_is_read(ssocket->iolooper, ssocket->fd)) {
            D("%s: Internal error, iolooper_is_read() not set!", __FUNCTION__);
            return -1;
        }
        do {
            ret = read(ssocket->fd, buf, size);
        } while( ret < 0 && errno == EINTR);
    } else if (ret == 0) {
        // Timed out
        errno = ETIMEDOUT;
        ret = -1;
    }
    return ret;
}
Exemplo n.º 2
0
static int
glooper_run(Looper*  ll, Duration loop_deadline_ms)
{
    GLooper*   looper = (GLooper*) ll;
    IoLooper*  iol    = looper->iolooper;

    looper->running = 1;

    while (looper->running)
    {
        int ret;

        /* Exit prematurely if we detect that we don't have any active timer
         * and no active LoopIo
         */
        if (looper->numActiveIos == 0 && looper->activeTimers == NULL)
            return EWOULDBLOCK;

        /* First, compute next deadline */
        Duration  deadline = DURATION_INFINITE;

        if (looper->activeTimers != NULL)
            deadline = looper->activeTimers->deadline;

        if (deadline > loop_deadline_ms)
            deadline = loop_deadline_ms;

        ret = iolooper_wait_absolute(iol, deadline);
        if (ret < 0) { /* error, force stop ! */
            break;
        }
        if (ret > 0) {
            unsigned ready;
            GLoopIo* io;

            /* Add io waiters to the pending list */
            AREFSET_FOREACH(looper->ios, io, {
                if (io->wanted == 0)
                    continue;

                ready = 0;

                if (iolooper_is_read(iol, io->fd))
                    ready |= LOOP_IO_READ;

                if (iolooper_is_write(iol, io->fd))
                    ready |= LOOP_IO_WRITE;

                io->ready = ready;
                if (ready != 0) {
                    arefSet_add(looper->pendingIos, io);
                }
            });
        }