Back to index

glibc  2.9
lowlevellock.h
Go to the documentation of this file.
00001 /* Low level locking macros used in NPTL implementation.  Stub version.
00002    Copyright (C) 2002, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <atomic.h>
00022 
00023 
00024 /* Mutex lock counter:
00025    bit 31 clear means unlocked;
00026    bit 31 set means locked.
00027 
00028    All code that looks at bit 31 first increases the 'number of
00029    interested threads' usage counter, which is in bits 0-30.
00030 
00031    All negative mutex values indicate that the mutex is still locked.  */
00032 
00033 
00034 static inline void
00035 __generic_mutex_lock (int *mutex)
00036 {
00037   unsigned int v;
00038 
00039   /* Bit 31 was clear, we got the mutex.  (this is the fastpath).  */
00040   if (atomic_bit_test_set (mutex, 31) == 0)
00041     return;
00042 
00043   atomic_increment (mutex);
00044 
00045   while (1)
00046     {
00047       if (atomic_bit_test_set (mutex, 31) == 0)
00048        {
00049          atomic_decrement (mutex);
00050          return;
00051        }
00052 
00053       /* We have to wait now. First make sure the futex value we are
00054         monitoring is truly negative (i.e. locked). */
00055       v = *mutex;
00056       if (v >= 0)
00057        continue;
00058 
00059       lll_futex_wait (mutex, v,
00060                     // XYZ check mutex flag
00061                     LLL_SHARED);
00062     }
00063 }
00064 
00065 
00066 static inline void
00067 __generic_mutex_unlock (int *mutex)
00068 {
00069   /* Adding 0x80000000 to the counter results in 0 if and only if
00070      there are not other interested threads - we can return (this is
00071      the fastpath).  */
00072   if (atomic_add_zero (mutex, 0x80000000))
00073     return;
00074 
00075   /* There are other threads waiting for this mutex, wake one of them
00076      up.  */
00077   lll_futex_wake (mutex, 1,
00078                 // XYZ check mutex flag
00079                 LLL_SHARED);
00080 }
00081 
00082 
00083 #define lll_mutex_lock(futex) __generic_mutex_lock (&(futex))
00084 #define lll_mutex_unlock(futex) __generic_mutex_unlock (&(futex))