]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/nptl/sysdeps/unix/sysv/linux/sparc/lowlevellock.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / nptl / sysdeps / unix / sysv / linux / sparc / lowlevellock.c
1 /* low level locking for pthread library.  SPARC version.
2    Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <errno.h>
22 #include <sysdep.h>
23 #include <lowlevellock.h>
24 #include <sys/time.h>
25 #include <tls.h>
26
27
28 void
29 __lll_lock_wait_private (int *futex)
30 {
31   do
32     {
33       int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
34       if (oldval != 0)
35         lll_futex_wait (futex, 2, LLL_PRIVATE);
36     }
37   while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
38 }
39
40
41 /* These functions don't get included in libc.so  */
42 #ifdef IS_IN_libpthread
43 void
44 __lll_lock_wait (int *futex, int private)
45 {
46   do
47     {
48       int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
49       if (oldval != 0)
50         lll_futex_wait (futex, 2, private);
51     }
52   while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
53 }
54
55
56 int
57 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
58 {
59   /* Reject invalid timeouts.  */
60   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
61     return EINVAL;
62
63   do
64     {
65       struct timeval tv;
66       struct timespec rt;
67
68       /* Get the current time.  */
69       (void) gettimeofday (&tv, NULL);
70
71       /* Compute relative timeout.  */
72       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
73       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
74       if (rt.tv_nsec < 0)
75         {
76           rt.tv_nsec += 1000000000;
77           --rt.tv_sec;
78         }
79
80       /* Already timed out?  */
81       if (rt.tv_sec < 0)
82         return ETIMEDOUT;
83
84       /* Wait.  */
85       int oldval = atomic_compare_and_exchange_val_24_acq (futex, 2, 1);
86       if (oldval != 0)
87         lll_futex_timed_wait (futex, 2, &rt, private);
88     }
89   while (atomic_compare_and_exchange_val_24_acq (futex, 2, 0) != 0);
90
91   return 0;
92 }
93
94
95 int
96 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
97 {
98   int tid;
99
100   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
101     return EINVAL;
102
103   /* Repeat until thread terminated.  */
104   while ((tid = *tidp) != 0)
105     {
106       struct timeval tv;
107       struct timespec rt;
108
109       /* Get the current time.  */
110       (void) gettimeofday (&tv, NULL);
111
112       /* Compute relative timeout.  */
113       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
114       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
115       if (rt.tv_nsec < 0)
116         {
117           rt.tv_nsec += 1000000000;
118           --rt.tv_sec;
119         }
120
121       /* Already timed out?  */
122       if (rt.tv_sec < 0)
123         return ETIMEDOUT;
124
125       /* Wait until thread terminates.  The kernel so far does not use
126          the private futex operations for this.  */
127       if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
128         return ETIMEDOUT;
129     }
130
131   return 0;
132 }
133 #endif