Back to index

glibc  2.9
lowlevelrobustlock.c
Go to the documentation of this file.
00001 /* Copyright (C) 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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 <sys/time.h>
00024 #include <pthreadP.h>
00025 
00026 
00027 int
00028 __lll_robust_lock_wait (int *futex, int private)
00029 {
00030   int oldval = *futex;
00031   int tid = THREAD_GETMEM (THREAD_SELF, tid);
00032 
00033   /* If the futex changed meanwhile try locking again.  */
00034   if (oldval == 0)
00035     goto try;
00036 
00037   do
00038     {
00039       if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
00040        return oldval;
00041 
00042       int newval = oldval | FUTEX_WAITERS;
00043       if (oldval != newval
00044          && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
00045        continue;
00046 
00047       lll_futex_wait (futex, newval, private);
00048 
00049     try:
00050       ;
00051     }
00052   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
00053                                                  tid | FUTEX_WAITERS,
00054                                                  0)) != 0);
00055   return 0;
00056 }
00057 
00058 
00059 int
00060 __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
00061                           int private)
00062 {
00063   /* Reject invalid timeouts.  */
00064   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00065     return EINVAL;
00066 
00067   int tid = THREAD_GETMEM (THREAD_SELF, tid);
00068   int oldval = *futex;
00069 
00070   /* If the futex changed meanwhile try locking again.  */
00071   if (oldval == 0)
00072     goto try;
00073 
00074   do
00075     {
00076       struct timeval tv;
00077       struct timespec rt;
00078 
00079       /* Get the current time.  */
00080       (void) __gettimeofday (&tv, NULL);
00081 
00082       /* Compute relative timeout.  */
00083       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
00084       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
00085       if (rt.tv_nsec < 0)
00086        {
00087          rt.tv_nsec += 1000000000;
00088          --rt.tv_sec;
00089        }
00090 
00091       /* Already timed out?  */
00092       if (rt.tv_sec < 0)
00093        return ETIMEDOUT;
00094 
00095       /* Wait.  */
00096       if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
00097        return oldval;
00098 
00099       int newval = oldval | FUTEX_WAITERS;
00100       if (oldval != newval
00101          && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
00102        continue;
00103 
00104       lll_futex_timed_wait (futex, newval, &rt, private);
00105 
00106     try:
00107       ;
00108     }
00109   while ((oldval = atomic_compare_and_exchange_val_acq (futex,
00110                                                  tid | FUTEX_WAITERS,
00111                                                  0)) != 0);
00112 
00113   return 0;
00114 }