Boost logo

Threads-Devel :

From: Russell Kliese (russell_at_[hidden])
Date: 2007-04-12 07:44:11


I've been trying to come up with a timed read/write lock for Linux to
replace the implementation that was pulled from Boost. After looking
over some of the messages is the mailing list, I decided to investigate
wrapping up pthreads.

It already has a timed read/write lock implementation. However, I can't
get it to work how I want it to. I was wondering if someone on this list
might be able to help me out and perhaps I can come up with something
that can help the new Boost implementation.

According to this:
http://cs.pub.ro/~apc/2003/resources/pthreads/uguide/users-89.htm, there
should be no problem promoting a read lock to a write lock (assuming you
already have a read lock, you just try to obtain a write lock).

However, it doesn't work as advertised (and function names and return
values are also different -- perhaps there is better linux pthreads
docs, but I have yet to stumble upon them).

After looking at the code (glibc-2.3.6.ds1), it looks like it is
impossible to upgrade to a write-lock without first releasing the read
lock (rwlock.c):

=====================================

int
__pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
                              const struct timespec *abstime)
{
  pthread_descr self;
  pthread_extricate_if extr;

  if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
    return EINVAL;

  self = thread_self ();

  /* Set up extrication interface */
  extr.pu_object = rwlock;
  extr.pu_extricate_func = rwlock_wr_extricate_func;

  /* Register extrication interface */
  __pthread_set_own_extricate_if (self, &extr);

  while(1)
    {
      __pthread_lock (&rwlock->__rw_lock, self);

      if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
        {
          rwlock->__rw_writer = self;
          __pthread_set_own_extricate_if (self, 0);
          __pthread_unlock (&rwlock->__rw_lock);
          return 0;
        }

      /* Suspend ourselves, then try again */
 
etc....

=====================================

__rw_readers has to be zero, therefore it looks like the read lock must
be released before obtaining a write lock (when a write lock is
obtained, this value is incremented). This seems bad to me as it makes
it tricky to handle upgrading and downgrading locks.

Am I missing something here?

Regards,

Russell Kliese


Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk