Back to index

glibc  2.9
pthread_rwlock_wrlock.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 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 write lock for RWLOCK.  */
00028 int
00029 __pthread_rwlock_wrlock (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 and no reader.  */
00040       if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
00041        {
00042          /* Mark self as writer.  */
00043          rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
00044          break;
00045        }
00046 
00047       /* Make sure we are not holding the rwlock as a writer.  This is
00048         a deadlock situation we recognize and report.  */
00049       if (__builtin_expect (rwlock->__data.__writer
00050                          == THREAD_GETMEM (THREAD_SELF, tid), 0))
00051        {
00052          result = EDEADLK;
00053          break;
00054        }
00055 
00056       /* Remember that we are a writer.  */
00057       if (++rwlock->__data.__nr_writers_queued == 0)
00058        {
00059          /* Overflow on number of queued writers.  */
00060          --rwlock->__data.__nr_writers_queued;
00061          result = EAGAIN;
00062          break;
00063        }
00064 
00065       int waitval = rwlock->__data.__writer_wakeup;
00066 
00067       /* Free the lock.  */
00068       lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
00069 
00070       /* Wait for the writer or reader(s) to finish.  */
00071       lll_futex_wait (&rwlock->__data.__writer_wakeup, waitval,
00072                     rwlock->__data.__shared);
00073 
00074       /* Get the lock.  */
00075       lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
00076 
00077       /* To start over again, remove the thread from the writer list.  */
00078       --rwlock->__data.__nr_writers_queued;
00079     }
00080 
00081   /* We are done, free the lock.  */
00082   lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
00083 
00084   return result;
00085 }
00086 
00087 weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
00088 strong_alias (__pthread_rwlock_wrlock, __pthread_rwlock_wrlock_internal)