jim_wide Jim_DeleteTimeHandler(Jim_Interp *interp, jim_wide id)
{
    Jim_TimeEvent *te;
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");

    if (id >= eventLoop->timeEventNextId) {
        return -2;              /* wrong event ID */
    }

    te = Jim_RemoveTimeHandler(eventLoop, id);
    if (te) {
        jim_wide remain;
        long cur_sec, cur_ms;

        JimGetTime(&cur_sec, &cur_ms);

        remain = (te->when_sec - cur_sec) * 1000;
        remain += (te->when_ms - cur_ms);
        remain = (remain < 0) ? 0 : remain;

        Jim_FreeTimeHandler(interp, te);
        return remain;
    }
    return -1;                  /* NO event with the specified ID found */
}
示例#2
0
jim_wide Jim_DeleteTimeHandler(Jim_Interp *interp, jim_wide id)
{
    Jim_TimeEvent *te, *prev = NULL;
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
    long cur_sec, cur_ms;
    jim_wide remain ;

    JimGetTime(&cur_sec, &cur_ms);

    te = eventLoop->timeEventHead;
    if (id >= eventLoop->timeEventNextId) 
    	return -2; /* wrong event ID */
    while(te) {
        if (te->id == id) {
            remain  = (te->when_sec - cur_sec) * 1000;
            remain += (te->when_ms  - cur_ms) ;
	    remain = (remain < 0) ? 0 : remain ;

            if (prev == NULL)
                eventLoop->timeEventHead = te->next;
            else
                prev->next = te->next;
            if (te->finalizerProc)
                te->finalizerProc(interp, te->clientData);
            Jim_Free(te);
            return remain;
        }
        prev = te;
        te = te->next;
    }
    return -1; /* NO event with the specified ID found */
}
示例#3
0
jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide milliseconds,
        Jim_TimeProc *proc, void *clientData,
        Jim_EventFinalizerProc *finalizerProc)
{
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
    jim_wide id = eventLoop->timeEventNextId++;
    Jim_TimeEvent *te;
    long cur_sec, cur_ms;

    JimGetTime(&cur_sec, &cur_ms);

    te = Jim_Alloc(sizeof(*te));
    te->id = id;
    te->mode = 0;
    te->initialms = milliseconds;
    te->when_sec = cur_sec + milliseconds/1000;
    te->when_ms = cur_ms + milliseconds%1000;
    if (te->when_ms >= 1000) {
        te->when_sec ++;
        te->when_ms -= 1000;
    }
    te->timeProc = proc;
    te->finalizerProc = finalizerProc;
    te->clientData = clientData;
    te->next = eventLoop->timeEventHead;
    eventLoop->timeEventHead = te;
    return id;
}
jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide milliseconds,
    Jim_TimeProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
{
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
    jim_wide id = eventLoop->timeEventNextId++;
    Jim_TimeEvent *te, *e, *prev;
    long cur_sec, cur_ms;

    JimGetTime(&cur_sec, &cur_ms);

    te = Jim_Alloc(sizeof(*te));
    te->id = id;
    te->mode = 0;
    te->initialms = milliseconds;
    te->when_sec = cur_sec + milliseconds / 1000;
    te->when_ms = cur_ms + milliseconds % 1000;
    if (te->when_ms >= 1000) {
        te->when_sec++;
        te->when_ms -= 1000;
    }
    te->timeProc = proc;
    te->finalizerProc = finalizerProc;
    te->clientData = clientData;

    /* Add to the appropriate place in the list */
    if (eventLoop->timeEventHead) {
        prev = NULL;
        for (e = eventLoop->timeEventHead; e; e = e->next) {
            if (te->when_sec < e->when_sec || (te->when_sec == e->when_sec && te->when_ms < e->when_ms)) {
                break;
            }
            prev = e;
        }
        if (prev) {
            te->next = prev->next;
            prev->next = te;
            return id;
        }
    }

    te->next = eventLoop->timeEventHead;
    eventLoop->timeEventHead = te;

    return id;
}
/* Process every pending time event, then every pending file event
 * (that may be registered by time event callbacks just processed).
 * Without special flags the function sleeps until some file event
 * fires, or when the next time event occurrs (if any).
 *
 * If flags is 0, the function does nothing and returns.
 * if flags has JIM_ALL_EVENTS set, all the kind of events are processed.
 * if flags has JIM_FILE_EVENTS set, file events are processed.
 * if flags has JIM_TIME_EVENTS set, time events are processed.
 * if flags has JIM_DONT_WAIT set the function returns ASAP until all
 * the events that's possible to process without to wait are processed.
 *
 * The function returns the number of events processed or -1 if
 * there are no matching handlers, or -2 on error.
 */
