Back to index

glibc  2.9
pthread_rwlock_rdlock.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <sysdep.h>
00022 #include <lowlevellock.h>
00023 #include <pthread.h>
00024 #include <pthreadP.h>
00025 
00026 
00027 /* Acquire read lock for RWLOCK.  */
00028 int
00029 __pthread_rwlock_rdlock (rwlock)
00030      pthread_rwlock_t *rwlock;
00031 {
00032   int result = 0;
00033 
00034   /* Make sure we are along.  */
00035   lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
00036 
00037   while (1)
00038     {
00039       /* Get the rwlock if there is no writer...  */
00040       if (rwlock->__data.__writer == 0
00041          /* ...and if either no writer is waiting or we prefer readers.  */
00042          && (!rwlock->__data.__nr_writers_queued
00043              || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
00044        {
00045          /* Increment the reader counter.  Avoid overflow.  */
00046          if (__builtin_expect (++rwlock->__data.__nr_readers == 0, 0))
00047            {
00048              /* Overflow on number of readers.    */
00049              --rwlock->__data.__nr_readers;
00050              result = EAGAIN;
00051            }
00052 
00053          break;
00054        }
00055 
00056       /* Make sure we are not holding the rwlock as a writer.  This is
00057         a deadlock situation we recognize and report.  */
00058       if (__builtin_expect (rwlock->__data.__writer
00059                          == THREAD_GETMEM (THREAD_SELF, tid), 0))
00060        {
00061          result = EDEADLK;
00062          break;
00063        }
00064 
00065       /* Remember that we are a reader.  */
00066       if (__builtin_expect (++rwlock->__data.__nr_readers_queued == 0, 0))
00067        {
00068          /* Overflow on number of queued readers.  */
00069          --rwlock->__data.__nr_readers_queued;
00070          result = EAGAIN;
00071          break;
00072        }
00073 
00074       int waitval = rwlock->__data.__readers_wakeup;
00075 
00076       /* Free the lock.  */
00077       lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
00078 
00079       /* Wait for the writer to finish.  */
00080       lll_futex_wait (&rwlock->__data.__readers_wakeup, waitval,
00081                     rwlock->__data.__shared);
00082 
00083       /* Get the lock.  */
00084       lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
00085 
00086       --rwlock->__data.__nr_readers_queued;
00087     }
00088 
00089   /* We are done, free the lock.  */
00090   lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
00091 
00092   return result;
00093 }
00094 
00095 weak_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
00096 strong_alias (__pthread_rwlock_rdlock, __pthread_rwlock_rdlock_internal)