DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 452ERll83409633 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=YBl3vhxR X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 45E6F389A129 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1717338465; bh=3vzyjmeWgWzAaolEQ5jZ8Ysh5elgJOy9JJPqpjb3l7Y=; h=Date:To:Cc:Subject:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=YBl3vhxRH1R6U7C4zi6ReWgfkO24gPBFbQkrwctT20cwBfBuj6evV22hZI548ITLX ar5imfQw/5yh32GfcoEK4F/SX3sZmzDXbBvXu9fQdltn8SA2yxWwmcOz6sLQeesaw5 xpiKEfmfSgQrkdZa4LvNsa3fh/np7k8AWBrvQA7k= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54B26389909A ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 54B26389909A ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717338444; cv=none; b=N90jb77Smpk9aIsgh3PNMpoR1t41phlaWHAzPzCHSECoIrDoc3boupif61J3J/dsvweSx7VL4I/HHPfBih9g0CLAiPrJNQGyTFLijoXlrsb/PsHuo1bseBLssHfbfS8JTpptHfXyaiJ+FZkcKG0diyky2C4gwny6PdG5B8mXOQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717338444; c=relaxed/simple; bh=TrK3c90sakmEsMH4WQFuVBDbcH/kfRP/tayJg1ZhzRA=; h=Date:From:To:Subject:Message-Id:Mime-Version:DKIM-Signature; b=Rzp1fEN0de4jTXY1QycJDxA3x4PoFgRgV6G83kPxuGZXXLLOhMyt2GylS4pZx0q+RInVa+FvdT17a8ylaiHvHL2qHA0DrfA9j5p1lOnNHnOldsuNdrO2QY9b3+ZxlD9XFzOU6DWHEOR/3d8kYd4erpHpt1/ubpCQne1dwt621Fo= ARC-Authentication-Results: i=1; server2.sourceware.org Date: Sun, 2 Jun 2024 23:27:17 +0900 To: Bruno Haible Cc: cygwin AT cygwin DOT com Subject: Re: [PATCH v2] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82 Message-Id: <20240602232717.25baf1bdfe7ac283dea96899@nifty.ne.jp> In-Reply-To: <1904124.oxc7TNfbsn@nimes> 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> <1904124 DOT oxc7TNfbsn AT nimes> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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: Takashi Yano via Cygwin Reply-To: Takashi Yano Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" On Sun, 02 Jun 2024 15:14:51 +0200 Bruno Haible wrote: > 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 Thanks for checking. I'll push the patch after the modification. -- Takashi Yano -- 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