OSDN Git Service

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