Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion convert read lock on pthread_rwlock to write lock...
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Joe Seigh  
View profile  
 More options Dec 21 2001, 6:25 am
Newsgroups: comp.programming.threads
From: Joe Seigh <jse...@genuity.com>
Date: Fri, 21 Dec 2001 11:25:00 GMT
Subject: Re: [Q] convert read lock on pthread_rwlock to write lock...

As an interesting side note, I always though that a bakery style
spin lock would be quite adaptable to use as an upgrade lock.

Basically the rules for bakery style read/write locks are

writers wait for all prior accesses to complete
readers wait for all prior write accesses to complete

read then write would be
        wait for all prior write accesses to complete
        perform read access
        wait for all prior read accesses to complete
        perform write access

In pseudo C w/ perl style assignments so I can show
the atomic bits which would probably be implemented
with compare and swap or something.

// global
int cs = 0; // current
int cx = 0;

int ns = 0; // next
int nx = 0;

// thread local
int ws = 0; // wait
int wx = 0;

// read
    (ws, wx) = (ns++, nx);  // atomic
    while (wx != cx) {}     // wait for prior write accesses to complete
    ... // read access
    cs++;                   // complete read access

// write
    (ws, wx) = (ns, nx++);  // atomic
    while (wx != cx && ws != cs) {}     // wait for prior accesses to complete
    ... // write access
    cx++;                   // complete write access

// note that
//    while (wx != cx && ws != cs)
// is equivalent to
//    while (wx != cx) {}
//    while (ws != cs) {}

// read then upgrade to write
    (ws, wx) = (ns, nx++);  // atomic
    while (wx != cx) {}     // wait for prior write accesses to complete
    ... // read access
    while (ws != cs) {}     // wait for prior read accesses to complete
    ... // write access
    cx++;                   // complete write access

Joe Seigh


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.