Diff
Not logged in

Differences From Artifact [21db18e2a3]:

To Artifact [93bf1da756]:


1
2
3
4
5
6
7
8
9
10
11


12
13
14
15
16
17
18
1
2
3
4
5
6
7
8
9


10
11
12
13
14
15
16
17
18









-
-
+
+







/*
 * 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 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.35 2008/04/16 14:29:26 das Exp $
 */

#include "tclInt.h"
#ifndef HAVE_COREFOUNDATION	/* Darwin/Mac OS X CoreFoundation notifier is
				 * in tclMacOSXNotify.c */
131
132
133
134
135
136
137
138

139
140
141
142
143
144
145
131
132
133
134
135
136
137

138
139
140
141
142
143
144
145







-
+







 * 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
 * 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.
 * You must hold the notifierMutex lock before writing to the pipe.
 */

static int triggerPipe = -1;

/*
 * The notifierMutex locks access to all of the global notifier state.
 */
228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
228
229
230
231
232
233
234

235
236
237
238
239
240
241
242







-
+








	while (triggerPipe < 0) {
	    Tcl_ConditionWait(&notifierCV, &notifierMutex, NULL);
	}

	Tcl_MutexUnlock(&notifierMutex);
#endif
	return (ClientData) tsdPtr;
	return tsdPtr;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_FinalizeNotifier --
287
288
289
290
291
292
293
294



295
296
297
298
299
300
301
287
288
289
290
291
292
293

294
295
296
297
298
299
300
301
302
303







-
+
+
+







	     * 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);
	    if (write(triggerPipe, "q", 1) != 1) {
			Tcl_Panic("Tcl_FinalizeNotifier: unable to write q to triggerPipe");
	    }
	    close(triggerPipe);
	    while(triggerPipe >= 0) {
		Tcl_ConditionWait(&notifierCV, &notifierMutex, NULL);
	    }

	    result = Tcl_JoinThread(notifierThread, NULL);
	    if (result) {
338
339
340
341
342
343
344
345


346
347
348
349
350
351
352
340
341
342
343
344
345
346

347
348
349
350
351
352
353
354
355







-
+
+







    ClientData clientData)
{
    if (tclNotifierHooks.alertNotifierProc) {
	tclNotifierHooks.alertNotifierProc(clientData);
	return;
    } else {
#ifdef TCL_THREADS
	ThreadSpecificData *tsdPtr = (ThreadSpecificData *) clientData;
	ThreadSpecificData *tsdPtr = clientData;

	Tcl_MutexLock(&notifierMutex);
	tsdPtr->eventReady = 1;
	Tcl_ConditionNotify(&tsdPtr->waitCV);
	Tcl_MutexUnlock(&notifierMutex);
#endif
    }
}
548
549
550
551
552
553
554
555


556
557
558
559
560

561
562
563

564
565
566
567
568
569
570
551
552
553
554
555
556
557

558
559
560
561
562
563

564
565
566
567
568
569
570
571
572
573
574
575







-
+
+




-
+



