1 /* Jim - A small embeddable Tcl interpreter
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * The views and conclusions contained in the software and documentation
38 * are those of the authors and should not be interpreted as representing
39 * official policies, either expressed or implied, of the Jim Tcl Project.
43 * - to really use flags in Jim_ProcessEvents()
44 * - more complete [after] command with [after info] and other subcommands.
49 #define __JIM_EVENTLOOP_CORE__
51 #include <pkgconf/jimtcl.h>
54 #include <cyg/jimtcl/jim.h>
55 #include <cyg/jimtcl/jim-eventloop.h>
58 #include "jim-eventloop.h"
63 #include <sys/types.h>
67 #include "replacements.h"
72 /* File event structure */
73 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
{
84 jim_wide id
; /* time event identifier. */
85 int mode
; /* restart, repetitive .. UK */
86 long initialms
; /* initial relativ timer value UK */
87 long when_sec
; /* seconds */
88 long when_ms
; /* milliseconds */
89 Jim_TimeProc
*timeProc
;
90 Jim_EventFinalizerProc
*finalizerProc
;
92 struct Jim_TimeEvent
*next
;
95 /* Per-interp stucture containing the state of the event loop */
96 typedef struct Jim_EventLoop
{
97 jim_wide timeEventNextId
;
98 Jim_FileEvent
*fileEventHead
;
99 Jim_TimeEvent
*timeEventHead
;
102 void Jim_CreateFileHandler(Jim_Interp
*interp
, void *handle
, int mask
,
103 Jim_FileProc
*proc
, void *clientData
,
104 Jim_EventFinalizerProc
*finalizerProc
)
107 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
109 // fprintf(stderr,"rein\n");
110 fe
= Jim_Alloc(sizeof(*fe
));
114 fe
->finalizerProc
= finalizerProc
;
115 fe
->clientData
= clientData
;
116 fe
->next
= eventLoop
->fileEventHead
;
117 eventLoop
->fileEventHead
= fe
;
118 // fprintf(stderr,"raus\n");
121 void Jim_DeleteFileHandler(Jim_Interp
*interp
, void *handle
)
123 Jim_FileEvent
*fe
, *prev
= NULL
;
124 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
126 fe
= eventLoop
->fileEventHead
;
128 if (fe
->handle
== handle
) {
130 eventLoop
->fileEventHead
= fe
->next
;
132 prev
->next
= fe
->next
;
133 if (fe
->finalizerProc
)
134 fe
->finalizerProc(interp
, fe
->clientData
);
143 // The same for signals.
144 void Jim_CreateSignalHandler(Jim_Interp
*interp
, int signum
,
145 Jim_FileProc
*proc
, void *clientData
,
146 Jim_EventFinalizerProc
*finalizerProc
)
149 void Jim_DeleteSignalHandler(Jim_Interp
*interp
, int signum
)
153 /* That's another part of this extension that needs to be ported
155 static void JimGetTime(long *seconds
, long *milliseconds
)
159 gettimeofday(&tv
, NULL
);
160 *seconds
= tv
.tv_sec
;
161 *milliseconds
= tv
.tv_usec
/1000;
164 jim_wide
Jim_CreateTimeHandler(Jim_Interp
*interp
, jim_wide milliseconds
,
165 Jim_TimeProc
*proc
, void *clientData
,
166 Jim_EventFinalizerProc
*finalizerProc
)
168 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
169 jim_wide id
= eventLoop
->timeEventNextId
++;
171 long cur_sec
, cur_ms
;
173 JimGetTime(&cur_sec
, &cur_ms
);
175 te
= Jim_Alloc(sizeof(*te
));
178 te
->initialms
= milliseconds
;
179 te
->when_sec
= cur_sec
+ milliseconds
/1000;
180 te
->when_ms
= cur_ms
+ milliseconds
%1000;
181 if (te
->when_ms
>= 1000) {
186 te
->finalizerProc
= finalizerProc
;
187 te
->clientData
= clientData
;
188 te
->next
= eventLoop
->timeEventHead
;
189 eventLoop
->timeEventHead
= te
;
193 jim_wide
Jim_DeleteTimeHandler(Jim_Interp
*interp
, jim_wide id
)
195 Jim_TimeEvent
*te
, *prev
= NULL
;
196 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
197 long cur_sec
, cur_ms
;
200 JimGetTime(&cur_sec
, &cur_ms
);
202 te
= eventLoop
->timeEventHead
;
203 if (id
>= eventLoop
->timeEventNextId
)
204 return -2; /* wrong event ID */
207 remain
= (te
->when_sec
- cur_sec
) * 1000;
208 remain
+= (te
->when_ms
- cur_ms
) ;
209 remain
= (remain
< 0) ? 0 : remain
;
212 eventLoop
->timeEventHead
= te
->next
;
214 prev
->next
= te
->next
;
215 if (te
->finalizerProc
)
216 te
->finalizerProc(interp
, te
->clientData
);
223 return -1; /* NO event with the specified ID found */
226 /* Search the first timer to fire.
227 * This operation is useful to know how many time the select can be
228 * put in sleep without to delay any event.
229 * If there are no timers NULL is returned. */
230 static Jim_TimeEvent
*JimSearchNearestTimer(Jim_EventLoop
*eventLoop
)
232 Jim_TimeEvent
*te
= eventLoop
->timeEventHead
;
233 Jim_TimeEvent
*nearest
= NULL
;
236 if (!nearest
|| te
->when_sec
< nearest
->when_sec
||
237 (te
->when_sec
== nearest
->when_sec
&&
238 te
->when_ms
< nearest
->when_ms
))
245 /* --- POSIX version of Jim_ProcessEvents, for now the only available --- */
246 #define JIM_FILE_EVENTS 1
247 #define JIM_TIME_EVENTS 2
248 #define JIM_ALL_EVENTS (JIM_FILE_EVENTS|JIM_TIME_EVENTS)
249 #define JIM_DONT_WAIT 4
251 /* Process every pending time event, then every pending file event
252 * (that may be registered by time event callbacks just processed).
253 * Without special flags the function sleeps until some file event
254 * fires, or when the next time event occurrs (if any).
256 * If flags is 0, the function does nothing and returns.
257 * if flags has JIM_ALL_EVENTS set, all the kind of events are processed.
258 * if flags has JIM_FILE_EVENTS set, file events are processed.
259 * if flags has JIM_TIME_EVENTS set, time events are processed.
260 * if flags has JIM_DONT_WAIT set the function returns ASAP until all
261 * the events that's possible to process without to wait are processed.
263 * The function returns the number of events processed. */
264 int Jim_ProcessEvents(Jim_Interp
*interp
, int flags
)
266 int maxfd
= 0, numfd
= 0, processed
= 0;
267 fd_set rfds
, wfds
, efds
;
268 Jim_EventLoop
*eventLoop
= Jim_GetAssocData(interp
, "eventloop");
269 Jim_FileEvent
*fe
= eventLoop
->fileEventHead
;
278 /* Check file events */
280 int fd
= fileno((FILE*)fe
->handle
);
282 if (fe
->mask
& JIM_EVENT_READABLE
)
284 if (fe
->mask
& JIM_EVENT_WRITABLE
) FD_SET(fd
, &wfds
);
285 if (fe
->mask
& JIM_EVENT_EXCEPTION
) FD_SET(fd
, &efds
);
286 if (maxfd
< fd
) maxfd
= fd
;
290 /* Note that we want call select() even if there are no
291 * file events to process as long as we want to process time
292 * events, in order to sleep until the next time event is ready
294 if (numfd
|| ((flags
& JIM_TIME_EVENTS
) && !(flags
& JIM_DONT_WAIT
))) {
296 Jim_TimeEvent
*shortest
;
297 struct timeval tv
, *tvp
;
300 shortest
= JimSearchNearestTimer(eventLoop
);
302 long now_sec
, now_ms
;
304 /* Calculate the time missing for the nearest
306 JimGetTime(&now_sec
, &now_ms
);
308 dt
= 1000 * (shortest
->when_sec
- now_sec
);
309 dt
+= ( shortest
->when_ms
- now_ms
);
313 tvp
->tv_sec
= dt
/ 1000;
314 tvp
->tv_usec
= dt
% 1000;
315 // fprintf(stderr,"Next %d.% 8d\n",(int)tvp->tv_sec,(int)tvp->tv_usec);
317 tvp
= NULL
; /* wait forever */
318 // fprintf(stderr,"No Event\n");
321 retval
= select(maxfd
+1, &rfds
, &wfds
, &efds
, tvp
);
324 case EINTR
: fprintf(stderr
,"select EINTR\n"); break;
325 case EINVAL
: fprintf(stderr
,"select EINVAL\n"); break;
326 case ENOMEM
: fprintf(stderr
,"select ENOMEM\n"); break;
328 } else if (retval
> 0) {
329 fe
= eventLoop
->fileEventHead
;
331 int fd
= fileno((FILE*)fe
->handle
);
333 // fprintf(stderr,"fd: %d mask: %02x \n",fd,fe->mask);
335 if ((fe
->mask
& JIM_EVENT_READABLE
&& FD_ISSET(fd
, &rfds
)) ||
336 (fe
->mask
& JIM_EVENT_WRITABLE
&& FD_ISSET(fd
, &wfds
)) ||
337 (fe
->mask
& JIM_EVENT_EXCEPTION
&& FD_ISSET(fd
, &efds
)))
341 if (fe
->mask
& JIM_EVENT_READABLE
&& FD_ISSET(fd
, &rfds
)) {
342 mask
|= JIM_EVENT_READABLE
;
343 if ((fe
->mask
& JIM_EVENT_FEOF
) && feof((FILE *)fe
->handle
))
344 mask
|= JIM_EVENT_FEOF
;
346 if (fe
->mask
& JIM_EVENT_WRITABLE
&& FD_ISSET(fd
, &wfds
))
347 mask
|= JIM_EVENT_WRITABLE
;
348 if (fe
->mask
& JIM_EVENT_EXCEPTION
&& FD_ISSET(fd
, &efds
))
349 mask
|= JIM_EVENT_EXCEPTION
;
350 if (fe
->fileProc(interp
, fe
->clientData
, mask
) == JIM_ERR
) {
351 /* Remove the element on handler error */
352 Jim_DeleteFileHandler(interp
, fe
->handle
);
355 /* After an event is processed our file event list
356 * may no longer be the same, so what we do
357 * is to clear the bit for this file descriptor and
358 * restart again from the head. */
359 fe
= eventLoop
->fileEventHead
;
369 /* Check time events */
370 te
= eventLoop
->timeEventHead
;
371 maxId
= eventLoop
->timeEventNextId
-1;
373 long now_sec
, now_ms
;
376 if (te
->id
> maxId
) {
380 JimGetTime(&now_sec
, &now_ms
);
381 if (now_sec
> te
->when_sec
||
382 (now_sec
== te
->when_sec
&& now_ms
>= te
->when_ms
))
385 te
->timeProc(interp
, te
->clientData
);
386 /* After an event is processed our time event list may
387 * no longer be the same, so we restart from head.
388 * Still we make sure to don't process events registered
389 * by event handlers itself in order to don't loop forever
390 * even in case an [after 0] that continuously register
391 * itself. To do so we saved the max ID we want to handle. */
392 Jim_DeleteTimeHandler(interp
, id
);
393 te
= eventLoop
->timeEventHead
;
401 /* ---------------------------------------------------------------------- */
403 void JimELAssocDataDeleProc(Jim_Interp
*interp
, void *data
)
408 Jim_EventLoop
*eventLoop
= data
;
410 fe
= eventLoop
->fileEventHead
;
413 if (fe
->finalizerProc
)
414 fe
->finalizerProc(interp
, fe
->clientData
);
419 te
= eventLoop
->timeEventHead
;
422 if (te
->finalizerProc
)
423 te
->finalizerProc(interp
, te
->clientData
);
430 static int JimELVwaitCommand(Jim_Interp
*interp
, int argc
,
431 Jim_Obj
*const *argv
)
436 Jim_WrongNumArgs(interp
, 1, argv
, "name");
439 oldValue
= Jim_GetGlobalVariable(interp
, argv
[1], JIM_NONE
);
440 if (oldValue
) Jim_IncrRefCount(oldValue
);
444 Jim_ProcessEvents(interp
, JIM_ALL_EVENTS
);
445 currValue
= Jim_GetGlobalVariable(interp
, argv
[1], JIM_NONE
);
446 /* Stop the loop if the vwait-ed variable changed value,
447 * or if was unset and now is set (or the contrary). */
448 if ((oldValue
&& !currValue
) ||
449 (!oldValue
&& currValue
) ||
450 (oldValue
&& currValue
&&
451 !Jim_StringEqObj(oldValue
, currValue
, JIM_CASESENS
)))
454 if (oldValue
) Jim_DecrRefCount(interp
, oldValue
);
458 void JimAfterTimeHandler(Jim_Interp
*interp
, void *clientData
)
460 Jim_Obj
*objPtr
= clientData
;
462 Jim_EvalObjBackground(interp
, objPtr
);
465 void JimAfterTimeEventFinalizer(Jim_Interp
*interp
, void *clientData
)
467 Jim_Obj
*objPtr
= clientData
;
469 Jim_DecrRefCount(interp
, objPtr
);
472 static int JimELAfterCommand(Jim_Interp
*interp
, int argc
,
473 Jim_Obj
*const *argv
)
476 Jim_Obj
*objPtr
, *idObjPtr
;
477 const char *options
[] = {
478 "info", "cancel", "restart", "expire", NULL
480 enum {INFO
, CANCEL
, RESTART
, EXPIRE
, CREATE
};
481 int option
= CREATE
;
484 Jim_WrongNumArgs(interp
, 1, argv
, "<after milliseconds> script");
487 if (Jim_GetWide(interp
, argv
[1], &ms
) != JIM_OK
)
488 if (Jim_GetEnum(interp
, argv
[1], options
, &option
, "after options",
489 JIM_ERRMSG
) != JIM_OK
)
493 Jim_IncrRefCount(argv
[2]);
494 id
= Jim_CreateTimeHandler(interp
, ms
, JimAfterTimeHandler
, argv
[2],
495 JimAfterTimeEventFinalizer
);
496 objPtr
= Jim_NewStringObj(interp
, NULL
, 0);
497 Jim_AppendString(interp
, objPtr
, "after#", -1);
498 idObjPtr
= Jim_NewIntObj(interp
, id
);
499 Jim_IncrRefCount(idObjPtr
);
500 Jim_AppendObj(interp
, objPtr
, idObjPtr
);
501 Jim_DecrRefCount(interp
, idObjPtr
);
502 Jim_SetResult(interp
, objPtr
);
508 const char *tok
= Jim_GetString(argv
[2], &tlen
);
509 if ( sscanf(tok
,"after#%lld",&id
) == 1) {
510 remain
= Jim_DeleteTimeHandler(interp
, id
);
512 Jim_SetResult(interp
, Jim_NewIntObj(interp
, remain
));
516 Jim_SetResultString(interp
, "invalid event" , -1);
520 fprintf(stderr
,"unserviced option to after %d\n",option
);
525 /* This extension is not dynamically loaded, instead it's linked statically,
526 which is why we shouldn't use the unspecific 'Jim_OnLoad' name */
527 #define Jim_OnLoad Jim_EventLoopOnLoad
529 int Jim_OnLoad(Jim_Interp
*interp
)
531 Jim_EventLoop
*eventLoop
;
533 Jim_InitExtension(interp
);
534 if (Jim_PackageProvide(interp
, "eventloop", "1.0", JIM_ERRMSG
) != JIM_OK
)
537 eventLoop
= Jim_Alloc(sizeof(*eventLoop
));
538 eventLoop
->fileEventHead
= NULL
;
539 eventLoop
->timeEventHead
= NULL
;
540 eventLoop
->timeEventNextId
= 1;
541 Jim_SetAssocData(interp
, "eventloop", JimELAssocDataDeleProc
, eventLoop
);
543 Jim_CreateCommand(interp
, "vwait", JimELVwaitCommand
, NULL
, NULL
);
544 Jim_CreateCommand(interp
, "after", JimELAfterCommand
, NULL
, NULL
);
546 /* Export events API */
547 Jim_RegisterApi(interp
, "Jim_CreateFileHandler", Jim_CreateFileHandler
);
548 Jim_RegisterApi(interp
, "Jim_DeleteFileHandler", Jim_DeleteFileHandler
);
549 Jim_RegisterApi(interp
, "Jim_CreateTimeHandler", Jim_CreateTimeHandler
);
550 Jim_RegisterApi(interp
, "Jim_DeleteTimeHandler", Jim_DeleteTimeHandler
);
551 Jim_RegisterApi(interp
, "Jim_ProcessEvents", Jim_ProcessEvents
);
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)