Tcl Source Code

Check-in [3fc8da2b5f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:code review and small optimizations
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sebres-8-5-event-perf-branch
Files: files | file ages | folders
SHA1: 3fc8da2b5f9e6b575779d3667a78c75e1dcaf296
User & Date: sebres 2017-07-03 13:32:19
Context
2017-07-03
13:32
after at: added simple workaround for absolute timers/sleep ("after at real-time"): because we use m... check-in: 12d1d63e18 user: sebres tags: sebres-8-5-event-perf-branch
13:32
code review and small optimizations check-in: 3fc8da2b5f user: sebres tags: sebres-8-5-event-perf-branch
13:32
fix check event source threshold (corresponds 100-ns ranges, if the wide-clicks supported); because... check-in: 68ebe50d7b user: sebres tags: sebres-8-5-event-perf-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclNotify.c.

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
	    return 1;
	}
	/* Async only */
	if ((flags & TCL_ALL_EVENTS) == TCL_ASYNC_EVENTS) {
	    return 0;
	}
    }








    /*
     * If timer marker reached, process timer events now.
     */
    if (flags & TCL_TIMER_EVENTS) { /* timer allowed */
	if ( tsdPtr->timerMarkerPtr == INT2PTR(-1) /* timer-event reached */
	  || ( tsdPtr->timerMarkerPtr == INT2PTR(-2) /* next cycle, but ... */
	    && ((flags & TCL_ALL_EVENTS) == TCL_TIMER_EVENTS) /* timers only */
	  )
	) {
	    goto processTimer;
	}
    }

    /*
     * Loop through all the events in the queue until we find one that can
     * actually be handled.
     */








>
>
>
>
>
>
>




<
|
<
<
<
<
|
<







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
	    return 1;
	}
	/* Async only */
	if ((flags & TCL_ALL_EVENTS) == TCL_ASYNC_EVENTS) {
	    return 0;
	}
    }

    /* Fast bypass case */
    if ( !tsdPtr->firstEventPtr /* no other events */
      || ((flags & TCL_ALL_EVENTS) == TCL_TIMER_EVENTS) /* timers only */
    ) {
	goto timer;
    }

    /*
     * If timer marker reached, process timer events now.
     */

    if ((flags & TCL_TIMER_EVENTS) && (tsdPtr->timerMarkerPtr == INT2PTR(-1))) {




	goto processTimer;

    }

    /*
     * Loop through all the events in the queue until we find one that can
     * actually be handled.
     */

841
842
843
844
845
846
847

