Skip to content
Tcl Source Code Artifact [7f40c15ed2]
Login

Artifact [7f40c15ed2]

Artifact 7f40c15ed2613ba8846c93938407559089ded57b65c9ac9f2babe3ae300d1fc7:

Ticket change [7f40c15ed2] - Ticket [893f8cc5db3ba8bd|893f8cc5db] <i>Nested mutexes following TIP 509</i> status still Open with 3 other changes by apnadkarni on 2025-09-18 05:37:52.
D 2025-09-18T05:37:52.097
J icomment I\sconfirmed\sthat\sWindows\snative\scondition\svariables\salso\sonly\swork\swhen\sthe\scritical\ssection\slock\sis\sheld\sexactly\sonce\sso\sChristian's\schanges\sare\sappropriate\sfor\sWindows\sas\swell.\r\n\r\nHowever,\snow\shaving\slooked\sat\sthe\srecursive\simplementation\sfor\sPOSIX\s(which\sis\sessentially\snow\scloned\sfor\sWindows),\sI\sam\snot\sconvinced\sthe\simplementation\sis\scorrect\sin\sthe\sfirst\splace\s(i.e.\sbefore\sChristian's\schanges).\r\n\r\nConsider\sthe\slock\sfunction:\r\n\r\n```\r\nstatic\svoid\r\nPMutexLock(\r\n\s\s\s\sPMutex\s*pmutexPtr)\r\n{\r\n\s\s\s\sif\s(pmutexPtr->thread\s!=\spthread_self()\s||\spmutexPtr->counter\s==\s0)\s{\r\n\tpthread_mutex_lock(&pmutexPtr->mutex);\r\n\tpmutexPtr->thread\s=\spthread_self();\r\n\tpmutexPtr->counter\s=\s0;\r\n\s\s\s\s}\r\n\s\s\s\spmutexPtr->counter++;\r\n}\r\n```\r\n\r\nI\sam\sno\sexpert\son\sarchitectures,\sbut\sI\sam\spretty\ssure\saccessing\sthe\sthread\sand\scounter\sfields\soutside\sof\sany\slocks\sor\smemory\sbarriers\scan\sresult\sin\sstale\svalues\sbeing\sread\swith\sthe\scaller\serroneously\sthinking\sit\sis\sholding\sthe\slock\swhen\sit\sisn't\sand\sforegoing\sexecuting\sthe\s`pthread_mutex_lock`.\r\n\r\nThe\sequivalent\scode\son\sWindows\s(added\sby\sChristian)\sis\r\n\r\n```\r\n\s\s\s\sif\s(wmPtr->thread\s!=\sTcl_GetCurrentThread()\s||\swmPtr->counter\s==\s0)\s{\r\n\tEnterCriticalSection(&wmPtr->crit);\r\n\twmPtr->thread\s=\sTcl_GetCurrentThread();\r\n\twmPtr->counter\s=\s0;\r\n\s\s\s\s}\r\n\s\s\s\swmPtr->counter++;\r\n```\r\n\r\nand\ssuffers\sfrom\sthe\ssame\sissue.\r\n\r\nOn\sWindows,\sthe\sfix\sshould\sbe\ssimple\sby\smoving\sthe\sEnterCriticalSection\sabove\sthe\s`if`\sstatement\ssince\scritical\ssections\scan\sbe\srecursively\slocked.\sThis\sis\snot\spossible\son\sPOSIX\sfor\sobvious\schicken\sand\segg\sreasons\sas\sthe\smutex\sis\snot\srecursively\slockable.\r\n\r\nA\spossible\ssolution\sis\sto\sadd\sanother\sinternal\smutex\sto\sthe\smutex\swrapper\sthat\scontrols\saccess\sto\sthe\scounter\sand\sthread\sbut\sthat\sneeds\sdeeper\sthought\sto\sdetermine\swhether\sit\sis\sindeed\sa\sfix\sand\sdoes\snot\scause\sproblems\sof\sits\sown.\r\n\r\nThis\sgets\sback\sto\sSergey's\scomment\sabout\simplementing\ssynchronization\smechanisms\sbeing\sdicey\sand\seven\sif\sthe\sabove\ssuggested\sadditional\smutex\sis\sdeemed\sto\swork,\swe\sshould\sexplore\seliminating\srecursive\smutexes.\r\n\r\nAs\san\sexample,\sit\swas\sonly\sa\sfew\syears\sago\sthat\sI\slearnt\sthat\sthe\sdouble\slocking\sidiom\sthat\sI\shad\slong\spresumed\swas\ssafe\s(and\sis\sin\sfact\sextensively\sused\sin\sTcl)\sin\sfact\shas\srace\sconditions\son\ssome\sarchitectures\sand\splatforms\s(thankfully,\sI\s*think*\snot\son\sx86\sor\sARM).
J login apnadkarni
J mimetype text/x-markdown
K 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
U apnadkarni
Z aadaff50d7134fd27f98092dff5526c0