+







	}

	/*
	 * Find current max fd.
	 */

	if (fd+1 == tsdPtr->numFdBits) {
	    tsdPtr->numFdBits = 0;
	    int numFdBits = 0;

	    for (i = fd-1; i >= 0; i--) {
		if (FD_ISSET(i, &(tsdPtr->checkMasks.readable))
			|| FD_ISSET(i, &(tsdPtr->checkMasks.writable))
			|| FD_ISSET(i, &(tsdPtr->checkMasks.exceptional))) {
		    tsdPtr->numFdBits = i+1;
		    numFdBits = i+1;
		    break;
		}
	    }
	    tsdPtr->numFdBits = numFdBits;
	}

	/*
	 * Clean up information in the callback record.
	 */

	if (prevPtr == NULL) {
673
674
675
676
677
678
679
680

681
682
683
684
685
686
687
688
689
690
678
679
680
681
682
683
684

685
686
687

688
689
690
691
692
693
694







-
+


-







{
    if (tclNotifierHooks.waitForEventProc) {
	return tclNotifierHooks.waitForEventProc(timePtr);
    } else {
	FileHandler *filePtr;
	FileHandlerEvent *fileEvPtr;
	int mask;
	Tcl_Time myTime;
	Tcl_Time vTime;
#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.
	 */

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
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







-
-
-
-
+
+
+
+
-

-
-
+
-
-
-
-
+
+

-
-
-










-

-
-
-












-
-
+








-
+












-
+

+


















-
+
+
+







-
+







	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.
	     */

	    myTime.sec  = timePtr->sec;
	    myTime.usec = timePtr->usec;

	    if (myTime.sec != 0 || myTime.usec != 0) {
	    if (timePtr->sec != 0 || timePtr->usec != 0) {
		vTime = *timePtr;
		tclScaleTimeProcPtr(&vTime, tclTimeClientData);
		timePtr = &vTime;
		tclScaleTimeProcPtr(&myTime, tclTimeClientData);
	    }

#ifdef TCL_THREADS
#ifndef TCL_THREADS
	    myTimePtr = &myTime;
#else
	    timeout.tv_sec = myTime.sec;
	    timeout.tv_usec = myTime.usec;
	    timeout.tv_sec = timePtr->sec;
	    timeout.tv_usec = timePtr->usec;
	    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.
	     */

	    return -1;
#endif /* !TCL_THREADS */
	} else {
#ifdef TCL_THREADS
	    myTimePtr = NULL;
#else
	    timeoutPtr = NULL;
#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(&notifierMutex);

	waitForFiles = (tsdPtr->numFdBits > 0);
	if (myTimePtr != NULL && myTimePtr->sec == 0 && (myTimePtr->usec == 0
	if (timePtr != NULL && timePtr->sec == 0 && (timePtr->usec == 0
#if defined(__APPLE__) && defined(__LP64__)
		/*
		 * On 64-bit Darwin, pthread_cond_timedwait() appears to have a
		 * bug that causes it to wait forever when passed an absolute
		 * time which has already been exceeded by the system time; as
		 * a workaround, when given a very brief timeout, just do a
		 * poll. [Bug 1457797]
		 */
		|| myTimePtr->usec < 10
		|| timePtr->usec < 10
#endif
		)) {
	    /*
	     * 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.
	     */

	    waitForFiles = 1;
	    tsdPtr->pollState = POLL_WANT;
	    myTimePtr = NULL;
	    timePtr = NULL;
	} else {
	    waitForFiles = (tsdPtr->numFdBits > 0);
	    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.
	     */

	    tsdPtr->nextPtr = waitingListPtr;
	    if (waitingListPtr) {
		waitingListPtr->prevPtr = tsdPtr;
	    }
	    tsdPtr->prevPtr = 0;
	    waitingListPtr = tsdPtr;
	    tsdPtr->onList = 1;

	    write(triggerPipe, "", 1);
	    if (write(triggerPipe, "", 1) != 1) {
			Tcl_Panic("Tcl_WaitForEvent: unable to write to triggerPipe");
	    }
	}

	FD_ZERO(&(tsdPtr->readyMasks.readable));
	FD_ZERO(&(tsdPtr->readyMasks.writable));
	FD_ZERO(&(tsdPtr->readyMasks.exceptional));

	if (!tsdPtr->eventReady) {
	    Tcl_ConditionWait(&tsdPtr->waitCV, &notifierMutex, myTimePtr);
	    Tcl_ConditionWait(&tsdPtr->waitCV, &notifierMutex, timePtr);
	}
	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
818
819
820
821
822
823
824
825



826
827
828
829
830
831
832
813
814
815
816
817
818
819

820
821
822
823
824
825
826
827
828
829







-
+
+
+







		waitingListPtr = tsdPtr->nextPtr;
	    }
	    if (tsdPtr->nextPtr) {
		tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr;
	    }
	    tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;
	    tsdPtr->onList = 0;
	    write(triggerPipe, "", 1);
	    if (write(triggerPipe, "", 1) != 1) {
			Tcl_Panic("Tcl_WaitForEvent: unable to write to triggerPipe");
	    }
	}

#else
	tsdPtr->readyMasks = tsdPtr->checkMasks;
	numFound = select(tsdPtr->numFdBits, &(tsdPtr->readyMasks.readable),
		&(tsdPtr->readyMasks.writable),
		&(tsdPtr->readyMasks.exceptional), timeoutPtr);