public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Takashi Yano <takashi.yano@nifty.ne.jp>,
	cygwin@cygwin.com, Noel Grandin <noelgrandin@gmail.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	[thread overview]
Message-ID: <1721092.SVj2RUA3Z2@nimes> (raw)
In-Reply-To: <0126a98a-3bdc-4303-a0d2-09f4c7009392@gmail.com>

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




       reply	other threads:[~2024-05-30  8:47 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240530050538.53724-1-takashi.yano@nifty.ne.jp>
     [not found] ` <0126a98a-3bdc-4303-a0d2-09f4c7009392@gmail.com>
2024-05-30  8:47   ` Bruno Haible [this message]
2024-05-30  8:59     ` Noel Grandin
2024-05-30  9:15       ` Bruno Haible
2024-05-30 10:28         ` Noel Grandin
2024-05-30 13:53           ` SRW locks Bruno Haible
2024-05-30 10:14 ` [PATCH v2] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82 Bruno Haible
     [not found] ` <20240530205012.2aff4d507acac144e50df2a4@nifty.ne.jp>
     [not found]   ` <20240530205918.7c730117b567bb3bec3a0c3f@nifty.ne.jp>
2024-05-30 13:38     ` Bruno Haible
2024-05-31 14:01     ` Bruno Haible
2024-06-01 14:18       ` Takashi Yano
2024-06-01 16:08         ` Ken Brown
2024-06-01 21:11           ` Takashi Yano
2024-06-02 13:14         ` Bruno Haible
2024-06-02 14:27           ` Takashi Yano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1721092.SVj2RUA3Z2@nimes \
    --to=bruno@clisp.org \
    --cc=cygwin@cygwin.com \
    --cc=noelgrandin@gmail.com \
    --cc=takashi.yano@nifty.ne.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).