From 0a588d204138059ddc43ebceb74de18ccf9c7836 Mon Sep 17 00:00:00 2001 From: sebres Date: Mon, 3 Jul 2017 13:32:19 +0000 Subject: code review and small optimizations --- generic/tclNotify.c | 18 ++++++++++-------- unix/tclUnixNotfy.c | 55 ++++++++++++++++++++++++++--------------------------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/generic/tclNotify.c b/generic/tclNotify.c index b16dea7..0dd55c4 100644 --- a/generic/tclNotify.c +++ b/generic/tclNotify.c @@ -726,17 +726,18 @@ Tcl_ServiceEvent( } } + /* 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) { /* 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; - } + if ((flags & TCL_TIMER_EVENTS) && (tsdPtr->timerMarkerPtr == INT2PTR(-1))) { + goto processTimer; } /* @@ -845,6 +846,7 @@ Tcl_ServiceEvent( } Tcl_MutexUnlock(&(tsdPtr->queueMutex)); + timer: /* * Process timer queue, if alloved and timers are enabled. */ diff --git a/unix/tclUnixNotfy.c b/unix/tclUnixNotfy.c index db10512..87f88d7 100644 --- a/unix/tclUnixNotfy.c +++ b/unix/tclUnixNotfy.c @@ -871,7 +871,7 @@ void TclpUSleep( Tcl_WideInt usec) /* Time to sleep. */ { - Tcl_WideInt now, desired, sleepTime; + Tcl_WideInt now, desired; if (usec < 0) { usec = 0; @@ -903,24 +903,19 @@ TclpUSleep( 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); + usleep((useconds_t)(usec - TCL_TMR_MIN_SLEEP)); } else { /* nanosleep has the same minimal sleep interval as usleep */ -#if 1 - sleepTime = 0; -#else +#if 0 struct timespec delay; delay.tv_sec = usec / 1000000; delay.tv_nsec = (usec % 1000000) * 1000; /* usec to nsec */ - sleepTime = usec; nanosleep(&delay, NULL); #endif } @@ -991,17 +986,18 @@ Tcl_WaitForEvent( 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; + } else { + /* + * Note the time can be switched (time-jump), so use monotonic time here. + */ + endTime = TclpGetUTimeMonotonic() + waitTime; } #ifndef TCL_THREADS @@ -1038,6 +1034,7 @@ Tcl_WaitForEvent( goto nowait; } + waitForFiles = (tsdPtr->numFdBits > 0); if (!canWait) { /* * Cannot emulate a polling select with a polling condition @@ -1047,10 +1044,10 @@ Tcl_WaitForEvent( * state as ours currently is. We block until that happens. */ - waitForFiles = 1; - tsdPtr->pollState = POLL_WANT; + if (waitForFiles) { + tsdPtr->pollState = POLL_WANT; + } } else { - waitForFiles = (tsdPtr->numFdBits > 0); tsdPtr->pollState = 0; } @@ -1083,7 +1080,7 @@ Tcl_WaitForEvent( while (!tsdPtr->eventReady) { - if (timePtr) { + if (canWait && timePtr) { waitTime = endTime - TclpGetUTimeMonotonic(); @@ -1095,7 +1092,7 @@ Tcl_WaitForEvent( } } -#ifdef __CYGWIN__ +# ifdef __CYGWIN__ if (!PeekMessageW(&msg, NULL, 0, 0, 0)) { DWORD timeout; @@ -1111,11 +1108,13 @@ Tcl_WaitForEvent( MsgWaitForMultipleObjects(1, &tsdPtr->event, 0, timeout, 1279); pthread_mutex_lock(¬ifierMutex); } -#else +# else /* prevent too long waiting (NRT-capability) */ if ( !canWait ) { /* short sleep */ - TclpUSleep(waitTime); + if (waitTime) { + TclpUSleep(waitTime); + } break; /* end of wait */ } else @@ -1134,7 +1133,7 @@ Tcl_WaitForEvent( ptime.tv_sec++; } -#if defined(__APPLE__) && defined(__LP64__) +# 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 @@ -1146,7 +1145,7 @@ Tcl_WaitForEvent( ptime.tv_sec = now.sec; ptime.tv_nsec = 1000 * now.usec + 10; /* + 10 nanosecond */ } -#else +# else /* remove overhead in nsec */ if (ptime.tv_nsec < TCL_TMR_OVERHEAD * 1000) { ptime.tv_nsec += 1000000*1000; @@ -1154,7 +1153,7 @@ Tcl_WaitForEvent( } ptime.tv_nsec -= TCL_TMR_OVERHEAD * 1000; -#endif /* __APPLE__ && __LP64__ */ +# endif /* __APPLE__ && __LP64__ */ if (ptime.tv_nsec > 1000000*1000) { ptime.tv_nsec -= 1000000*1000; @@ -1167,7 +1166,7 @@ Tcl_WaitForEvent( } else { pthread_cond_wait(&tsdPtr->waitCV, ¬ifierMutex); } -#endif /* __CYGWIN__ */ +# endif /* __CYGWIN__ */ break; /* end of wait */ } @@ -1175,7 +1174,7 @@ Tcl_WaitForEvent( tsdPtr->eventReady--; } -#ifdef __CYGWIN__ +# ifdef __CYGWIN__ while (PeekMessageW(&msg, NULL, 0, 0, 0)) { /* * Retrieve and dispatch the message. @@ -1192,7 +1191,7 @@ Tcl_WaitForEvent( } } ResetEvent(tsdPtr->event); -#endif /* __CYGWIN__ */ +# endif /* __CYGWIN__ */ if (waitForFiles && tsdPtr->onList) { /* @@ -1218,7 +1217,7 @@ Tcl_WaitForEvent( } } -#else +#else /* !TCL_THREADS */ tsdPtr->readyMasks = tsdPtr->checkMasks; numFound = select(tsdPtr->numFdBits, &tsdPtr->readyMasks.readable, &tsdPtr->readyMasks.writable, &tsdPtr->readyMasks.exception, @@ -1234,7 +1233,7 @@ Tcl_WaitForEvent( FD_ZERO(&tsdPtr->readyMasks.writable); FD_ZERO(&tsdPtr->readyMasks.exception); } -#endif /* TCL_THREADS */ +#endif /* !TCL_THREADS */ /* * Queue all detected file events before returning. -- cgit v0.12