From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 24435 invoked from network); 23 Feb 2022 20:06:50 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 23 Feb 2022 20:06:50 -0000 Received: (qmail 20412 invoked by uid 550); 23 Feb 2022 20:06:48 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 20380 invoked from network); 23 Feb 2022 20:06:48 -0000 Date: Wed, 23 Feb 2022 15:06:35 -0500 From: Rich Felker To: Markus Wichmann Cc: musl@lists.openwall.com Message-ID: <20220223200633.GY7074@brightrain.aerifal.cx> References: <20220221174223.GA2079@voyager> <20220223185746.GB2079@voyager> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220223185746.GB2079@voyager> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] Suggestion for thread safety On Wed, Feb 23, 2022 at 07:57:46PM +0100, Markus Wichmann wrote: > On Wed, Feb 23, 2022 at 12:30:43AM +0000, Lee Shallis wrote: > > in other words just > > with LOCK & pauseCB I've achieved thread safety without the file > > knowing anything about the system api, > > You have indeed not done that. You have instead written the word "lock" > enough times to give someone skim-reading the file false confidence that > this stuff will actually work in a multi-threaded context, only to then > fail under high load for inexplicable reasons. > > I keep seeing this behavior from programmers that ought to know better. > You see, an exclusive lock consists of two parts: The mutual exclusion > and the sleep. And yes, spinlocks skip the second part, but my point is: > The mutual exclusion is actually the easy part, and any hack with a > Messiah complex and a CPU manual can do it. The sleep is the hard part, > if you want to do it right. It needs to be Goldilocks. Too short, and > you are wasting resources (every time your thread spins in the loop is > time the CPU could have better spent on other threads), too long and you > are wasting time. > > Your sleep is definitely too short, and you didn't even get the mutual > exclusion part right. It's worse: it has *three* parts, the third being the _synchronizing memory_ part, which I'm guessing this made no attempt to do at all. That's where all the time in a lock is actually spent, and if you somehow avoid doing that (note: x86 will mostly do it for you and send you the bill), things will blow up spectacularly.