Back to index

glibc  2.9
pthread_mutex_setprioceiling.c
Go to the documentation of this file.
00001 /* Set current priority ceiling of pthread_mutex_t.
00002    Copyright (C) 2006, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdbool.h>
00022 #include <errno.h>
00023 #include <pthreadP.h>
00024 
00025 
00026 int
00027 pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling)
00028      pthread_mutex_t *mutex;
00029      int prioceiling;
00030      int *old_ceiling;
00031 {
00032   /* The low bits of __kind aren't ever changed after pthread_mutex_init,
00033      so we don't need a lock yet.  */
00034   if ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
00035     return EINVAL;
00036 
00037   if (__sched_fifo_min_prio == -1)
00038     __init_sched_fifo_prio ();
00039 
00040   if (__builtin_expect (prioceiling < __sched_fifo_min_prio, 0)
00041       || __builtin_expect (prioceiling > __sched_fifo_max_prio, 0)
00042       || __builtin_expect ((prioceiling
00043                          & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
00044                             >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
00045                         != prioceiling, 0))
00046     return EINVAL;
00047 
00048   /* Check whether we already hold the mutex.  */
00049   bool locked = false;
00050   int kind = PTHREAD_MUTEX_TYPE (mutex);
00051   if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
00052     {
00053       if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
00054        return EDEADLK;
00055 
00056       if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
00057        locked = true;
00058     }
00059 
00060   int oldval = mutex->__data.__lock;
00061   if (! locked)
00062     do
00063       {
00064        /* Need to lock the mutex, but without obeying the priority
00065           protect protocol.  */
00066        int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
00067 
00068        oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
00069                                                 ceilval | 1, ceilval);
00070        if (oldval == ceilval)
00071          break;
00072 
00073        do
00074          {
00075            oldval
00076              = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
00077                                                ceilval | 2,
00078                                                ceilval | 1);
00079 
00080            if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
00081              break;
00082 
00083            if (oldval != ceilval)
00084              lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
00085                            PTHREAD_MUTEX_PSHARED (mutex));
00086          }
00087        while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
00088                                               ceilval | 2, ceilval)
00089               != ceilval);
00090 
00091        if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
00092          continue;
00093       }
00094     while (0);
00095 
00096   int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
00097               >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
00098   if (locked)
00099     {
00100       int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
00101       if (ret)
00102        return ret;
00103     }
00104 
00105   if (old_ceiling != NULL)
00106     *old_ceiling = oldprio;
00107 
00108   int newlock = 0;
00109   if (locked)
00110     newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
00111   mutex->__data.__lock = newlock
00112                       | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
00113   atomic_full_barrier ();
00114 
00115   lll_futex_wake (&mutex->__data.__lock, INT_MAX,
00116                 PTHREAD_MUTEX_PSHARED (mutex));
00117 
00118   return 0;
00119 }