4 * This file implements the generic portion of the Tcl notifier. The
5 * notifier is lowest-level part of the event system. It manages an event
6 * queue that holds Tcl_Event structures. The platform specific portion
7 * of the notifier is defined in the tcl*Notify.c files in each platform
10 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
11 * Copyright (c) 1998 by Scriptics Corporation.
12 * Copyright (c) 2003 by Kevin B. Kenny. All rights reserved.
14 * See the file "license.terms" for information on usage and redistribution of
15 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
21 * Module-scope struct of notifier hooks that are checked in the default
22 * notifier functions (for overriding via Tcl_SetNotifier).
25 Tcl_NotifierProcs tclNotifierHooks = {
26 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
30 * For each event source (created with Tcl_CreateEventSource) there is a
31 * structure of the following type:
34 typedef struct EventSource {
35 Tcl_EventSetupProc *setupProc;
36 Tcl_EventCheckProc *checkProc;
37 ClientData clientData;
38 struct EventSource *nextPtr;
42 * The following structure keeps track of the state of the notifier on a
43 * per-thread basis. The first three elements keep track of the event queue.
44 * In addition to the first (next to be serviced) and last events in the
45 * queue, we keep track of a "marker" event. This provides a simple priority
46 * mechanism whereby events can be inserted at the front of the queue but
47 * behind all other high-priority events already in the queue (this is used
48 * for things like a sequence of Enter and Leave events generated during a
49 * grab in Tk). These elements are protected by the queueMutex so that any
50 * thread can queue an event on any notifier. Note that all of the values in
51 * this structure will be initialized to 0.
54 typedef struct ThreadSpecificData {
55 Tcl_Event *firstEventPtr; /* First pending event, or NULL if none. */
56 Tcl_Event *lastEventPtr; /* Last pending event, or NULL if none. */
57 Tcl_Event *markerEventPtr; /* Last high-priority event in queue, or NULL
59 Tcl_Mutex queueMutex; /* Mutex to protect access to the previous
61 int serviceMode; /* One of TCL_SERVICE_NONE or
63 int blockTimeSet; /* 0 means there is no maximum block time:
65 Tcl_Time blockTime; /* If blockTimeSet is 1, gives the maximum
66 * elapsed time for the next block. */
67 int inTraversal; /* 1 if Tcl_SetMaxBlockTime is being called
68 * during an event source traversal. */
69 EventSource *firstEventSourcePtr;
70 /* Pointer to first event source in list of
71 * event sources for this thread. */
72 Tcl_ThreadId threadId; /* Thread that owns this notifier instance. */
73 ClientData clientData; /* Opaque handle for platform specific
75 int initialized; /* 1 if notifier has been initialized. */
76 struct ThreadSpecificData *nextPtr;
77 /* Next notifier in global list of notifiers.
78 * Access is controlled by the listLock global
82 static Tcl_ThreadDataKey dataKey;
85 * Global list of notifiers. Access to this list is controlled by the listLock
86 * mutex. If this becomes a performance bottleneck, this could be replaced
90 static ThreadSpecificData *firstNotifierPtr = NULL;
91 TCL_DECLARE_MUTEX(listLock)
94 * Declarations for routines used only in this file.
97 static void QueueEvent(ThreadSpecificData *tsdPtr,
98 Tcl_Event *evPtr, Tcl_QueuePosition position);
101 *----------------------------------------------------------------------
105 * Initialize the thread local data structures for the notifier
112 * Adds the current thread to the global list of notifiers.
114 *----------------------------------------------------------------------
118 TclInitNotifier(void)
120 ThreadSpecificData *tsdPtr;
121 Tcl_ThreadId threadId = Tcl_GetCurrentThread();
123 Tcl_MutexLock(&listLock);
124 for (tsdPtr = firstNotifierPtr; tsdPtr && tsdPtr->threadId != threadId;
125 tsdPtr = tsdPtr->nextPtr) {
126 /* Empty loop body. */
129 if (NULL == tsdPtr) {
131 * Notifier not yet initialized in this thread.
134 tsdPtr = TCL_TSD_INIT(&dataKey);
135 tsdPtr->threadId = threadId;
136 tsdPtr->clientData = Tcl_InitNotifier();
137 tsdPtr->initialized = 1;
138 tsdPtr->nextPtr = firstNotifierPtr;
139 firstNotifierPtr = tsdPtr;
141 Tcl_MutexUnlock(&listLock);
145 *----------------------------------------------------------------------
147 * TclFinalizeNotifier --
149 * Finalize the thread local data structures for the notifier subsystem.
155 * Removes the notifier associated with the current thread from the
156 * global notifier list. This is done only if the notifier was
157 * initialized for this thread by call to TclInitNotifier(). This is
158 * always true for threads which have been seeded with an Tcl
159 * interpreter, since the call to Tcl_CreateInterp will, among other
160 * things, call TclInitializeSubsystems() and this one will, in turn,
161 * call the TclInitNotifier() for the thread. For threads created without
162 * the Tcl interpreter, though, nobody is explicitly nor implicitly
163 * calling the TclInitNotifier hence, TclFinalizeNotifier should not be
166 *----------------------------------------------------------------------
170 TclFinalizeNotifier(void)
172 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
173 ThreadSpecificData **prevPtrPtr;
174 Tcl_Event *evPtr, *hold;
176 if (!tsdPtr->initialized) {
177 return; /* Notifier not initialized for the current thread */
180 Tcl_MutexLock(&(tsdPtr->queueMutex));
181 for (evPtr = tsdPtr->firstEventPtr; evPtr != NULL; ) {
183 evPtr = evPtr->nextPtr;
186 tsdPtr->firstEventPtr = NULL;
187 tsdPtr->lastEventPtr = NULL;
188 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
190 Tcl_MutexLock(&listLock);
192 Tcl_FinalizeNotifier(tsdPtr->clientData);
193 Tcl_MutexFinalize(&(tsdPtr->queueMutex));
194 for (prevPtrPtr = &firstNotifierPtr; *prevPtrPtr != NULL;
195 prevPtrPtr = &((*prevPtrPtr)->nextPtr)) {
196 if (*prevPtrPtr == tsdPtr) {
197 *prevPtrPtr = tsdPtr->nextPtr;
201 tsdPtr->initialized = 0;
203 Tcl_MutexUnlock(&listLock);
207 *----------------------------------------------------------------------
211 * Install a set of alternate functions for use with the notifier. In
212 * particular, this can be used to install the Xt-based notifier for use
213 * with the Browser plugin.
219 * Set the tclNotifierHooks global, which is checked in the default
220 * notifier functions.
222 *----------------------------------------------------------------------
227 Tcl_NotifierProcs *notifierProcPtr)
229 tclNotifierHooks = *notifierProcPtr;
233 *----------------------------------------------------------------------
235 * Tcl_CreateEventSource --
237 * This function is invoked to create a new source of events. The source
238 * is identified by a function that gets invoked during Tcl_DoOneEvent to
239 * check for events on that source and queue them.
246 * SetupProc and checkProc will be invoked each time that Tcl_DoOneEvent
247 * runs out of things to do. SetupProc will be invoked before
248 * Tcl_DoOneEvent calls select or whatever else it uses to wait for
249 * events. SetupProc typically calls functions like Tcl_SetMaxBlockTime
250 * to indicate what to wait for.
252 * CheckProc is called after select or whatever operation was actually
253 * used to wait. It figures out whether anything interesting actually
254 * happened (e.g. by calling Tcl_AsyncReady), and then calls
255 * Tcl_QueueEvent to queue any events that are ready.
257 * Each of these functions is passed two arguments, e.g.
258 * (*checkProc)(ClientData clientData, int flags));
259 * ClientData is the same as the clientData argument here, and flags is a
260 * combination of things like TCL_FILE_EVENTS that indicates what events
261 * are of interest: setupProc and checkProc use flags to figure out
262 * whether their events are relevant or not.
264 *----------------------------------------------------------------------
268 Tcl_CreateEventSource(
269 Tcl_EventSetupProc *setupProc,
270 /* Function to invoke to figure out what to
272 Tcl_EventCheckProc *checkProc,
273 /* Function to call after waiting to see what
275 ClientData clientData) /* One-word argument to pass to setupProc and
278 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
279 EventSource *sourcePtr = ckalloc(sizeof(EventSource));
281 sourcePtr->setupProc = setupProc;
282 sourcePtr->checkProc = checkProc;
283 sourcePtr->clientData = clientData;
284 sourcePtr->nextPtr = tsdPtr->firstEventSourcePtr;
285 tsdPtr->firstEventSourcePtr = sourcePtr;
289 *----------------------------------------------------------------------
291 * Tcl_DeleteEventSource --
293 * This function is invoked to delete the source of events given by proc
300 * The given event source is canceled, so its function will never again
301 * be called. If no such source exists, nothing happens.
303 *----------------------------------------------------------------------
307 Tcl_DeleteEventSource(
308 Tcl_EventSetupProc *setupProc,
309 /* Function to invoke to figure out what to
311 Tcl_EventCheckProc *checkProc,
312 /* Function to call after waiting to see what
314 ClientData clientData) /* One-word argument to pass to setupProc and
317 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
318 EventSource *sourcePtr, *prevPtr;
320 for (sourcePtr = tsdPtr->firstEventSourcePtr, prevPtr = NULL;
322 prevPtr = sourcePtr, sourcePtr = sourcePtr->nextPtr) {
323 if ((sourcePtr->setupProc != setupProc)
324 || (sourcePtr->checkProc != checkProc)
325 || (sourcePtr->clientData != clientData)) {
328 if (prevPtr == NULL) {
329 tsdPtr->firstEventSourcePtr = sourcePtr->nextPtr;
331 prevPtr->nextPtr = sourcePtr->nextPtr;
339 *----------------------------------------------------------------------
343 * Queue an event on the event queue associated with the current thread.
351 *----------------------------------------------------------------------
356 Tcl_Event *evPtr, /* Event to add to queue. The storage space
357 * must have been allocated the caller with
358 * malloc (ckalloc), and it becomes the
359 * property of the event queue. It will be
360 * freed after the event has been handled. */
361 Tcl_QueuePosition position) /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
364 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
366 QueueEvent(tsdPtr, evPtr, position);
370 *----------------------------------------------------------------------
372 * Tcl_ThreadQueueEvent --
374 * Queue an event on the specified thread's event queue.
382 *----------------------------------------------------------------------
386 Tcl_ThreadQueueEvent(
387 Tcl_ThreadId threadId, /* Identifier for thread to use. */
388 Tcl_Event *evPtr, /* Event to add to queue. The storage space
389 * must have been allocated the caller with
390 * malloc (ckalloc), and it becomes the
391 * property of the event queue. It will be
392 * freed after the event has been handled. */
393 Tcl_QueuePosition position) /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
396 ThreadSpecificData *tsdPtr;
399 * Find the notifier associated with the specified thread.
402 Tcl_MutexLock(&listLock);
403 for (tsdPtr = firstNotifierPtr; tsdPtr && tsdPtr->threadId != threadId;
404 tsdPtr = tsdPtr->nextPtr) {
405 /* Empty loop body. */
409 * Queue the event if there was a notifier associated with the thread.
413 QueueEvent(tsdPtr, evPtr, position);
417 Tcl_MutexUnlock(&listLock);
421 *----------------------------------------------------------------------
425 * Insert an event into the specified thread's event queue at one of
426 * three positions: the head, the tail, or before a floating marker.
427 * Events inserted before the marker will be processed in first-in-
428 * first-out order, but before any events inserted at the tail of the
429 * queue. Events inserted at the head of the queue will be processed in
430 * last-in-first-out order.
438 *----------------------------------------------------------------------
443 ThreadSpecificData *tsdPtr, /* Handle to thread local data that indicates
444 * which event queue to use. */
445 Tcl_Event *evPtr, /* Event to add to queue. The storage space
446 * must have been allocated the caller with
447 * malloc (ckalloc), and it becomes the
448 * property of the event queue. It will be
449 * freed after the event has been handled. */
450 Tcl_QueuePosition position) /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
453 Tcl_MutexLock(&(tsdPtr->queueMutex));
454 if (position == TCL_QUEUE_TAIL) {
456 * Append the event on the end of the queue.
459 evPtr->nextPtr = NULL;
460 if (tsdPtr->firstEventPtr == NULL) {
461 tsdPtr->firstEventPtr = evPtr;
463 tsdPtr->lastEventPtr->nextPtr = evPtr;
465 tsdPtr->lastEventPtr = evPtr;
466 } else if (position == TCL_QUEUE_HEAD) {
468 * Push the event on the head of the queue.
471 evPtr->nextPtr = tsdPtr->firstEventPtr;
472 if (tsdPtr->firstEventPtr == NULL) {
473 tsdPtr->lastEventPtr = evPtr;
475 tsdPtr->firstEventPtr = evPtr;
476 } else if (position == TCL_QUEUE_MARK) {
478 * Insert the event after the current marker event and advance the
479 * marker to the new event.
482 if (tsdPtr->markerEventPtr == NULL) {
483 evPtr->nextPtr = tsdPtr->firstEventPtr;
484 tsdPtr->firstEventPtr = evPtr;
486 evPtr->nextPtr = tsdPtr->markerEventPtr->nextPtr;
487 tsdPtr->markerEventPtr->nextPtr = evPtr;
489 tsdPtr->markerEventPtr = evPtr;
490 if (evPtr->nextPtr == NULL) {
491 tsdPtr->lastEventPtr = evPtr;
494 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
498 *----------------------------------------------------------------------
500 * Tcl_DeleteEvents --
502 * Calls a function for each event in the queue and deletes those for
503 * which the function returns 1. Events for which the function returns 0
504 * are left in the queue. Operates on the queue associated with the
511 * Potentially removes one or more events from the event queue.
513 *----------------------------------------------------------------------
518 Tcl_EventDeleteProc *proc, /* The function to call. */
519 ClientData clientData) /* The type-specific data. */
521 Tcl_Event *evPtr; /* Pointer to the event being examined */
522 Tcl_Event *prevPtr; /* Pointer to evPtr's predecessor, or NULL if
523 * evPtr designates the first event in the
524 * queue for the thread. */
526 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
528 Tcl_MutexLock(&(tsdPtr->queueMutex));
531 * Walk the queue of events for the thread, applying 'proc' to each to
532 * decide whether to eliminate the event.
536 evPtr = tsdPtr->firstEventPtr;
537 while (evPtr != NULL) {
538 if (proc(evPtr, clientData) == 1) {
540 * This event should be deleted. Unlink it.
543 if (prevPtr == NULL) {
544 tsdPtr->firstEventPtr = evPtr->nextPtr;
546 prevPtr->nextPtr = evPtr->nextPtr;
550 * Update 'last' and 'marker' events if either has been deleted.
553 if (evPtr->nextPtr == NULL) {
554 tsdPtr->lastEventPtr = prevPtr;
556 if (tsdPtr->markerEventPtr == evPtr) {
557 tsdPtr->markerEventPtr = prevPtr;
561 * Delete the event data structure.
565 evPtr = evPtr->nextPtr;
569 * Event is to be retained.
573 evPtr = evPtr->nextPtr;
576 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
580 *----------------------------------------------------------------------
582 * Tcl_ServiceEvent --
584 * Process one event from the event queue, or invoke an asynchronous
585 * event handler. Operates on event queue for current thread.
588 * The return value is 1 if the function actually found an event to
589 * process. If no processing occurred, then 0 is returned.
592 * Invokes all of the event handlers for the highest priority event in
593 * the event queue. May collapse some events into a single event or
594 * discard stale events.
596 *----------------------------------------------------------------------
601 int flags) /* Indicates what events should be processed.
602 * May be any combination of TCL_WINDOW_EVENTS
603 * TCL_FILE_EVENTS, TCL_TIMER_EVENTS, or other
604 * flags defined elsewhere. Events not
605 * matching this will be skipped for
606 * processing later. */
608 Tcl_Event *evPtr, *prevPtr;
611 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
614 * Asynchronous event handlers are considered to be the highest priority
615 * events, and so must be invoked before we process events on the event
619 if (Tcl_AsyncReady()) {
620 (void) Tcl_AsyncInvoke(NULL, 0);
625 * No event flags is equivalent to TCL_ALL_EVENTS.
628 if ((flags & TCL_ALL_EVENTS) == 0) {
629 flags |= TCL_ALL_EVENTS;
633 * Loop through all the events in the queue until we find one that can
634 * actually be handled.
637 Tcl_MutexLock(&(tsdPtr->queueMutex));
638 for (evPtr = tsdPtr->firstEventPtr; evPtr != NULL;
639 evPtr = evPtr->nextPtr) {
641 * Call the handler for the event. If it actually handles the event
642 * then free the storage for the event. There are two tricky things
643 * here, both stemming from the fact that the event code may be
644 * re-entered while servicing the event:
646 * 1. Set the "proc" field to NULL. This is a signal to ourselves
647 * that we shouldn't reexecute the handler if the event loop is
649 * 2. When freeing the event, must search the queue again from the
650 * front to find it. This is because the event queue could change
651 * almost arbitrarily while handling the event, so we can't depend
652 * on pointers found now still being valid when the handler
663 * Release the lock before calling the event function. This allows
664 * other threads to post events if we enter a recursive event loop in
665 * this thread. Note that we are making the assumption that if the
666 * proc returns 0, the event is still in the list.
669 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
670 result = proc(evPtr, flags);
671 Tcl_MutexLock(&(tsdPtr->queueMutex));
675 * The event was processed, so remove it from the queue.
678 if (tsdPtr->firstEventPtr == evPtr) {
679 tsdPtr->firstEventPtr = evPtr->nextPtr;
680 if (evPtr->nextPtr == NULL) {
681 tsdPtr->lastEventPtr = NULL;
683 if (tsdPtr->markerEventPtr == evPtr) {
684 tsdPtr->markerEventPtr = NULL;
687 for (prevPtr = tsdPtr->firstEventPtr;
688 prevPtr && prevPtr->nextPtr != evPtr;
689 prevPtr = prevPtr->nextPtr) {
690 /* Empty loop body. */
693 prevPtr->nextPtr = evPtr->nextPtr;
694 if (evPtr->nextPtr == NULL) {
695 tsdPtr->lastEventPtr = prevPtr;
697 if (tsdPtr->markerEventPtr == evPtr) {
698 tsdPtr->markerEventPtr = prevPtr;
707 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
711 * The event wasn't actually handled, so we have to restore the
712 * proc field to allow the event to be attempted again.
718 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
723 *----------------------------------------------------------------------
725 * Tcl_GetServiceMode --
727 * This routine returns the current service mode of the notifier.
730 * Returns either TCL_SERVICE_ALL or TCL_SERVICE_NONE.
735 *----------------------------------------------------------------------
739 Tcl_GetServiceMode(void)
741 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
743 return tsdPtr->serviceMode;
747 *----------------------------------------------------------------------
749 * Tcl_SetServiceMode --
751 * This routine sets the current service mode of the tsdPtr->
754 * Returns the previous service mode.
757 * Invokes the notifier service mode hook function.
759 *----------------------------------------------------------------------
764 int mode) /* New service mode: TCL_SERVICE_ALL or
765 * TCL_SERVICE_NONE */
768 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
770 oldMode = tsdPtr->serviceMode;
771 tsdPtr->serviceMode = mode;
772 Tcl_ServiceModeHook(mode);
777 *----------------------------------------------------------------------
779 * Tcl_SetMaxBlockTime --
781 * This function is invoked by event sources to tell the notifier how
782 * long it may block the next time it blocks. The timePtr argument gives
783 * a maximum time; the actual time may be less if some other event source
784 * requested a smaller time.
790 * May reduce the length of the next sleep in the tsdPtr->
792 *----------------------------------------------------------------------
797 const Tcl_Time *timePtr) /* Specifies a maximum elapsed time for the
798 * next blocking operation in the event
801 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
803 if (!tsdPtr->blockTimeSet || (timePtr->sec < tsdPtr->blockTime.sec)
804 || ((timePtr->sec == tsdPtr->blockTime.sec)
805 && (timePtr->usec < tsdPtr->blockTime.usec))) {
806 tsdPtr->blockTime = *timePtr;
807 tsdPtr->blockTimeSet = 1;
811 * If we are called outside an event source traversal, set the timeout
815 if (!tsdPtr->inTraversal) {
816 Tcl_SetTimer(&tsdPtr->blockTime);
821 *----------------------------------------------------------------------
825 * Process a single event of some sort. If there's no work to do, wait
826 * for an event to occur, then process it.
829 * The return value is 1 if the function actually found an event to
830 * process. If no processing occurred, then 0 is returned (this can
831 * happen if the TCL_DONT_WAIT flag is set or if there are no event
832 * handlers to wait for in the set specified by flags).
835 * May delay execution of process while waiting for an event, unless
836 * TCL_DONT_WAIT is set in the flags argument. Event sources are invoked
837 * to check for and queue events. Event handlers may produce arbitrary
840 *----------------------------------------------------------------------
845 int flags) /* Miscellaneous flag values: may be any
846 * combination of TCL_DONT_WAIT,
847 * TCL_WINDOW_EVENTS, TCL_FILE_EVENTS,
848 * TCL_TIMER_EVENTS, TCL_IDLE_EVENTS, or
849 * others defined by event sources. */
851 int result = 0, oldMode;
852 EventSource *sourcePtr;
854 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
857 * The first thing we do is to service any asynchronous event handlers.
860 if (Tcl_AsyncReady()) {
861 (void) Tcl_AsyncInvoke(NULL, 0);
866 * No event flags is equivalent to TCL_ALL_EVENTS.
869 if ((flags & TCL_ALL_EVENTS) == 0) {
870 flags |= TCL_ALL_EVENTS;
874 * Set the service mode to none so notifier event routines won't try to
875 * service events recursively.
878 oldMode = tsdPtr->serviceMode;
879 tsdPtr->serviceMode = TCL_SERVICE_NONE;
882 * The core of this function is an infinite loop, even though we only
883 * service one event. The reason for this is that we may be processing
884 * events that don't do anything inside of Tcl.
889 * If idle events are the only things to service, skip the main part
890 * of the loop and go directly to handle idle events (i.e. don't wait
891 * even if TCL_DONT_WAIT isn't set).
894 if ((flags & TCL_ALL_EVENTS) == TCL_IDLE_EVENTS) {
895 flags = TCL_IDLE_EVENTS | TCL_DONT_WAIT;
900 * Ask Tcl to service a queued event, if there are any.
903 if (Tcl_ServiceEvent(flags)) {
909 * If TCL_DONT_WAIT is set, be sure to poll rather than blocking,
910 * otherwise reset the block time to infinity.
913 if (flags & TCL_DONT_WAIT) {
914 tsdPtr->blockTime.sec = 0;
915 tsdPtr->blockTime.usec = 0;
916 tsdPtr->blockTimeSet = 1;
918 tsdPtr->blockTimeSet = 0;
922 * Set up all the event sources for new events. This will cause the
923 * block time to be updated if necessary.
926 tsdPtr->inTraversal = 1;
927 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
928 sourcePtr = sourcePtr->nextPtr) {
929 if (sourcePtr->setupProc) {
930 sourcePtr->setupProc(sourcePtr->clientData, flags);
933 tsdPtr->inTraversal = 0;
935 if ((flags & TCL_DONT_WAIT) || tsdPtr->blockTimeSet) {
936 timePtr = &tsdPtr->blockTime;
942 * Wait for a new event or a timeout. If Tcl_WaitForEvent returns -1,
943 * we should abort Tcl_DoOneEvent.
946 result = Tcl_WaitForEvent(timePtr);
953 * Check all the event sources for new events.
956 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
957 sourcePtr = sourcePtr->nextPtr) {
958 if (sourcePtr->checkProc) {
959 sourcePtr->checkProc(sourcePtr->clientData, flags);
964 * Check for events queued by the notifier or event sources.
967 if (Tcl_ServiceEvent(flags)) {
973 * We've tried everything at this point, but nobody we know about had
974 * anything to do. Check for idle events. If none, either quit or go
975 * back to the top and try again.
979 if (flags & TCL_IDLE_EVENTS) {
980 if (TclServiceIdle()) {
985 if (flags & TCL_DONT_WAIT) {
990 * If Tcl_WaitForEvent has returned 1, indicating that one system
991 * event has been dispatched (and thus that some Tcl code might have
992 * been indirectly executed), we break out of the loop. We do this to
993 * give VwaitCmd for instance a chance to check if that system event
994 * had the side effect of changing the variable (so the vwait can
995 * return and unwind properly).
997 * NB: We will process idle events if any first, because otherwise we
998 * might never do the idle events if the notifier always gets
1007 tsdPtr->serviceMode = oldMode;
1012 *----------------------------------------------------------------------
1016 * This routine checks all of the event sources, processes events that
1017 * are on the Tcl event queue, and then calls the any idle handlers.
1018 * Platform specific notifier callbacks that generate events should call
1019 * this routine before returning to the system in order to ensure that
1020 * Tcl gets a chance to process the new events.
1023 * Returns 1 if an event or idle handler was invoked, else 0.
1026 * Anything that an event or idle handler may do.
1028 *----------------------------------------------------------------------
1032 Tcl_ServiceAll(void)
1035 EventSource *sourcePtr;
1036 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
1038 if (tsdPtr->serviceMode == TCL_SERVICE_NONE) {
1043 * We need to turn off event servicing like we to in Tcl_DoOneEvent, to
1044 * avoid recursive calls.
1047 tsdPtr->serviceMode = TCL_SERVICE_NONE;
1050 * Check async handlers first.
1053 if (Tcl_AsyncReady()) {
1054 (void) Tcl_AsyncInvoke(NULL, 0);
1058 * Make a single pass through all event sources, queued events, and idle
1059 * handlers. Note that we wait to update the notifier timer until the end
1060 * so we can avoid multiple changes.
1063 tsdPtr->inTraversal = 1;
1064 tsdPtr->blockTimeSet = 0;
1066 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
1067 sourcePtr = sourcePtr->nextPtr) {
1068 if (sourcePtr->setupProc) {
1069 sourcePtr->setupProc(sourcePtr->clientData, TCL_ALL_EVENTS);
1072 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
1073 sourcePtr = sourcePtr->nextPtr) {
1074 if (sourcePtr->checkProc) {
1075 sourcePtr->checkProc(sourcePtr->clientData, TCL_ALL_EVENTS);
1079 while (Tcl_ServiceEvent(0)) {
1082 if (TclServiceIdle()) {
1086 if (!tsdPtr->blockTimeSet) {
1089 Tcl_SetTimer(&tsdPtr->blockTime);
1091 tsdPtr->inTraversal = 0;
1092 tsdPtr->serviceMode = TCL_SERVICE_ALL;
1097 *----------------------------------------------------------------------
1099 * Tcl_ThreadAlert --
1101 * This function wakes up the notifier associated with the specified
1102 * thread (if there is one).
1110 *----------------------------------------------------------------------
1115 Tcl_ThreadId threadId) /* Identifier for thread to use. */
1117 ThreadSpecificData *tsdPtr;
1120 * Find the notifier associated with the specified thread. Note that we
1121 * need to hold the listLock while calling Tcl_AlertNotifier to avoid a
1122 * race condition where the specified thread might destroy its notifier.
1125 Tcl_MutexLock(&listLock);
1126 for (tsdPtr = firstNotifierPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
1127 if (tsdPtr->threadId == threadId) {
1128 Tcl_AlertNotifier(tsdPtr->clientData);
1132 Tcl_MutexUnlock(&listLock);