DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 452DFmqU3385662 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=rI3vZLw3 X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 90B24389EC90 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1717334146; bh=S9886tmxJfqJ827Rxn+C3Wse/N2mN5lZwRzSazeWXK8=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=rI3vZLw3E2hu4TiA8eqkn9iffkHd3AcjonZr6+UvP9Vnm276fkGM0Io1agVTlqnt5 dodd/296I8/sNGIk2cgRTbh6kNL35nLx5JgIghZmqUmsHYdwOIWbRvr3T4SkWrnL0Z KR5tLgx8Mxm3iuXf2z6gmYJHm2uS8/Ux0tvm7mGQ= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48F9038930CF ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 48F9038930CF ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717334096; cv=pass; b=Qs+Ro1dPENCV8emfzJAdRLvK2qaRsovWafWMqsgKoModXp//+P+6l4eXzAZyQpmH/P7NwbN7tkwy33fYPmcWtsG6ktjSdifSyz8s+ZQuLctaA40NENe5HsMaIWviBE84lNQi/0W87ENUek4ts0Bn7K3zae2hcHa+czUbKgk4wyQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1717334096; c=relaxed/simple; bh=7dDUa9r9wps3HIpAI8esuo2nj3KTyw5r2E4M3BM8kiw=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=tlPm6E25wvwwBh0krKtrRYgX2q3O3TM3nSDrOqElGnqbutMiHYHlHQ0sdabNJvTluCr9NNBiwcAZ+Uj0PSuei3rJXKzUrjfkVfnsi/n4OAKRZ0dvmBo58ktY/aTQwnjW7WY/BjnGeFa6wP8ynvysd8GkijwcXqxQiwdxGqgW54Q= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; t=1717334092; cv=none; d=strato.com; s=strato-dkim-0002; b=WR6GBV5i25f9n1Oj5evehHErXGsQE+lwK8ti9pQh7+Xja67nwFMH1dlu/B74VKfW3B JqueLTSgBsQwyDIeYX2GNvoEk2hOmh+XrDzzpqixXoyMZMxbhxheAfNmvsaSLeqcLmEQ 6y2ejnF7VEOQU6456YK5d71mkGXiCX/MOCoFGq090JHXY8Itdv245+4876+Ff4zPcGYr k2SPzncgJCQYS2Vry7AhYdmGRVO7zoI7YPxmORe2lpEdegYZx20kDLo8I1YK8QR+l9F9 svgyBSlEayTzSnB51WxRM2+9P8QuWWH3IIPVLVPCue0A32vzc5uo94563aMJOG0hb6Ql jjaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1717334092; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=XZPqo0s+w2EqvGITyCZ6OP9fqc+F3DvyAKrpsWwoLDw=; b=c8dSwdvtRZWsxIsf1JDLm3rhuHTdh29rr+/j0eFHTGv86xkxYTWeJI0LmNYz6eoclb D2m/qaAXopSeV3ktaITpMZvMGEanPBwjjYxegY7LqgSA9kKVWpYYmZ1903KiHGJzmfDJ ZAjsFtj+LG6aTvpGtE0eb4wPDqkUZyFPclhBdCSdyi0fqKg7KO8svvWCJ4ZMmZT+lX/l kBmtwXa/bPPASChygLB+lV25cXmC31PwfR/6Dvz0+4r4jLO1Y7DZtXBk22GqtgEl254f dnaztPOBu47hAOQj2tLNcDCBml6zHqkEkfrliqeE5CZzFkwIhJoyEQvzYfbIF79InZlY i1Lg== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpPD1acihFmvqn7V2YisZYMQHV5gcw==" To: Takashi Yano Cc: cygwin AT cygwin DOT com Subject: Re: [PATCH v2] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82 Date: Sun, 02 Jun 2024 15:14:51 +0200 Message-ID: <1904124.oxc7TNfbsn@nimes> In-Reply-To: <20240601231830.882dc56aadb9c3087bcf4b9c@nifty.ne.jp> References: <20240530050538 DOT 53724-1-takashi DOT yano AT nifty DOT ne DOT jp> <4338587 DOT 3DMzsUbDvx AT nimes> <20240601231830 DOT 882dc56aadb9c3087bcf4b9c AT nifty DOT ne DOT jp> 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Bruno Haible via Cygwin Reply-To: Bruno Haible Content-Type: text/plain; charset="utf-8" Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 452DFmqU3385662 Hi Takashi Yano, > The result is as follows (submitted as v4 patch). > > int > pthread::once (pthread_once_t *once_control, void (*init_routine) (void)) > { > /* Sign bit of once_control->state is used as done flag. > Similary, the next significant bit is used as destroyed flag. */ > const int done = INT_MIN; /* 0b1000000000000000 */ > const int destroyed = INT_MIN >> 1; /* 0b1100000000000000 */ > if (once_control->state & done) > return 0; > > /* The type of &once_control->state is int *, which is compatible with > LONG * (the type of the pointer argument of InterlockedXxx()). */ > if ((InterlockedIncrement (&once_control->state) & done) == 0) > { > pthread_mutex_lock (&once_control->mutex); > if (!(once_control->state & done)) > { > init_routine (); > InterlockedOr (&once_control->state, done); > } > pthread_mutex_unlock (&once_control->mutex); > } > InterlockedDecrement (&once_control->state); > if (InterlockedCompareExchange (&once_control->state, > destroyed, done) == done) > pthread_mutex_destroy (&once_control->mutex); > return 0; > } > ... > I believe both codes are equivalent. Could you please check? Yes, they are equivalent. This code is free of race conditions. (Let's hope I am not making a mistake again.) For legibility I would write the constant values as bit masks: 0x80000000 0xc0000000 and - following the habit that constant integers should have names in upper case - I would rename done → DONE destroyed → DESTROYED 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