.
D 2001-03-27T10:49:31
J assignee nobody
J cmimetype text/plain
J comment \nHi,\n\na\scollegue\sof\smine\shas\sdebugged\sa\sstrange\sbehaviour\sof\na\nTk\sscript\swhich\swas\sexecuted\sin\sa\sTcl\sinterpreter\swith\nthreads\senabled\sunder\sSolaris\s2.6.\sWe\sobserved\sthat\neven\ntrivial\sscripts\swhich\sinclude\san\sentry\swidget\swith\sa\nblinking\ncursor\sdrive\sthe\scpu\sload\sto\ssubstantial\svalues\s(up\sto\n100%\non\ssmaller\sworkstations,\si.e.\sSparcstation\s5),\sas\slong\nas\nthe\scursor\sis\sblinking!\n\nThis\scan\sbe\sreproduced\swith\sthe\sfollowing\strivial\nscript:\n\n\s\sentry\s.entry\s-bg\swhite\s-relief\ssunken\n\s\spack\s.entry\s-side\sleft\s-fill\sx\s-expand\syes\n\nTo\sreproduce:\sExecute\sthese\stwo\slines\sin\sa\swish\swhich\nhas\sbeen\s\nbuilt\swith\s--enable-scripts,\sclick\sinto\sthe\sentry\nwidget\s(which\nmakes\sthe\scursor\sstart\sblinking)\sand\sobserve\sthe\scpu\nload\sin\na\sperfmeter.\n\nWe\scould\sreproduce\sthis\swith\sTk\sversions\s8.3.0,\s8.3.2\nand\s8.4a2\non\sSolaris\s2.6;\sa\scollegue\sreproduced\sthis\salso\son\sa\nLinux\sbox.\n\nAfter\ssome\sextensive\sdebugging,\sthe\sultimate\sreason\sfor\nthis\nwas\sfound:\sa\stimeout\svalue\sin\sTcl_ConditionWait\n(tclUnixThrd.c)\nis\scomputed\sin\sa\sway\swhich\sis,\sfor\stimeout\svalues\s<\n1sec,\salmost\nalways\sincorrect:\n\n------------------------------------------------------------------\n\s\svoid\n\s\sTcl_ConditionWait(condPtr,\smutexPtr,\stimePtr)\n\s\s\s\sTcl_Condition\s*condPtr;/*\sReally\s(pthread_cond_t\n**)\s*/\n\s\s\s\sTcl_Mutex\s*mutexPtr;/*\sReally\s(pthread_mutex_t\s**)\n*/\n\s\s\s\sTcl_Time\s*timePtr;/*\sTimeout\son\swaiting\speriod\s*/\n\s\s{\n\s\s\s\spthread_cond_t\s*pcondPtr;\n\s\s\s\spthread_mutex_t\s*pmutexPtr;\n\s\s\s\sstruct\stimespec\sptime;\n\n\s\s\s\sif\s(*condPtr\s==\sNULL)\s{\nMASTER_LOCK;\n\n/*\s\n\s*\sDouble\scheck\sinside\smutex\sto\savoid\srace,\n\s*\sthen\sinitialize\scondition\svariable\sif\snecessary.\n\s*/\n\nif\s(*condPtr\s==\sNULL)\s{\n\s\s\s\spcondPtr\s=\s(pthread_cond_t\n*)ckalloc(sizeof(pthread_cond_t));\n\s\s\s\spthread_cond_init(pcondPtr,\sNULL);\n\s\s\s\s*condPtr\s=\s(Tcl_Condition)pcondPtr;\n\s\s\s\sTclRememberCondition(condPtr);\n}\nMASTER_UNLOCK;\n\s\s\s\s}\n\s\s\s\spmutexPtr\s=\s*((pthread_mutex_t\s**)mutexPtr);\n\s\s\s\spcondPtr\s=\s*((pthread_cond_t\s**)condPtr);\n\s\s\s\sif\s(timePtr\s==\sNULL)\s{\npthread_cond_wait(pcondPtr,\spmutexPtr);\n\s\s\s\s}\selse\s{\nptime.tv_sec\s=\stimePtr->sec\s+\sTclpGetSeconds();\s\s\s\s\s\n<------\nptime.tv_nsec\s=\s1000\s*\stimePtr->usec;\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\n<------\npthread_cond_timedwait(pcondPtr,\spmutexPtr,\s&ptime);\n\s\s\s\s}\n\s\s}\n---------------------------------------------------------------\n\nExplanation:\sthe\slines\smarked\swith\sthe\sarrows\sattempt\nto\scalculate\na\stime\svalue\sfrom\sa\sdesired\stime\sinterval\s(*timePtr),\nwhich\sis\scomposed\nfrom\sseconds\sand\smicroseconds,\sand\sthe\scurrent\stime,\nwhich\sis\sobtained\swith\sTclpGetSeconds,\sthus\scompletely\nignoring\sfractions\nof\sa\ssecond.\s\n\nAssume\sa\sdesired\stimeout\svalue\sof\s300\sms,\swhich\sis\sthe\ndefault\svalue\nfor\scursor\sblinking,\sscrollbar\srepeat\srates\setc.\sin\sTk:\ntimePtr->sec\nis\szero,\sand\stimePtr->usec\sis\s300000.\sptime.tv_sec\s(the\ndesired\n"firing\stime"\sof\spthread_cond_timedwait)\swill\sthus\nalways\sbe\sthe\s"current"\nsecond\svalue,\sand\sptime.tv_nsec\scorresponds\sto\s300\sms.\nTherefore,\s70%\nof\sthe\stime,\spthread_cond_timedwait\swill\sreturn\nimmediately.\s\nBecause\sthis\scode\sis\sinvoked\sfrom\sa\sloop\swaiting\sfor\nevents\sin\ssituations\nwhen\sa\sblinking\scursor\shas\sto\sbe\supdated,\sthe\sfunction\nTcl_ConditionWait\salmost\snever\sreally\swaits,\sbut\ninstead\sexecutes\nmore\sor\sless\spermanently,\sas\slong\sas\sthe\scursor\sis\nblinking,\sthus\sleading\s\nto\sthe\sobserved\scpu\sload\svalues.\n\nSuggested\sfix:\sthe\stimeout\svalue\sneeds\sto\scorrectly\ntake\sinto\saccount\nthe\smicrosecond\scomponent\sof\sthe\scurrent\stime,\nincluding\spossible\soverflow\nsituations\swhen\smicrosecond\svalues\sadd\sup\sto\s>\s1000000.\nWe\ssuggest\sto\nreplace\sthe\sabove\stwo\slines\swith\sthe\sfollowing:\n\nTcl_Time\snow;\nTclpGetTime(&now);\nptime.tv_sec\s=\stimePtr->sec\s+\snow.sec\s+\s(\stimePtr->usec\n+\snow.usec\s)\s/\s1000000;\nptime.tv_nsec\s=\s1000\s*\s((\stimePtr->usec\s+\snow.usec\s)\s%\n1000000);\n\nWe\shave\stried\sthis\sfix,\sand\sthis\shas\sremoved\sthe\nproblem\scompletely.\n\nBest\sRegards\nD.Ruppert\nRTS\sGmbH\nSchwieberdingen/Germany\[email protected]\n\n
J foundin None
J priority 5\sMedium
J resolution None
J status Open
J submitter ruppert
J subsystem 49.\sThreading
J title timeout\sbug\sin\stclUnixThrd.c
J type Bug
K 411603ffffffffffffffffffffffffffffffffff
U ruppert
Z 2934c1f866abd00e5d2849178efe5263