OSDN Git Service

xtensa: add support for NPTL
[uclinux-h8/uclibc-ng.git] / libpthread / nptl / sysdeps / unix / sysv / linux / xtensa / lowlevellock.c
1 /* low level locking for pthread library.  Generic futex-using version.
2    Copyright (C) 2003-2013 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <sys/time.h>
23
24 void
25 __lll_lock_wait_private (int *futex)
26 {
27   do
28     {
29       int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
30       if (oldval != 0)
31         lll_futex_wait (futex, 2, LLL_PRIVATE);
32     }
33   while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
34 }
35
36
37 /* These functions don't get included in libc.so  */
38 #ifdef IS_IN_libpthread
39 void
40 __lll_lock_wait (int *futex, int private)
41 {
42   do
43     {
44       int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
45       if (oldval != 0)
46         lll_futex_wait (futex, 2, private);
47     }
48   while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
49 }
50
51
52 int
53 __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
54 {
55   struct timespec rt;
56
57   /* Reject invalid timeouts.  */
58   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
59     return EINVAL;
60
61   /* Upgrade the lock.  */
62   if (atomic_exchange_acq (futex, 2) == 0)
63     return 0;
64
65   do
66     {
67       struct timeval tv;
68
69       /* Get the current time.  */
70       (void) __gettimeofday (&tv, NULL);
71
72       /* Compute relative timeout.  */
73       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
74       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
75       if (rt.tv_nsec < 0)
76         {
77           rt.tv_nsec += 1000000000;
78           --rt.tv_sec;
79         }
80
81       /* Already timed out?  */
82       if (rt.tv_sec < 0)
83         return ETIMEDOUT;
84
85       // XYZ: Lost the lock to check whether it was private.
86       lll_futex_timed_wait (futex, 2, &rt, private);
87     }
88   while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
89
90   return 0;
91 }
92
93
94 int
95 __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
96 {
97   int tid;
98
99   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
100     return EINVAL;
101
102   /* Repeat until thread terminated.  */
103   while ((tid = *tidp) != 0)
104     {
105       struct timeval tv;
106       struct timespec rt;
107
108       /* Get the current time.  */
109       (void) __gettimeofday (&tv, NULL);
110
111       /* Compute relative timeout.  */
112       rt.tv_sec = abstime->tv_sec - tv.tv_sec;
113       rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
114       if (rt.tv_nsec < 0)
115         {
116           rt.tv_nsec += 1000000000;
117           --rt.tv_sec;
118         }
119
120       /* Already timed out?  */
121       if (rt.tv_sec < 0)
122         return ETIMEDOUT;
123
124       /* Wait until thread terminates.  */
125       // XYZ: Lost the lock to check whether it was private.
126       if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
127         return ETIMEDOUT;
128     }
129
130   return 0;
131 }
132 #endif