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 
00027 void
00028 __lll_lock_wait_private (int *futex)
00029 {
00030   if (*futex == 2)
00031     lll_futex_wait (futex, 2, LLL_PRIVATE);
00032 
00033   while (atomic_exchange_acq (futex, 2) != 0)
00034     lll_futex_wait (futex, 2, LLL_PRIVATE);
00035 }
00036 
00037 
00038 /* These functions don't get included in libc.so  */
00039 #ifdef IS_IN_libpthread
00040 void
00041 __lll_lock_wait (int *futex, int private)
00042 {
00043   if (*futex == 2)
00044     lll_futex_wait (futex, 2, private);
00045 
00046   while (atomic_exchange_acq (futex, 2) != 0)
00047     lll_futex_wait (futex, 2, private);
00048 }
00049 
00050 
00051 int
00052 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
00053 {
00054   /* Reject invalid timeouts.  */
00055   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00056     return EINVAL;
00057 
00058   /* Try locking.  */
00059   while (atomic_exchange_acq (futex, 2) != 0)
00060     {
00061       struct timeval tv;
00062 
00063       /* Get the current time.  */
00064       (void) __gettimeofday (&tv, NULL);
00065 
00066       /* Compute relative timeout.  */
00067       struct timespec rt;
00068       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
00069       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
00070       if (rt.tv_nsec < 0)
00071        {
00072          rt.tv_nsec += 1000000000;
00073          --rt.tv_sec;
00074        }
00075 
00076       if (rt.tv_sec < 0)
00077        return ETIMEDOUT;
00078 
00079       /* Wait.  */
00080       lll_futex_timed_wait (futex, 2, &rt, private);
00081     }
00082 
00083   return 0;
00084 }
00085 
00086 
00087 int
00088 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
00089 {
00090   int tid;
00091 
00092   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
00093     return EINVAL;
00094 
00095   /* Repeat until thread terminated.  */
00096   while ((tid = *tidp) != 0)
00097     {
00098       struct timeval tv;
00099       struct timespec rt;
00100 
00101       /* Get the current time.  */
00102       (void) __gettimeofday (&tv, NULL);
00103 
00104       /* Compute relative timeout.  */
00105       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
00106       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
00107       if (rt.tv_nsec < 0)
00108        {
00109          rt.tv_nsec += 1000000000;
00110          --rt.tv_sec;
00111        }
00112 
00113       /* Already timed out?  */
00114       if (rt.tv_sec < 0)
00115        return ETIMEDOUT;
00116 
00117       /* Wait until thread terminates.  The kernel so far does not use
00118         the private futex operations for this.  */
00119       if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
00120        return ETIMEDOUT;
00121     }
00122 
00123   return 0;
00124 }
00125 #endif