pthread_rwlock_tryrdlock bug (with test & patch)

Antti Kantee pooka@iki.fi
Sun Jan 6 17:24:00 GMT 2013


[I'd submit this to cygwin-patches, but the web page says only 
subscribers can post to that list, so please bear with me for this 
simple issue]

Hi,

Calling pthread_rwlock_tryrdlock() twice from the same thread always 
fails with EBUSY the second time.  See attached test.c.  Replacing the 
second tryrdlock() with rdlock() makes the call succeed, leading me to 
believe there is a bug in tryrdlock().

Assuming I was looking at the correct source file, the attached patch 
should fix the issue.  I'm not sure why the lookup_reader() call was 
there in the first place; perhaps a remnant from a time when recursive 
read locking was not supported?

While looking at tryrdlock(), the handling of ULONG_MAX also seems 
wrong, as you'd probably want to return EAGAIN instead of allocating 
another reader structure.  However, that problem is arguably more of an 
academic issue and therefore I didn't touch it in my patch.

   - antti

p.s. please cc me on any responses
-------------- next part --------------
#include <err.h>
#include <pthread.h>
#include <stdio.h>

int
main()
{
	pthread_rwlock_t lck;
	int rv;

	pthread_rwlock_init(&lck, NULL);
	if ((rv = pthread_rwlock_tryrdlock(&lck)) != 0)
		errx(1, "lock 1: %d", rv);
	if ((rv = pthread_rwlock_tryrdlock(&lck)) != 0)
		errx(1, "lock 2: %d", rv);
	printf("success\n");
}
-------------- next part --------------
--- src/winsup/cygwin/thread.cc.old	2012-08-17 01:34:45.000000000 +0200
+++ src/winsup/cygwin/thread.cc	2013-01-06 17:58:46.258963200 +0100
@@ -1427,7 +1427,7 @@ pthread_rwlock::tryrdlock ()
 
   mtx.lock ();
 
-  if (writer || waiting_writers || lookup_reader (self))
+  if (writer || waiting_writers)
     result = EBUSY;
   else
     {

-------------- next part --------------
--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


More information about the Cygwin mailing list