Priority Inversion and Unlimited Spin of pthread_rwlock_t

Peng Zheng pengzheng@apache.org
Tue Mar 12 03:48:27 GMT 2024


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



More information about the Libc-alpha mailing list