int Jim_ProcessEvents(Jim_Interp *interp, int flags)
{
    jim_wide sleep_ms = -1;
    int processed = 0;
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
    Jim_FileEvent *fe = eventLoop->fileEventHead;
    Jim_TimeEvent *te;
    jim_wide maxId;

    if ((flags & JIM_FILE_EVENTS) == 0 || fe == NULL) {
        /* No file events */
        if ((flags & JIM_TIME_EVENTS) == 0 || eventLoop->timeEventHead == NULL) {
            /* No time events */
            return -1;
        }
    }

    /* Note that we want call select() even if there are no
     * file events to process as long as we want to process time
     * events, in order to sleep until the next time event is ready
     * to fire. */

    if (flags & JIM_DONT_WAIT) {
        /* Wait no time */
        sleep_ms = 0;
    }
    else if (flags & JIM_TIME_EVENTS) {
        /* The nearest timer is always at the head of the list */
        if (eventLoop->timeEventHead) {
            Jim_TimeEvent *shortest = eventLoop->timeEventHead;
            long now_sec, now_ms;

            /* Calculate the time missing for the nearest
             * timer to fire. */
            JimGetTime(&now_sec, &now_ms);
            sleep_ms = 1000 * (shortest->when_sec - now_sec) + (shortest->when_ms - now_ms);
            if (sleep_ms < 0) {
                sleep_ms = 1;
            }
        }
        else {
            /* Wait forever */
            sleep_ms = -1;
        }
    }

#ifdef HAVE_SELECT
    if (flags & JIM_FILE_EVENTS) {
        int retval;
        struct timeval tv, *tvp = NULL;
        fd_set rfds, wfds, efds;
        int maxfd = -1;

        FD_ZERO(&rfds);
        FD_ZERO(&wfds);
        FD_ZERO(&efds);

        /* Check file events */
        while (fe != NULL) {
            int fd = fileno(fe->handle);

            if (fe->mask & JIM_EVENT_READABLE)
                FD_SET(fd, &rfds);
            if (fe->mask & JIM_EVENT_WRITABLE)
                FD_SET(fd, &wfds);
            if (fe->mask & JIM_EVENT_EXCEPTION)
                FD_SET(fd, &efds);
            if (maxfd < fd)
                maxfd = fd;
            fe = fe->next;
        }

        if (sleep_ms >= 0) {
            tvp = &tv;
            tvp->tv_sec = sleep_ms / 1000;
            tvp->tv_usec = 1000 * (sleep_ms % 1000);
        }

        retval = select(maxfd + 1, &rfds, &wfds, &efds, tvp);

        if (retval < 0) {
            if (errno == EINVAL) {
                /* This can happen on mingw32 if a non-socket filehandle is passed */
                Jim_SetResultString(interp, "non-waitable filehandle", -1);
                return -2;
            }
            /* XXX: What about EINTR? */
        }
        else if (retval > 0) {
            fe = eventLoop->fileEventHead;
            while (fe != NULL) {
                int fd = fileno(fe->handle);
                int mask = 0;

                if ((fe->mask & JIM_EVENT_READABLE) && FD_ISSET(fd, &rfds))
                    mask |= JIM_EVENT_READABLE;
                if (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds))
                    mask |= JIM_EVENT_WRITABLE;
                if (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds))
                    mask |= JIM_EVENT_EXCEPTION;

                if (mask) {
                    if (fe->fileProc(interp, fe->clientData, mask) != JIM_OK) {
                        /* Remove the element on handler error */
                        Jim_DeleteFileHandler(interp, fe->handle);
                    }
                    processed++;
                    /* After an event is processed our file event list
                     * may no longer be the same, so what we do
                     * is to clear the bit for this file descriptor and
                     * restart again from the head. */
                    fe = eventLoop->fileEventHead;
                    FD_CLR(fd, &rfds);
                    FD_CLR(fd, &wfds);
                    FD_CLR(fd, &efds);
                }
                else {
                    fe = fe->next;
                }
            }
        }
    }
