Use alternate fastlock functons everywhere.
[kopensolaris-gnu/glibc.git] / linuxthreads / rwlock.c
1 /* Read-write lock implementation.
2    Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Xavier Leroy <Xavier.Leroy@inria.fr>
5    and Ulrich Drepper <drepper@cygnus.com>, 1998.
6
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the GNU C Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <errno.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include "internals.h"
26 #include "queue.h"
27 #include "spinlock.h"
28 #include "restart.h"
29
30 /*
31  * Check whether the calling thread already owns one or more read locks on the
32  * specified lock. If so, return a pointer to the read lock info structure
33  * corresponding to that lock.
34  */
35
36 static pthread_readlock_info *
37 rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
38 {
39   pthread_readlock_info *info;
40
41   for (info = self->p_readlock_list; info != NULL; info = info->pr_next)
42     {
43       if (info->pr_lock == rwlock)
44         return info;
45     }
46
47   return NULL;
48 }
49
50 /*
51  * Add a new lock to the thread's list of locks for which it has a read lock.
52  * A new info node must be allocated for this, which is taken from the thread's
53  * free list, or by calling malloc. If malloc fails, a null pointer is
54  * returned. Otherwise the lock info structure is initialized and pushed
55  * onto the thread's list.
56  */
57
58 static pthread_readlock_info *
59 rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
60 {
61   pthread_readlock_info *info = self->p_readlock_free;
62
63   if (info != NULL)
64     self->p_readlock_free = info->pr_next;
65   else
66     info = malloc(sizeof *info);
67
68   if (info == NULL)
69     return NULL;
70
71   info->pr_lock_count = 1;
72   info->pr_lock = rwlock;
73   info->pr_next = self->p_readlock_list;
74   self->p_readlock_list = info;
75
76   return info;
77 }
78
79 /*
80  * If the thread owns a read lock over the given pthread_rwlock_t,
81  * and this read lock is tracked in the thread's lock list,
82  * this function returns a pointer to the info node in that list.
83  * It also decrements the lock count within that node, and if
84  * it reaches zero, it removes the node from the list.
85  * If nothing is found, it returns a null pointer.
86  */
87
88 static pthread_readlock_info *
89 rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
90 {
91   pthread_readlock_info **pinfo;
92
93   for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
94     {
95       if ((*pinfo)->pr_lock == rwlock)
96         {
97           pthread_readlock_info *info = *pinfo;
98           if (--info->pr_lock_count == 0)
99             *pinfo = info->pr_next;
100           return info;
101         }
102     }
103
104   return NULL;
105 }
106
107 /*
108  * This function checks whether the conditions are right to place a read lock.
109  * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
110  * locked upon entry.
111  */
112
113 static int
114 rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
115 {
116   /* Can't readlock; it is write locked. */
117   if (rwlock->__rw_writer != NULL)
118     return 0;
119
120   /* Lock prefers readers; get it. */
121   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
122     return 1;
123
124   /* Lock prefers writers, but none are waiting. */
125   if (queue_is_empty(&rwlock->__rw_write_waiting))
126     return 1;
127
128   /* Writers are waiting, but this thread already has a read lock */
129   if (have_lock_already)
130     return 1;
131
132   /* Writers are waiting, and this is a new lock */
133   return 0;
134 }
135
136 /*
137  * This function helps support brain-damaged recursive read locking
138  * semantics required by Unix 98, while maintaining write priority.
139  * This basically determines whether this thread already holds a read lock
140  * already. It returns 1 if so, otherwise it returns 0.
141  *
142  * If the thread has any ``untracked read locks'' then it just assumes
143  * that this lock is among them, just to be safe, and returns 1.
144  *
145  * Also, if it finds the thread's lock in the list, it sets the pointer
146  * referenced by pexisting to refer to the list entry.
147  *
148  * If the thread has no untracked locks, and the lock is not found
149  * in its list, then it is added to the list. If this fails,
150  * then *pout_of_mem is set to 1.
151  */
152
153 static int
154 rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
155     pthread_readlock_info **pexisting, int *pout_of_mem)
156 {
157   pthread_readlock_info *existing = NULL;
158   int out_of_mem = 0, have_lock_already = 0;
159   pthread_descr self = *pself;
160
161   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
162     {
163       if (!self)
164         self = thread_self();
165
166       existing = rwlock_is_in_list(self, rwlock);
167
168       if (existing != NULL || self->p_untracked_readlock_count > 0)
169         have_lock_already = 1;
170       else
171         {
172           existing = rwlock_add_to_list(self, rwlock);
173           if (existing == NULL)
174             out_of_mem = 1;
175         }
176     }
177
178   *pout_of_mem = out_of_mem;
179   *pexisting = existing;
180   *pself = self;
181
182   return have_lock_already;
183 }
184
185 int
186 __pthread_rwlock_init (pthread_rwlock_t *rwlock,
187                        const pthread_rwlockattr_t *attr)
188 {
189   __pthread_init_lock(&rwlock->__rw_lock);
190   rwlock->__rw_readers = 0;
191   rwlock->__rw_writer = NULL;
192   rwlock->__rw_read_waiting = NULL;
193   rwlock->__rw_write_waiting = NULL;
194
195   if (attr == NULL)
196     {
197       rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
198       rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
199     }
200   else
201     {
202       rwlock->__rw_kind = attr->__lockkind;
203       rwlock->__rw_pshared = attr->__pshared;
204     }
205
206   return 0;
207 }
208 strong_alias (__pthread_rwlock_init, pthread_rwlock_init)
209
210
211 int
212 __pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
213 {
214   int readers;
215   _pthread_descr writer;
216
217   __pthread_alt_lock (&rwlock->__rw_lock, NULL);
218   readers = rwlock->__rw_readers;
219   writer = rwlock->__rw_writer;
220   __pthread_alt_unlock (&rwlock->__rw_lock);
221
222   if (readers > 0 || writer != NULL)
223     return EBUSY;
224
225   return 0;
226 }
227 strong_alias (__pthread_rwlock_destroy, pthread_rwlock_destroy)
228
229 int
230 __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
231 {
232   pthread_descr self = NULL;
233   pthread_readlock_info *existing;
234   int out_of_mem, have_lock_already;
235
236   have_lock_already = rwlock_have_already(&self, rwlock,
237                                           &existing, &out_of_mem);
238
239   if (self == NULL)
240     self = thread_self ();
241
242   for (;;)
243     {
244       __pthread_alt_lock (&rwlock->__rw_lock, self);
245
246       if (rwlock_can_rdlock(rwlock, have_lock_already))
247         break;
248
249       enqueue (&rwlock->__rw_read_waiting, self);
250       __pthread_alt_unlock (&rwlock->__rw_lock);
251       suspend (self); /* This is not a cancellation point */
252     }
253
254   ++rwlock->__rw_readers;
255   __pthread_alt_unlock (&rwlock->__rw_lock);
256
257   if (have_lock_already || out_of_mem)
258     {
259       if (existing != NULL)
260         existing->pr_lock_count++;
261       else
262         self->p_untracked_readlock_count++;
263     }
264
265   return 0;
266 }
267 strong_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
268
269 int
270 __pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
271                               const struct timespec *abstime)
272 {
273   pthread_descr self = NULL;
274   pthread_readlock_info *existing;
275   int out_of_mem, have_lock_already;
276
277   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
278     return EINVAL;
279
280   have_lock_already = rwlock_have_already(&self, rwlock,
281                                           &existing, &out_of_mem);
282
283   if (self == NULL)
284     self = thread_self ();
285
286   for (;;)
287     {
288       if (__pthread_alt_timedlock (&rwlock->__rw_lock, self, abstime) == 0)
289         return ETIMEDOUT;
290
291       if (rwlock_can_rdlock(rwlock, have_lock_already))
292         break;
293
294       enqueue (&rwlock->__rw_read_waiting, self);
295       __pthread_alt_unlock (&rwlock->__rw_lock);
296       suspend (self); /* This is not a cancellation point */
297     }
298
299   ++rwlock->__rw_readers;
300   __pthread_alt_unlock (&rwlock->__rw_lock);
301
302   if (have_lock_already || out_of_mem)
303     {
304       if (existing != NULL)
305         existing->pr_lock_count++;
306       else
307         self->p_untracked_readlock_count++;
308     }
309
310   return 0;
311 }
312 strong_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)
313
314 int
315 __pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
316 {
317   pthread_descr self = thread_self();
318   pthread_readlock_info *existing;
319   int out_of_mem, have_lock_already;
320   int retval = EBUSY;
321
322   have_lock_already = rwlock_have_already(&self, rwlock,
323       &existing, &out_of_mem);
324
325   __pthread_alt_lock (&rwlock->__rw_lock, self);
326
327   /* 0 is passed to here instead of have_lock_already.
328      This is to meet Single Unix Spec requirements:
329      if writers are waiting, pthread_rwlock_tryrdlock
330      does not acquire a read lock, even if the caller has
331      one or more read locks already. */
332
333   if (rwlock_can_rdlock(rwlock, 0))
334     {
335       ++rwlock->__rw_readers;
336       retval = 0;
337     }
338
339   __pthread_alt_unlock (&rwlock->__rw_lock);
340
341   if (retval == 0)
342     {
343       if (have_lock_already || out_of_mem)
344         {
345           if (existing != NULL)
346             existing->pr_lock_count++;
347           else
348             self->p_untracked_readlock_count++;
349         }
350     }
351
352   return retval;
353 }
354 strong_alias (__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock)
355
356
357 int
358 __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
359 {
360   pthread_descr self = thread_self ();
361
362   while(1)
363     {
364       __pthread_alt_lock (&rwlock->__rw_lock, self);
365       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
366         {
367           rwlock->__rw_writer = self;
368           __pthread_alt_unlock (&rwlock->__rw_lock);
369           return 0;
370         }
371
372       /* Suspend ourselves, then try again */
373       enqueue (&rwlock->__rw_write_waiting, self);
374       __pthread_alt_unlock (&rwlock->__rw_lock);
375       suspend (self); /* This is not a cancellation point */
376     }
377 }
378 strong_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
379
380
381 int
382 __pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
383                               const struct timespec *abstime)
384 {
385   pthread_descr self;
386
387   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
388     return EINVAL;
389
390   self = thread_self ();
391
392   while(1)
393     {
394       if (__pthread_alt_timedlock (&rwlock->__rw_lock, self, abstime) == 0)
395         return ETIMEDOUT;
396
397       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
398         {
399           rwlock->__rw_writer = self;
400           __pthread_alt_unlock (&rwlock->__rw_lock);
401           return 0;
402         }
403
404       /* Suspend ourselves, then try again */
405       enqueue (&rwlock->__rw_write_waiting, self);
406       __pthread_alt_unlock (&rwlock->__rw_lock);
407       suspend (self); /* This is not a cancellation point */
408     }
409 }
410 strong_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
411
412
413 int
414 __pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
415 {
416   int result = EBUSY;
417
418   __pthread_alt_lock (&rwlock->__rw_lock, NULL);
419   if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
420     {
421       rwlock->__rw_writer = thread_self ();
422       result = 0;
423     }
424   __pthread_alt_unlock (&rwlock->__rw_lock);
425
426   return result;
427 }
428 strong_alias (__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock)
429
430
431 int
432 __pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
433 {
434   pthread_descr torestart;
435   pthread_descr th;
436
437   __pthread_alt_lock (&rwlock->__rw_lock, NULL);
438   if (rwlock->__rw_writer != NULL)
439     {
440       /* Unlocking a write lock.  */
441       if (rwlock->__rw_writer != thread_self ())
442         {
443           __pthread_alt_unlock (&rwlock->__rw_lock);
444           return EPERM;
445         }
446       rwlock->__rw_writer = NULL;
447
448       if ((rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
449            && !queue_is_empty(&rwlock->__rw_read_waiting))
450           || (th = dequeue(&rwlock->__rw_write_waiting)) == NULL)
451         {
452           /* Restart all waiting readers.  */
453           torestart = rwlock->__rw_read_waiting;
454           rwlock->__rw_read_waiting = NULL;
455           __pthread_alt_unlock (&rwlock->__rw_lock);
456           while ((th = dequeue (&torestart)) != NULL)
457             restart (th);
458         }
459       else
460         {
461           /* Restart one waiting writer.  */
462           __pthread_alt_unlock (&rwlock->__rw_lock);
463           restart (th);
464         }
465     }
466   else
467     {
468       /* Unlocking a read lock.  */
469       if (rwlock->__rw_readers == 0)
470         {
471           __pthread_alt_unlock (&rwlock->__rw_lock);
472           return EPERM;
473         }
474
475       --rwlock->__rw_readers;
476       if (rwlock->__rw_readers == 0)
477         /* Restart one waiting writer, if any.  */
478         th = dequeue (&rwlock->__rw_write_waiting);
479       else
480         th = NULL;
481
482       __pthread_alt_unlock (&rwlock->__rw_lock);
483       if (th != NULL)
484         restart (th);
485
486       /* Recursive lock fixup */
487
488       if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
489         {
490           pthread_descr self = thread_self();
491           pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
492
493           if (victim != NULL)
494             {
495               if (victim->pr_lock_count == 0)
496                 {
497                   victim->pr_next = self->p_readlock_free;
498                   self->p_readlock_free = victim;
499                 }
500             }
501           else
502             {
503               if (self->p_untracked_readlock_count > 0)
504                 self->p_untracked_readlock_count--;
505             }
506         }
507     }
508
509   return 0;
510 }
511 strong_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock)
512
513
514
515 int
516 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
517 {
518   attr->__lockkind = 0;
519   attr->__pshared = 0;
520
521   return 0;
522 }
523
524
525 int
526 __pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
527 {
528   return 0;
529 }
530 strong_alias (__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy)
531
532
533 int
534 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
535 {
536   *pshared = attr->__pshared;
537   return 0;
538 }
539
540
541 int
542 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
543 {
544   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
545     return EINVAL;
546
547   attr->__pshared = pshared;
548
549   return 0;
550 }
551
552
553 int
554 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
555 {
556   *pref = attr->__lockkind;
557   return 0;
558 }
559
560
561 int
562 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
563 {
564   if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
565       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
566       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
567       && pref != PTHREAD_RWLOCK_DEFAULT_NP)
568     return EINVAL;
569
570   attr->__lockkind = pref;
571
572   return 0;
573 }