Back to index

glibc  2.9
rwlock.c
Go to the documentation of this file.
00001 /* Read-write lock implementation.
00002    Copyright (C) 1998, 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Xavier Leroy <Xavier.Leroy@inria.fr>
00005    and Ulrich Drepper <drepper@cygnus.com>, 1998.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public License as
00009    published by the Free Software Foundation; either version 2.1 of the
00010    License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00019    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.  */
00021 
00022 #include <bits/libc-lock.h>
00023 #include <errno.h>
00024 #include <pthread.h>
00025 #include <stdlib.h>
00026 #include "internals.h"
00027 #include "queue.h"
00028 #include "spinlock.h"
00029 #include "restart.h"
00030 
00031 /* Function called by pthread_cancel to remove the thread from
00032    waiting inside pthread_rwlock_timedrdlock or pthread_rwlock_timedwrlock. */
00033 
00034 static int rwlock_rd_extricate_func(void *obj, pthread_descr th)
00035 {
00036   pthread_rwlock_t *rwlock = obj;
00037   int did_remove = 0;
00038 
00039   __pthread_lock(&rwlock->__rw_lock, NULL);
00040   did_remove = remove_from_queue(&rwlock->__rw_read_waiting, th);
00041   __pthread_unlock(&rwlock->__rw_lock);
00042 
00043   return did_remove;
00044 }
00045 
00046 static int rwlock_wr_extricate_func(void *obj, pthread_descr th)
00047 {
00048   pthread_rwlock_t *rwlock = obj;
00049   int did_remove = 0;
00050 
00051   __pthread_lock(&rwlock->__rw_lock, NULL);
00052   did_remove = remove_from_queue(&rwlock->__rw_write_waiting, th);
00053   __pthread_unlock(&rwlock->__rw_lock);
00054 
00055   return did_remove;
00056 }
00057 
00058 /*
00059  * Check whether the calling thread already owns one or more read locks on the
00060  * specified lock. If so, return a pointer to the read lock info structure
00061  * corresponding to that lock.
00062  */
00063 
00064 static pthread_readlock_info *
00065 rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
00066 {
00067   pthread_readlock_info *info;
00068 
00069   for (info = THREAD_GETMEM (self, p_readlock_list); info != NULL;
00070        info = info->pr_next)
00071     {
00072       if (info->pr_lock == rwlock)
00073        return info;
00074     }
00075 
00076   return NULL;
00077 }
00078 
00079 /*
00080  * Add a new lock to the thread's list of locks for which it has a read lock.
00081  * A new info node must be allocated for this, which is taken from the thread's
00082  * free list, or by calling malloc. If malloc fails, a null pointer is
00083  * returned. Otherwise the lock info structure is initialized and pushed
00084  * onto the thread's list.
00085  */
00086 
00087 static pthread_readlock_info *
00088 rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
00089 {
00090   pthread_readlock_info *info = THREAD_GETMEM (self, p_readlock_free);
00091 
00092   if (info != NULL)
00093     THREAD_SETMEM (self, p_readlock_free, info->pr_next);
00094   else
00095     info = malloc(sizeof *info);
00096 
00097   if (info == NULL)
00098     return NULL;
00099 
00100   info->pr_lock_count = 1;
00101   info->pr_lock = rwlock;
00102   info->pr_next = THREAD_GETMEM (self, p_readlock_list);
00103   THREAD_SETMEM (self, p_readlock_list, info);
00104 
00105   return info;
00106 }
00107 
00108 /*
00109  * If the thread owns a read lock over the given pthread_rwlock_t,
00110  * and this read lock is tracked in the thread's lock list,
00111  * this function returns a pointer to the info node in that list.
00112  * It also decrements the lock count within that node, and if
00113  * it reaches zero, it removes the node from the list.
00114  * If nothing is found, it returns a null pointer.
00115  */
00116 
00117 static pthread_readlock_info *
00118 rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
00119 {
00120   pthread_readlock_info **pinfo;
00121 
00122   for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
00123     {
00124       if ((*pinfo)->pr_lock == rwlock)
00125        {
00126          pthread_readlock_info *info = *pinfo;
00127          if (--info->pr_lock_count == 0)
00128            *pinfo = info->pr_next;
00129          return info;
00130        }
00131     }
00132 
00133   return NULL;
00134 }
00135 
00136 /*
00137  * This function checks whether the conditions are right to place a read lock.
00138  * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
00139  * locked upon entry.
00140  */
00141 
00142 static int
00143 rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
00144 {
00145   /* Can't readlock; it is write locked. */
00146   if (rwlock->__rw_writer != NULL)
00147     return 0;
00148 
00149   /* Lock prefers readers; get it. */
00150   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
00151     return 1;
00152 
00153   /* Lock prefers writers, but none are waiting. */
00154   if (queue_is_empty(&rwlock->__rw_write_waiting))
00155     return 1;
00156 
00157   /* Writers are waiting, but this thread already has a read lock */
00158   if (have_lock_already)
00159     return 1;
00160 
00161   /* Writers are waiting, and this is a new lock */
00162   return 0;
00163 }
00164 
00165 /*
00166  * This function helps support brain-damaged recursive read locking
00167  * semantics required by Unix 98, while maintaining write priority.
00168  * This basically determines whether this thread already holds a read lock
00169  * already. It returns 1 if so, otherwise it returns 0.
00170  *
00171  * If the thread has any ``untracked read locks'' then it just assumes
00172  * that this lock is among them, just to be safe, and returns 1.
00173  *
00174  * Also, if it finds the thread's lock in the list, it sets the pointer
00175  * referenced by pexisting to refer to the list entry.
00176  *
00177  * If the thread has no untracked locks, and the lock is not found
00178  * in its list, then it is added to the list. If this fails,
00179  * then *pout_of_mem is set to 1.
00180  */
00181 
00182 static int
00183 rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
00184     pthread_readlock_info **pexisting, int *pout_of_mem)
00185 {
00186   pthread_readlock_info *existing = NULL;
00187   int out_of_mem = 0, have_lock_already = 0;
00188   pthread_descr self = *pself;
00189 
00190   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
00191     {
00192       if (!self)
00193        *pself = self = thread_self();
00194 
00195       existing = rwlock_is_in_list(self, rwlock);
00196 
00197       if (existing != NULL
00198          || THREAD_GETMEM (self, p_untracked_readlock_count) > 0)
00199        have_lock_already = 1;
00200       else
00201        {
00202          existing = rwlock_add_to_list(self, rwlock);
00203          if (existing == NULL)
00204            out_of_mem = 1;
00205        }
00206     }
00207 
00208   *pout_of_mem = out_of_mem;
00209   *pexisting = existing;
00210 
00211   return have_lock_already;
00212 }
00213 
00214 int
00215 __pthread_rwlock_init (pthread_rwlock_t *rwlock,
00216                      const pthread_rwlockattr_t *attr)
00217 {
00218   __pthread_init_lock(&rwlock->__rw_lock);
00219   rwlock->__rw_readers = 0;
00220   rwlock->__rw_writer = NULL;
00221   rwlock->__rw_read_waiting = NULL;
00222   rwlock->__rw_write_waiting = NULL;
00223 
00224   if (attr == NULL)
00225     {
00226       rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
00227       rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
00228     }
00229   else
00230     {
00231       rwlock->__rw_kind = attr->__lockkind;
00232       rwlock->__rw_pshared = attr->__pshared;
00233     }
00234 
00235   return 0;
00236 }
00237 strong_alias (__pthread_rwlock_init, pthread_rwlock_init)
00238 
00239 
00240 int
00241 __pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
00242 {
00243   int readers;
00244   _pthread_descr writer;
00245 
00246   __pthread_lock (&rwlock->__rw_lock, NULL);
00247   readers = rwlock->__rw_readers;
00248   writer = rwlock->__rw_writer;
00249   __pthread_unlock (&rwlock->__rw_lock);
00250 
00251   if (readers > 0 || writer != NULL)
00252     return EBUSY;
00253 
00254   return 0;
00255 }
00256 strong_alias (__pthread_rwlock_destroy, pthread_rwlock_destroy)
00257 
00258 int
00259 __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
00260 {
00261   pthread_descr self = NULL;
00262   pthread_readlock_info *existing;
00263   int out_of_mem, have_lock_already;
00264 
00265   have_lock_already = rwlock_have_already(&self, rwlock,
00266                                      &existing, &out_of_mem);
00267 
00268   if (self == NULL)
00269     self = thread_self ();
00270 
00271   for (;;)
00272     {
00273       __pthread_lock (&rwlock->__rw_lock, self);
00274 
00275       if (rwlock_can_rdlock(rwlock, have_lock_already))
00276        break;
00277 
00278       enqueue (&rwlock->__rw_read_waiting, self);
00279       __pthread_unlock (&rwlock->__rw_lock);
00280       suspend (self); /* This is not a cancellation point */
00281     }
00282 
00283   ++rwlock->__rw_readers;
00284   __pthread_unlock (&rwlock->__rw_lock);
00285 
00286   if (have_lock_already || out_of_mem)
00287     {
00288       if (existing != NULL)
00289        ++existing->pr_lock_count;
00290       else
00291        ++self->p_untracked_readlock_count;
00292     }
00293 
00294   return 0;
00295 }
00296 strong_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
00297 
00298 int
00299 __pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
00300                            const struct timespec *abstime)
00301 {
00302   pthread_descr self = NULL;
00303   pthread_readlock_info *existing;
00304   int out_of_mem, have_lock_already;
00305   pthread_extricate_if extr;
00306 
00307   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00308     return EINVAL;
00309 
00310   have_lock_already = rwlock_have_already(&self, rwlock,
00311                                      &existing, &out_of_mem);
00312 
00313   if (self == NULL)
00314     self = thread_self ();
00315 
00316   /* Set up extrication interface */
00317   extr.pu_object = rwlock;
00318   extr.pu_extricate_func = rwlock_rd_extricate_func;
00319 
00320   /* Register extrication interface */
00321   __pthread_set_own_extricate_if (self, &extr);
00322 
00323   for (;;)
00324     {
00325       __pthread_lock (&rwlock->__rw_lock, self);
00326 
00327       if (rwlock_can_rdlock(rwlock, have_lock_already))
00328        break;
00329 
00330       enqueue (&rwlock->__rw_read_waiting, self);
00331       __pthread_unlock (&rwlock->__rw_lock);
00332       /* This is not a cancellation point */
00333       if (timedsuspend (self, abstime) == 0)
00334        {
00335          int was_on_queue;
00336 
00337          __pthread_lock (&rwlock->__rw_lock, self);
00338          was_on_queue = remove_from_queue (&rwlock->__rw_read_waiting, self);
00339          __pthread_unlock (&rwlock->__rw_lock);
00340 
00341          if (was_on_queue)
00342            {
00343              __pthread_set_own_extricate_if (self, 0);
00344              return ETIMEDOUT;
00345            }
00346 
00347          /* Eat the outstanding restart() from the signaller */
00348          suspend (self);
00349        }
00350     }
00351 
00352   __pthread_set_own_extricate_if (self, 0);
00353 
00354   ++rwlock->__rw_readers;
00355   __pthread_unlock (&rwlock->__rw_lock);
00356 
00357   if (have_lock_already || out_of_mem)
00358     {
00359       if (existing != NULL)
00360        ++existing->pr_lock_count;
00361       else
00362        ++self->p_untracked_readlock_count;
00363     }
00364 
00365   return 0;
00366 }
00367 strong_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)
00368 
00369 int
00370 __pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
00371 {
00372   pthread_descr self = thread_self();
00373   pthread_readlock_info *existing;
00374   int out_of_mem, have_lock_already;
00375   int retval = EBUSY;
00376 
00377   have_lock_already = rwlock_have_already(&self, rwlock,
00378       &existing, &out_of_mem);
00379 
00380   __pthread_lock (&rwlock->__rw_lock, self);
00381 
00382   /* 0 is passed to here instead of have_lock_already.
00383      This is to meet Single Unix Spec requirements:
00384      if writers are waiting, pthread_rwlock_tryrdlock
00385      does not acquire a read lock, even if the caller has
00386      one or more read locks already. */
00387 
00388   if (rwlock_can_rdlock(rwlock, 0))
00389     {
00390       ++rwlock->__rw_readers;
00391       retval = 0;
00392     }
00393 
00394   __pthread_unlock (&rwlock->__rw_lock);
00395 
00396   if (retval == 0)
00397     {
00398       if (have_lock_already || out_of_mem)
00399        {
00400          if (existing != NULL)
00401            ++existing->pr_lock_count;
00402          else
00403            ++self->p_untracked_readlock_count;
00404        }
00405     }
00406 
00407   return retval;
00408 }
00409 strong_alias (__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock)
00410 
00411 
00412 int
00413 __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
00414 {
00415   pthread_descr self = thread_self ();
00416 
00417   while(1)
00418     {
00419       __pthread_lock (&rwlock->__rw_lock, self);
00420       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
00421        {
00422          rwlock->__rw_writer = self;
00423          __pthread_unlock (&rwlock->__rw_lock);
00424          return 0;
00425        }
00426 
00427       /* Suspend ourselves, then try again */
00428       enqueue (&rwlock->__rw_write_waiting, self);
00429       __pthread_unlock (&rwlock->__rw_lock);
00430       suspend (self); /* This is not a cancellation point */
00431     }
00432 }
00433 strong_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
00434 
00435 
00436 int
00437 __pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
00438                            const struct timespec *abstime)
00439 {
00440   pthread_descr self;
00441   pthread_extricate_if extr;
00442 
00443   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00444     return EINVAL;
00445 
00446   self = thread_self ();
00447 
00448   /* Set up extrication interface */
00449   extr.pu_object = rwlock;
00450   extr.pu_extricate_func =  rwlock_wr_extricate_func;
00451 
00452   /* Register extrication interface */
00453   __pthread_set_own_extricate_if (self, &extr);
00454 
00455   while(1)
00456     {
00457       __pthread_lock (&rwlock->__rw_lock, self);
00458 
00459       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
00460        {
00461          rwlock->__rw_writer = self;
00462          __pthread_set_own_extricate_if (self, 0);
00463          __pthread_unlock (&rwlock->__rw_lock);
00464          return 0;
00465        }
00466 
00467       /* Suspend ourselves, then try again */
00468       enqueue (&rwlock->__rw_write_waiting, self);
00469       __pthread_unlock (&rwlock->__rw_lock);
00470       /* This is not a cancellation point */
00471       if (timedsuspend (self, abstime) == 0)
00472        {
00473          int was_on_queue;
00474 
00475          __pthread_lock (&rwlock->__rw_lock, self);
00476          was_on_queue = remove_from_queue (&rwlock->__rw_write_waiting, self);
00477          __pthread_unlock (&rwlock->__rw_lock);
00478 
00479          if (was_on_queue)
00480            {
00481              __pthread_set_own_extricate_if (self, 0);
00482              return ETIMEDOUT;
00483            }
00484 
00485          /* Eat the outstanding restart() from the signaller */
00486          suspend (self);
00487        }
00488     }
00489 }
00490 strong_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
00491 
00492 
00493 int
00494 __pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
00495 {
00496   int result = EBUSY;
00497 
00498   __pthread_lock (&rwlock->__rw_lock, NULL);
00499   if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
00500     {
00501       rwlock->__rw_writer = thread_self ();
00502       result = 0;
00503     }
00504   __pthread_unlock (&rwlock->__rw_lock);
00505 
00506   return result;
00507 }
00508 strong_alias (__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock)
00509 
00510 
00511 int
00512 __pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
00513 {
00514   pthread_descr torestart;
00515   pthread_descr th;
00516 
00517   __pthread_lock (&rwlock->__rw_lock, NULL);
00518   if (rwlock->__rw_writer != NULL)
00519     {
00520       /* Unlocking a write lock.  */
00521       if (rwlock->__rw_writer != thread_self ())
00522        {
00523          __pthread_unlock (&rwlock->__rw_lock);
00524          return EPERM;
00525        }
00526       rwlock->__rw_writer = NULL;
00527 
00528       if ((rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
00529           && !queue_is_empty(&rwlock->__rw_read_waiting))
00530          || (th = dequeue(&rwlock->__rw_write_waiting)) == NULL)
00531        {
00532          /* Restart all waiting readers.  */
00533          torestart = rwlock->__rw_read_waiting;
00534          rwlock->__rw_read_waiting = NULL;
00535          __pthread_unlock (&rwlock->__rw_lock);
00536          while ((th = dequeue (&torestart)) != NULL)
00537            restart (th);
00538        }
00539       else
00540        {
00541          /* Restart one waiting writer.  */
00542          __pthread_unlock (&rwlock->__rw_lock);
00543          restart (th);
00544        }
00545     }
00546   else
00547     {
00548       /* Unlocking a read lock.  */
00549       if (rwlock->__rw_readers == 0)
00550        {
00551          __pthread_unlock (&rwlock->__rw_lock);
00552          return EPERM;
00553        }
00554 
00555       --rwlock->__rw_readers;
00556       if (rwlock->__rw_readers == 0)
00557        /* Restart one waiting writer, if any.  */
00558        th = dequeue (&rwlock->__rw_write_waiting);
00559       else
00560        th = NULL;
00561 
00562       __pthread_unlock (&rwlock->__rw_lock);
00563       if (th != NULL)
00564        restart (th);
00565 
00566       /* Recursive lock fixup */
00567 
00568       if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
00569        {
00570          pthread_descr self = thread_self();
00571          pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
00572 
00573          if (victim != NULL)
00574            {
00575              if (victim->pr_lock_count == 0)
00576               {
00577                 victim->pr_next = THREAD_GETMEM (self, p_readlock_free);
00578                 THREAD_SETMEM (self, p_readlock_free, victim);
00579               }
00580            }
00581          else
00582            {
00583              int val = THREAD_GETMEM (self, p_untracked_readlock_count);
00584              if (val > 0)
00585               THREAD_SETMEM (self, p_untracked_readlock_count, val - 1);
00586            }
00587        }
00588     }
00589 
00590   return 0;
00591 }
00592 strong_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock)
00593 
00594 
00595 
00596 int
00597 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
00598 {
00599   attr->__lockkind = 0;
00600   attr->__pshared = PTHREAD_PROCESS_PRIVATE;
00601 
00602   return 0;
00603 }
00604 
00605 
00606 int
00607 __pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
00608 {
00609   return 0;
00610 }
00611 strong_alias (__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy)
00612 
00613 
00614 int
00615 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
00616 {
00617   *pshared = attr->__pshared;
00618   return 0;
00619 }
00620 
00621 
00622 int
00623 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
00624 {
00625   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
00626     return EINVAL;
00627 
00628   /* For now it is not possible to shared a conditional variable.  */
00629   if (pshared != PTHREAD_PROCESS_PRIVATE)
00630     return ENOSYS;
00631 
00632   attr->__pshared = pshared;
00633 
00634   return 0;
00635 }
00636 
00637 
00638 int
00639 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
00640 {
00641   *pref = attr->__lockkind;
00642   return 0;
00643 }
00644 
00645 
00646 int
00647 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
00648 {
00649   if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
00650       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
00651       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
00652       && pref != PTHREAD_RWLOCK_DEFAULT_NP)
00653     return EINVAL;
00654 
00655   attr->__lockkind = pref;
00656 
00657   return 0;
00658 }