Back to index

glibc  2.9
pthread_mutex_unlock.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2005-2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <stdlib.h>
00022 #include "pthreadP.h"
00023 #include <lowlevellock.h>
00024 
00025 
00026 int
00027 internal_function attribute_hidden
00028 __pthread_mutex_unlock_usercnt (mutex, decr)
00029      pthread_mutex_t *mutex;
00030      int decr;
00031 {
00032   int newowner = 0;
00033 
00034   switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex),
00035                          PTHREAD_MUTEX_TIMED_NP))
00036     {
00037     case PTHREAD_MUTEX_RECURSIVE_NP:
00038       /* Recursive mutex.  */
00039       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
00040        return EPERM;
00041 
00042       if (--mutex->__data.__count != 0)
00043        /* We still hold the mutex.  */
00044        return 0;
00045       goto normal;
00046 
00047     case PTHREAD_MUTEX_ERRORCHECK_NP:
00048       /* Error checking mutex.  */
00049       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
00050          || ! lll_islocked (mutex->__data.__lock))
00051        return EPERM;
00052       /* FALLTHROUGH */
00053 
00054     case PTHREAD_MUTEX_TIMED_NP:
00055     case PTHREAD_MUTEX_ADAPTIVE_NP:
00056       /* Always reset the owner field.  */
00057     normal:
00058       mutex->__data.__owner = 0;
00059       if (decr)
00060        /* One less user.  */
00061        --mutex->__data.__nusers;
00062 
00063       /* Unlock.  */
00064       lll_unlock (mutex->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex));
00065       break;
00066 
00067     case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
00068       /* Recursive mutex.  */
00069       if ((mutex->__data.__lock & FUTEX_TID_MASK)
00070          == THREAD_GETMEM (THREAD_SELF, tid)
00071          && __builtin_expect (mutex->__data.__owner
00072                             == PTHREAD_MUTEX_INCONSISTENT, 0))
00073        {
00074          if (--mutex->__data.__count != 0)
00075            /* We still hold the mutex.  */
00076            return ENOTRECOVERABLE;
00077 
00078          goto notrecoverable;
00079        }
00080 
00081       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
00082        return EPERM;
00083 
00084       if (--mutex->__data.__count != 0)
00085        /* We still hold the mutex.  */
00086        return 0;
00087 
00088       goto robust;
00089 
00090     case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
00091     case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
00092     case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
00093       if ((mutex->__data.__lock & FUTEX_TID_MASK)
00094          != THREAD_GETMEM (THREAD_SELF, tid)
00095          || ! lll_islocked (mutex->__data.__lock))
00096        return EPERM;
00097 
00098       /* If the previous owner died and the caller did not succeed in
00099         making the state consistent, mark the mutex as unrecoverable
00100         and make all waiters.  */
00101       if (__builtin_expect (mutex->__data.__owner
00102                          == PTHREAD_MUTEX_INCONSISTENT, 0))
00103       notrecoverable:
00104        newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
00105 
00106     robust:
00107       /* Remove mutex from the list.  */
00108       THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
00109                    &mutex->__data.__list.__next);
00110       DEQUEUE_MUTEX (mutex);
00111 
00112       mutex->__data.__owner = newowner;
00113       if (decr)
00114        /* One less user.  */
00115        --mutex->__data.__nusers;
00116 
00117       /* Unlock.  */
00118       lll_robust_unlock (mutex->__data.__lock,
00119                       PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
00120 
00121       THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
00122       break;
00123 
00124     case PTHREAD_MUTEX_PI_RECURSIVE_NP:
00125       /* Recursive mutex.  */
00126       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
00127        return EPERM;
00128 
00129       if (--mutex->__data.__count != 0)
00130        /* We still hold the mutex.  */
00131        return 0;
00132       goto continue_pi;
00133 
00134     case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
00135       /* Recursive mutex.  */
00136       if ((mutex->__data.__lock & FUTEX_TID_MASK)
00137          == THREAD_GETMEM (THREAD_SELF, tid)
00138          && __builtin_expect (mutex->__data.__owner
00139                             == PTHREAD_MUTEX_INCONSISTENT, 0))
00140        {
00141          if (--mutex->__data.__count != 0)
00142            /* We still hold the mutex.  */
00143            return ENOTRECOVERABLE;
00144 
00145          goto pi_notrecoverable;
00146        }
00147 
00148       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
00149        return EPERM;
00150 
00151       if (--mutex->__data.__count != 0)
00152        /* We still hold the mutex.  */
00153        return 0;
00154 
00155       goto continue_pi;
00156 
00157     case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
00158     case PTHREAD_MUTEX_PI_NORMAL_NP:
00159     case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
00160     case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
00161     case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
00162     case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
00163       if ((mutex->__data.__lock & FUTEX_TID_MASK)
00164          != THREAD_GETMEM (THREAD_SELF, tid)
00165          || ! lll_islocked (mutex->__data.__lock))
00166        return EPERM;
00167 
00168       /* If the previous owner died and the caller did not succeed in
00169         making the state consistent, mark the mutex as unrecoverable
00170         and make all waiters.  */
00171       if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0
00172          && __builtin_expect (mutex->__data.__owner
00173                             == PTHREAD_MUTEX_INCONSISTENT, 0))
00174       pi_notrecoverable:
00175        newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
00176 
00177     continue_pi:
00178       if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) != 0)
00179        {
00180          /* Remove mutex from the list.
00181             Note: robust PI futexes are signaled by setting bit 0.  */
00182          THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
00183                       (void *) (((uintptr_t) &mutex->__data.__list.__next)
00184                                | 1));
00185          DEQUEUE_MUTEX (mutex);
00186        }
00187 
00188       mutex->__data.__owner = newowner;
00189       if (decr)
00190        /* One less user.  */
00191        --mutex->__data.__nusers;
00192 
00193       /* Unlock.  */
00194       if ((mutex->__data.__lock & FUTEX_WAITERS) != 0
00195          || atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock, 0,
00196                                              THREAD_GETMEM (THREAD_SELF,
00197                                                           tid)))
00198        {
00199          int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;
00200          int private = (robust
00201                       ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
00202                       : PTHREAD_MUTEX_PSHARED (mutex));
00203          INTERNAL_SYSCALL_DECL (__err);
00204          INTERNAL_SYSCALL (futex, __err, 2, &mutex->__data.__lock,
00205                          __lll_private_flag (FUTEX_UNLOCK_PI, private));
00206        }
00207 
00208       THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
00209       break;
00210 
00211     case PTHREAD_MUTEX_PP_RECURSIVE_NP:
00212       /* Recursive mutex.  */
00213       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
00214        return EPERM;
00215 
00216       if (--mutex->__data.__count != 0)
00217        /* We still hold the mutex.  */
00218        return 0;
00219       goto pp;
00220 
00221     case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
00222       /* Error checking mutex.  */
00223       if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid)
00224          || (mutex->__data.__lock & ~ PTHREAD_MUTEX_PRIO_CEILING_MASK) == 0)
00225        return EPERM;
00226       /* FALLTHROUGH */
00227 
00228     case PTHREAD_MUTEX_PP_NORMAL_NP:
00229     case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
00230       /* Always reset the owner field.  */
00231     pp:
00232       mutex->__data.__owner = 0;
00233 
00234       if (decr)
00235        /* One less user.  */
00236        --mutex->__data.__nusers;
00237 
00238       /* Unlock.  */
00239       int newval, oldval;
00240       do
00241        {
00242          oldval = mutex->__data.__lock;
00243          newval = oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK;
00244        }
00245       while (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock,
00246                                              newval, oldval));
00247 
00248       if ((oldval & ~PTHREAD_MUTEX_PRIO_CEILING_MASK) > 1)
00249        lll_futex_wake (&mutex->__data.__lock, 1,
00250                      PTHREAD_MUTEX_PSHARED (mutex));
00251 
00252       int oldprio = newval >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
00253       return __pthread_tpp_change_priority (oldprio, -1);
00254 
00255     default:
00256       /* Correct code cannot set any other type.  */
00257       return EINVAL;
00258     }
00259 
00260   return 0;
00261 }
00262 
00263 
00264 int
00265 __pthread_mutex_unlock (mutex)
00266      pthread_mutex_t *mutex;
00267 {
00268   return __pthread_mutex_unlock_usercnt (mutex, 1);
00269 }
00270 strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
00271 strong_alias (__pthread_mutex_unlock, __pthread_mutex_unlock_internal)