2 /* Jim - A small embeddable Tcl interpreter
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
6 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
7 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
8 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
9 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
10 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The views and conclusions contained in the software and documentation
37 * are those of the authors and should not be interpreted as representing
38 * official policies, either expressed or implied, of the Jim Tcl Project.
42 #include "jimautoconf.h"
43 #include "jim-eventloop.h"
47 #include <sys/types.h>
52 #if defined(__MINGW32__)
57 #define usleep(US) msleep((US) / 1000)
60 #include <sys/select.h>
63 /* XXX: Implement this in terms of select() or nanosleep() */
66 #define msleep(MS) sleep((MS) / 1000); usleep(((MS) % 1000) * 1000);
71 /* File event structure */
72 typedef struct Jim_FileEvent
75 int mask
; /* one of JIM_EVENT_(READABLE|WRITABLE|EXCEPTION) */
76 Jim_FileProc
*fileProc
;
77 Jim_EventFinalizerProc
*finalizerProc
;
79 struct Jim_FileEvent
*next
;
82 /* Time event structure */
83 typedef struct Jim_TimeEvent
85 jim_wide id
; /* time event identifier. */
86 int mode
; /* restart, repetitive .. UK */
87 long initialms
; /* initial relativ timer value UK */
88 long when_sec
; /* seconds */
89 long when_ms
; /* milliseconds */
90 Jim_TimeProc
*timeProc
;
91 Jim_EventFinalizerProc
*finalizerProc
;
93 struct Jim_TimeEvent
*next
;
96 /* Per-interp stucture containing the state of the event loop */
97 typedef struct Jim_EventLoop
99 jim_wide timeEventNextId
;
100 Jim_FileEvent
*fileEventHead
;
101 Jim_TimeEvent
*timeEventHead
;
102 int suppress_bgerror
; /* bgerror returned break, so don't call it again */
105 static void JimAfterTimeHandler(Jim_Interp
*interp
, void *clientData
);
106 static void JimAfterTimeEventFinalizer(Jim_Interp
*interp
, void *clientData
);
108 int Jim_EvalObjBackground(Jim_Interp
*interp
, Jim_Obj
*scriptObjPtr
)
110 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
111 Jim_CallFrame
*savedFramePtr
;
114 savedFramePtr
= interp
->framePtr
;
115 interp
->framePtr
= interp
->topFramePtr
;
116 retval
= Jim_EvalObj(interp
, scriptObjPtr
);
117 interp
->framePtr
= savedFramePtr
;
118 /* Try to report the error (if any) via the bgerror proc */
119 if (retval
!= JIM_OK
&& !eventLoop
->suppress_bgerror
) {
123 objv
[0] = Jim_NewStringObj(interp
, "bgerror", -1);
124 objv
[1] = Jim_GetResult(interp
);
125 Jim_IncrRefCount(objv
[0]);
126 Jim_IncrRefCount(objv
[1]);
127 if (Jim_GetCommand(interp
, objv
[0], JIM_NONE
) == NULL
|| (rc
= Jim_EvalObjVector(interp
, 2, objv
)) != JIM_OK
) {
128 if (rc
== JIM_BREAK
) {
129 /* No more bgerror calls */
130 eventLoop
->suppress_bgerror
++;
133 /* Report the error to stderr. */
134 Jim_MakeErrorMessage(interp
);
135 fprintf(stderr
, "%s\n", Jim_String(Jim_GetResult(interp
)));
136 /* And reset the result */
137 Jim_SetResultString(interp
, "", -1);
140 Jim_DecrRefCount(interp
, objv
[0]);
141 Jim_DecrRefCount(interp
, objv
[1]);
147 void Jim_CreateFileHandler(Jim_Interp
*interp
, FILE * handle
, int mask
,
148 Jim_FileProc
* proc
, void *clientData
, Jim_EventFinalizerProc
* finalizerProc
)
151 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
153 fe
= Jim_Alloc(sizeof(*fe
));
157 fe
->finalizerProc
= finalizerProc
;
158 fe
->clientData
= clientData
;
159 fe
->next
= eventLoop
->fileEventHead
;
160 eventLoop
->fileEventHead
= fe
;
163 void Jim_DeleteFileHandler(Jim_Interp
*interp
, FILE * handle
)
165 Jim_FileEvent
*fe
, *prev
= NULL
;
166 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
168 fe
= eventLoop
->fileEventHead
;
170 if (fe
->handle
== handle
) {
172 eventLoop
->fileEventHead
= fe
->next
;
174 prev
->next
= fe
->next
;
175 if (fe
->finalizerProc
)
176 fe
->finalizerProc(interp
, fe
->clientData
);
185 static void JimGetTime(long *seconds
, long *milliseconds
)
189 gettimeofday(&tv
, NULL
);
190 *seconds
= tv
.tv_sec
;
191 *milliseconds
= tv
.tv_usec
/ 1000;
194 jim_wide
Jim_CreateTimeHandler(Jim_Interp
*interp
, jim_wide milliseconds
,
195 Jim_TimeProc
* proc
, void *clientData
, Jim_EventFinalizerProc
* finalizerProc
)
197 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
198 jim_wide id
= eventLoop
->timeEventNextId
++;
199 Jim_TimeEvent
*te
, *e
, *prev
;
200 long cur_sec
, cur_ms
;
202 JimGetTime(&cur_sec
, &cur_ms
);
204 te
= Jim_Alloc(sizeof(*te
));
207 te
->initialms
= milliseconds
;
208 te
->when_sec
= cur_sec
+ milliseconds
/ 1000;
209 te
->when_ms
= cur_ms
+ milliseconds
% 1000;
210 if (te
->when_ms
>= 1000) {
215 te
->finalizerProc
= finalizerProc
;
216 te
->clientData
= clientData
;
218 /* Add to the appropriate place in the list */
219 if (eventLoop
->timeEventHead
) {
221 for (e
= eventLoop
->timeEventHead
; e
; e
= e
->next
) {
222 if (te
->when_sec
< e
->when_sec
|| (te
->when_sec
== e
->when_sec
&& te
->when_ms
< e
->when_ms
)) {
228 te
->next
= prev
->next
;
234 te
->next
= eventLoop
->timeEventHead
;
235 eventLoop
->timeEventHead
= te
;
240 static jim_wide
JimParseAfterId(Jim_Obj
*idObj
)
243 const char *tok
= Jim_GetString(idObj
, &len
);
246 if (strncmp(tok
, "after#", 6) == 0 && Jim_StringToWide(tok
+ 6, &id
, 10) == JIM_OK
) {
247 /* Got an event by id */
253 static jim_wide
JimFindAfterByScript(Jim_EventLoop
*eventLoop
, Jim_Obj
*scriptObj
)
257 for (te
= eventLoop
->timeEventHead
; te
; te
= te
->next
) {
258 /* Is this an 'after' event? */
259 if (te
->timeProc
== JimAfterTimeHandler
) {
260 if (Jim_StringEqObj(scriptObj
, te
->clientData
)) {
265 return -1; /* NO event with the specified ID found */
268 static Jim_TimeEvent
*JimFindTimeHandlerById(Jim_EventLoop
*eventLoop
, jim_wide id
)
272 for (te
= eventLoop
->timeEventHead
; te
; te
= te
->next
) {
280 static Jim_TimeEvent
*Jim_RemoveTimeHandler(Jim_EventLoop
*eventLoop
, jim_wide id
)
282 Jim_TimeEvent
*te
, *prev
= NULL
;
284 for (te
= eventLoop
->timeEventHead
; te
; te
= te
->next
) {
287 eventLoop
->timeEventHead
= te
->next
;
289 prev
->next
= te
->next
;
297 static void Jim_FreeTimeHandler(Jim_Interp
*interp
, Jim_TimeEvent
*te
)
299 if (te
->finalizerProc
)
300 te
->finalizerProc(interp
, te
->clientData
);
304 jim_wide
Jim_DeleteTimeHandler(Jim_Interp
*interp
, jim_wide id
)
307 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
309 if (id
>= eventLoop
->timeEventNextId
) {
310 return -2; /* wrong event ID */
313 te
= Jim_RemoveTimeHandler(eventLoop
, id
);
316 long cur_sec
, cur_ms
;
318 JimGetTime(&cur_sec
, &cur_ms
);
320 remain
= (te
->when_sec
- cur_sec
) * 1000;
321 remain
+= (te
->when_ms
- cur_ms
);
322 remain
= (remain
< 0) ? 0 : remain
;
324 Jim_FreeTimeHandler(interp
, te
);
327 return -1; /* NO event with the specified ID found */
330 /* --- POSIX version of Jim_ProcessEvents, for now the only available --- */
332 /* Process every pending time event, then every pending file event
333 * (that may be registered by time event callbacks just processed).
334 * Without special flags the function sleeps until some file event
335 * fires, or when the next time event occurrs (if any).
337 * If flags is 0, the function does nothing and returns.
338 * if flags has JIM_ALL_EVENTS set, all the kind of events are processed.
339 * if flags has JIM_FILE_EVENTS set, file events are processed.
340 * if flags has JIM_TIME_EVENTS set, time events are processed.
341 * if flags has JIM_DONT_WAIT set the function returns ASAP until all
342 * the events that's possible to process without to wait are processed.
344 * The function returns the number of events processed or -1 if
345 * there are no matching handlers, or -2 on error.
347 int Jim_ProcessEvents(Jim_Interp
*interp
, int flags
)
349 jim_wide sleep_ms
= -1;
351 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
352 Jim_FileEvent
*fe
= eventLoop
->fileEventHead
;
356 if ((flags
& JIM_FILE_EVENTS
) == 0 || fe
== NULL
) {
358 if ((flags
& JIM_TIME_EVENTS
) == 0 || eventLoop
->timeEventHead
== NULL
) {
364 /* Note that we want call select() even if there are no
365 * file events to process as long as we want to process time
366 * events, in order to sleep until the next time event is ready
369 if (flags
& JIM_DONT_WAIT
) {
373 else if (flags
& JIM_TIME_EVENTS
) {
374 /* The nearest timer is always at the head of the list */
375 if (eventLoop
->timeEventHead
) {
376 Jim_TimeEvent
*shortest
= eventLoop
->timeEventHead
;
377 long now_sec
, now_ms
;
379 /* Calculate the time missing for the nearest
381 JimGetTime(&now_sec
, &now_ms
);
382 sleep_ms
= 1000 * (shortest
->when_sec
- now_sec
) + (shortest
->when_ms
- now_ms
);
394 if (flags
& JIM_FILE_EVENTS
) {
396 struct timeval tv
, *tvp
= NULL
;
397 fd_set rfds
, wfds
, efds
;
404 /* Check file events */
406 int fd
= fileno(fe
->handle
);
408 if (fe
->mask
& JIM_EVENT_READABLE
)
410 if (fe
->mask
& JIM_EVENT_WRITABLE
)
412 if (fe
->mask
& JIM_EVENT_EXCEPTION
)
421 tvp
->tv_sec
= sleep_ms
/ 1000;
422 tvp
->tv_usec
= 1000 * (sleep_ms
% 1000);
425 retval
= select(maxfd
+ 1, &rfds
, &wfds
, &efds
, tvp
);
428 if (errno
== EINVAL
) {
429 /* This can happen on mingw32 if a non-socket filehandle is passed */
430 Jim_SetResultString(interp
, "non-waitable filehandle", -1);
433 /* XXX: What about EINTR? */
435 else if (retval
> 0) {
436 fe
= eventLoop
->fileEventHead
;
438 int fd
= fileno(fe
->handle
);
441 if ((fe
->mask
& JIM_EVENT_READABLE
) && FD_ISSET(fd
, &rfds
))
442 mask
|= JIM_EVENT_READABLE
;
443 if (fe
->mask
& JIM_EVENT_WRITABLE
&& FD_ISSET(fd
, &wfds
))
444 mask
|= JIM_EVENT_WRITABLE
;
445 if (fe
->mask
& JIM_EVENT_EXCEPTION
&& FD_ISSET(fd
, &efds
))
446 mask
|= JIM_EVENT_EXCEPTION
;
449 if (fe
->fileProc(interp
, fe
->clientData
, mask
) != JIM_OK
) {
450 /* Remove the element on handler error */
451 Jim_DeleteFileHandler(interp
, fe
->handle
);
454 /* After an event is processed our file event list
455 * may no longer be the same, so what we do
456 * is to clear the bit for this file descriptor and
457 * restart again from the head. */
458 fe
= eventLoop
->fileEventHead
;
475 /* Check time events */
476 te
= eventLoop
->timeEventHead
;
477 maxId
= eventLoop
->timeEventNextId
- 1;
479 long now_sec
, now_ms
;
482 if (te
->id
> maxId
) {
486 JimGetTime(&now_sec
, &now_ms
);
487 if (now_sec
> te
->when_sec
|| (now_sec
== te
->when_sec
&& now_ms
>= te
->when_ms
)) {
489 /* Remove from the list before executing */
490 Jim_RemoveTimeHandler(eventLoop
, id
);
491 te
->timeProc(interp
, te
->clientData
);
492 /* After an event is processed our time event list may
493 * no longer be the same, so we restart from head.
494 * Still we make sure to don't process events registered
495 * by event handlers itself in order to don't loop forever
496 * even in case an [after 0] that continuously register
497 * itself. To do so we saved the max ID we want to handle. */
498 Jim_FreeTimeHandler(interp
, te
);
500 te
= eventLoop
->timeEventHead
;
511 /* ---------------------------------------------------------------------- */
513 static void JimELAssocDataDeleProc(Jim_Interp
*interp
, void *data
)
518 Jim_EventLoop
*eventLoop
= data
;
520 fe
= eventLoop
->fileEventHead
;
523 if (fe
->finalizerProc
)
524 fe
->finalizerProc(interp
, fe
->clientData
);
529 te
= eventLoop
->timeEventHead
;
532 if (te
->finalizerProc
)
533 te
->finalizerProc(interp
, te
->clientData
);
540 static int JimELVwaitCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
542 Jim_EventLoop
*eventLoop
= Jim_CmdPrivData(interp
);
547 Jim_WrongNumArgs(interp
, 1, argv
, "name");
551 oldValue
= Jim_GetGlobalVariable(interp
, argv
[1], JIM_NONE
);
553 Jim_IncrRefCount(oldValue
);
556 /* If a result was left, it is an error */
558 Jim_GetString(interp
->result
, &len
);
564 eventLoop
->suppress_bgerror
= 0;
566 while ((rc
= Jim_ProcessEvents(interp
, JIM_ALL_EVENTS
)) >= 0) {
568 currValue
= Jim_GetGlobalVariable(interp
, argv
[1], JIM_NONE
);
569 /* Stop the loop if the vwait-ed variable changed value,
570 * or if was unset and now is set (or the contrary). */
571 if ((oldValue
&& !currValue
) ||
572 (!oldValue
&& currValue
) ||
573 (oldValue
&& currValue
&& !Jim_StringEqObj(oldValue
, currValue
)))
577 Jim_DecrRefCount(interp
, oldValue
);
584 Jim_SetEmptyResult(interp
);
588 static int JimELUpdateCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
590 Jim_EventLoop
*eventLoop
= Jim_CmdPrivData(interp
);
591 static const char * const options
[] = {
594 enum { UPDATE_IDLE
, UPDATE_NONE
};
595 int option
= UPDATE_NONE
;
596 int flags
= JIM_TIME_EVENTS
;
599 flags
= JIM_ALL_EVENTS
;
601 else if (argc
> 2 || Jim_GetEnum(interp
, argv
[1], options
, &option
, NULL
, JIM_ERRMSG
| JIM_ENUM_ABBREV
) != JIM_OK
) {
602 Jim_WrongNumArgs(interp
, 1, argv
, "?idletasks?");
606 eventLoop
->suppress_bgerror
= 0;
608 while (Jim_ProcessEvents(interp
, flags
| JIM_DONT_WAIT
) > 0) {
614 static void JimAfterTimeHandler(Jim_Interp
*interp
, void *clientData
)
616 Jim_Obj
*objPtr
= clientData
;
618 Jim_EvalObjBackground(interp
, objPtr
);
621 static void JimAfterTimeEventFinalizer(Jim_Interp
*interp
, void *clientData
)
623 Jim_Obj
*objPtr
= clientData
;
625 Jim_DecrRefCount(interp
, objPtr
);
628 static int JimELAfterCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
630 Jim_EventLoop
*eventLoop
= Jim_CmdPrivData(interp
);
632 Jim_Obj
*objPtr
, *idObjPtr
;
633 static const char * const options
[] = {
634 "cancel", "info", "idle", NULL
637 { AFTER_CANCEL
, AFTER_INFO
, AFTER_IDLE
, AFTER_RESTART
, AFTER_EXPIRE
, AFTER_CREATE
};
638 int option
= AFTER_CREATE
;
641 Jim_WrongNumArgs(interp
, 1, argv
, "option ?arg ...?");
644 if (Jim_GetWide(interp
, argv
[1], &ms
) != JIM_OK
) {
645 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "argument", JIM_ERRMSG
) != JIM_OK
) {
648 Jim_SetEmptyResult(interp
);
650 else if (argc
== 2) {
659 Jim_WrongNumArgs(interp
, 2, argv
, "script ?script ...?");
664 Jim_Obj
*scriptObj
= Jim_ConcatObj(interp
, argc
- 2, argv
+ 2);
665 Jim_IncrRefCount(scriptObj
);
666 id
= Jim_CreateTimeHandler(interp
, ms
, JimAfterTimeHandler
, scriptObj
,
667 JimAfterTimeEventFinalizer
);
668 objPtr
= Jim_NewStringObj(interp
, NULL
, 0);
669 Jim_AppendString(interp
, objPtr
, "after#", -1);
670 idObjPtr
= Jim_NewIntObj(interp
, id
);
671 Jim_IncrRefCount(idObjPtr
);
672 Jim_AppendObj(interp
, objPtr
, idObjPtr
);
673 Jim_DecrRefCount(interp
, idObjPtr
);
674 Jim_SetResult(interp
, objPtr
);
679 Jim_WrongNumArgs(interp
, 2, argv
, "id|command");
685 id
= JimParseAfterId(argv
[2]);
687 /* Not an event id, so search by script */
688 Jim_Obj
*scriptObj
= Jim_ConcatObj(interp
, argc
- 2, argv
+ 2);
689 id
= JimFindAfterByScript(eventLoop
, scriptObj
);
690 Jim_FreeNewObj(interp
, scriptObj
);
696 remain
= Jim_DeleteTimeHandler(interp
, id
);
698 Jim_SetResultInt(interp
, remain
);
705 Jim_TimeEvent
*te
= eventLoop
->timeEventHead
;
706 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
708 const char *fmt
= "after#%" JIM_WIDE_MODIFIER
;
711 snprintf(buf
, sizeof(buf
), fmt
, te
->id
);
712 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, buf
, -1));
715 Jim_SetResult(interp
, listObj
);
717 else if (argc
== 3) {
718 id
= JimParseAfterId(argv
[2]);
720 Jim_TimeEvent
*e
= JimFindTimeHandlerById(eventLoop
, id
);
721 if (e
&& e
->timeProc
== JimAfterTimeHandler
) {
722 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
723 Jim_ListAppendElement(interp
, listObj
, e
->clientData
);
724 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, e
->initialms
? "timer" : "idle", -1));
725 Jim_SetResult(interp
, listObj
);
729 Jim_SetResultFormatted(interp
, "event \"%#s\" doesn't exist", argv
[2]);
733 Jim_WrongNumArgs(interp
, 2, argv
, "?id?");
741 int Jim_eventloopInit(Jim_Interp
*interp
)
743 Jim_EventLoop
*eventLoop
;
745 if (Jim_PackageProvide(interp
, "eventloop", "1.0", JIM_ERRMSG
))
748 eventLoop
= Jim_Alloc(sizeof(*eventLoop
));
749 eventLoop
->fileEventHead
= NULL
;
750 eventLoop
->timeEventHead
= NULL
;
751 eventLoop
->timeEventNextId
= 1;
752 eventLoop
->suppress_bgerror
= 0;
753 Jim_SetAssocData(interp
, "eventloop", JimELAssocDataDeleProc
, eventLoop
);
755 Jim_CreateCommand(interp
, "vwait", JimELVwaitCommand
, eventLoop
, NULL
);
756 Jim_CreateCommand(interp
, "update", JimELUpdateCommand
, eventLoop
, NULL
);
757 Jim_CreateCommand(interp
, "after", JimELAfterCommand
, eventLoop
, NULL
);