Back to index

glibc  2.9
lowlevellock.c
Go to the documentation of this file.
00001 /* low level locking for pthread library.  Generic futex-using version.
00002    Copyright (C) 2003, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
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 <errno.h>
00022 #include <sysdep.h>
00023 #include <lowlevellock.h>
00024 #include <sys/time.h>
00025 
00026 void
00027 __lll_lock_wait (lll_lock_t *futex, int private)
00028 {
00029   do
00030     {
00031       int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
00032       if (oldval != 0)
00033        lll_futex_wait (futex, 2, private);
00034     }
00035   while (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0);
00036 }
00037 
00038 void
00039 __lll_lock_wait_private (lll_lock_t *futex)
00040 {
00041   do
00042     {
00043       int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
00044       if (oldval != 0)
00045        lll_futex_wait (futex, 2, LLL_PRIVATE);
00046     }
00047   while (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0);
00048 }
00049 
00050 int
00051 __lll_timedlock_wait (lll_lock_t *futex, const struct timespec *abstime, int private)
00052 {
00053   /* Reject invalid timeouts.  */
00054   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00055     return EINVAL;
00056 
00057   do
00058     {
00059       struct timeval tv;
00060       struct timespec rt;
00061 
00062       /* Get the current time.  */
00063       (void) __gettimeofday (&tv, NULL);
00064 
00065       /* Compute relative timeout.  */
00066       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
00067       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
00068       if (rt.tv_nsec < 0)
00069        {
00070          rt.tv_nsec += 1000000000;
00071          --rt.tv_sec;
00072        }
00073 
00074       /* Already timed out?  */
00075       if (rt.tv_sec < 0)
00076        return ETIMEDOUT;
00077 
00078       /* Wait.  */
00079       int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
00080       if (oldval != 0)
00081        lll_futex_timed_wait (futex, 2, &rt, private);
00082     }
00083   while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
00084   return 0;
00085 }
00086 
00087 
00088 /* These don't get included in libc.so  */
00089 #ifdef IS_IN_libpthread
00090 int
00091 lll_unlock_wake_cb (lll_lock_t *futex)
00092 {
00093   int val = atomic_exchange_rel (futex, 0);
00094 
00095   if (__builtin_expect (val > 1, 0))
00096     lll_private_futex_wake (futex, 1);
00097   return 0;
00098 }
00099 
00100 
00101 int
00102 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
00103 {
00104   int tid;
00105 
00106   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00107     return EINVAL;
00108 
00109   /* Repeat until thread terminated.  */
00110   while ((tid = *tidp) != 0)
00111     {
00112       struct timeval tv;
00113       struct timespec rt;
00114 
00115       /* Get the current time.  */
00116       (void) __gettimeofday (&tv, NULL);
00117 
00118       /* Compute relative timeout.  */
00119       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
00120       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
00121       if (rt.tv_nsec < 0)
00122        {
00123          rt.tv_nsec += 1000000000;
00124          --rt.tv_sec;
00125        }
00126 
00127       /* Already timed out?  */
00128       if (rt.tv_sec < 0)
00129        return ETIMEDOUT;
00130 
00131       /* Wait until thread terminates.  */
00132       if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
00133        return ETIMEDOUT;
00134     }
00135 
00136   return 0;
00137 }
00138 
00139 #endif
00140