DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 44U8mXq92025392 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=IVSgB324 X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97FA23858C50 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1717058912; bh=fhTVSj+hvMZ7dk6R9WCGEbop0NerYIKwZh7GGt/mzIg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=IVSgB324/s38NreR1oxxCVm3Na+FD2GNmVVUDJc90Z4ta/e5Q85iKZ9i+QTIT9rXz IXHufqE9efmHVv6BRrVG46heCx7O0aVNA7o4rbk/fSk4xRJnyJanowpv6QO10TiK6R 2tQ9PcuPOjy5PPXRwVAUovdWtQ1qsZuTkOVzLbaI= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 990643858D20 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 990643858D20 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717058862; cv=pass; b=gS3HLJ8n69OmV3ZLlnyLuRByTXGTqIFjJ1JeqlzB+3m6rSfEnU6f+PeK3OXFSPoNdCi3QWbBpdzU9O1TK+QwptFKGoLn9V81U+4XQ/+BWPzYT71Xla1m+EH86nmk0FD38tB+uXN8sGx6/x/B7Mzjo6U71kyj360mMq4UtLGDaro= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717058862; c=relaxed/simple; bh=v78j/jeP5pyLQ4KxaVTwDKmXQqI6ddiK+/7+G0AUrG4=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=UQaNgEypqgYR2kwKLGRYzgdw/7CpbELXjvdbYhu/dWPcBDvgJHbMP4XgdOlET+y8jRHuIaDf544rg7Fg6bfqegcycYvn6Pk8HlGIcf1etwwwJoPwxExUhBHwWXpA45hoMxJYd4yiboK8AXhISSSiXQpziDP1XjTemAs56fAIEfI= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1717058858; cv=none; d=strato.com; s=strato-dkim-0002; b=k2NeS3a+l2+DuF4YtY9rqJaA51dCFAZaftHID2o+lLJeGwqI80BPztugdCI2vwSGSN 4IuLFu4lVHK5uVUBvXg5v5rKqpAMWjDAzZ5drq+sT6NRt07yQAOOuk40WMWdga7rCjnl IeiM61yskBeKJYhr9JRh2F6uvwz8dO7JBhKWk7pREgPbE/VJMPJrpknKftEzLfIDpViI iUs9EN0g5kxVcYmc1cZ3wrUNSqLVcteAUujS/W83fu5Nt9Fm85wrrv1uEG+SL4SQBXeC vOH48tkWvBIIOZ5/5+dA4qp63Y1pZMeT48lw572/BThmn0dhpsp8swAGyNULEB8olQbE 4IDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1717058858; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-ID:Date:Subject:To:From:Cc:Date:From: Subject:Sender; bh=fNiSxEZB+K05TI8gy9BJlF5ohwqTpAF/h2OAOkqbMT0=; b=mOUMCwQsZmSQN69+bBvBDPmqa7LEX5n+G/0PfmIdfYnW8v4FM4i71OPqTpNMO3WO1n rxyFbFP4TbVcwK87cMaUE6sfHZjfBTAQ+hNYe1+tk6iKD74nKZ/b9lzz7nBrd/YDM6o9 wLNPD22/tLXH04e4K0jI+/K46i1VMj+ggs0ecOww0C0PHSqd21D4TeQU3eyflrpuPaD2 lCY4a52lvsj8mN++5l7rQxxc5YKFd5mu0s8p6Ao5GOFV+2XlMWUMjMUiNI/AuEs4WVpW a/mYIraLNJdwfmKt2b8wRgHPlpdMvRzi4JcAoNRX4skvavR3aaSUilHvjkCDeAExtkun KQJg== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpOfjqSRCBA3DxwVMPzYVsfrcNDf" To: Takashi Yano <takashi DOT yano AT nifty DOT ne DOT jp>, cygwin AT cygwin DOT com, Noel Grandin <noelgrandin AT gmail DOT com> Subject: Re: [PATCH v2] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82 Date: Thu, 30 May 2024 10:47:38 +0200 Message-ID: <1721092.SVj2RUA3Z2@nimes> In-Reply-To: <0126a98a-3bdc-4303-a0d2-09f4c7009392@gmail.com> References: <20240530050538 DOT 53724-1-takashi DOT yano AT nifty DOT ne DOT jp> <0126a98a-3bdc-4303-a0d2-09f4c7009392 AT gmail DOT com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com> List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>, <mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe> List-Archive: <https://cygwin.com/pipermail/cygwin/> List-Post: <mailto:cygwin AT cygwin DOT com> List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help> List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>, <mailto:cygwin-request AT cygwin DOT com?subject=subscribe> From: Bruno Haible via Cygwin <cygwin AT cygwin DOT com> Reply-To: Bruno Haible <bruno AT clisp DOT org> Content-Type: text/plain; charset="utf-8" Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com> Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 44U8mXq92025392 Noel Grandin wrote in cygwin-patches: > Pardon my ignorance, but why not rather use the Windows SRWLock functionality? > https://learn.microsoft.com/en-us/windows/win32/sync/slim-reader-writer--srw--locks > > SRW locks are very fast, only require a single pointer-sized storage area, can be statically initialised, and do not > need to be destroyed, so there is no possibibilty of memory leakage. > > Then the implementation simply becomes > > int pthread::once (pthread_once_t *once_control, void (*init_routine) (void)) > { > AcquireSRWLockExclusive(once_control->lock); > if (!once_control->state) > { > init_routine() > once_control->state = 1; > } > ReleaseSRWLockExclusive(once_control->lock); > } SRW locks are spin-locks. Since they are only pointer-sized, ReleaseSRWLockExclusive cannot notify other threads — unlike CRITICAL_SECTION. Therefore, AcquireSRWLockExclusive must busy-loop when the lock is already held. But busy-looping is a bad practice for pthread_once. In the case, for example, that the init_routine reads a multi-megabyte data structure into memory and parses it, the other threads that wait on the result of this operation would by busy-looping, eating full CPU time. Bruno -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple