<i>Nested mutexes following TIP 509</i> status still Closed with 5 other changes
by
.
D 2025-10-01T09:01:53.194
J closedate 2460949.87631012
J closer jan.nijtmans
J icomment @ashok,\s+1\sto\sall\syour\sremarks.\sYou\stestcases\sare\sgreat,\sI'm\sa\shappy\suser.\r\n\r\nNext\sstep\sin\sChristians\sreasoning\s(it's\sindeed\sabout\sover-engineering)\scan\sbe\sfound\s[27ba494cc3a179e0|here].\sWe\sneed\sto\sassure\sthat\spmutexPtr->thread\sis\sfully\sinitialized\swhen\sthe\smutex\sis\sused\sfirst.\sIdea:\sinitialize\spmutexPtr->thread\s_before_\sinitializing\spmutexPtr->mutex.\sThe\sresulting\sassembler-code\sfor\sPMutexInit\sbecomes\smuch\sshorter:\r\n\r\n<pre>\r\n\s\s\s\s\s\s\s\smov\s\s\s\s\sQWORD\sPTR\s[rdi+40],\s0;\s#\spthread->thread\s=\s0\r\n\s\s\s\s\s\s\s\sxor\s\s\s\s\sesi,\sesi\s\s\s\s\s\s\s\s\s\s\s\s\s;\s#\sNULL\sargument\sof\spthread_mutex_init\r\n\s\s\s\s\s\s\s\smov\s\s\s\s\sDWORD\sPTR\s[rdi+48],\s0;\s#\spthread->counter\s=\s0\r\n\s\s\s\s\s\s\s\sjmp\s\s\s\s\spthread_mutex_init\r\n</pre>\r\n\r\nSince\spthread_mutex_init\saddresses\s[rdi+0]\sand\smemory-operations\sare\snot\sre-ordered\sby\sthe\sprocessor,\sthis\salso\sassures\sthat\s[rdi+40]\sis\sfully\swritten\sto\smemory\sbefore\s[rdi+0].\sWe\ssee\sthat\sthe\snull-argument\sof\spthread_mutex_init()\sis\sset\sbefore\spthread->counter\sis\sset,\sthis\sgives\spthread->thread\sa\slittle\sbit\smore\stime\sto\sbe\sreally\swritten\sto\smemory.\r\n\r\nSince\smemory-operations\sare\snot\sre-ordered\sby\sthe\sprocessor,\swe\scan\sbe\ssure\sthis\sway\sthat\spthread->thread\s(and\spthread->counter)\sare\sinitialized\sbefore\sthe\slock\sis\sused.\sWithout\susing\sany\sextra\slocks.\r\n\r\nWith\sthis\schange,\sPMutexLock()\sis\sthe\slast\sfunction\swhich\suses\satomic\soperations.\sBut\sthat's\sfor\sanother\sday.
J login jan.nijtmans
J mimetype text/x-fossil-wiki
K 893f8cc5db3ba8bd6bed80ce89a0a75c6088a37c
U jan.nijtmans
Z 7916665cf013dd416ac6d6e48e1aea75