From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12864 invoked by alias); 26 Nov 2008 15:57:42 -0000 Received: (qmail 9662 invoked by uid 48); 26 Nov 2008 15:56:26 -0000 Date: Wed, 26 Nov 2008 15:57:00 -0000 From: "udig at il dot ibm dot com" To: glibc-bugs@sources.redhat.com Message-ID: <20081126155626.7057.udig@il.ibm.com> Reply-To: sourceware-bugzilla@sourceware.org Subject: [Bug nptl/7057] New: pthread rwlock does not implement 'writer preferred' option X-Bugzilla-Reason: CC Mailing-List: contact glibc-bugs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: glibc-bugs-owner@sourceware.org X-SW-Source: 2008-11/txt/msg00073.txt.bz2 When creating a rwlock, ask for PTHREAD_RWLOCK_PREFER_WRITER_NP. The resulting behavior is identical to PTHREAD_RWLOCK_PREFER_READER_NP, as can be seen in the source. The following test case shows that as long as there are readers holding the lock, a writer thread will be starved forever. However, if the PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP option is used, the writer thread gets to run. It is not allowed to be recursive, however. ------------------------------------------------------------- #define _XOPEN_SOURCE 600 #include #include #include #include #include #include #include #define NUM_THREADS (250) pthread_rwlock_t lock; void *readfunc(void *arg) { long long id = (long long)arg; while (1) { struct timespec ts = {.tv_sec = 0,.tv_nsec = (id%25 +1)*1000*1000 }; assert(0==pthread_rwlock_rdlock(&lock)); nanosleep(&ts,NULL); assert(0==pthread_rwlock_unlock(&lock)); } } void *writefunc(void *arg) { sleep(1); assert(0==pthread_rwlock_wrlock(&lock)); // assert(0==pthread_rwlock_wrlock(&lock)); //would fail if non-recursive printf("Writer got a chance!\n"); // assert(0==pthread_rwlock_unlock(&lock)); assert(0==pthread_rwlock_unlock(&lock)); return 0; } int main(int argc,char *argv[]) { pthread_t writer,readers[NUM_THREADS]; pthread_rwlockattr_t lockattr; assert(0==pthread_rwlockattr_init(&lockattr)); assert(0==pthread_rwlockattr_setkind_np(&lockattr,PTHREAD_RWLOCK_PREFER_WRITER_NP)); assert(0==pthread_rwlock_init(&lock,&lockattr)); assert(0==pthread_rwlockattr_destroy(&lockattr)); for (long long i=0;i