1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
-
-
-
+
+
+
-
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
+
+
-
+
-
+
-
-
-
+
+
+
-
-
+
+
-
-
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
-
+
-
-
-
-
+
+
+
+
+
|
/*
* tclUnixNotify.c --
*
* This file contains the implementation of the select-based
* Unix-specific notifier, which is the lowest-level part
* of the Tcl event loop. This file works together with
* This file contains the implementation of the select()-based
* Unix-specific notifier, which is the lowest-level part of the Tcl
* event loop. This file works together with generic/tclNotify.c.
* generic/tclNotify.c.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclUnixNotfy.c,v 1.12.2.11 2005/07/05 15:09:13 dgp Exp $
* RCS: @(#) $Id: tclUnixNotfy.c,v 1.12.2.12 2005/07/26 04:12:33 dgp Exp $
*/
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier
* is in tclMacOSXNotify.c */
#ifndef HAVE_COREFOUNDATION /* Darwin/Mac OS X CoreFoundation notifier is
* in tclMacOSXNotify.c */
#include "tclInt.h"
#include <signal.h>
/*
* This code does deep stub magic to allow replacement of the notifier at
* runtime.
*/
extern TclStubs tclStubs;
extern Tcl_NotifierProcs tclOriginalNotifier;
/*
* This structure is used to keep track of the notifier info for a
* a registered file.
* This structure is used to keep track of the notifier info for a registered
* file.
*/
typedef struct FileHandler {
int fd;
int mask; /* Mask of desired events: TCL_READABLE,
* etc. */
int readyMask; /* Mask of events that have been seen since the
* last time file handlers were invoked for
* this file. */
Tcl_FileProc *proc; /* Procedure to call, in the style of
int readyMask; /* Mask of events that have been seen since
* the last time file handlers were invoked
* for this file. */
Tcl_FileProc *proc; /* Function to call, in the style of
* Tcl_CreateFileHandler. */
ClientData clientData; /* Argument to pass to proc. */
struct FileHandler *nextPtr;/* Next in list of all files we care about. */
} FileHandler;
/*
* The following structure is what is added to the Tcl event queue when
* file handlers are ready to fire.
* The following structure is what is added to the Tcl event queue when file
* handlers are ready to fire.
*/
typedef struct FileHandlerEvent {
Tcl_Event header; /* Information that is standard for
* all events. */
int fd; /* File descriptor that is ready. Used
* to find the FileHandler structure for
* the file (can't point directly to the
* FileHandler structure because it could
* go away while the event is queued). */
Tcl_Event header; /* Information that is standard for all
* events. */
int fd; /* File descriptor that is ready. Used to find
* the FileHandler structure for the file
* (can't point directly to the FileHandler
* structure because it could go away while
* the event is queued). */
} FileHandlerEvent;
/*
*
* The following structure contains a set of select() masks to track
* readable, writable, and exceptional conditions.
* The following structure contains a set of select() masks to track readable,
* writable, and exceptional conditions.
*/
typedef struct SelectMasks {
fd_set readable;
fd_set writable;
fd_set exceptional;
} SelectMasks;
/*
* The following static structure contains the state information for the
* select based implementation of the Tcl notifier. One of these structures
* is created for each thread that is using the notifier.
* select based implementation of the Tcl notifier. One of these structures is
* created for each thread that is using the notifier.
*/
typedef struct ThreadSpecificData {
FileHandler *firstFileHandlerPtr;
/* Pointer to head of file handler list. */
SelectMasks checkMasks; /* This structure is used to build up the masks
* to be used in the next call to select.
* Bits are set in response to calls to
* Tcl_CreateFileHandler. */
SelectMasks checkMasks; /* This structure is used to build up the
* masks to be used in the next call to
* select. Bits are set in response to calls
* to Tcl_CreateFileHandler. */
SelectMasks readyMasks; /* This array reflects the readable/writable
* conditions that were found to exist by the
* last call to select. */
int numFdBits; /* Number of valid bits in checkMasks
* (one more than highest fd for which
int numFdBits; /* Number of valid bits in checkMasks (one
* more than highest fd for which
* Tcl_WatchFile has been called). */
#ifdef TCL_THREADS
int onList; /* True if it is in this list */
unsigned int pollState; /* pollState is used to implement a polling
unsigned int pollState; /* pollState is used to implement a polling
* handshake between each thread and the
* notifier thread. Bits defined below. */
struct ThreadSpecificData *nextPtr, *prevPtr;
/* All threads that are currently waiting on
/* All threads that are currently waiting on
* an event have their ThreadSpecificData
* structure on a doubly-linked listed formed
* from these pointers. You must hold the
* notifierMutex lock before accessing these
* fields. */
Tcl_Condition waitCV; /* Any other thread alerts a notifier
* that an event is ready to be processed
* by signaling this condition variable. */
Tcl_Condition waitCV; /* Any other thread alerts a notifier that an
* event is ready to be processed by signaling
* this condition variable. */
int eventReady; /* True if an event is ready to be processed.
* Used as condition flag together with
* waitCV above. */
* Used as condition flag together with waitCV
* above. */
#endif
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
#ifdef TCL_THREADS
/*
* The following static indicates the number of threads that have
* initialized notifiers.
* The following static indicates the number of threads that have initialized
* notifiers.
*
* You must hold the notifierMutex lock before accessing this variable.
*/
static int notifierCount = 0;
/*
* The following variable points to the head of a doubly-linked list of
* of ThreadSpecificData structures for all threads that are currently
* waiting on an event.
* The following variable points to the head of a doubly-linked list of
* ThreadSpecificData structures for all threads that are currently waiting on
* an event.
*
* You must hold the notifierMutex lock before accessing this list.
*/
static ThreadSpecificData *waitingListPtr = NULL;
/*
* The notifier thread spends all its time in select() waiting for a
* file descriptor associated with one of the threads on the waitingListPtr
* list to do something interesting. But if the contents of the
* waitingListPtr list ever changes, we need to wake up and restart
* the select() system call. You can wake up the notifier thread by
* The notifier thread spends all its time in select() waiting for a file
* descriptor associated with one of the threads on the waitingListPtr list to
* do something interesting. But if the contents of the waitingListPtr list
* ever changes, we need to wake up and restart the select() system call. You
* can wake up the notifier thread by writing a single byte to the file
* writing a single byte to the file descriptor defined below. This
* file descriptor is the input-end of a pipe and the notifier thread is
* listening for data on the output-end of the same pipe. Hence writing
* to this file descriptor will cause the select() system call to return
* and wake up the notifier thread.
* descriptor defined below. This file descriptor is the input-end of a pipe
* and the notifier thread is listening for data on the output-end of the same
* pipe. Hence writing to this file descriptor will cause the select() system
* call to return and wake up the notifier thread.
*
* You must hold the notifierMutex lock before accessing this list.
*/
static int triggerPipe = -1;
/*
* The notifierMutex locks access to all of the global notifier state.
* The notifierMutex locks access to all of the global notifier state.
*/
TCL_DECLARE_MUTEX(notifierMutex)
/*
* The notifier thread signals the notifierCV when it has finished
* initializing the triggerPipe and right before the notifier
* thread terminates.
* initializing the triggerPipe and right before the notifier thread
* terminates.
*/
static Tcl_Condition notifierCV;
/*
* The pollState bits
* The pollState bits:
* POLL_WANT is set by each thread before it waits on its condition
* variable. It is checked by the notifier before it does
* variable. It is checked by the notifier before it does select.
* select.
* POLL_DONE is set by the notifier if it goes into select after
* seeing POLL_WANT. The idea is to ensure it tries a select
* with the same bits the initial thread had set.
* POLL_DONE is set by the notifier if it goes into select after seeing
* POLL_WANT. The idea is to ensure it tries a select with the
* same bits the initial thread had set.
*/
#define POLL_WANT 0x1
#define POLL_DONE 0x2
/*
* This is the thread ID of the notifier thread that does select.
*/
static Tcl_ThreadId notifierThread;
#endif
/*
* Static routines defined in this file.
*/
|
| ︙ | | |
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
-
-
+
+
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
|
}
/*
*----------------------------------------------------------------------
*
* Tcl_FinalizeNotifier --
*
* This function is called to cleanup the notifier state before
* a thread is terminated.
* This function is called to cleanup the notifier state before a thread
* is terminated.
*
* Results:
* None.
*
* Side effects:
* May terminate the background notifier thread if this is the
* last notifier instance.
* May terminate the background notifier thread if this is the last
* notifier instance.
*
*----------------------------------------------------------------------
*/
void
Tcl_FinalizeNotifier(clientData)
ClientData clientData; /* Not used. */
{
#ifdef TCL_THREADS
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
Tcl_MutexLock(¬ifierMutex);
notifierCount--;
/*
* If this is the last thread to use the notifier, close the notifier
* pipe and wait for the background thread to terminate.
* If this is the last thread to use the notifier, close the notifier pipe
* and wait for the background thread to terminate.
*/
if (notifierCount == 0) {
int result;
if (triggerPipe < 0) {
Tcl_Panic("Tcl_FinalizeNotifier: notifier pipe not initialized");
}
/*
* Send "q" message to the notifier thread so that it will
* terminate. The notifier will return from its call to select()
* and notice that a "q" message has arrived, it will then close
* its side of the pipe and terminate its thread. Note the we can
* not just close the pipe and check for EOF in the notifier
* thread because if a background child process was created with
* exec, select() would not register the EOF on the pipe until the
* child processes had terminated. [Bug: 4139] [Bug: 1222872]
* Send "q" message to the notifier thread so that it will terminate.
* The notifier will return from its call to select() and notice that
* a "q" message has arrived, it will then close its side of the pipe
* and terminate its thread. Note the we can not just close the pipe
* and check for EOF in the notifier thread because if a background
* child process was created with exec, select() would not register
* the EOF on the pipe until the child processes had terminated. [Bug:
* 4139] [Bug: 1222872]
*/
write(triggerPipe, "q", 1);
close(triggerPipe);
while(triggerPipe >= 0) {
Tcl_ConditionWait(¬ifierCV, ¬ifierMutex, NULL);
}
|
| ︙ | | |
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
-
-
+
+
-
-
-
+
+
-
+
-
|
}
/*
*----------------------------------------------------------------------
*
* Tcl_AlertNotifier --
*
* Wake up the specified notifier from any thread. This routine
* is called by the platform independent notifier code whenever
* Wake up the specified notifier from any thread. This routine is called
* by the platform independent notifier code whenever the Tcl_ThreadAlert
* the Tcl_ThreadAlert routine is called. This routine is
* guaranteed not to be called on a given notifier after
* Tcl_FinalizeNotifier is called for that notifier.
* routine is called. This routine is guaranteed not to be called on a
* given notifier after Tcl_FinalizeNotifier is called for that notifier.
*
* Results:
* None.
*
* Side effects:
* Signals the notifier condition variable for the specified
* Signals the notifier condition variable for the specified notifier.
* notifier.
*
*----------------------------------------------------------------------
*/
void
Tcl_AlertNotifier(clientData)
ClientData clientData;
|
| ︙ | | |
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
-
-
-
+
+
+
-
-
-
+
+
+
|
}
/*
*----------------------------------------------------------------------
*
* Tcl_SetTimer --
*
* This procedure sets the current notifier timer value. This
* interface is not implemented in this notifier because we are
* always running inside of Tcl_DoOneEvent.
* This function sets the current notifier timer value. This interface is
* not implemented in this notifier because we are always running inside
* of Tcl_DoOneEvent.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Tcl_SetTimer(timePtr)
Tcl_Time *timePtr; /* Timeout value, may be NULL. */
{
/*
* The interval timer doesn't do anything in this implementation,
* because the only event loop is via Tcl_DoOneEvent, which passes
* timeout values to Tcl_WaitForEvent.
* The interval timer doesn't do anything in this implementation, because
* the only event loop is via Tcl_DoOneEvent, which passes timeout values
* to Tcl_WaitForEvent.
*/
if (tclStubs.tcl_SetTimer != tclOriginalNotifier.setTimerProc) {
tclStubs.tcl_SetTimer(timePtr);
}
}
|
| ︙ | | |
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
-
+
-
-
-
-
-
+
+
+
+
+
|
}
/*
*----------------------------------------------------------------------
*
* Tcl_CreateFileHandler --
*
* This procedure registers a file handler with the select notifier.
* This function registers a file handler with the select notifier.
*
* Results:
* None.
*
* Side effects:
* Creates a new file handler structure.
*
*----------------------------------------------------------------------
*/
void
Tcl_CreateFileHandler(fd, mask, proc, clientData)
int fd; /* Handle of stream to watch. */
int mask; /* OR'ed combination of TCL_READABLE,
* TCL_WRITABLE, and TCL_EXCEPTION:
* indicates conditions under which
* proc should be called. */
Tcl_FileProc *proc; /* Procedure to call for each
* selected event. */
* TCL_WRITABLE, and TCL_EXCEPTION: indicates
* conditions under which proc should be
* called. */
Tcl_FileProc *proc; /* Function to call for each selected
* event. */
ClientData clientData; /* Arbitrary data to pass to proc. */
{
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
FileHandler *filePtr;
if (tclStubs.tcl_CreateFileHandler != tclOriginalNotifier.createFileHandlerProc) {
tclStubs.tcl_CreateFileHandler(fd, mask, proc, clientData);
|
| ︙ | | |
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
-
+
-
-
+
+
-
+
|
}
/*
*----------------------------------------------------------------------
*
* Tcl_DeleteFileHandler --
*
* Cancel a previously-arranged callback arrangement for
* Cancel a previously-arranged callback arrangement for a file.
* a file.
*
* Results:
* None.
*
* Side effects:
* If a callback was previously registered on file, remove it.
*
*----------------------------------------------------------------------
*/
void
Tcl_DeleteFileHandler(fd)
int fd; /* Stream id for which to remove callback procedure. */
int fd; /* Stream id for which to remove callback
* function. */
{
FileHandler *filePtr, *prevPtr;
int i;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (tclStubs.tcl_DeleteFileHandler != tclOriginalNotifier.deleteFileHandlerProc) {
tclStubs.tcl_DeleteFileHandler(fd);
return;
}
/*
* Find the entry for the given file (and return if there isn't one).
*/
for (prevPtr = NULL, filePtr = tsdPtr->firstFileHandlerPtr; ;
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
prevPtr = filePtr, filePtr = filePtr->nextPtr) {
if (filePtr == NULL) {
return;
}
if (filePtr->fd == fd) {
break;
}
}
|
| ︙ | | |
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
+
-
+
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
+
-
+
-
-
+
+
-
-
-
-
-
+
+
+
+
+
-
+
-
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
+
-
+
-
-
-
-
+
+
-
-
+
-
-
+
+
-
-
-
+
+
+
-
+
|
}
/*
*----------------------------------------------------------------------
*
* FileHandlerEventProc --
*
* This procedure is called by Tcl_ServiceEvent when a file event
* reaches the front of the event queue. This procedure is
* responsible for actually handling the event by invoking the
* callback for the file handler.
* This function is called by Tcl_ServiceEvent when a file event reaches
* the front of the event queue. This function is responsible for
* actually handling the event by invoking the callback for the file
* handler.
*
* Results:
* Returns 1 if the event was handled, meaning it should be removed
* from the queue. Returns 0 if the event was not handled, meaning
* it should stay on the queue. The only time the event isn't
* handled is if the TCL_FILE_EVENTS flag bit isn't set.
* Returns 1 if the event was handled, meaning it should be removed from
* the queue. Returns 0 if the event was not handled, meaning it should
* stay on the queue. The only time the event isn't handled is if the
* TCL_FILE_EVENTS flag bit isn't set.
*
* Side effects:
* Whatever the file handler's callback procedure does.
* Whatever the file handler's callback function does.
*
*----------------------------------------------------------------------
*/
static int
FileHandlerEventProc(evPtr, flags)
Tcl_Event *evPtr; /* Event to service. */
int flags; /* Flags that indicate what events to
* handle, such as TCL_FILE_EVENTS. */
int flags; /* Flags that indicate what events to handle,
* such as TCL_FILE_EVENTS. */
{
int mask;
FileHandler *filePtr;
FileHandlerEvent *fileEvPtr = (FileHandlerEvent *) evPtr;
ThreadSpecificData *tsdPtr;
if (!(flags & TCL_FILE_EVENTS)) {
return 0;
}
/*
* Search through the file handlers to find the one whose handle matches
* the event. We do this rather than keeping a pointer to the file
* handler directly in the event, so that the handler can be deleted
* while the event is queued without leaving a dangling pointer.
* the event. We do this rather than keeping a pointer to the file handler
* directly in the event, so that the handler can be deleted while the
* event is queued without leaving a dangling pointer.
*/
tsdPtr = TCL_TSD_INIT(&dataKey);
for (filePtr = tsdPtr->firstFileHandlerPtr; filePtr != NULL;
filePtr = filePtr->nextPtr) {
if (filePtr->fd != fileEvPtr->fd) {
continue;
}
/*
* The code is tricky for two reasons:
* 1. The file handler's desired events could have changed
* since the time when the event was queued, so AND the
* ready mask with the desired mask.
* 2. The file could have been closed and re-opened since
* the time when the event was queued. This is why the
* ready mask is stored in the file handler rather than
* 1. The file handler's desired events could have changed since the
* time when the event was queued, so AND the ready mask with the
* desired mask.
* 2. The file could have been closed and re-opened since the time
* when the event was queued. This is why the ready mask is stored
* in the file handler rather than the queued event: it will be
* the queued event: it will be zeroed when a new
* file handler is created for the newly opened file.
* zeroed when a new file handler is created for the newly opened
* file.
*/
mask = filePtr->readyMask & filePtr->mask;
filePtr->readyMask = 0;
if (mask != 0) {
(*filePtr->proc)(filePtr->clientData, mask);
}
break;
}
return 1;
}
/*
*----------------------------------------------------------------------
*
* Tcl_WaitForEvent --
*
* This function is called by Tcl_DoOneEvent to wait for new
* events on the message queue. If the block time is 0, then
* Tcl_WaitForEvent just polls without blocking.
* This function is called by Tcl_DoOneEvent to wait for new events on
* the message queue. If the block time is 0, then Tcl_WaitForEvent just
* polls without blocking.
*
* Results:
* Returns -1 if the select would block forever, otherwise
* Returns -1 if the select would block forever, otherwise returns 0.
* returns 0.
*
* Side effects:
* Queues file events that are detected by the select.
*
*----------------------------------------------------------------------
*/
int
Tcl_WaitForEvent(timePtr)
Tcl_Time *timePtr; /* Maximum block time, or NULL. */
{
FileHandler *filePtr;
FileHandlerEvent *fileEvPtr;
int mask;
Tcl_Time myTime;
#ifdef TCL_THREADS
int waitForFiles;
Tcl_Time *myTimePtr;
#else
/*
/* Impl. notes: timeout & timeoutPtr are used if, and only if
* threads are not enabled. They are the arguments for the regular
* select() used when the core is not thread-enabled. */
* Impl. notes: timeout & timeoutPtr are used if, and only if threads are
* not enabled. They are the arguments for the regular select() used when
* the core is not thread-enabled.
*/
struct timeval timeout, *timeoutPtr;
int numFound;
#endif
#endif /* TCL_THREADS */
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
if (tclStubs.tcl_WaitForEvent != tclOriginalNotifier.waitForEventProc) {
return tclStubs.tcl_WaitForEvent(timePtr);
}
/*
* Set up the timeout structure. Note that if there are no events to
* check for, we return with a negative result rather than blocking
* Set up the timeout structure. Note that if there are no events to check
* for, we return with a negative result rather than blocking forever.
* forever.
*/
if (timePtr != NULL) {
/*
/* TIP #233 (Virtualized Time). Is virtual time in effect ?
* And do we actually have something to scale ? If yes to both
* then we call the handler to do this scaling */
* TIP #233 (Virtualized Time). Is virtual time in effect? And do we
* actually have something to scale? If yes to both then we call the
* handler to do this scaling.
*/
myTime.sec = timePtr->sec;
myTime.usec = timePtr->usec;
if (myTime.sec != 0 || myTime.usec != 0) {
(*tclScaleTimeProcPtr) (&myTime, tclTimeClientData);
}
#ifdef TCL_THREADS
myTimePtr = &myTime;
#else
timeout.tv_sec = myTime.sec;
timeout.tv_sec = myTime.sec;
timeout.tv_usec = myTime.usec;
timeoutPtr = &timeout;
#endif
timeoutPtr = &timeout;
#endif /* TCL_THREADS */
#ifndef TCL_THREADS
} else if (tsdPtr->numFdBits == 0) {
/*
* If there are no threads, no timeout, and no fds registered,
* then there are no events possible and we must avoid deadlock.
* Note that this is not entirely correct because there might
* be a signal that could interrupt the select call, but we
* don't handle that case if we aren't using threads.
* If there are no threads, no timeout, and no fds registered, then
* there are no events possible and we must avoid deadlock. Note that
* this is not entirely correct because there might be a signal that
* could interrupt the select call, but we don't handle that case if
* we aren't using threads.
*/
return -1;
#endif
#endif /* !TCL_THREADS */
} else {
#ifdef TCL_THREADS
myTimePtr = NULL;
#else
timeoutPtr = NULL;
#endif
#endif /* TCL_THREADS */
}
#ifdef TCL_THREADS
/*
* Place this thread on the list of interested threads, signal the
* notifier thread, and wait for a response or a timeout.
*/
Tcl_MutexLock(¬ifierMutex);
waitForFiles = (tsdPtr->numFdBits > 0);
if (myTimePtr != NULL && myTimePtr->sec == 0 && myTimePtr->usec == 0) {
/*
* Cannot emulate a polling select with a polling condition variable.
* Instead, pretend to wait for files and tell the notifier
* thread what we are doing. The notifier thread makes sure
* it goes through select with its select mask in the same state
* as ours currently is. We block until that happens.
* Instead, pretend to wait for files and tell the notifier thread
* what we are doing. The notifier thread makes sure it goes through
* select with its select mask in the same state as ours currently is.
* We block until that happens.
*/
waitForFiles = 1;
tsdPtr->pollState = POLL_WANT;
myTimePtr = NULL;
} else {
tsdPtr->pollState = 0;
}
if (waitForFiles) {
/*
* Add the ThreadSpecificData structure of this thread to the list
* of ThreadSpecificData structures of all threads that are waiting
* on file events.
* Add the ThreadSpecificData structure of this thread to the list of
* ThreadSpecificData structures of all threads that are waiting on
* file events.
*/
tsdPtr->nextPtr = waitingListPtr;
if (waitingListPtr) {
waitingListPtr->prevPtr = tsdPtr;
}
tsdPtr->prevPtr = 0;
waitingListPtr = tsdPtr;
tsdPtr->onList = 1;
write(triggerPipe, "", 1);
}
FD_ZERO(&(tsdPtr->readyMasks.readable));
FD_ZERO(&(tsdPtr->readyMasks.writable));
FD_ZERO(&(tsdPtr->readyMasks.exceptional));
if (!tsdPtr->eventReady) {
Tcl_ConditionWait(&tsdPtr->waitCV, ¬ifierMutex, myTimePtr);
Tcl_ConditionWait(&tsdPtr->waitCV, ¬ifierMutex, myTimePtr);
}
tsdPtr->eventReady = 0;
if (waitForFiles && tsdPtr->onList) {
/*
* Remove the ThreadSpecificData structure of this thread from the
* waiting list. Alert the notifier thread to recompute its select
* waiting list. Alert the notifier thread to recompute its select
* masks - skipping this caused a hang when trying to close a pipe
* which the notifier thread was still doing a select on.
*/
if (tsdPtr->prevPtr) {
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr;
} else {
waitingListPtr = tsdPtr->nextPtr;
}
if (tsdPtr->nextPtr) {
tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr;
}
tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;
tsdPtr->onList = 0;
write(triggerPipe, "", 1);
}
#else
tsdPtr->readyMasks = tsdPtr->checkMasks;
numFound = select( tsdPtr->numFdBits,
&(tsdPtr->readyMasks.readable),
&(tsdPtr->readyMasks.writable),
numFound = select(tsdPtr->numFdBits, &(tsdPtr->readyMasks.readable),
&(tsdPtr->readyMasks.writable), &(tsdPtr->readyMasks.exceptional),
&(tsdPtr->readyMasks.exceptional),
timeoutPtr );
timeoutPtr);
/*
* Some systems don't clear the masks after an error, so
* we have to do it here.
* Some systems don't clear the masks after an error, so we have to do it
* here.
*/
if (numFound == -1) {
FD_ZERO( &(tsdPtr->readyMasks.readable ) );
FD_ZERO( &(tsdPtr->readyMasks.writable ) );
FD_ZERO( &(tsdPtr->readyMasks.exceptional ) );
FD_ZERO(&(tsdPtr->readyMasks.readable));
FD_ZERO(&(tsdPtr->readyMasks.writable));
FD_ZERO(&(tsdPtr->readyMasks.exceptional));
}
#endif
#endif /* TCL_THREADS */
/*
* Queue all detected file events before returning.
*/
for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL);
filePtr = filePtr->nextPtr) {
|
| ︙ | | |
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
|
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
|
-
-
+
+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
-
+
+
-
-
+
+
|
}
if (!mask) {
continue;
}
/*
* Don't bother to queue an event if the mask was previously
* non-zero since an event must still be on the queue.
* Don't bother to queue an event if the mask was previously non-zero
* since an event must still be on the queue.
*/
if (filePtr->readyMask == 0) {
fileEvPtr = (FileHandlerEvent *) ckalloc(sizeof(FileHandlerEvent));
fileEvPtr->header.proc = FileHandlerEventProc;
fileEvPtr->fd = filePtr->fd;
Tcl_QueueEvent((Tcl_Event *) fileEvPtr, TCL_QUEUE_TAIL);
}
filePtr->readyMask = mask;
}
#ifdef TCL_THREADS
Tcl_MutexUnlock(¬ifierMutex);
#endif
#endif /* TCL_THREADS */
return 0;
}
#ifdef TCL_THREADS
/*
*----------------------------------------------------------------------
*
* NotifierThreadProc --
*
* This routine is the initial (and only) function executed by the
* special notifier thread. Its job is to wait for file descriptors
* to become readable or writable or to have an exception condition
* and then to notify other threads who are interested in this
* information by signalling a condition variable. Other threads
* can signal this notifier thread of a change in their interests
* by writing a single byte to a special pipe that the notifier
* special notifier thread. Its job is to wait for file descriptors to
* become readable or writable or to have an exception condition and then
* to notify other threads who are interested in this information by
* signalling a condition variable. Other threads can signal this
* notifier thread of a change in their interests by writing a single
* byte to a special pipe that the notifier thread is monitoring.
* thread is monitoring.
*
* Result:
* None. Once started, this routine never exits. It dies with
* the overall process.
* None. Once started, this routine never exits. It dies with the overall
* process.
*
* Side effects:
* The trigger pipe used to signal the notifier thread is created
* when the notifier thread first starts.
* The trigger pipe used to signal the notifier thread is created when
* the notifier thread first starts.
*
*----------------------------------------------------------------------
*/
static void
NotifierThreadProc(clientData)
ClientData clientData; /* Not used. */
|
| ︙ | | |
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
|
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
|
-
+
|
#else
if (ioctl(receivePipe, (int) FIONBIO, &status) < 0) {
Tcl_Panic("NotifierThreadProc: could not make receive pipe non blocking.");
}
if (ioctl(fds[1], (int) FIONBIO, &status) < 0) {
Tcl_Panic("NotifierThreadProc: could not make trigger pipe non blocking.");
}
#endif
#endif /* FIONBIO */
/*
* Install the write end of the pipe into the global variable.
*/
Tcl_MutexLock(¬ifierMutex);
triggerPipe = fds[1];
|
| ︙ | | |
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
|
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
|
-
-
+
+
|
while (1) {
FD_ZERO(&readableMask);
FD_ZERO(&writableMask);
FD_ZERO(&exceptionalMask);
/*
* Compute the logical OR of the select masks from all the
* waiting notifiers.
* Compute the logical OR of the select masks from all the waiting
* notifiers.
*/
Tcl_MutexLock(¬ifierMutex);
timePtr = NULL;
for (tsdPtr = waitingListPtr; tsdPtr; tsdPtr = tsdPtr->nextPtr) {
for (i = tsdPtr->numFdBits-1; i >= 0; --i) {
if (FD_ISSET(i, &(tsdPtr->checkMasks.readable))) {
|
| ︙ | | |
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
|
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
-
-
+
+
|
}
}
if (tsdPtr->numFdBits > numFdBits) {
numFdBits = tsdPtr->numFdBits;
}
if (tsdPtr->pollState & POLL_WANT) {
/*
* Here we make sure we go through select() with the same
* mask bits that were present when the thread tried to poll.
* Here we make sure we go through select() with the same mask
* bits that were present when the thread tried to poll.
*/
tsdPtr->pollState |= POLL_DONE;
timePtr = &poll;
}
}
Tcl_MutexUnlock(¬ifierMutex);
|
| ︙ | | |
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
|
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
-
-
-
-
+
+
+
+
|
}
}
if (found || (tsdPtr->pollState & POLL_DONE)) {
tsdPtr->eventReady = 1;
if (tsdPtr->onList) {
/*
* Remove the ThreadSpecificData structure of this
* thread from the waiting list. This prevents us from
* continuously spining on select until the other
* threads runs and services the file event.
* Remove the ThreadSpecificData structure of this thread
* from the waiting list. This prevents us from
* continuously spining on select until the other threads
* runs and services the file event.
*/
if (tsdPtr->prevPtr) {
tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr;
} else {
waitingListPtr = tsdPtr->nextPtr;
}
|
| ︙ | | |
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
|
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
|
-
-
+
+
-
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
|
Tcl_ConditionNotify(&tsdPtr->waitCV);
}
}
Tcl_MutexUnlock(¬ifierMutex);
/*
* Consume the next byte from the notifier pipe if the pipe was
* readable. Note that there may be multiple bytes pending, but
* to avoid a race condition we only read one at a time.
* readable. Note that there may be multiple bytes pending, but to
* avoid a race condition we only read one at a time.
*/
if (FD_ISSET(receivePipe, &readableMask)) {
i = read(receivePipe, buf, 1);
if ((i == 0) || ((i == 1) && (buf[0] == 'q'))) {
/*
* Someone closed the write end of the pipe or sent us a
* Quit message [Bug: 4139] and then closed the write end
* of the pipe so we need to shut down the notifier thread.
* Someone closed the write end of the pipe or sent us a Quit
* message [Bug: 4139] and then closed the write end of the
* pipe so we need to shut down the notifier thread.
*/
break;
}
}
}
/*
* Clean up the read end of the pipe and signal any threads waiting on
* termination of the notifier thread.
*/
close(receivePipe);
Tcl_MutexLock(¬ifierMutex);
triggerPipe = -1;
Tcl_ConditionNotify(¬ifierCV);
Tcl_MutexUnlock(¬ifierMutex);
TclpThreadExit (0);
}
#endif
#endif /* TCL_THREADS */
#endif /* HAVE_COREFOUNDATION */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|