<i>Nested mutexes following TIP 509</i> status still Open with 3 other changes
by
.
D 2025-08-24T05:37:10.816
J icomment It\sgets\sworse\severy\sday:\snow\sread\sthis\sfine\sprint\squoted\sfrom\r\nhttps://forums.oracle.com/ords/apexds/post/recursive-mutex-and-conditional-wait-4703\r\n\r\n\s\sIn\spthread_mutexattr_settype\sman\spage\son\sSolaris\s7,\sit\sis\sstated\sthat\r\n\s\s"It\sis\sadvised\sthat\san\sapplication\sshould\snot\suse\sa\sPTHREAD_MUTEX_RECURSIVE\smutex\r\n\s\swith\scondition\svariables\sPTHREAD_MUTEX_RECURSIVE\sbecause\sthe\simplicit\sunlock\sperformed\r\n\s\sfor\sa\spthread_cond_wait()\sor\spthread_cond_timedwait()\swill\snot\sactually\srelease\sthe\r\n\s\smutex\s(if\sit\shad\sbeen\slocked\smultiple\stimes)...\r\n\r\nSo\sthe\scombination\sof\srecursive\smutex\sand\scondition\svariable\sis\r\na\sclassical\stwo\scomponent\sexplosive.\sAnd\smy\sinitial\sthoughts\son\r\ntrying\sto\sfix\sit\sfor\ssystems\swhich\snatively\sdo\snot\ssupport\r\nrecursive\smutexes\sthus\swere\smost\slikely\sa\scomplete\swaste\sof\stime.\r\n\r\nNow,\swhen\swe\stake\sthe\squoted\sstatement\s(which\sis\s25\syears\sold)\sserious\r\nwe\sshould\sprovide\sboth\srecursive\sand\splain\smutexes\sor\sditch\srecursive\r\nmutexes\sentirely.\sI'm\sin\sfavor\sof\sthe\slatter\s(and\sam\sstill\snot\ssorry\r\nfor\smy\snotorious\snoise).\r\n\r\nBTW:\shere\sis\sa\ssnippet\swith\swhich\sthe\squoted\sstatement\scan\snicely\sbe\r\nreproduced\s(it\spanics)\swhen\sthe\ssnippet\sis\scompiled\sin\stcl\strunk's\sready\r\nbuilt\sunix\ssubdirectory:\r\n\r\n---snip---\r\n#include\s<tcl.h>\r\n\r\nTCL_DECLARE_MUTEX(mutex);\r\nTcl_Condition\scond;\r\nstatic\sint\sdeadlock;\r\nstatic\sint\sstopit;\r\nstatic\sint\sstate;\r\n\r\nstatic\sTcl_ThreadCreateType\r\nWatchDog(void\s*arg)\r\n{\r\n\s\s\s\sint\sn\s=\s0;\r\n\r\n\s\s\s\swhile\s(!stopit\s&&\sn\s<\s20)\s{\r\n\s\s\s\s\s\s\s\sTcl_Sleep(100);\r\n\s\s\s\s\s\s\s\s++n;\r\n\s\s\s\s}\r\n\s\s\s\sif\s(deadlock)\s{\r\n\s\s\s\s\s\s\s\sTcl_Panic("Deadlock\sdetected");\r\n\s\s\s\s}\r\n\s\s\s\sTCL_THREAD_CREATE_RETURN;\r\n}\r\n\r\nstatic\sTcl_ThreadCreateType\r\nStrayCat(void\s*arg)\r\n{\r\n\s\s\s\swhile\s(!stopit)\s{\r\n\s\s\s\s\s\s\s\sTcl_MutexLock(&mutex);\r\n\s\s\s\s\s\s\s\sif\s(state\s==\s0)\s{\r\n\s\s\s\s\s\s\s\s\s\s\s\sstate\s=\s1;\r\n\s\s\s\s\s\s\s\s\s\s\s\sTcl_ConditionNotify(&cond);\r\n\s\s\s\s\s\s\s\s}\selse\sif\s(state\s==\s1)\s{\r\n\s\s\s\s\s\s\s\s\s\s\s\sstate\s=\s2;\r\n\s\s\s\s\s\s\s\s\s\s\s\sTcl_ConditionNotify(&cond);\r\n\s\s\s\s\s\s\s\s}\r\n\s\s\s\s\s\s\s\sTcl_MutexUnlock(&mutex);\r\n\s\s\s\s\s\s\s\sTcl_Sleep(100);\r\n\s\s\s\s}\r\n\s\s\s\sTCL_THREAD_CREATE_RETURN;\r\n}\r\n\r\nstatic\svoid\r\nTestFunc(void)\r\n{\r\n\s\s\s\sTcl_ThreadId\stid1,\stid2;\r\n\s\s\s\sint\sdummy;\r\n\r\n\s\s\s\sdeadlock\s=\s1;\r\n\s\s\s\sstopit\s=\s0;\r\n\s\s\s\sstate\s=\s0;\r\n\s\s\s\sTcl_CreateThread(&tid1,\sWatchDog,\sNULL,\r\n\s\s\s\s\s\s\s\s\s\s\s\sTCL_THREAD_STACK_DEFAULT,\sTCL_THREAD_JOINABLE);\r\n\s\s\s\sTcl_MutexLock(&mutex);\r\n\s\s\s\sTcl_CreateThread(&tid1,\sStrayCat,\sNULL,\r\n\s\s\s\s\s\s\s\s\s\s\s\sTCL_THREAD_STACK_DEFAULT,\sTCL_THREAD_JOINABLE);\r\n\s\s\s\swhile\s(state\s!=\s1)\s{\r\n\s\s\s\s\s\s\s\sTcl_ConditionWait(&cond,\s&mutex,\sNULL);\r\n\s\s\s\s}\r\n\s\s\s\sTcl_MutexLock(&mutex);\r\n\s\s\s\swhile\s(state\s!=\s2)\s{\r\n\s\s\s\s\s\s\s\sTcl_ConditionWait(&cond,\s&mutex,\sNULL);\r\n\s\s\s\s}\r\n\s\s\s\sTcl_MutexUnlock(&mutex);\r\n\s\s\s\sTcl_MutexUnlock(&mutex);\r\n\s\s\s\sstopit\s=\s1;\r\n\s\s\s\sTcl_JoinThread(tid2,\s&dummy);\r\n\s\s\s\sdeadlock\s=\s0;\r\n\s\s\s\sTcl_JoinThread(tid1,\s&dummy);\r\n}\r\n\r\nint\r\nmain(int\sargc,\schar\s**argv)\r\n{\r\n\s\s\s\sTestFunc();\r\n\s\s\s\sreturn\s0;\r\n}\r\n\r\n/*\r\n\s*\sLocal\sVariables:\r\n\s*\smode:\sc\r\n\s*\sc-basic-offset:\s4\r\n\s*\sfill-column:\s78\r\n\s*\scompile-command:\s"cc\stest.c\s-I../generic\s-I.\s-L.\s-ltcl9.1"\r\n\s*\sEnd:\r\n\s*/
J login chw
J mimetype text/plain
K 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
U chw
Z 35e157906b4e4dda0c4d5b2ae9ad7600