Tcl Source Code

View Ticket
Login
Ticket UUID: 411603
Title: timeout bug in tclUnixThrd.c
Type: Bug Created on: 2001-03-27 10:49:31
Submitter: ruppert Assigned to: hobbs
Subsystem: 49. Threading Severity:
Priority: 5 Medium Last modified: 2001-03-30 04:53:21
Status: Closed Closed by: dgp
Resolution: Fixed Closed on: 2001-03-29 21:53:21
Version: None
Description:
Hi,

a collegue of mine has debugged a strange behaviour of
a
Tk script which was executed in a Tcl interpreter with
threads enabled under Solaris 2.6. We observed that
even
trivial scripts which include an entry widget with a
blinking
cursor drive the cpu load to substantial values (up to
100%
on smaller workstations, i.e. Sparcstation 5), as long
as
the cursor is blinking!

This can be reproduced with the following trivial
script:

  entry .entry -bg white -relief sunken
  pack .entry -side left -fill x -expand yes

To reproduce: Execute these two lines in a wish which
has been 
built with --enable-scripts, click into the entry
widget (which
makes the cursor start blinking) and observe the cpu
load in
a perfmeter.

We could reproduce this with Tk versions 8.3.0, 8.3.2
and 8.4a2
on Solaris 2.6; a collegue reproduced this also on a
Linux box.

After some extensive debugging, the ultimate reason for
this
was found: a timeout value in Tcl_ConditionWait
(tclUnixThrd.c)
is computed in a way which is, for timeout values <
1sec, almost
always incorrect:

------------------------------------------------------------------
  void
  Tcl_ConditionWait(condPtr, mutexPtr, timePtr)
    Tcl_Condition *condPtr;/* Really (pthread_cond_t
**) */
    Tcl_Mutex *mutexPtr;/* Really (pthread_mutex_t **)
*/
    Tcl_Time *timePtr;/* Timeout on waiting period */
  {
    pthread_cond_t *pcondPtr;
    pthread_mutex_t *pmutexPtr;
    struct timespec ptime;

    if (*condPtr == NULL) {
MASTER_LOCK;

/* 
 * Double check inside mutex to avoid race,
 * then initialize condition variable if necessary.
 */

if (*condPtr == NULL) {
    pcondPtr = (pthread_cond_t
*)ckalloc(sizeof(pthread_cond_t));
    pthread_cond_init(pcondPtr, NULL);
    *condPtr = (Tcl_Condition)pcondPtr;
    TclRememberCondition(condPtr);
}
MASTER_UNLOCK;
    }
    pmutexPtr = *((pthread_mutex_t **)mutexPtr);
    pcondPtr = *((pthread_cond_t **)condPtr);
    if (timePtr == NULL) {
pthread_cond_wait(pcondPtr, pmutexPtr);
    } else {
ptime.tv_sec = timePtr->sec + TclpGetSeconds();     
<------
ptime.tv_nsec = 1000 * timePtr->usec;               
<------
pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime);
    }
  }
---------------------------------------------------------------

Explanation: the lines marked with the arrows attempt
to calculate
a time value from a desired time interval (*timePtr),
which is composed
from seconds and microseconds, and the current time,
which is obtained with TclpGetSeconds, thus completely
ignoring fractions
of a second. 

Assume a desired timeout value of 300 ms, which is the
default value
for cursor blinking, scrollbar repeat rates etc. in Tk:
timePtr->sec
is zero, and timePtr->usec is 300000. ptime.tv_sec (the
desired
"firing time" of pthread_cond_timedwait) will thus
always be the "current"
second value, and ptime.tv_nsec corresponds to 300 ms.
Therefore, 70%
of the time, pthread_cond_timedwait will return
immediately. 
Because this code is invoked from a loop waiting for
events in situations
when a blinking cursor has to be updated, the function
Tcl_ConditionWait almost never really waits, but
instead executes
more or less permanently, as long as the cursor is
blinking, thus leading 
to the observed cpu load values.

Suggested fix: the timeout value needs to correctly
take into account
the microsecond component of the current time,
including possible overflow
situations when microsecond values add up to > 1000000.
We suggest to
replace the above two lines with the following:

Tcl_Time now;
TclpGetTime(&now);
ptime.tv_sec = timePtr->sec + now.sec + ( timePtr->usec
+ now.usec ) / 1000000;
ptime.tv_nsec = 1000 * (( timePtr->usec + now.usec ) %
1000000);

We have tried this fix, and this has removed the
problem completely.

Best Regards
D.Ruppert
RTS GmbH
Schwieberdingen/Germany
[email protected]
User Comments:
hobbs added on 2001-03-28 08:03:59:
Logged In: YES 
user_id=72656

patched as noted for 8.4a2, see patch 411773.

hobbs added on 2001-03-28 08:04:58:
Logged In: YES 
user_id=72656

patched as noted for 8.4a2, see patch 411773.

hobbs added on 2001-03-28 08:13:04:
Logged In: YES 
user_id=72656

patched as noted for 8.4a2, see patch 411773.

hobbs added on 2001-03-28 08:54:34:
Logged In: YES 
user_id=72656

patched as noted for 8.4a2, see patch 411773.