This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug nptl/7057] New: pthread rwlock does not implement 'writer preferred' option
- From: "udig at il dot ibm dot com" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 26 Nov 2008 15:56:26 -0000
- Subject: [Bug nptl/7057] New: pthread rwlock does not implement 'writer preferred' option
- Reply-to: sourceware-bugzilla at sourceware dot org
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 <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <error.h>
#include <string.h>
#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<NUM_THREADS;i++)
assert(0==pthread_create(readers+i,NULL,readfunc,(void *)i));
assert(0==pthread_create(&writer,NULL,writefunc,0));
printf("main waits\n");
pthread_join(writer,NULL);
return 0;
}
--
Summary: pthread rwlock does not implement 'writer preferred'
option
Product: glibc
Version: unspecified
Status: NEW
Severity: normal
Priority: P2
Component: nptl
AssignedTo: drepper at redhat dot com
ReportedBy: udig at il dot ibm dot com
CC: glibc-bugs at sources dot redhat dot com
http://sourceware.org/bugzilla/show_bug.cgi?id=7057
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.