Use THREAD_GETMEM And THREAD_SETMEM.
[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 /* Function called by pthread_cancel to remove the thread from
31    waiting inside pthread_rwlock_timedrdlock or pthread_rwlock_timedwrlock. */
32
33 static int rwlock_rd_extricate_func(void *obj, pthread_descr th)
34 {
35   pthread_rwlock_t *rwlock = obj;
36   int did_remove = 0;
37
38   __pthread_lock((struct _pthread_fastlock *) &rwlock->__rw_lock, NULL);
39   did_remove = remove_from_queue(&rwlock->__rw_read_waiting, th);
40   __pthread_unlock((struct _pthread_fastlock *) &rwlock->__rw_lock);
41
42   return did_remove;
43 }
44
45 static int rwlock_wr_extricate_func(void *obj, pthread_descr th)
46 {
47   pthread_rwlock_t *rwlock = obj;
48   int did_remove = 0;
49
50   __pthread_lock((struct _pthread_fastlock *) &rwlock->__rw_lock, NULL);
51   did_remove = remove_from_queue(&rwlock->__rw_write_waiting, th);
52   __pthread_unlock((struct _pthread_fastlock *) &rwlock->__rw_lock);
53
54   return did_remove;
55 }
56
57 /*
58  * Check whether the calling thread already owns one or more read locks on the
59  * specified lock. If so, return a pointer to the read lock info structure
60  * corresponding to that lock.
61  */
62
63 static pthread_readlock_info *
64 rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
65 {
66   pthread_readlock_info *info;
67
68   for (info = THREAD_GETMEM (self, p_readlock_list); info != NULL;
69        info = info->pr_next)
70     {
71       if (info->pr_lock == rwlock)
72         return info;
73     }
74
75   return NULL;
76 }
77
78 /*
79  * Add a new lock to the thread's list of locks for which it has a read lock.
80  * A new info node must be allocated for this, which is taken from the thread's
81  * free list, or by calling malloc. If malloc fails, a null pointer is
82  * returned. Otherwise the lock info structure is initialized and pushed
83  * onto the thread's list.
84  */
85
86 static pthread_readlock_info *
87 rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
88 {
89   pthread_readlock_info *info = THREAD_GETMEM (self, p_readlock_free);
90
91   if (info != NULL)
92     THREAD_SETMEM (self, p_readlock_free, info->pr_next);
93   else
94     info = malloc(sizeof *info);
95
96   if (info == NULL)
97     return NULL;
98
99   info->pr_lock_count = 1;
100   info->pr_lock = rwlock;
101   info->pr_next = THREAD_GETMEM (self, p_readlock_list);
102   THREAD_SETMEM (self, p_readlock_list, info);
103
104   return info;
105 }
106
107 /*
108  * If the thread owns a read lock over the given pthread_rwlock_t,
109  * and this read lock is tracked in the thread's lock list,
110  * this function returns a pointer to the info node in that list.
111  * It also decrements the lock count within that node, and if
112  * it reaches zero, it removes the node from the list.
113  * If nothing is found, it returns a null pointer.
114  */
115
116 static pthread_readlock_info *
117 rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
118 {
119   pthread_readlock_info **pinfo;
120
121   for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
122     {
123       if ((*pinfo)->pr_lock == rwlock)
124         {
125           pthread_readlock_info *info = *pinfo;
126           if (--info->pr_lock_count == 0)
127             *pinfo = info->pr_next;
128           return info;
129         }
130     }
131
132   return NULL;
133 }
134
135 /*
136  * This function checks whether the conditions are right to place a read lock.
137  * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
138  * locked upon entry.
139  */
140
141 static int
142 rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
143 {
144   /* Can't readlock; it is write locked. */
145   if (rwlock->__rw_writer != NULL)
146     return 0;
147
148   /* Lock prefers readers; get it. */
149   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
150     return 1;
151
152   /* Lock prefers writers, but none are waiting. */
153   if (queue_is_empty(&rwlock->__rw_write_waiting))
154     return 1;
155
156   /* Writers are waiting, but this thread already has a read lock */
157   if (have_lock_already)
158     return 1;
159
160   /* Writers are waiting, and this is a new lock */
161   return 0;
162 }
163
164 /*
165  * This function helps support brain-damaged recursive read locking
166  * semantics required by Unix 98, while maintaining write priority.
167  * This basically determines whether this thread already holds a read lock
168  * already. It returns 1 if so, otherwise it returns 0.
169  *
170  * If the thread has any ``untracked read locks'' then it just assumes
171  * that this lock is among them, just to be safe, and returns 1.
172  *
173  * Also, if it finds the thread's lock in the list, it sets the pointer
174  * referenced by pexisting to refer to the list entry.
175  *
176  * If the thread has no untracked locks, and the lock is not found
177  * in its list, then it is added to the list. If this fails,
178  * then *pout_of_mem is set to 1.
179  */
180
181 static int
182 rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
183     pthread_readlock_info **pexisting, int *pout_of_mem)
184 {
185   pthread_readlock_info *existing = NULL;
186   int out_of_mem = 0, have_lock_already = 0;
187   pthread_descr self = *pself;
188
189   if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
190     {
191       if (!self)
192         *pself = self = thread_self();
193
194       existing = rwlock_is_in_list(self, rwlock);
195
196       if (existing != NULL
197           || THREAD_GETMEM (self, p_untracked_readlock_count) > 0)
198         have_lock_already = 1;
199       else
200         {
201           existing = rwlock_add_to_list(self, rwlock);
202           if (existing == NULL)
203             out_of_mem = 1;
204         }
205     }
206
207   *pout_of_mem = out_of_mem;
208   *pexisting = existing;
209
210   return have_lock_already;
211 }
212
213 int
214 __pthread_rwlock_init (pthread_rwlock_t *rwlock,
215                        const pthread_rwlockattr_t *attr)
216 {
217   __pthread_init_lock(&rwlock->__rw_lock);
218   rwlock->__rw_readers = 0;
219   rwlock->__rw_writer = NULL;
220   rwlock->__rw_read_waiting = NULL;
221   rwlock->__rw_write_waiting = NULL;
222
223   if (attr == NULL)
224     {
225       rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
226       rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
227     }
228   else
229     {
230       rwlock->__rw_kind = attr->__lockkind;
231       rwlock->__rw_pshared = attr->__pshared;
232     }
233
234   return 0;
235 }
236 strong_alias (__pthread_rwlock_init, pthread_rwlock_init)
237
238
239 int
240 __pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
241 {
242   int readers;
243   _pthread_descr writer;
244
245   __pthread_lock (&rwlock->__rw_lock, NULL);
246   readers = rwlock->__rw_readers;
247   writer = rwlock->__rw_writer;
248   __pthread_unlock (&rwlock->__rw_lock);
249
250   if (readers > 0 || writer != NULL)
251     return EBUSY;
252
253   return 0;
254 }
255 strong_alias (__pthread_rwlock_destroy, pthread_rwlock_destroy)
256
257 int
258 __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
259 {
260   pthread_descr self = NULL;
261   pthread_readlock_info *existing;
262   int out_of_mem, have_lock_already;
263
264   have_lock_already = rwlock_have_already(&self, rwlock,
265                                           &existing, &out_of_mem);
266
267   if (self == NULL)
268     self = thread_self ();
269
270   for (;;)
271     {
272       __pthread_lock (&rwlock->__rw_lock, self);
273
274       if (rwlock_can_rdlock(rwlock, have_lock_already))
275         break;
276
277       enqueue (&rwlock->__rw_read_waiting, self);
278       __pthread_unlock (&rwlock->__rw_lock);
279       suspend (self); /* This is not a cancellation point */
280     }
281
282   ++rwlock->__rw_readers;
283   __pthread_unlock (&rwlock->__rw_lock);
284
285   if (have_lock_already || out_of_mem)
286     {
287       if (existing != NULL)
288         ++existing->pr_lock_count;
289       else
290         ++self->p_untracked_readlock_count;
291     }
292
293   return 0;
294 }
295 strong_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
296
297 int
298 __pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
299                               const struct timespec *abstime)
300 {
301   pthread_descr self = NULL;
302   pthread_readlock_info *existing;
303   int out_of_mem, have_lock_already;
304   pthread_extricate_if extr;
305
306   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
307     return EINVAL;
308
309   have_lock_already = rwlock_have_already(&self, rwlock,
310                                           &existing, &out_of_mem);
311
312   if (self == NULL)
313     self = thread_self ();
314
315   /* Set up extrication interface */
316   extr.pu_object = rwlock;
317   extr.pu_extricate_func = rwlock_rd_extricate_func;
318
319   /* Register extrication interface */
320   __pthread_set_own_extricate_if (self, &extr);
321
322   for (;;)
323     {
324       __pthread_lock (&rwlock->__rw_lock, self);
325
326       if (rwlock_can_rdlock(rwlock, have_lock_already))
327         break;
328
329       enqueue (&rwlock->__rw_read_waiting, self);
330       __pthread_unlock (&rwlock->__rw_lock);
331       /* This is not a cancellation point */
332       if (timedsuspend (self, abstime) == 0)
333         {
334           int was_on_queue;
335
336           __pthread_lock (&rwlock->__rw_lock, self);
337           was_on_queue = remove_from_queue (&rwlock->__rw_read_waiting, self);
338           __pthread_unlock (&rwlock->__rw_lock);
339
340           if (was_on_queue)
341             {
342               __pthread_set_own_extricate_if (self, 0);
343               return ETIMEDOUT;
344             }
345
346           /* Eat the outstanding restart() from the signaller */
347           suspend (self);
348         }
349     }
350
351   __pthread_set_own_extricate_if (self, 0);
352
353   ++rwlock->__rw_readers;
354   __pthread_unlock (&rwlock->__rw_lock);
355
356   if (have_lock_already || out_of_mem)
357     {
358       if (existing != NULL)
359         ++existing->pr_lock_count;
360       else
361         ++self->p_untracked_readlock_count;
362     }
363
364   return 0;
365 }
366 strong_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)
367
368 int
369 __pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
370 {
371   pthread_descr self = thread_self();
372   pthread_readlock_info *existing;
373   int out_of_mem, have_lock_already;
374   int retval = EBUSY;
375
376   have_lock_already = rwlock_have_already(&self, rwlock,
377       &existing, &out_of_mem);
378
379   __pthread_lock (&rwlock->__rw_lock, self);
380
381   /* 0 is passed to here instead of have_lock_already.
382      This is to meet Single Unix Spec requirements:
383      if writers are waiting, pthread_rwlock_tryrdlock
384      does not acquire a read lock, even if the caller has
385      one or more read locks already. */
386
387   if (rwlock_can_rdlock(rwlock, 0))
388     {
389       ++rwlock->__rw_readers;
390       retval = 0;
391     }
392
393   __pthread_unlock (&rwlock->__rw_lock);
394
395   if (retval == 0)
396     {
397       if (have_lock_already || out_of_mem)
398         {
399           if (existing != NULL)
400             ++existing->pr_lock_count;
401           else
402             ++self->p_untracked_readlock_count;
403         }
404     }
405
406   return retval;
407 }
408 strong_alias (__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock)
409
410
411 int
412 __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
413 {
414   pthread_descr self = thread_self ();
415
416   while(1)
417     {
418       __pthread_lock (&rwlock->__rw_lock, self);
419       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
420         {
421           rwlock->__rw_writer = self;
422           __pthread_unlock (&rwlock->__rw_lock);
423           return 0;
424         }
425
426       /* Suspend ourselves, then try again */
427       enqueue (&rwlock->__rw_write_waiting, self);
428       __pthread_unlock (&rwlock->__rw_lock);
429       suspend (self); /* This is not a cancellation point */
430     }
431 }
432 strong_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
433
434
435 int
436 __pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
437                               const struct timespec *abstime)
438 {
439   pthread_descr self;
440   pthread_extricate_if extr;
441
442   if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
443     return EINVAL;
444
445   self = thread_self ();
446
447   /* Set up extrication interface */
448   extr.pu_object = rwlock;
449   extr.pu_extricate_func =  rwlock_wr_extricate_func;
450
451   /* Register extrication interface */
452   __pthread_set_own_extricate_if (self, &extr);
453
454   while(1)
455     {
456       __pthread_lock (&rwlock->__rw_lock, self);
457
458       if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
459         {
460           rwlock->__rw_writer = self;
461           __pthread_set_own_extricate_if (self, 0);
462           __pthread_unlock (&rwlock->__rw_lock);
463           return 0;
464         }
465
466       /* Suspend ourselves, then try again */
467       enqueue (&rwlock->__rw_write_waiting, self);
468       __pthread_unlock (&rwlock->__rw_lock);
469       /* This is not a cancellation point */
470       if (timedsuspend (self, abstime) == 0)
471         {
472           int was_on_queue;
473
474           __pthread_lock (&rwlock->__rw_lock, self);
475           was_on_queue = remove_from_queue (&rwlock->__rw_write_waiting, self);
476           __pthread_unlock (&rwlock->__rw_lock);
477
478           if (was_on_queue)
479             {
480               __pthread_set_own_extricate_if (self, 0);
481               return ETIMEDOUT;
482             }
483
484           /* Eat the outstanding restart() from the signaller */
485           suspend (self);
486         }
487     }
488 }
489 strong_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
490
491
492 int
493 __pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
494 {
495   int result = EBUSY;
496
497   __pthread_lock (&rwlock->__rw_lock, NULL);
498   if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
499     {
500       rwlock->__rw_writer = thread_self ();
501       result = 0;
502     }
503   __pthread_unlock (&rwlock->__rw_lock);
504
505   return result;
506 }
507 strong_alias (__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock)
508
509
510 int
511 __pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
512 {
513   pthread_descr torestart;
514   pthread_descr th;
515
516   __pthread_lock (&rwlock->__rw_lock, NULL);
517   if (rwlock->__rw_writer != NULL)
518     {
519       /* Unlocking a write lock.  */
520       if (rwlock->__rw_writer != thread_self ())
521         {
522           __pthread_unlock (&rwlock->__rw_lock);
523           return EPERM;
524         }
525       rwlock->__rw_writer = NULL;
526
527       if ((rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
528            && !queue_is_empty(&rwlock->__rw_read_waiting))
529           || (th = dequeue(&rwlock->__rw_write_waiting)) == NULL)
530         {
531           /* Restart all waiting readers.  */
532           torestart = rwlock->__rw_read_waiting;
533           rwlock->__rw_read_waiting = NULL;
534           __pthread_unlock (&rwlock->__rw_lock);
535           while ((th = dequeue (&torestart)) != NULL)
536             restart (th);
537         }
538       else
539         {
540           /* Restart one waiting writer.  */
541           __pthread_unlock (&rwlock->__rw_lock);
542           restart (th);
543         }
544     }
545   else
546     {
547       /* Unlocking a read lock.  */
548       if (rwlock->__rw_readers == 0)
549         {
550           __pthread_unlock (&rwlock->__rw_lock);
551           return EPERM;
552         }
553
554       --rwlock->__rw_readers;
555       if (rwlock->__rw_readers == 0)
556         /* Restart one waiting writer, if any.  */
557         th = dequeue (&rwlock->__rw_write_waiting);
558       else
559         th = NULL;
560
561       __pthread_unlock (&rwlock->__rw_lock);
562       if (th != NULL)
563         restart (th);
564
565       /* Recursive lock fixup */
566
567       if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
568         {
569           pthread_descr self = thread_self();
570           pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
571
572           if (victim != NULL)
573             {
574               if (victim->pr_lock_count == 0)
575                 {
576                   victim->pr_next = THREAD_GETMEM (self, p_readlock_free);
577                   THREAD_SETMEM (self, p_readlock_free, victim);
578                 }
579             }
580           else
581             {
582               int val = THREAD_GETMEM (self, p_untracked_readlock_count);
583               if (val > 0)
584                 THREAD_SETMEM (self, p_untracked_readlock_count, val - 1);
585             }
586         }
587     }
588
589   return 0;
590 }
591 strong_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock)
592
593
594
595 int
596 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
597 {
598   attr->__lockkind = 0;
599   attr->__pshared = PTHREAD_PROCESS_PRIVATE;
600
601   return 0;
602 }
603
604
605 int
606 __pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
607 {
608   return 0;
609 }
610 strong_alias (__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy)
611
612
613 int
614 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
615 {
616   *pshared = attr->__pshared;
617   return 0;
618 }
619
620
621 int
622 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
623 {
624   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
625     return EINVAL;
626
627   /* For now it is not possible to shared a conditional variable.  */
628   if (pshared != PTHREAD_PROCESS_PRIVATE)
629     return ENOSYS;
630
631   attr->__pshared = pshared;
632
633   return 0;
634 }
635
636
637 int
638 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
639 {
640   *pref = attr->__lockkind;
641   return 0;
642 }
643
644
645 int
646 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
647 {
648   if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
649       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
650       && pref != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
651       && pref != PTHREAD_RWLOCK_DEFAULT_NP)
652     return EINVAL;
653
654   attr->__lockkind = pref;
655
656   return 0;
657 }