ARC defconfigs: enable some items
[uclibc-ng.git] / libpthread / linuxthreads.old / rwlock.c
blob3ab9402619959639530b1649ba3cde273b504073
1 /* Read-write lock implementation.
2 Copyright (C) 1998 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.
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.
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.
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 see <http://www.gnu.org/licenses/>. */
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include "internals.h"
25 #include "queue.h"
26 #include "spinlock.h"
27 #include "restart.h"
30 * Check whether the calling thread already owns one or more read locks on the
31 * specified lock. If so, return a pointer to the read lock info structure
32 * corresponding to that lock.
35 static pthread_readlock_info *
36 rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
38 pthread_readlock_info *info;
40 for (info = self->p_readlock_list; info != NULL; info = info->pr_next)
42 if (info->pr_lock == rwlock)
43 return info;
46 return NULL;
50 * Add a new lock to the thread's list of locks for which it has a read lock.
51 * A new info node must be allocated for this, which is taken from the thread's
52 * free list, or by calling malloc. If malloc fails, a null pointer is
53 * returned. Otherwise the lock info structure is initialized and pushed
54 * onto the thread's list.
57 static pthread_readlock_info *
58 rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
60 pthread_readlock_info *info = self->p_readlock_free;
62 if (info != NULL)
63 self->p_readlock_free = info->pr_next;
64 else
65 info = malloc(sizeof *info);
67 if (info == NULL)
68 return NULL;
70 info->pr_lock_count = 1;
71 info->pr_lock = rwlock;
72 info->pr_next = self->p_readlock_list;
73 self->p_readlock_list = info;
75 return info;
79 * If the thread owns a read lock over the given pthread_rwlock_t,
80 * and this read lock is tracked in the thread's lock list,
81 * this function returns a pointer to the info node in that list.
82 * It also decrements the lock count within that node, and if
83 * it reaches zero, it removes the node from the list.
84 * If nothing is found, it returns a null pointer.
87 static pthread_readlock_info *
88 rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
90 pthread_readlock_info **pinfo;
92 for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
94 if ((*pinfo)->pr_lock == rwlock)
96 pthread_readlock_info *info = *pinfo;
97 if (--info->pr_lock_count == 0)
98 *pinfo = info->pr_next;
99 return info;
103 return NULL;
107 * This function checks whether the conditions are right to place a read lock.
108 * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
109 * locked upon entry.
112 static int
113 rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
115 /* Can't readlock; it is write locked. */
116 if (rwlock->__rw_writer != NULL)
117 return 0;
119 /* Lock prefers readers; get it. */
120 if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
121 return 1;
123 /* Lock prefers writers, but none are waiting. */
124 if (queue_is_empty(&rwlock->__rw_write_waiting))
125 return 1;
127 /* Writers are waiting, but this thread already has a read lock */
128 if (have_lock_already)
129 return 1;
131 /* Writers are waiting, and this is a new lock */
132 return 0;
136 * This function helps support brain-damaged recursive read locking
137 * semantics required by Unix 98, while maintaining write priority.
138 * This basically determines whether this thread already holds a read lock
139 * already. It returns 1 if so, otherwise it returns 0.
141 * If the thread has any ``untracked read locks'' then it just assumes
142 * that this lock is among them, just to be safe, and returns 1.
144 * Also, if it finds the thread's lock in the list, it sets the pointer
145 * referenced by pexisting to refer to the list entry.
147 * If the thread has no untracked locks, and the lock is not found
148 * in its list, then it is added to the list. If this fails,
149 * then *pout_of_mem is set to 1.
152 static int
153 rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
154 pthread_readlock_info **pexisting, int *pout_of_mem)
156 pthread_readlock_info *existing = NULL;
157 int out_of_mem = 0, have_lock_already = 0;
158 pthread_descr self = *pself;
160 if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
162 if (!self)
163 self = thread_self();
165 existing = rwlock_is_in_list(self, rwlock);
167 if (existing != NULL || self->p_untracked_readlock_count > 0)
168 have_lock_already = 1;
169 else
171 existing = rwlock_add_to_list(self, rwlock);
172 if (existing == NULL)
173 out_of_mem = 1;
177 *pout_of_mem = out_of_mem;
178 *pexisting = existing;
179 *pself = self;
181 return have_lock_already;
185 pthread_rwlock_init (pthread_rwlock_t *rwlock,
186 const pthread_rwlockattr_t *attr)
188 __pthread_init_lock(&rwlock->__rw_lock);
189 rwlock->__rw_readers = 0;
190 rwlock->__rw_writer = NULL;
191 rwlock->__rw_read_waiting = NULL;
192 rwlock->__rw_write_waiting = NULL;
194 if (attr == NULL)
196 rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
197 rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
199 else
201 rwlock->__rw_kind = attr->__lockkind;
202 rwlock->__rw_pshared = attr->__pshared;
205 return 0;
210 pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
212 int readers;
213 _pthread_descr writer;
215 __pthread_lock (&rwlock->__rw_lock, NULL);
216 readers = rwlock->__rw_readers;
217 writer = rwlock->__rw_writer;
218 __pthread_unlock (&rwlock->__rw_lock);
220 if (readers > 0 || writer != NULL)
221 return EBUSY;
223 return 0;
227 pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
229 pthread_descr self = NULL;
230 pthread_readlock_info *existing;
231 int out_of_mem, have_lock_already;
233 have_lock_already = rwlock_have_already(&self, rwlock,
234 &existing, &out_of_mem);
236 for (;;)
238 if (self == NULL)
239 self = thread_self ();
241 __pthread_lock (&rwlock->__rw_lock, self);
243 if (rwlock_can_rdlock(rwlock, have_lock_already))
244 break;
246 enqueue (&rwlock->__rw_read_waiting, self);
247 __pthread_unlock (&rwlock->__rw_lock);
248 suspend (self); /* This is not a cancellation point */
251 ++rwlock->__rw_readers;
252 __pthread_unlock (&rwlock->__rw_lock);
254 if (have_lock_already || out_of_mem)
256 if (existing != NULL)
257 existing->pr_lock_count++;
258 else
259 self->p_untracked_readlock_count++;
262 return 0;
266 pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
268 pthread_descr self = thread_self();
269 pthread_readlock_info *existing;
270 int out_of_mem, have_lock_already;
271 int retval = EBUSY;
273 have_lock_already = rwlock_have_already(&self, rwlock,
274 &existing, &out_of_mem);
276 __pthread_lock (&rwlock->__rw_lock, self);
278 /* 0 is passed to here instead of have_lock_already.
279 This is to meet Single Unix Spec requirements:
280 if writers are waiting, pthread_rwlock_tryrdlock
281 does not acquire a read lock, even if the caller has
282 one or more read locks already. */
284 if (rwlock_can_rdlock(rwlock, 0))
286 ++rwlock->__rw_readers;
287 retval = 0;
290 __pthread_unlock (&rwlock->__rw_lock);
292 if (retval == 0)
294 if (have_lock_already || out_of_mem)
296 if (existing != NULL)
297 existing->pr_lock_count++;
298 else
299 self->p_untracked_readlock_count++;
303 return retval;
308 pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
310 pthread_descr self = thread_self ();
312 while(1)
314 __pthread_lock (&rwlock->__rw_lock, self);
315 if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
317 rwlock->__rw_writer = self;
318 __pthread_unlock (&rwlock->__rw_lock);
319 return 0;
322 /* Suspend ourselves, then try again */
323 enqueue (&rwlock->__rw_write_waiting, self);
324 __pthread_unlock (&rwlock->__rw_lock);
325 suspend (self); /* This is not a cancellation point */
331 pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
333 int result = EBUSY;
335 __pthread_lock (&rwlock->__rw_lock, NULL);
336 if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
338 rwlock->__rw_writer = thread_self ();
339 result = 0;
341 __pthread_unlock (&rwlock->__rw_lock);
343 return result;
348 pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
350 pthread_descr torestart;
351 pthread_descr th;
353 __pthread_lock (&rwlock->__rw_lock, NULL);
354 if (rwlock->__rw_writer != NULL)
356 /* Unlocking a write lock. */
357 if (rwlock->__rw_writer != thread_self ())
359 __pthread_unlock (&rwlock->__rw_lock);
360 return EPERM;
362 rwlock->__rw_writer = NULL;
364 if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
365 || (th = dequeue (&rwlock->__rw_write_waiting)) == NULL)
367 /* Restart all waiting readers. */
368 torestart = rwlock->__rw_read_waiting;
369 rwlock->__rw_read_waiting = NULL;
370 __pthread_unlock (&rwlock->__rw_lock);
371 while ((th = dequeue (&torestart)) != NULL)
372 restart (th);
374 else
376 /* Restart one waiting writer. */
377 __pthread_unlock (&rwlock->__rw_lock);
378 restart (th);
381 else
383 /* Unlocking a read lock. */
384 if (rwlock->__rw_readers == 0)
386 __pthread_unlock (&rwlock->__rw_lock);
387 return EPERM;
390 --rwlock->__rw_readers;
391 if (rwlock->__rw_readers == 0)
392 /* Restart one waiting writer, if any. */
393 th = dequeue (&rwlock->__rw_write_waiting);
394 else
395 th = NULL;
397 __pthread_unlock (&rwlock->__rw_lock);
398 if (th != NULL)
399 restart (th);
401 /* Recursive lock fixup */
403 if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
405 pthread_descr self = thread_self();
406 pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
408 if (victim != NULL)
410 if (victim->pr_lock_count == 0)
412 victim->pr_next = self->p_readlock_free;
413 self->p_readlock_free = victim;
416 else
418 if (self->p_untracked_readlock_count > 0)
419 self->p_untracked_readlock_count--;
424 return 0;
430 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
432 attr->__lockkind = 0;
433 attr->__pshared = 0;
435 return 0;
440 pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr attribute_unused)
442 return 0;
447 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
449 *pshared = attr->__pshared;
450 return 0;
455 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
457 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
458 return EINVAL;
460 attr->__pshared = pshared;
462 return 0;
467 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
469 *pref = attr->__lockkind;
470 return 0;
475 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
477 if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
478 && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
479 && pref != PTHREAD_RWLOCK_DEFAULT_NP)
480 return EINVAL;
482 attr->__lockkind = pref;
484 return 0;