public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Peng Zheng <pengzheng@apache.org>
To: libc-alpha@sourceware.org
Subject: Re: Priority Inversion and Unlimited Spin of pthread_rwlock_t
Date: Tue, 12 Mar 2024 11:48:27 +0800	[thread overview]
Message-ID: <b68aa613-7a1f-4fae-8bb9-f306b76cba74@apache.org> (raw)
In-Reply-To: <0e210b7b-5349-41fe-a476-87958990e03e@apache.org>

On 2024/3/12 11:19, Peng Zheng wrote:
> Hi,
> 
> I found that there are several unlimited spins in the current 
> pthread_rwlock's implementation.
> Therefore, it suffers from the same issue of user-space spinlocks as 
> mentioned in this LWN article ([0]):

I forget to mention this issue is glibc-specific.
Just checked musl's source code, all spin in its pthread_rwlock_t is 
limited to 100. For example:

int __pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rw, const 
struct timespec *restrict at)
{
	int r, t;
	
	r = pthread_rwlock_trywrlock(rw);
	if (r != EBUSY) return r;
	
	int spins = 100;
	while (spins-- && rw->_rw_lock && !rw->_rw_waiters) a_spin();

	while ((r=__pthread_rwlock_trywrlock(rw))==EBUSY) {
		if (!(r=rw->_rw_lock)) continue;
		t = r | 0x80000000;
		a_inc(&rw->_rw_waiters);
		a_cas(&rw->_rw_lock, r, t);
		r = __timedwait(&rw->_rw_lock, t, CLOCK_REALTIME, at, rw->_rw_shared^128);
		a_dec(&rw->_rw_waiters);
		if (r && r != EINTR) return r;
	}
	return r;
}

Regards,

-- 
Peng Zheng


  reply	other threads:[~2024-03-12  3:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-12  3:19 Peng Zheng
2024-03-12  3:48 ` Peng Zheng [this message]
2024-03-14  7:32   ` Peng Zheng
2024-03-12 14:39 ` Florian Weimer
2024-03-13  1:48   ` Peng Zheng

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=b68aa613-7a1f-4fae-8bb9-f306b76cba74@apache.org \
    --to=pengzheng@apache.org \
    --cc=libc-alpha@sourceware.org \
    /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).