iconv: Suppress array out of bounds warning.
[platform/upstream/glibc.git] / nptl / pthread_cond_wait.c
1 /* Copyright (C) 2003-2015 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
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 <endian.h>
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
25 #include <kernel-features.h>
26
27 #include <shlib-compat.h>
28 #include <stap-probe.h>
29
30 struct _condvar_cleanup_buffer
31 {
32   int oldtype;
33   pthread_cond_t *cond;
34   pthread_mutex_t *mutex;
35   unsigned int bc_seq;
36 };
37
38
39 void
40 __attribute__ ((visibility ("hidden")))
41 __condvar_cleanup (void *arg)
42 {
43   struct _condvar_cleanup_buffer *cbuffer =
44     (struct _condvar_cleanup_buffer *) arg;
45   unsigned int destroying;
46   int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
47                 ? LLL_SHARED : LLL_PRIVATE;
48
49   /* We are going to modify shared data.  */
50   lll_lock (cbuffer->cond->__data.__lock, pshared);
51
52   if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
53     {
54       /* This thread is not waiting anymore.  Adjust the sequence counters
55          appropriately.  We do not increment WAKEUP_SEQ if this would
56          bump it over the value of TOTAL_SEQ.  This can happen if a thread
57          was woken and then canceled.  */
58       if (cbuffer->cond->__data.__wakeup_seq
59           < cbuffer->cond->__data.__total_seq)
60         {
61           ++cbuffer->cond->__data.__wakeup_seq;
62           ++cbuffer->cond->__data.__futex;
63         }
64       ++cbuffer->cond->__data.__woken_seq;
65     }
66
67   cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
68
69   /* If pthread_cond_destroy was called on this variable already,
70      notify the pthread_cond_destroy caller all waiters have left
71      and it can be successfully destroyed.  */
72   destroying = 0;
73   if (cbuffer->cond->__data.__total_seq == -1ULL
74       && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
75     {
76       lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
77       destroying = 1;
78     }
79
80   /* We are done.  */
81   lll_unlock (cbuffer->cond->__data.__lock, pshared);
82
83   /* Wake everybody to make sure no condvar signal gets lost.  */
84   if (! destroying)
85     lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
86
87   /* Get the mutex before returning unless asynchronous cancellation
88      is in effect.  We don't try to get the mutex if we already own it.  */
89   if (!(USE_REQUEUE_PI (cbuffer->mutex))
90       || ((cbuffer->mutex->__data.__lock & FUTEX_TID_MASK)
91           != THREAD_GETMEM (THREAD_SELF, tid)))
92   {
93     __pthread_mutex_cond_lock (cbuffer->mutex);
94   }
95   else
96     __pthread_mutex_cond_lock_adjust (cbuffer->mutex);
97 }
98
99
100 int
101 __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
102 {
103   struct _pthread_cleanup_buffer buffer;
104   struct _condvar_cleanup_buffer cbuffer;
105   int err;
106   int pshared = (cond->__data.__mutex == (void *) ~0l)
107                 ? LLL_SHARED : LLL_PRIVATE;
108
109 #if (defined lll_futex_wait_requeue_pi \
110      && defined __ASSUME_REQUEUE_PI)
111   int pi_flag = 0;
112 #endif
113
114   LIBC_PROBE (cond_wait, 2, cond, mutex);
115
116   /* Make sure we are alone.  */
117   lll_lock (cond->__data.__lock, pshared);
118
119   /* Now we can release the mutex.  */
120   err = __pthread_mutex_unlock_usercnt (mutex, 0);
121   if (__glibc_unlikely (err))
122     {
123       lll_unlock (cond->__data.__lock, pshared);
124       return err;
125     }
126
127   /* We have one new user of the condvar.  */
128   ++cond->__data.__total_seq;
129   ++cond->__data.__futex;
130   cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
131
132   /* Remember the mutex we are using here.  If there is already a
133      different address store this is a bad user bug.  Do not store
134      anything for pshared condvars.  */
135   if (cond->__data.__mutex != (void *) ~0l)
136     cond->__data.__mutex = mutex;
137
138   /* Prepare structure passed to cancellation handler.  */
139   cbuffer.cond = cond;
140   cbuffer.mutex = mutex;
141
142   /* Before we block we enable cancellation.  Therefore we have to
143      install a cancellation handler.  */
144   __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
145
146   /* The current values of the wakeup counter.  The "woken" counter
147      must exceed this value.  */
148   unsigned long long int val;
149   unsigned long long int seq;
150   val = seq = cond->__data.__wakeup_seq;
151   /* Remember the broadcast counter.  */
152   cbuffer.bc_seq = cond->__data.__broadcast_seq;
153
154   do
155     {
156       unsigned int futex_val = cond->__data.__futex;
157       /* Prepare to wait.  Release the condvar futex.  */
158       lll_unlock (cond->__data.__lock, pshared);
159
160       /* Enable asynchronous cancellation.  Required by the standard.  */
161       cbuffer.oldtype = __pthread_enable_asynccancel ();
162
163 #if (defined lll_futex_wait_requeue_pi \
164      && defined __ASSUME_REQUEUE_PI)
165       /* If pi_flag remained 1 then it means that we had the lock and the mutex
166          but a spurious waker raced ahead of us.  Give back the mutex before
167          going into wait again.  */
168       if (pi_flag)
169         {
170           __pthread_mutex_cond_lock_adjust (mutex);
171           __pthread_mutex_unlock_usercnt (mutex, 0);
172         }
173       pi_flag = USE_REQUEUE_PI (mutex);
174
175       if (pi_flag)
176         {
177           err = lll_futex_wait_requeue_pi (&cond->__data.__futex,
178                                            futex_val, &mutex->__data.__lock,
179                                            pshared);
180
181           pi_flag = (err == 0);
182         }
183       else
184 #endif
185           /* Wait until woken by signal or broadcast.  */
186         lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
187
188       /* Disable asynchronous cancellation.  */
189       __pthread_disable_asynccancel (cbuffer.oldtype);
190
191       /* We are going to look at shared data again, so get the lock.  */
192       lll_lock (cond->__data.__lock, pshared);
193
194       /* If a broadcast happened, we are done.  */
195       if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
196         goto bc_out;
197
198       /* Check whether we are eligible for wakeup.  */
199       val = cond->__data.__wakeup_seq;
200     }
201   while (val == seq || cond->__data.__woken_seq == val);
202
203   /* Another thread woken up.  */
204   ++cond->__data.__woken_seq;
205
206  bc_out:
207
208   cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
209
210   /* If pthread_cond_destroy was called on this varaible already,
211      notify the pthread_cond_destroy caller all waiters have left
212      and it can be successfully destroyed.  */
213   if (cond->__data.__total_seq == -1ULL
214       && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
215     lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
216
217   /* We are done with the condvar.  */
218   lll_unlock (cond->__data.__lock, pshared);
219
220   /* The cancellation handling is back to normal, remove the handler.  */
221   __pthread_cleanup_pop (&buffer, 0);
222
223   /* Get the mutex before returning.  Not needed for PI.  */
224 #if (defined lll_futex_wait_requeue_pi \
225      && defined __ASSUME_REQUEUE_PI)
226   if (pi_flag)
227     {
228       __pthread_mutex_cond_lock_adjust (mutex);
229       return 0;
230     }
231   else
232 #endif
233     return __pthread_mutex_cond_lock (mutex);
234 }
235
236 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
237                   GLIBC_2_3_2);