public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "udig at il dot ibm dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sources.redhat.com
Subject: [Bug nptl/7057] New: pthread rwlock does not implement 'writer preferred' option
Date: Wed, 26 Nov 2008 15:57:00 -0000	[thread overview]
Message-ID: <20081126155626.7057.udig@il.ibm.com> (raw)

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.


             reply	other threads:[~2008-11-26 15:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-26 15:57 udig at il dot ibm dot com [this message]
2008-11-26 16:11 ` [Bug nptl/7057] " drepper at redhat dot com
2008-11-30 17:35 ` sjmunroe at us dot ibm dot com

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=20081126155626.7057.udig@il.ibm.com \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sources.redhat.com \
    /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).