Debian Codegraph @

pTk/mTk/tclGeneric/tclNotify.c

raw symbols SCIP
1 /*
2 * tclNotify.c --
3 *
4 * This file implements the generic portion of the Tcl notifier.
5 * The notifier is lowest-level part of the event system. It
6 * manages an event queue that holds Tcl_Event structures. The
7 * platform specific portion of the notifier is defined in the
8 * tcl*Notify.c files in each platform directory.
9 *
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.
13 *
14 * See the file "license.terms" for information on usage and redistribution
15 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16 *
17 * RCS: @(#) $Id: tclNotify.c,v 1.11 2003/02/15 20:24:10 kennykb Exp $
18 */
19 #include "tkPort.h"
20 #include "Lang.h"
21
22 #ifdef TCL_EVENT_IMPLEMENT
23
24 #ifndef _LANG
25 extern TclStubs tclStubs;
26 #else
27 #include "tkEvent_f.h"
28 #endif
29
30 /*
31 * For each event source (created with Tcl_CreateEventSource) there
32 * is a structure of the following type:
33 */
34
35 typedef struct EventSource {
36 Tcl_EventSetupProc *setupProc;
37 Tcl_EventCheckProc *checkProc;
38 ClientData clientData;
39 struct EventSource *nextPtr;
40 } EventSource;
41
42 /*
43 * The following structure keeps track of the state of the notifier on a
44 * per-thread basis. The first three elements keep track of the event queue.
45 * In addition to the first (next to be serviced) and last events in the queue,
46 * we keep track of a "marker" event. This provides a simple priority
47 * mechanism whereby events can be inserted at the front of the queue but
48 * behind all other high-priority events already in the queue (this is used for
49 * things like a sequence of Enter and Leave events generated during a grab in
50 * Tk). These elements are protected by the queueMutex so that any thread
51 * can queue an event on any notifier. Note that all of the values in this
52 * structure will be initialized to 0.
53 */
54
55 typedef struct ThreadSpecificData {
56 Tcl_Event *firstEventPtr; /* First pending event, or NULL if none. */
57 Tcl_Event *lastEventPtr; /* Last pending event, or NULL if none. */
58 Tcl_Event *markerEventPtr; /* Last high-priority event in queue, or
59 * NULL if none. */
60 Tcl_Mutex queueMutex; /* Mutex to protect access to the previous
61 * three fields. */
62 int serviceMode; /* One of TCL_SERVICE_NONE or
63 * TCL_SERVICE_ALL. */
64 int blockTimeSet; /* 0 means there is no maximum block
65 * time: block forever. */
66 Tcl_Time blockTime; /* If blockTimeSet is 1, gives the
67 * maximum elapsed time for the next block. */
68 int inTraversal; /* 1 if Tcl_SetMaxBlockTime is being
69 * called during an event source traversal. */
70 EventSource *firstEventSourcePtr;
71 /* Pointer to first event source in
72 * list of event sources for this thread. */
73 Tcl_ThreadId threadId; /* Thread that owns this notifier instance. */
74 ClientData clientData; /* Opaque handle for platform specific
75 * notifier. */
76 struct ThreadSpecificData *nextPtr;
77 /* Next notifier in global list of notifiers.
78 * Access is controlled by the listLock global
79 * mutex. */
80 } ThreadSpecificData;
81
82 static Tcl_ThreadDataKey dataKey;
83
84 /*
85 * Global list of notifiers. Access to this list is controlled by the
86 * listLock mutex. If this becomes a performance bottleneck, this could
87 * be replaced with a hashtable.
88 */
89
90 static ThreadSpecificData *firstNotifierPtr;
91 TCL_DECLARE_MUTEX(listLock)
92
93 /*
94 * Declarations for routines used only in this file.
95 */
96
97 static void QueueEvent _ANSI_ARGS_((ThreadSpecificData *tsdPtr,
98 Tcl_Event* evPtr, Tcl_QueuePosition position));
99
100 /*
101 *----------------------------------------------------------------------
102 *
103 * TclInitNotifier --
104 *
105 * Initialize the thread local data structures for the notifier
106 * subsystem.
107 *
108 * Results:
109 * None.
110 *
111 * Side effects:
112 * Adds the current thread to the global list of notifiers.
113 *
114 *----------------------------------------------------------------------
115 */
116
117 void
118 TclInitNotifier()
119 {
120 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
121
122 Tcl_MutexLock(&listLock);
123
124 tsdPtr->threadId = Tcl_GetCurrentThread();
125 tsdPtr->clientData = tclStubs.tcl_InitNotifier();
126 tsdPtr->nextPtr = firstNotifierPtr;
127 firstNotifierPtr = tsdPtr;
128
129 Tcl_MutexUnlock(&listLock);
130 }
131
132 /*
133 *----------------------------------------------------------------------
134 *
135 * TclFinalizeNotifier --
136 *
137 * Finalize the thread local data structures for the notifier
138 * subsystem.
139 *
140 * Results:
141 * None.
142 *
143 * Side effects:
144 * Removes the notifier associated with the current thread from
145 * the global notifier list.
146 *
147 *----------------------------------------------------------------------
148 */
149
150 void
151 TclFinalizeNotifier()
152 {
153 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
154 ThreadSpecificData **prevPtrPtr;
155 Tcl_Event *evPtr, *hold;
156
157 Tcl_MutexLock(&(tsdPtr->queueMutex));
158 for (evPtr = tsdPtr->firstEventPtr; evPtr != (Tcl_Event *) NULL; ) {
159 hold = evPtr;
160 evPtr = evPtr->nextPtr;
161 ckfree((char *) hold);
162 }
163 tsdPtr->firstEventPtr = NULL;
164 tsdPtr->lastEventPtr = NULL;
165 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
166
167 Tcl_MutexLock(&listLock);
168
169 if (tclStubs.tcl_FinalizeNotifier) {
170 tclStubs.tcl_FinalizeNotifier(tsdPtr->clientData);
171 }
172 Tcl_MutexFinalize(&(tsdPtr->queueMutex));
173 for (prevPtrPtr = &firstNotifierPtr; *prevPtrPtr != NULL;
174 prevPtrPtr = &((*prevPtrPtr)->nextPtr)) {
175 if (*prevPtrPtr == tsdPtr) {
176 *prevPtrPtr = tsdPtr->nextPtr;
177 break;
178 }
179 }
180
181 Tcl_MutexUnlock(&listLock);
182 }
183
184 /*
185 *----------------------------------------------------------------------
186 *
187 * Tcl_SetNotifier --
188 *
189 * Install a set of alternate functions for use with the notifier.
190 * In particular, this can be used to install the Xt-based
191 * notifier for use with the Browser plugin.
192 *
193 * Results:
194 * None.
195 *
196 * Side effects:
197 * Overstomps part of the stub vector. This relies on hooks
198 * added to the default procedures in case those are called
199 * directly (i.e., not through the stub table.)
200 *
201 *----------------------------------------------------------------------
202 */
203
204 void
205 Tcl_SetNotifier(notifierProcPtr)
206 Tcl_NotifierProcs *notifierProcPtr;
207 {
208 #if !defined(__WIN32__) && !defined(MAC_TCL) /* UNIX */
209 tclStubs.tcl_CreateFileHandler = notifierProcPtr->createFileHandlerProc;
210 tclStubs.tcl_DeleteFileHandler = notifierProcPtr->deleteFileHandlerProc;
211 #endif
212 tclStubs.tcl_SetTimer = notifierProcPtr->setTimerProc;
213 tclStubs.tcl_WaitForEvent = notifierProcPtr->waitForEventProc;
214 tclStubs.tcl_InitNotifier = notifierProcPtr->initNotifierProc;
215 tclStubs.tcl_FinalizeNotifier = notifierProcPtr->finalizeNotifierProc;
216 tclStubs.tcl_AlertNotifier = notifierProcPtr->alertNotifierProc;
217 tclStubs.tcl_ServiceModeHook = notifierProcPtr->serviceModeHookProc;
218 }
219
220 /*
221 *----------------------------------------------------------------------
222 *
223 * Tcl_CreateEventSource --
224 *
225 * This procedure is invoked to create a new source of events.
226 * The source is identified by a procedure that gets invoked
227 * during Tcl_DoOneEvent to check for events on that source
228 * and queue them.
229 *
230 *
231 * Results:
232 * None.
233 *
234 * Side effects:
235 * SetupProc and checkProc will be invoked each time that Tcl_DoOneEvent
236 * runs out of things to do. SetupProc will be invoked before
237 * Tcl_DoOneEvent calls select or whatever else it uses to wait
238 * for events. SetupProc typically calls functions like
239 * Tcl_SetMaxBlockTime to indicate what to wait for.
240 *
241 * CheckProc is called after select or whatever operation was actually
242 * used to wait. It figures out whether anything interesting actually
243 * happened (e.g. by calling Tcl_AsyncReady), and then calls
244 * Tcl_QueueEvent to queue any events that are ready.
245 *
246 * Each of these procedures is passed two arguments, e.g.
247 * (*checkProc)(ClientData clientData, int flags));
248 * ClientData is the same as the clientData argument here, and flags
249 * is a combination of things like TCL_FILE_EVENTS that indicates
250 * what events are of interest: setupProc and checkProc use flags
251 * to figure out whether their events are relevant or not.
252 *
253 *----------------------------------------------------------------------
254 */
255
256 void
257 Tcl_CreateEventSource(setupProc, checkProc, clientData)
258 Tcl_EventSetupProc *setupProc; /* Procedure to invoke to figure out
259 * what to wait for. */
260 Tcl_EventCheckProc *checkProc; /* Procedure to call after waiting
261 * to see what happened. */
262 ClientData clientData; /* One-word argument to pass to
263 * setupProc and checkProc. */
264 {
265 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
266 EventSource *sourcePtr = (EventSource *) ckalloc(sizeof(EventSource));
267
268 sourcePtr->setupProc = setupProc;
269 sourcePtr->checkProc = checkProc;
270 sourcePtr->clientData = clientData;
271 sourcePtr->nextPtr = tsdPtr->firstEventSourcePtr;
272 tsdPtr->firstEventSourcePtr = sourcePtr;
273 }
274
275 /*
276 *----------------------------------------------------------------------
277 *
278 * Tcl_DeleteEventSource --
279 *
280 * This procedure is invoked to delete the source of events
281 * given by proc and clientData.
282 *
283 * Results:
284 * None.
285 *
286 * Side effects:
287 * The given event source is cancelled, so its procedure will
288 * never again be called. If no such source exists, nothing
289 * happens.
290 *
291 *----------------------------------------------------------------------
292 */
293
294 void
295 Tcl_DeleteEventSource(setupProc, checkProc, clientData)
296 Tcl_EventSetupProc *setupProc; /* Procedure to invoke to figure out
297 * what to wait for. */
298 Tcl_EventCheckProc *checkProc; /* Procedure to call after waiting
299 * to see what happened. */
300 ClientData clientData; /* One-word argument to pass to
301 * setupProc and checkProc. */
302 {
303 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
304 EventSource *sourcePtr, *prevPtr;
305
306 for (sourcePtr = tsdPtr->firstEventSourcePtr, prevPtr = NULL;
307 sourcePtr != NULL;
308 prevPtr = sourcePtr, sourcePtr = sourcePtr->nextPtr) {
309 if ((sourcePtr->setupProc != setupProc)
310 || (sourcePtr->checkProc != checkProc)
311 || (sourcePtr->clientData != clientData)) {
312 continue;
313 }
314 if (prevPtr == NULL) {
315 tsdPtr->firstEventSourcePtr = sourcePtr->nextPtr;
316 } else {
317 prevPtr->nextPtr = sourcePtr->nextPtr;
318 }
319 ckfree((char *) sourcePtr);
320 return;
321 }
322 }
323
324 /*
325 *----------------------------------------------------------------------
326 *
327 * Tcl_QueueEvent --
328 *
329 * Queue an event on the event queue associated with the
330 * current thread.
331 *
332 * Results:
333 * None.
334 *
335 * Side effects:
336 * None.
337 *
338 *----------------------------------------------------------------------
339 */
340
341 void
342 Tcl_QueueEvent(evPtr, position)
343 Tcl_Event* evPtr; /* Event to add to queue. The storage
344 * space must have been allocated the caller
345 * with malloc (ckalloc), and it becomes
346 * the property of the event queue. It
347 * will be freed after the event has been
348 * handled. */
349 Tcl_QueuePosition position; /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
350 * TCL_QUEUE_MARK. */
351 {
352 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
353 QueueEvent(tsdPtr, evPtr, position);
354 }
355
356 /*
357 *----------------------------------------------------------------------
358 *
359 * Tcl_ThreadQueueEvent --
360 *
361 * Queue an event on the specified thread's event queue.
362 *
363 * Results:
364 * None.
365 *
366 * Side effects:
367 * None.
368 *
369 *----------------------------------------------------------------------
370 */
371
372 void
373 Tcl_ThreadQueueEvent(threadId, evPtr, position)
374 Tcl_ThreadId threadId; /* Identifier for thread to use. */
375 Tcl_Event* evPtr; /* Event to add to queue. The storage
376 * space must have been allocated the caller
377 * with malloc (ckalloc), and it becomes
378 * the property of the event queue. It
379 * will be freed after the event has been
380 * handled. */
381 Tcl_QueuePosition position; /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
382 * TCL_QUEUE_MARK. */
383 {
384 ThreadSpecificData *tsdPtr;
385
386 /*
387 * Find the notifier associated with the specified thread.
388 */
389
390 Tcl_MutexLock(&listLock);
391 for (tsdPtr = firstNotifierPtr; tsdPtr && tsdPtr->threadId != threadId;
392 tsdPtr = tsdPtr->nextPtr) {
393 /* Empty loop body. */
394 }
395
396 /*
397 * Queue the event if there was a notifier associated with the thread.
398 */
399
400 if (tsdPtr) {
401 QueueEvent(tsdPtr, evPtr, position);
402 }
403 Tcl_MutexUnlock(&listLock);
404 }
405
406 /*
407 *----------------------------------------------------------------------
408 *
409 * QueueEvent --
410 *
411 * Insert an event into the specified thread's event queue at one
412 * of three positions: the head, the tail, or before a floating
413 * marker. Events inserted before the marker will be processed in
414 * first-in-first-out order, but before any events inserted at
415 * the tail of the queue. Events inserted at the head of the
416 * queue will be processed in last-in-first-out order.
417 *
418 * Results:
419 * None.
420 *
421 * Side effects:
422 * None.
423 *
424 *----------------------------------------------------------------------
425 */
426
427 static void
428 QueueEvent(tsdPtr, evPtr, position)
429 ThreadSpecificData *tsdPtr; /* Handle to thread local data that indicates
430 * which event queue to use. */
431 Tcl_Event* evPtr; /* Event to add to queue. The storage
432 * space must have been allocated the caller
433 * with malloc (ckalloc), and it becomes
434 * the property of the event queue. It
435 * will be freed after the event has been
436 * handled. */
437 Tcl_QueuePosition position; /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
438 * TCL_QUEUE_MARK. */
439 {
440 Tcl_MutexLock(&(tsdPtr->queueMutex));
441 if (position == TCL_QUEUE_TAIL) {
442 /*
443 * Append the event on the end of the queue.
444 */
445
446 evPtr->nextPtr = NULL;
447 if (tsdPtr->firstEventPtr == NULL) {
448 tsdPtr->firstEventPtr = evPtr;
449 } else {
450 tsdPtr->lastEventPtr->nextPtr = evPtr;
451 }
452 tsdPtr->lastEventPtr = evPtr;
453 } else if (position == TCL_QUEUE_HEAD) {
454 /*
455 * Push the event on the head of the queue.
456 */
457
458 evPtr->nextPtr = tsdPtr->firstEventPtr;
459 if (tsdPtr->firstEventPtr == NULL) {
460 tsdPtr->lastEventPtr = evPtr;
461 }
462 tsdPtr->firstEventPtr = evPtr;
463 } else if (position == TCL_QUEUE_MARK) {
464 /*
465 * Insert the event after the current marker event and advance
466 * the marker to the new event.
467 */
468
469 if (tsdPtr->markerEventPtr == NULL) {
470 evPtr->nextPtr = tsdPtr->firstEventPtr;
471 tsdPtr->firstEventPtr = evPtr;
472 } else {
473 evPtr->nextPtr = tsdPtr->markerEventPtr->nextPtr;
474 tsdPtr->markerEventPtr->nextPtr = evPtr;
475 }
476 tsdPtr->markerEventPtr = evPtr;
477 if (evPtr->nextPtr == NULL) {
478 tsdPtr->lastEventPtr = evPtr;
479 }
480 }
481 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
482 }
483
484 /*
485 *----------------------------------------------------------------------
486 *
487 * Tcl_DeleteEvents --
488 *
489 * Calls a procedure for each event in the queue and deletes those
490 * for which the procedure returns 1. Events for which the
491 * procedure returns 0 are left in the queue. Operates on the
492 * queue associated with the current thread.
493 *
494 * Results:
495 * None.
496 *
497 * Side effects:
498 * Potentially removes one or more events from the event queue.
499 *
500 *----------------------------------------------------------------------
501 */
502
503 void
504 Tcl_DeleteEvents(proc, clientData)
505 Tcl_EventDeleteProc *proc; /* The procedure to call. */
506 ClientData clientData; /* type-specific data. */
507 {
508 Tcl_Event *evPtr, *prevPtr, *hold;
509 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
510
511 Tcl_MutexLock(&(tsdPtr->queueMutex));
512 for (prevPtr = (Tcl_Event *) NULL, evPtr = tsdPtr->firstEventPtr;
513 evPtr != (Tcl_Event *) NULL;
514 ) {
515 if ((*proc) (evPtr, clientData) == 1) {
516 if (tsdPtr->firstEventPtr == evPtr) {
517 tsdPtr->firstEventPtr = evPtr->nextPtr;
518 } else {
519 prevPtr->nextPtr = evPtr->nextPtr;
520 }
521 if (evPtr->nextPtr == (Tcl_Event *) NULL) {
522 tsdPtr->lastEventPtr = prevPtr;
523 }
524 if (tsdPtr->markerEventPtr == evPtr) {
525 tsdPtr->markerEventPtr = prevPtr;
526 }
527 hold = evPtr;
528 evPtr = evPtr->nextPtr;
529 ckfree((char *) hold);
530 } else {
531 prevPtr = evPtr;
532 evPtr = evPtr->nextPtr;
533 }
534 }
535 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
536 }
537
538 /*
539 *----------------------------------------------------------------------
540 *
541 * Tcl_ServiceEvent --
542 *
543 * Process one event from the event queue, or invoke an
544 * asynchronous event handler. Operates on event queue for
545 * current thread.
546 *
547 * Results:
548 * The return value is 1 if the procedure actually found an event
549 * to process. If no processing occurred, then 0 is returned.
550 *
551 * Side effects:
552 * Invokes all of the event handlers for the highest priority
553 * event in the event queue. May collapse some events into a
554 * single event or discard stale events.
555 *
556 *----------------------------------------------------------------------
557 */
558
559 int
560 Tcl_ServiceEvent(flags)
561 int flags; /* Indicates what events should be processed.
562 * May be any combination of TCL_WINDOW_EVENTS
563 * TCL_FILE_EVENTS, TCL_TIMER_EVENTS, or other
564 * flags defined elsewhere. Events not
565 * matching this will be skipped for processing
566 * later. */
567 {
568 Tcl_Event *evPtr, *prevPtr;
569 Tcl_EventProc *proc;
570 int result;
571 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
572
573 /*
574 * Asynchronous event handlers are considered to be the highest
575 * priority events, and so must be invoked before we process events
576 * on the event queue.
577 */
578
579 if (Tcl_AsyncReady()) {
580 (void) Tcl_AsyncInvoke((Tcl_Interp *) NULL, 0);
581 return 1;
582 }
583
584 /*
585 * No event flags is equivalent to TCL_ALL_EVENTS.
586 */
587
588 if ((flags & TCL_ALL_EVENTS) == 0) {
589 flags |= TCL_ALL_EVENTS;
590 }
591
592 /*
593 * Loop through all the events in the queue until we find one
594 * that can actually be handled.
595 */
596
597 Tcl_MutexLock(&(tsdPtr->queueMutex));
598 for (evPtr = tsdPtr->firstEventPtr; evPtr != NULL;
599 evPtr = evPtr->nextPtr) {
600 /*
601 * Call the handler for the event. If it actually handles the
602 * event then free the storage for the event. There are two
603 * tricky things here, both stemming from the fact that the event
604 * code may be re-entered while servicing the event:
605 *
606 * 1. Set the "proc" field to NULL. This is a signal to ourselves
607 * that we shouldn't reexecute the handler if the event loop
608 * is re-entered.
609 * 2. When freeing the event, must search the queue again from the
610 * front to find it. This is because the event queue could
611 * change almost arbitrarily while handling the event, so we
612 * can't depend on pointers found now still being valid when
613 * the handler returns.
614 */
615
616 proc = evPtr->proc;
617 if (proc == NULL) {
618 continue;
619 }
620 evPtr->proc = NULL;
621
622 /*
623 * Release the lock before calling the event procedure. This
624 * allows other threads to post events if we enter a recursive
625 * event loop in this thread. Note that we are making the assumption
626 * that if the proc returns 0, the event is still in the list.
627 */
628
629 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
630 result = (*proc)(evPtr, flags);
631 Tcl_MutexLock(&(tsdPtr->queueMutex));
632
633 if (result) {
634 /*
635 * The event was processed, so remove it from the queue.
636 */
637
638 if (tsdPtr->firstEventPtr == evPtr) {
639 tsdPtr->firstEventPtr = evPtr->nextPtr;
640 if (evPtr->nextPtr == NULL) {
641 tsdPtr->lastEventPtr = NULL;
642 }
643 if (tsdPtr->markerEventPtr == evPtr) {
644 tsdPtr->markerEventPtr = NULL;
645 }
646 } else {
647 for (prevPtr = tsdPtr->firstEventPtr;
648 prevPtr && prevPtr->nextPtr != evPtr;
649 prevPtr = prevPtr->nextPtr) {
650 /* Empty loop body. */
651 }
652 if (prevPtr) {
653 prevPtr->nextPtr = evPtr->nextPtr;
654 if (evPtr->nextPtr == NULL) {
655 tsdPtr->lastEventPtr = prevPtr;
656 }
657 if (tsdPtr->markerEventPtr == evPtr) {
658 tsdPtr->markerEventPtr = prevPtr;
659 }
660 } else {
661 evPtr = NULL;
662 }
663 }
664 if (evPtr) {
665 ckfree((char *) evPtr);
666 }
667 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
668 return 1;
669 } else {
670 /*
671 * The event wasn't actually handled, so we have to restore
672 * the proc field to allow the event to be attempted again.
673 */
674
675 evPtr->proc = proc;
676 }
677 }
678 Tcl_MutexUnlock(&(tsdPtr->queueMutex));
679 return 0;
680 }
681
682 /*
683 *----------------------------------------------------------------------
684 *
685 * Tcl_GetServiceMode --
686 *
687 * This routine returns the current service mode of the notifier.
688 *
689 * Results:
690 * Returns either TCL_SERVICE_ALL or TCL_SERVICE_NONE.
691 *
692 * Side effects:
693 * None.
694 *
695 *----------------------------------------------------------------------
696 */
697
698 int
699 Tcl_GetServiceMode()
700 {
701 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
702
703 return tsdPtr->serviceMode;
704 }
705
706 /*
707 *----------------------------------------------------------------------
708 *
709 * Tcl_SetServiceMode --
710 *
711 * This routine sets the current service mode of the tsdPtr->
712 *
713 * Results:
714 * Returns the previous service mode.
715 *
716 * Side effects:
717 * Invokes the notifier service mode hook procedure.
718 *
719 *----------------------------------------------------------------------
720 */
721
722 int
723 Tcl_SetServiceMode(mode)
724 int mode; /* New service mode: TCL_SERVICE_ALL or
725 * TCL_SERVICE_NONE */
726 {
727 int oldMode;
728 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
729
730 oldMode = tsdPtr->serviceMode;
731 tsdPtr->serviceMode = mode;
732 if (tclStubs.tcl_ServiceModeHook) {
733 tclStubs.tcl_ServiceModeHook(mode);
734 }
735 return oldMode;
736 }
737
738 /*
739 *----------------------------------------------------------------------
740 *
741 * Tcl_SetMaxBlockTime --
742 *
743 * This procedure is invoked by event sources to tell the notifier
744 * how long it may block the next time it blocks. The timePtr
745 * argument gives a maximum time; the actual time may be less if
746 * some other event source requested a smaller time.
747 *
748 * Results:
749 * None.
750 *
751 * Side effects:
752 * May reduce the length of the next sleep in the tsdPtr->
753 *
754 *----------------------------------------------------------------------
755 */
756
757 void
758 Tcl_SetMaxBlockTime(timePtr)
759 Tcl_Time *timePtr; /* Specifies a maximum elapsed time for
760 * the next blocking operation in the
761 * event tsdPtr-> */
762 {
763 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
764
765 if (!tsdPtr->blockTimeSet || (timePtr->sec < tsdPtr->blockTime.sec)
766 || ((timePtr->sec == tsdPtr->blockTime.sec)
767 && (timePtr->usec < tsdPtr->blockTime.usec))) {
768 tsdPtr->blockTime = *timePtr;
769 tsdPtr->blockTimeSet = 1;
770 }
771
772 /*
773 * If we are called outside an event source traversal, set the
774 * timeout immediately.
775 */
776
777 if (!tsdPtr->inTraversal) {
778 if (tsdPtr->blockTimeSet) {
779 Tcl_SetTimer(&tsdPtr->blockTime);
780 } else {
781 Tcl_SetTimer(NULL);
782 }
783 }
784 }
785
786 /*
787 *----------------------------------------------------------------------
788 *
789 * Tcl_DoOneEvent --
790 *
791 * Process a single event of some sort. If there's no work to
792 * do, wait for an event to occur, then process it.
793 *
794 * Results:
795 * The return value is 1 if the procedure actually found an event
796 * to process. If no processing occurred, then 0 is returned (this
797 * can happen if the TCL_DONT_WAIT flag is set or if there are no
798 * event handlers to wait for in the set specified by flags).
799 *
800 * Side effects:
801 * May delay execution of process while waiting for an event,
802 * unless TCL_DONT_WAIT is set in the flags argument. Event
803 * sources are invoked to check for and queue events. Event
804 * handlers may produce arbitrary side effects.
805 *
806 *----------------------------------------------------------------------
807 */
808
809 int
810 Tcl_DoOneEvent(flags)
811 int flags; /* Miscellaneous flag values: may be any
812 * combination of TCL_DONT_WAIT,
813 * TCL_WINDOW_EVENTS, TCL_FILE_EVENTS,
814 * TCL_TIMER_EVENTS, TCL_IDLE_EVENTS, or
815 * others defined by event sources. */
816 {
817 int result = 0, oldMode;
818 EventSource *sourcePtr;
819 Tcl_Time *timePtr;
820 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
821
822 /*
823 * The first thing we do is to service any asynchronous event
824 * handlers.
825 */
826
827 if (Tcl_AsyncReady()) {
828 (void) Tcl_AsyncInvoke((Tcl_Interp *) NULL, 0);
829 return 1;
830 }
831
832 /*
833 * No event flags is equivalent to TCL_ALL_EVENTS.
834 */
835
836 if ((flags & TCL_ALL_EVENTS) == 0) {
837 flags |= TCL_ALL_EVENTS;
838 }
839
840 /*
841 * Set the service mode to none so notifier event routines won't
842 * try to service events recursively.
843 */
844
845 oldMode = tsdPtr->serviceMode;
846 tsdPtr->serviceMode = TCL_SERVICE_NONE;
847
848 /*
849 * The core of this procedure is an infinite loop, even though
850 * we only service one event. The reason for this is that we
851 * may be processing events that don't do anything inside of Tcl.
852 */
853
854 while (1) {
855
856 /*
857 * If idle events are the only things to service, skip the
858 * main part of the loop and go directly to handle idle
859 * events (i.e. don't wait even if TCL_DONT_WAIT isn't set).
860 */
861
862 if ((flags & TCL_ALL_EVENTS) == TCL_IDLE_EVENTS) {
863 flags = TCL_IDLE_EVENTS|TCL_DONT_WAIT;
864 goto idleEvents;
865 }
866
867 /*
868 * Ask Tcl to service a queued event, if there are any.
869 */
870
871 if (Tcl_ServiceEvent(flags)) {
872 result = 1;
873 break;
874 }
875
876 /*
877 * If TCL_DONT_WAIT is set, be sure to poll rather than
878 * blocking, otherwise reset the block time to infinity.
879 */
880
881 if (flags & TCL_DONT_WAIT) {
882 tsdPtr->blockTime.sec = 0;
883 tsdPtr->blockTime.usec = 0;
884 tsdPtr->blockTimeSet = 1;
885 } else {
886 tsdPtr->blockTimeSet = 0;
887 }
888
889 /*
890 * Set up all the event sources for new events. This will
891 * cause the block time to be updated if necessary.
892 */
893
894 tsdPtr->inTraversal = 1;
895 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
896 sourcePtr = sourcePtr->nextPtr) {
897 if (sourcePtr->setupProc) {
898 (sourcePtr->setupProc)(sourcePtr->clientData, flags);
899 }
900 }
901 tsdPtr->inTraversal = 0;
902
903 if ((flags & TCL_DONT_WAIT) || tsdPtr->blockTimeSet) {
904 timePtr = &tsdPtr->blockTime;
905 } else {
906 timePtr = NULL;
907 }
908
909 /*
910 * Wait for a new event or a timeout. If Tcl_WaitForEvent
911 * returns -1, we should abort Tcl_DoOneEvent.
912 */
913
914 result = Tcl_WaitForEvent(timePtr);
915 if (result < 0) {
916 result = 0;
917 break;
918 }
919
920 /*
921 * Check all the event sources for new events.
922 */
923
924 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
925 sourcePtr = sourcePtr->nextPtr) {
926 if (sourcePtr->checkProc) {
927 (sourcePtr->checkProc)(sourcePtr->clientData, flags);
928 }
929 }
930
931 /*
932 * Check for events queued by the notifier or event sources.
933 */
934
935 if (Tcl_ServiceEvent(flags)) {
936 result = 1;
937 break;
938 }
939
940 /*
941 * We've tried everything at this point, but nobody we know
942 * about had anything to do. Check for idle events. If none,
943 * either quit or go back to the top and try again.
944 */
945
946 idleEvents:
947 if (flags & TCL_IDLE_EVENTS) {
948 if (TclServiceIdle()) {
949 result = 1;
950 break;
951 }
952 }
953 if (flags & TCL_DONT_WAIT) {
954 break;
955 }
956
957 /*
958 * If Tcl_WaitForEvent has returned 1,
959 * indicating that one system event has been dispatched
960 * (and thus that some Tcl code might have been indirectly executed),
961 * we break out of the loop.
962 * We do this to give VwaitCmd for instance a chance to check
963 * if that system event had the side effect of changing the
964 * variable (so the vwait can return and unwind properly).
965 *
966 * NB: We will process idle events if any first, because
967 * otherwise we might never do the idle events if the notifier
968 * always gets system events.
969 */
970
971 if (result) {
972 break;
973 }
974
975 }
976
977 tsdPtr->serviceMode = oldMode;
978 return result;
979 }
980
981 /*
982 *----------------------------------------------------------------------
983 *
984 * Tcl_ServiceAll --
985 *
986 * This routine checks all of the event sources, processes
987 * events that are on the Tcl event queue, and then calls the
988 * any idle handlers. Platform specific notifier callbacks that
989 * generate events should call this routine before returning to
990 * the system in order to ensure that Tcl gets a chance to
991 * process the new events.
992 *
993 * Results:
994 * Returns 1 if an event or idle handler was invoked, else 0.
995 *
996 * Side effects:
997 * Anything that an event or idle handler may do.
998 *
999 *----------------------------------------------------------------------
1000 */
1001
1002 int
1003 Tcl_ServiceAll()
1004 {
1005 int result = 0;
1006 EventSource *sourcePtr;
1007 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
1008
1009 if (tsdPtr->serviceMode == TCL_SERVICE_NONE) {
1010 return result;
1011 }
1012
1013 /*
1014 * We need to turn off event servicing like we to in Tcl_DoOneEvent,
1015 * to avoid recursive calls.
1016 */
1017
1018 tsdPtr->serviceMode = TCL_SERVICE_NONE;
1019
1020 /*
1021 * Check async handlers first.
1022 */
1023
1024 if (Tcl_AsyncReady()) {
1025 (void) Tcl_AsyncInvoke((Tcl_Interp *) NULL, 0);
1026 }
1027
1028 /*
1029 * Make a single pass through all event sources, queued events,
1030 * and idle handlers. Note that we wait to update the notifier
1031 * timer until the end so we can avoid multiple changes.
1032 */
1033
1034 tsdPtr->inTraversal = 1;
1035 tsdPtr->blockTimeSet = 0;
1036
1037 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
1038 sourcePtr = sourcePtr->nextPtr) {
1039 if (sourcePtr->setupProc) {
1040 (sourcePtr->setupProc)(sourcePtr->clientData, TCL_ALL_EVENTS);
1041 }
1042 }
1043 for (sourcePtr = tsdPtr->firstEventSourcePtr; sourcePtr != NULL;
1044 sourcePtr = sourcePtr->nextPtr) {
1045 if (sourcePtr->checkProc) {
1046 (sourcePtr->checkProc)(sourcePtr->clientData, TCL_ALL_EVENTS);
1047 }
1048 }
1049
1050 while (Tcl_ServiceEvent(0)) {
1051 result = 1;
1052 }
1053 if (TclServiceIdle()) {
1054 result = 1;
1055 }
1056
1057 if (!tsdPtr->blockTimeSet) {
1058 Tcl_SetTimer(NULL);
1059 } else {
1060 Tcl_SetTimer(&tsdPtr->blockTime);
1061 }
1062 tsdPtr->inTraversal = 0;
1063 tsdPtr->serviceMode = TCL_SERVICE_ALL;
1064 return result;
1065 }
1066
1067 /*
1068 *----------------------------------------------------------------------
1069 *
1070 * Tcl_ThreadAlert --
1071 *
1072 * This function wakes up the notifier associated with the
1073 * specified thread (if there is one).
1074 *
1075 * Results:
1076 * None.
1077 *
1078 * Side effects:
1079 * None.
1080 *
1081 *----------------------------------------------------------------------
1082 */
1083
1084 void
1085 Tcl_ThreadAlert(threadId)
1086 Tcl_ThreadId threadId; /* Identifier for thread to use. */
1087 {
1088 ThreadSpecificData *tsdPtr;
1089
1090 /*
1091 * Find the notifier associated with the specified thread.
1092 * Note that we need to hold the listLock while calling
1093 * Tcl_AlertNotifier to avoid a race condition where
1094 * the specified thread might destroy its notifier.
1095 */
1096
1097 Tcl_MutexLock(&listLock);
1098 for (tsdPtr = firstNotifierPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
1099 if (tsdPtr->threadId == threadId) {
1100 if (tclStubs.tcl_AlertNotifier) {
1101 tclStubs.tcl_AlertNotifier(tsdPtr->clientData);
1102 }
1103 break;
1104 }
1105 }
1106 Tcl_MutexUnlock(&listLock);
1107 }
1108
1109
1110
1111 void
1112 Tcl_QueueProcEvent(proc, evPtr, position)
1113 Tcl_EventProc *proc;
1114 Tcl_Event* evPtr; /* Event to add to queue. The storage
1115 * space must have been allocated the caller
1116 * with malloc (ckalloc), and it becomes
1117 * the property of the event queue. It
1118 * will be freed after the event has been
1119 * handled. */
1120 Tcl_QueuePosition position; /* One of TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
1121 * TCL_QUEUE_MARK. */
1122 {
1123 evPtr->proc = proc;
1124 Tcl_QueueEvent(evPtr, position);
1125 }
1126
1127
1128
1129 #endif /* TCL_EVENT_IMPLEMENT */