#else
    if (sleep_ms > 0) {
        msleep(sleep_ms);
    }
#endif

    /* Check time events */
    te = eventLoop->timeEventHead;
    maxId = eventLoop->timeEventNextId - 1;
    while (te) {
        long now_sec, now_ms;
        jim_wide id;

        if (te->id > maxId) {
            te = te->next;
            continue;
        }
        JimGetTime(&now_sec, &now_ms);
        if (now_sec > te->when_sec || (now_sec == te->when_sec && now_ms >= te->when_ms)) {
            id = te->id;
            /* Remove from the list before executing */
            Jim_RemoveTimeHandler(eventLoop, id);
            te->timeProc(interp, te->clientData);
            /* After an event is processed our time event list may
             * no longer be the same, so we restart from head.
             * Still we make sure to don't process events registered
             * by event handlers itself in order to don't loop forever
             * even in case an [after 0] that continuously register
             * itself. To do so we saved the max ID we want to handle. */
            Jim_FreeTimeHandler(interp, te);

            te = eventLoop->timeEventHead;
            processed++;
        }
        else {
            te = te->next;
        }
    }

    return processed;
}
示例#6
0
/* Process every pending time event, then every pending file event
 * (that may be registered by time event callbacks just processed).
 * Without special flags the function sleeps until some file event
 * fires, or when the next time event occurrs (if any).
 *
 * If flags is 0, the function does nothing and returns.
 * if flags has JIM_ALL_EVENTS set, all the kind of events are processed.
 * if flags has JIM_FILE_EVENTS set, file events are processed.
 * if flags has JIM_TIME_EVENTS set, time events are processed.
 * if flags has JIM_DONT_WAIT set the function returns ASAP until all
 * the events that's possible to process without to wait are processed.
 *
 * The function returns the number of events processed. */