848
849
850
851
852
853
854
	     */

	    evPtr->proc = proc;
	}
    }
    Tcl_MutexUnlock(&(tsdPtr->queueMutex));


    /*
     * Process timer queue, if alloved and timers are enabled.
     */

    if (flags & TCL_TIMER_EVENTS) {

	/* If available pending timer-events of new generation */







>







842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
	     */

	    evPtr->proc = proc;
	}
    }
    Tcl_MutexUnlock(&(tsdPtr->queueMutex));

  timer:
    /*
     * Process timer queue, if alloved and timers are enabled.
     */

    if (flags & TCL_TIMER_EVENTS) {

	/* If available pending timer-events of new generation */

Changes to unix/tclUnixNotfy.c.

867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
 *----------------------------------------------------------------------
 */

void
TclpUSleep(
    Tcl_WideInt usec)	/* Time to sleep. */
{
    Tcl_WideInt now, desired, sleepTime;

    if (usec < 0) {
	usec = 0;
    }
    /*
     * The only trick here is that select appears to return early under some
     * conditions, so we have to check to make sure that the right amount of







|







867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
 *----------------------------------------------------------------------
 */

void
TclpUSleep(
    Tcl_WideInt usec)	/* Time to sleep. */
{
    Tcl_WideInt now, desired;

    if (usec < 0) {
	usec = 0;
    }
    /*
     * The only trick here is that select appears to return early under some
     * conditions, so we have to check to make sure that the right amount of
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
	if (usec >= TCL_TMR_MIN_DELAY) {

	    struct timeval delay;

	    delay.tv_sec  = usec / 1000000;
	    delay.tv_usec = usec % 1000000;

	    sleepTime = usec;
	    (void) select(0, (SELECT_MASK *) 0, (SELECT_MASK *) 0,
		(SELECT_MASK *) 0, &delay);

	} else if (usec >= TCL_TMR_MIN_SLEEP) {
	    sleepTime = usec - TCL_TMR_MIN_SLEEP;
	    usleep(sleepTime);
	} else {
	    /* nanosleep has the same minimal sleep interval as usleep */
#if 1
	    sleepTime = 0;
#else
	    struct timespec delay;

	    delay.tv_sec  = usec / 1000000;
	    delay.tv_nsec = (usec % 1000000) * 1000; /* usec to nsec */

	    sleepTime = usec;
	    nanosleep(&delay, NULL);
#endif
	}
	
	now = TclpGetUTimeMonotonic();

	if ((usec = (desired - now)) <= 0 /* or tolerance */) {







<




|
<


|
<
<





<







899
900
901
902
903
904
905

906
907
908
909
910

911
912
913


914
915
916
917
918

919
920
921
922
923
924
925
	if (usec >= TCL_TMR_MIN_DELAY) {

	    struct timeval delay;

	    delay.tv_sec  = usec / 1000000;
	    delay.tv_usec = usec % 1000000;


	    (void) select(0, (SELECT_MASK *) 0, (SELECT_MASK *) 0,
		(SELECT_MASK *) 0, &delay);

	} else if (usec >= TCL_TMR_MIN_SLEEP) {
	    usleep((useconds_t)(usec - TCL_TMR_MIN_SLEEP));

	} else {
	    /* nanosleep has the same minimal sleep interval as usleep */
#if 0


	    struct timespec delay;

	    delay.tv_sec  = usec / 1000000;
	    delay.tv_nsec = (usec % 1000000) * 1000; /* usec to nsec */


	    nanosleep(&delay, NULL);
#endif
	}
	
	now = TclpGetUTimeMonotonic();

	if ((usec = (desired - now)) <= 0 /* or tolerance */) {
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

1004





1005
1006
1007
1008
1009
1010
1011
     * forever.
     */

    if (timePtr != NULL) {

	waitTime = TCL_TIME_TO_USEC(*timePtr);

	/*
	 * Note the time can be switched (time-jump), so use monotonic time here.
	 */
	endTime = TclpGetUTimeMonotonic() + waitTime;

	/* 
	 * If short wait or no wait at all, just process events already available
	 * right now, avoid waiting too long somewhere (NRT-capability fix).
	 */
	if (!timePtr->sec && timePtr->usec < TCL_TMR_MIN_DELAY) {

	    canWait = 0;





	}

#ifndef TCL_THREADS
	timeout.tv_sec = timePtr->sec;
	timeout.tv_usec = timePtr->usec;
	timeoutPtr = &timeout;
    } else if (tsdPtr->numFdBits == 0) {







<
<
<
<
<





>

>
>
>
>
>







982
983
984
985
986
987
988





989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
     * forever.
     */

    if (timePtr != NULL) {

	waitTime = TCL_TIME_TO_USEC(*timePtr);






	/* 
	 * If short wait or no wait at all, just process events already available
	 * right now, avoid waiting too long somewhere (NRT-capability fix).
	 */
	if (!timePtr->sec && timePtr->usec < TCL_TMR_MIN_DELAY) {

	    canWait = 0;
	} else {
	    /*
	     * Note the time can be switched (time-jump), so use monotonic time here.
	     */
	    endTime = TclpGetUTimeMonotonic() + waitTime;
	}

#ifndef TCL_THREADS
	timeout.tv_sec = timePtr->sec;
	timeout.tv_usec = timePtr->usec;
	timeoutPtr = &timeout;
    } else if (tsdPtr->numFdBits == 0) {
1034
1035
1036
1037
1038
1039
1040

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051

1052
1053
1054
1055
1056
1057
1058
1059
1060
    pthread_mutex_lock(&notifierMutex);

    /* if cannot wait (but not really necessary to wait), bypass triggering pipe */
    if (!canWait && (!tsdPtr->numFdBits || tsdPtr->eventReady)) {
	goto nowait;
    }


    if (!canWait) {
	/*
	 * 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;

    } 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







>









|
|
>

<







1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050

1051
1052
1053
1054
1055
1056
1057
    pthread_mutex_lock(&notifierMutex);

    /* if cannot wait (but not really necessary to wait), bypass triggering pipe */
    if (!canWait && (!tsdPtr->numFdBits || tsdPtr->eventReady)) {
	goto nowait;
    }

    waitForFiles = (tsdPtr->numFdBits > 0);
    if (!canWait) {
	/*
	 * 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.
	 */

	if (waitForFiles) {
	    tsdPtr->pollState = POLL_WANT;
	}
    } else {

	tsdPtr->pollState = 0;
    }

    if (waitForFiles) {
	/*
	 * Add the ThreadSpecificData structure of this thread to the list
	 * of ThreadSpecificData structures of all threads that are
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117

1118

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202

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

    while (!tsdPtr->eventReady) {

	if (timePtr) {

	    waitTime = endTime - TclpGetUTimeMonotonic();

	    if (waitTime <= 0) {
		break; /* end of wait */
	    }
	    if (waitTime <= TCL_TMR_OVERHEAD) {
		canWait = 0;
	    }
	}

#ifdef __CYGWIN__
	if (!PeekMessageW(&msg, NULL, 0, 0, 0)) {
	    DWORD timeout;

	    if (timePtr) {
		/* TIP #233: Scale from virtual time to real-time  */
		TclpScaleUTime(&waitTime);

		timeout = waitTime / 1000;
	    } else {
		timeout = 0xFFFFFFFF;
	    }
	    pthread_mutex_unlock(&notifierMutex);
	    MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279);
	    pthread_mutex_lock(&notifierMutex);
	}
#else
	/* prevent too long waiting (NRT-capability) */
	if ( !canWait ) {
	    /* short sleep */

	    TclpUSleep(waitTime);

	    break; /* end of wait */
	}
	else
	if (timePtr) {
	    struct timespec ptime;

	    /* TIP #233: Scale from virtual time to real-time  */
	    TclpScaleUTime(&waitTime);

	    clock_gettime(tsdPtr->waitCVMono ? 
		CLOCK_MONOTONIC : CLOCK_REALTIME, &ptime);
	    ptime.tv_sec += waitTime / 1000000;
	    ptime.tv_nsec += (waitTime % 1000000) * 1000;
	    if (ptime.tv_nsec > 1000000*1000) {
		ptime.tv_nsec -= 1000000*1000;
		ptime.tv_sec++;
	    }

#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 increment a bit the waiting-time from now. [Bug 1457797]
	     */
	    if (waitTime <= 0) {
		ptime.tv_sec = now.sec;
		ptime.tv_nsec = 1000 * now.usec + 10; /* + 10 nanosecond */
	    }
#else
	    /* remove overhead in nsec */
	    if (ptime.tv_nsec < TCL_TMR_OVERHEAD * 1000) {
	    	ptime.tv_nsec += 1000000*1000;
	    	ptime.tv_sec--;
	    }
	    ptime.tv_nsec -= TCL_TMR_OVERHEAD * 1000;
	    
#endif /* __APPLE__ && __LP64__ */

	    if (ptime.tv_nsec > 1000000*1000) {
		ptime.tv_nsec -= 1000000*1000;
		ptime.tv_sec++;
	    }
	    if (pthread_cond_timedwait(&tsdPtr->waitCV, &notifierMutex,
			&ptime) == ETIMEDOUT) {
		continue; /* repeat wait (if not yet real timeout) */
	    };
	} else {
	    pthread_cond_wait(&tsdPtr->waitCV, &notifierMutex);
	}
#endif /* __CYGWIN__ */

	break; /* end of wait */
    }
    if (tsdPtr->eventReady > 0) {
	tsdPtr->eventReady--;
    }

#ifdef __CYGWIN__
    while (PeekMessageW(&msg, NULL, 0, 0, 0)) {
	/*
	 * Retrieve and dispatch the message.
	 */

	DWORD result = GetMessageW(&msg, NULL, 0, 0);

	if (result == 0) {
	    PostQuitMessage(msg.wParam);
	    /* What to do here? */
	} else if (result != (DWORD) -1) {
	    TranslateMessage(&msg);
	    DispatchMessageW(&msg);
	}
    }
    ResetEvent(tsdPtr->event);
#endif /* __CYGWIN__ */

    if (waitForFiles && tsdPtr->onList) {
	/*
	 * Remove the ThreadSpecificData structure of this thread from the
	 * 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.







|











|















|



>
|
>


















|











|







|












|







|
















|







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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

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

    while (!tsdPtr->eventReady) {

	if (canWait && timePtr) {

	    waitTime = endTime - TclpGetUTimeMonotonic();

	    if (waitTime <= 0) {
		break; /* end of wait */
	    }
	    if (waitTime <= TCL_TMR_OVERHEAD) {
		canWait = 0;
	    }
	}

#  ifdef __CYGWIN__
	if (!PeekMessageW(&msg, NULL, 0, 0, 0)) {
	    DWORD timeout;

	    if (timePtr) {
		/* TIP #233: Scale from virtual time to real-time  */
		TclpScaleUTime(&waitTime);

		timeout = waitTime / 1000;
	    } else {
		timeout = 0xFFFFFFFF;
	    }
	    pthread_mutex_unlock(&notifierMutex);
	    MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279);
	    pthread_mutex_lock(&notifierMutex);
	}
#  else
	/* prevent too long waiting (NRT-capability) */
	if ( !canWait ) {
	    /* short sleep */
	    if (waitTime) {
		TclpUSleep(waitTime);
	    }
	    break; /* end of wait */
	}
	else
	if (timePtr) {
	    struct timespec ptime;

	    /* TIP #233: Scale from virtual time to real-time  */
	    TclpScaleUTime(&waitTime);

	    clock_gettime(tsdPtr->waitCVMono ? 
		CLOCK_MONOTONIC : CLOCK_REALTIME, &ptime);
	    ptime.tv_sec += waitTime / 1000000;
	    ptime.tv_nsec += (waitTime % 1000000) * 1000;
	    if (ptime.tv_nsec > 1000000*1000) {
		ptime.tv_nsec -= 1000000*1000;
		ptime.tv_sec++;
	    }

#    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 increment a bit the waiting-time from now. [Bug 1457797]
	     */
	    if (waitTime <= 0) {
		ptime.tv_sec = now.sec;
		ptime.tv_nsec = 1000 * now.usec + 10; /* + 10 nanosecond */
	    }
#    else
	    /* remove overhead in nsec */
	    if (ptime.tv_nsec < TCL_TMR_OVERHEAD * 1000) {
	    	ptime.tv_nsec += 1000000*1000;
	    	ptime.tv_sec--;
	    }
	    ptime.tv_nsec -= TCL_TMR_OVERHEAD * 1000;
	    
#    endif /* __APPLE__ && __LP64__ */

	    if (ptime.tv_nsec > 1000000*1000) {
		ptime.tv_nsec -= 1000000*1000;
		ptime.tv_sec++;
	    }
	    if (pthread_cond_timedwait(&tsdPtr->waitCV, &notifierMutex,
			&ptime) == ETIMEDOUT) {
		continue; /* repeat wait (if not yet real timeout) */
	    };
	} else {
	    pthread_cond_wait(&tsdPtr->waitCV, &notifierMutex);
	}
#  endif /* __CYGWIN__ */

	break; /* end of wait */
    }
    if (tsdPtr->eventReady > 0) {
	tsdPtr->eventReady--;
    }

#  ifdef __CYGWIN__
    while (PeekMessageW(&msg, NULL, 0, 0, 0)) {
	/*
	 * Retrieve and dispatch the message.
	 */

	DWORD result = GetMessageW(&msg, NULL, 0, 0);

	if (result == 0) {
	    PostQuitMessage(msg.wParam);
	    /* What to do here? */
	} else if (result != (DWORD) -1) {
	    TranslateMessage(&msg);
	    DispatchMessageW(&msg);
	}
    }
    ResetEvent(tsdPtr->event);
#  endif /* __CYGWIN__ */

    if (waitForFiles && tsdPtr->onList) {
	/*
	 * Remove the ThreadSpecificData structure of this thread from the
	 * 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.
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
	tsdPtr->onList = 0;
	if ((write(triggerPipe, "", 1) == -1) && (errno != EAGAIN)) {
	    Tcl_Panic("Tcl_WaitForEvent: %s",
		    "unable to write to triggerPipe");
	}
    }

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

    /*
     * 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.exception);
    }
#endif /* TCL_THREADS */

    /*
     * Queue all detected file events before returning.
     */

    for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL);
	    filePtr = filePtr->nextPtr) {







|















|







1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
	tsdPtr->onList = 0;
	if ((write(triggerPipe, "", 1) == -1) && (errno != EAGAIN)) {
	    Tcl_Panic("Tcl_WaitForEvent: %s",
		    "unable to write to triggerPipe");
	}
    }

#else /* !TCL_THREADS */
    tsdPtr->readyMasks = tsdPtr->checkMasks;
    numFound = select(tsdPtr->numFdBits, &tsdPtr->readyMasks.readable,
	    &tsdPtr->readyMasks.writable, &tsdPtr->readyMasks.exception,
	    timeoutPtr);

    /*
     * 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.exception);
    }
#endif /* !TCL_THREADS */

    /*
     * Queue all detected file events before returning.
     */

    for (filePtr = tsdPtr->firstFileHandlerPtr; (filePtr != NULL);
	    filePtr = filePtr->nextPtr) {