Back to index

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