.
D 2023-05-21T20:35:27.059
J assignee nobody
J closer nobody
J cmimetype text/x-markdown
J comment "Double-checked\slocking"\spervades\sthe\sTcl\simplementation\sbut\sis\sbroken\sunder\sPOSIX\sand\smany\sprocessor\smemory\smodels.\r\n=======================================================================================================================\r\n\r\n##\sProblem\sdescription\r\n\r\n"Double-checked\slocking"\sis\sa\sdiscredited\sprogramming\spattern\sthat\sgoes\slike\sthis:\r\n```\r\nif\s(0\s==\svar)\s{\r\n\s\s\s\sacquire\slock;\r\n\s\s\s\sif\s(0\s==\svar)\s{\s/*\sdouble\scheck\s*/\r\n\s\s\s\s\s\s\stemp\s=\sallocate\s(sizeof\s*var);\r\n\s\s\s\s\s\s\sinitialize\s*temp;\r\n\s\s\s\s\s\s\svar\s=\stemp;\r\n\s\s\s\s}\r\n\s\s\s\srelease\slock;\r\n}\s/*\svar\spoints\sto\san\sinitialized\sobject\sthat\shas\sbeen\sinitialized\sexactly\sonce\r\n\s\s\s\s\sNOT\s*/\r\n```\r\n\r\nA\sprocessor\soperating\sunder\sa\srelaxed\smemory\smodel\scan\sread\sa\snon-null\s`var`,\r\nyet\ssee\sa\snon-initialized\s`*var`.\s\s(See\sthe\sreferences\sin\s[double-checked\slocking](https://en.wikipedia.org/wiki/Double-checked_locking)).\r\n\r\nIt\smay\sbe\sthat\sthis\sbug\sdoes\snot\soccur\swith\sprocessors\simplementing\sthe\sTotal-Store\sOrdering\s(TSO)\smemory\smodel,\si.e.,\sx86,\sand\sas\san\soptional\sfeature\son\sSPARC,\sRISC-V\sand\ssome\sARM\smodels\sfrom\sApple,\sbut\sthat\sis\ssomething\sI\swould\snot\sput\smy\shand\sin\sthe\sfire\sfor.\r\n\r\nPOSIX\scompliant\ssystems\sare\snot\srequired\sto\ssupport\sthis\spattern,\ssee\s[Memory\sSynchronization](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12):\r\n\r\n>\sApplications\sshall\sensure\sthat\saccess\sto\sany\smemory\slocation\sby\smore\sthan\sone\sthread\sof\scontrol\s(threads\sor\sprocesses)\sis\srestricted\ssuch\sthat\sno\sthread\sof\scontrol\scan\sread\sor\smodify\sa\smemory\slocation\swhile\sanother\sthread\sof\scontrol\smay\sbe\smodifying\sit.\sSuch\saccess\sis\srestricted\susing\sfunctions\sthat\ssynchronize\sthread\sexecution\sand\salso\ssynchronize\smemory\swith\srespect\sto\sother\sthreads.\sThe\sfollowing\sfunctions\ssynchronize\smemory\swith\srespect\sto\sother\sthreads:\s\s\r\n\\[The\slist\scomprises\sthe\sPOSIX\smutex/condition\svariable/rw-locks/barrier/spin-lock/semaphore\sand\sthread\s(un-)locking/signaling/waiting/create/joining\soperations,\sas\swell\sas\sthe\sonce\sobject.\\]\r\n\r\nI\sam\sunaware\sif\sWindows/ARM\srequires\sTSO.\s\sAs\sstandard\sARM\scores\sdo\snot\ssupport\sTSO,\sI\swould\sbe\ssurprised.\r\n\r\nUnfortunately,\sthis\spattern\spervades\sthe\sTcl\simplementation.\sClean\sup\sis\sa\s**major**\sundertaking.\r\n\r\nI\scan\sonly\sspeculate\sto\swhy\sdouble-checked\slocking\swas\sused\sby\sthe\soriginal\sTcl\simplementers:\sit\scertainly\swas\sa\sextremely\spopular\spattern\saround\sthe\smillennium;\sthe\sfact\sthat\sWindows\sintroduced\sOnce\svariables\s(and\scondition\svariables)\sonly\swith\sVista\sand\sSolaris\sThreads\snot\sat\sall\sprobably\splayed\sa\srole.\r\n\r\nThe\schanges\salso\saffect\sthe\spublic\sC\sAPI.\r\nThe\s`Tcl_Condition`,\s`Tcl_Mutex`,\sand\s`Tcl_ThreadDataKey`\stypes\sall\ssuffer\sfrom\sthis\sproblem:\sthese\stypes\sare\s(opaque)\spointers\sthat\sare\sinitialized\simplicitly\swith\sexactly\sthe\slogic\sshown\sabove:\sif\son\se.g.\sa\smutex\slock\soperation,\sthe\svariable\scontains\sa\snull\spointer,\sa\scentral\slock\sis\staken\sand\sthe\svariable\sre-checked.\r\n\r\n##\sHow\sto\ssolve\r\nExplicit\sinitialization\sfunctions\sfor\sthese\sdata\stypes\smust\sbe\sprovided\s(i.e.,\scounterparts\sto\sthe\s`Tcl_MutexFinalize`,\setc.\sroutines).\r\n\r\nPOSIX\sprovides\sthe\s`pthread_once_t`\stype\sto\smanage\sinitialization\sin\sa\smulti-threaded\sprocess:\sinvoking\s`pthread_once()`\son\sa\svariable\sof\sthis\stype\stogether\swith\sa\scallback\sroutine\sensures\sthe\scallback\sroutine\sis\sexecuted\sexactly\sonce\sin\sa\smulti-threaded\sprogram.\s\sA\sTcl\sversion\sof\sthis\stype\smust\sbe\sprovided.\r\n\r\nA\scomplication\sarises\sbecause\sa\s`pthread_once_t`\svariable\smust\sbe\sinitialized\svia\san\sinitializer\sspecified\sin\sPOSIX\sas\sthe\smacro\s`PTHREAD_ONCE_INIT`.\s\sAs\sthe\sTcl\s`Once`\simplementation\smust\swrap\sthe\sPOSIX\s(resp.\sWindows,\swhich\shas\sa\sanalogous\stype\sand\smacro)\simplementation,\s`pthread_once_t`\smust\sbe\svisible\swhen\sincluding\s`tcl.h`,\swhich\shas\sup\sto\snow\snot\sincluded\seither\s`<pthread.h>`\sor\s`<windows.h>`,\sat\sleast\sif\s`pthread_once_t`\s(resp.\sthe\sWindows\svariant)\sare\sto\s\sbe\sunderstood\sby\sthe\scompiler\s(i.e.,\sreferred\sto\sby\s`typedef`\sor\smember\sof\san\saggregate\stype).\r\n\r\nSince\sa\sTcl\s`Tcl_Once`\stype\scannot\sbe\sopaque\sany\smore\s--\sit\smust\sbe\sinitialized\svia\s`TCL_ONCE_INIT`,\sthe\squestion\sarises\sif\s`Tcl_Mutex`,\s`Tcl_Condition`\sand\s`Tcl_ThreadKeyData`\sshould\sremain\spointers\sto\sopaque\stypes.\s\sThe\sdrawback\sis\sthe\sextra\sindirection\srequired;\sthe\sadvantage\sof\sthe\scurrent\ssystem\sis\sthat\sthe\ssize\sof\sthe\sunderlying\sobjects\sis\snot\svisible\sin\sthe\sAPI\sand\shence\scan\sbe\schanged\sfrom\sversion\sto\sversion.\s\sAs\sthese\sobjects\sare\svery\sstable\s(as\sare\sthe\sOS-objects\sunderlying\sthem;\sthey\shave\sthe\ssame\sconstraints\son\sbinary\scompatibility),\sI\swould\sargue\sto\smake\sthese\sobjects\snon-opaque,\si.e.,\shave\s`Tcl_Mutex`\setc.\sbe\stypedefs\sto\sstructures.\r\n\r\nSome\sanalysis\sof\sthe\s`win`\sand\s`generic`\sportions\sof\sthe\ssource\sbase\sshow\smany,\smany\sinstances\sof\sdouble-checked\slocking,\sespecially\swith\sregards\sto\sinitialization.\sNewer\sUnix\sportions\sdo\suse\s`pthread_once_t`\sinternally,\sbut\sthe\scode\sneeds\sreview\sas\swell.\r\n\r\n##\sI\sthink\sthis\sshould\sbe\sresolved\sas\spart\sof\sthe\sTcl\s9.0\sreleases,\ssince\schanges\sare\srequired\sto\sthe\sAPI.
J foundin all
J is_private 0
J login konrad.schwarz
J priority 5\sMedium
J private_contact 80ea11e9d893f90ae1b5d3c2e6c94600b62863e9
J resolution None
J severity Critical
J status Open
J submitter konrad.schwarz
J subsystem 41.\sMemory\sAllocation
J title Double-checked\slocking\spervades\sthe\sTcl\simplemetation
J type Bug
K 112af76b090286f9f0f04c8b994c252bb1b82bd8
U konrad.schwarz
Z 821ddf5f617c85d3c8d9eed7b07e1dea