int Jim_ProcessEvents(Jim_Interp *interp, int flags)
{
    int maxfd = 0, numfd = 0, processed = 0;
    fd_set rfds, wfds, efds;
    Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
    Jim_FileEvent *fe = eventLoop->fileEventHead;
    Jim_TimeEvent *te;
    jim_wide maxId;
    JIM_NOTUSED(flags);

    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    FD_ZERO(&efds);

    /* Check file events */
    while (fe != NULL) {
        int fd = fileno((FILE*)fe->handle);

        if (fe->mask & JIM_EVENT_READABLE) 
		FD_SET(fd, &rfds);
        if (fe->mask & JIM_EVENT_WRITABLE) FD_SET(fd, &wfds);
        if (fe->mask & JIM_EVENT_EXCEPTION) FD_SET(fd, &efds);
        if (maxfd < fd) maxfd = fd;
        numfd++;
        fe = fe->next;
    }
    /* Note that we want call select() even if there are no
     * file events to process as long as we want to process time
     * events, in order to sleep until the next time event is ready
     * to fire. */
    if (numfd || ((flags & JIM_TIME_EVENTS) && !(flags & JIM_DONT_WAIT))) {
        int retval;
        Jim_TimeEvent *shortest;
        struct timeval tv, *tvp;
	jim_wide dt;

        shortest = JimSearchNearestTimer(eventLoop);
        if (shortest) {
            long now_sec, now_ms;

            /* Calculate the time missing for the nearest
             * timer to fire. */
            JimGetTime(&now_sec, &now_ms);
            tvp = &tv;
	    dt   = 1000 * (shortest->when_sec - now_sec);
	    dt  += ( shortest->when_ms  - now_ms);
            if (dt < 0) {
		dt = 1;
	    }
	    tvp->tv_sec  = dt / 1000;
	    tvp->tv_usec = dt % 1000;
	    // fprintf(stderr,"Next %d.% 8d\n",(int)tvp->tv_sec,(int)tvp->tv_usec);
        } else {
            tvp = NULL; /* wait forever */
		// fprintf(stderr,"No Event\n");
        }

        retval = select(maxfd+1, &rfds, &wfds, &efds, tvp);
        if (retval < 0) {
	   switch (errno) {
	       case EINTR:   fprintf(stderr,"select EINTR\n"); break;
	       case EINVAL:  fprintf(stderr,"select EINVAL\n"); break;
 	       case ENOMEM:  fprintf(stderr,"select ENOMEM\n"); break;
	   }
	} else if (retval > 0) {
            fe = eventLoop->fileEventHead;
            while(fe != NULL) {
                int fd = fileno((FILE*)fe->handle);

		// fprintf(stderr,"fd: %d mask: %02x \n",fd,fe->mask);

                if ((fe->mask & JIM_EVENT_READABLE && FD_ISSET(fd, &rfds)) ||
                    (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds)) ||
                    (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds)))
                {
                    int mask = 0;

                    if (fe->mask & JIM_EVENT_READABLE && FD_ISSET(fd, &rfds)) {
                        mask |= JIM_EVENT_READABLE;
			if ((fe->mask & JIM_EVENT_FEOF) && feof((FILE *)fe->handle))
				mask |= JIM_EVENT_FEOF;
		    }
                    if (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds))
                        mask |= JIM_EVENT_WRITABLE;
                    if (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds))
                        mask |= JIM_EVENT_EXCEPTION;
                    if (fe->fileProc(interp, fe->clientData, mask) == JIM_ERR) {
                        /* Remove the element on handler error */
                        Jim_DeleteFileHandler(interp, fe->handle);
                    }
                    processed++;
                    /* After an event is processed our file event list
                     * may no longer be the same, so what we do
                     * is to clear the bit for this file descriptor and
                     * restart again from the head. */
                    fe = eventLoop->fileEventHead;
                    FD_CLR(fd, &rfds);
                    FD_CLR(fd, &wfds);
                    FD_CLR(fd, &efds);
                } else {
                    fe = fe->next;
                }
            }
        }
    }
    /* Check time events */
    te = eventLoop->timeEventHead;
    maxId = eventLoop->timeEventNextId-1;
    while(te) {
        long now_sec, now_ms;
        jim_wide id;

        if (te->id > maxId) {
            te = te->next;
            continue;
        }
        JimGetTime(&now_sec, &now_ms);
        if (now_sec > te->when_sec ||
            (now_sec == te->when_sec && now_ms >= te->when_ms))
        {
            id = te->id;
            te->timeProc(interp, te->clientData);
            /* After an event is processed our time event list may
             * no longer be the same, so we restart from head.
             * Still we make sure to don't process events registered
             * by event handlers itself in order to don't loop forever
             * even in case an [after 0] that continuously register
             * itself. To do so we saved the max ID we want to handle. */
            Jim_DeleteTimeHandler(interp, id);
            te = eventLoop->timeEventHead;
        } else {
            te = te->next;
        }
    }

    return processed;
}