Update.
[glibc/history.git] / linuxthreads / rwlock.c
blob99209f75da2b3644cb561af710aa45f07b4e21a3
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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <errno.h>
23 #include <pthread.h>
24 #include "internals.h"
25 #include "queue.h"
26 #include "restart.h"
27 #include "spinlock.h"
29 int
30 pthread_rwlock_init (pthread_rwlock_t *rwlock,
31 const pthread_rwlockattr_t *attr)
33 __pthread_init_lock(&rwlock->rw_lock);
34 rwlock->rw_readers = 0;
35 rwlock->rw_writer = NULL;
36 rwlock->rw_read_waiting = NULL;
37 rwlock->rw_write_waiting = NULL;
39 if (attr == NULL)
41 rwlock->rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
42 rwlock->rw_pshared = PTHREAD_PROCESS_PRIVATE;
44 else
46 rwlock->rw_kind = attr->lockkind;
47 rwlock->rw_pshared = attr->pshared;
50 return 0;
54 int
55 pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
57 int readers;
58 _pthread_descr writer;
60 __pthread_lock (&rwlock->rw_lock);
61 readers = rwlock->rw_readers;
62 writer = rwlock->rw_writer;
63 __pthread_unlock (&rwlock->rw_lock);
65 if (readers > 0 || writer != NULL)
66 return EBUSY;
68 return 0;
72 int
73 pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
75 pthread_descr self;
77 while (1)
79 __pthread_lock (&rwlock->rw_lock);
80 if (rwlock->rw_writer == NULL
81 || (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
82 && rwlock->rw_readers != 0))
83 /* We can add a reader lock. */
84 break;
86 /* Suspend ourselves, then try again */
87 self = thread_self ();
88 enqueue (&rwlock->rw_read_waiting, self);
89 __pthread_unlock (&rwlock->rw_lock);
90 suspend (self); /* This is not a cancellation point */
93 ++rwlock->rw_readers;
94 __pthread_unlock (&rwlock->rw_lock);
96 return 0;
101 pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
103 int result = EBUSY;
105 __pthread_lock (&rwlock->rw_lock);
106 if (rwlock->rw_writer == NULL
107 || (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
108 && rwlock->rw_readers != 0))
110 ++rwlock->rw_readers;
111 result = 0;
113 __pthread_unlock (&rwlock->rw_lock);
115 return result;
120 pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
122 pthread_descr self = thread_self ();
124 while(1)
126 __pthread_lock (&rwlock->rw_lock);
127 if (rwlock->rw_readers == 0 && rwlock->rw_writer == NULL)
129 rwlock->rw_writer = self;
130 __pthread_unlock (&rwlock->rw_lock);
131 return 0;
134 /* Suspend ourselves, then try again */
135 enqueue (&rwlock->rw_write_waiting, self);
136 __pthread_unlock (&rwlock->rw_lock);
137 suspend (self); /* This is not a cancellation point */
143 pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
145 int result = EBUSY;
147 __pthread_lock (&rwlock->rw_lock);
148 if (rwlock->rw_readers == 0 && rwlock->rw_writer == NULL)
150 rwlock->rw_writer = thread_self ();
151 result = 0;
153 __pthread_unlock (&rwlock->rw_lock);
155 return result;
160 pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
162 pthread_descr torestart;
163 pthread_descr th;
165 __pthread_lock (&rwlock->rw_lock);
166 if (rwlock->rw_writer != NULL)
168 /* Unlocking a write lock. */
169 if (rwlock->rw_writer != thread_self ())
171 __pthread_unlock (&rwlock->rw_lock);
172 return EPERM;
174 rwlock->rw_writer = NULL;
176 if (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
177 || (th = dequeue (&rwlock->rw_write_waiting)) == NULL)
179 /* Restart all waiting readers. */
180 torestart = rwlock->rw_read_waiting;
181 rwlock->rw_read_waiting = NULL;
182 __pthread_unlock (&rwlock->rw_lock);
183 while ((th = dequeue (&torestart)) != NULL)
184 restart (th);
186 else
188 /* Restart one waiting writer. */
189 __pthread_unlock (&rwlock->rw_lock);
190 restart (th);
193 else
195 /* Unlocking a read lock. */
196 if (rwlock->rw_readers == 0)
198 __pthread_unlock (&rwlock->rw_lock);
199 return EPERM;
202 --rwlock->rw_readers;
203 if (rwlock->rw_readers == 0)
204 /* Restart one waiting writer, if any. */
205 th = dequeue (&rwlock->rw_write_waiting);
206 else
207 th = NULL;
209 __pthread_unlock (&rwlock->rw_lock);
210 if (th != NULL)
211 restart (th);
214 return 0;
220 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
222 attr->lockkind = 0;
223 attr->pshared = 0;
225 return 0;
230 pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
232 return 0;
237 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
239 *pshared = attr->pshared;
240 return 0;
245 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
247 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
248 return EINVAL;
250 attr->pshared = pshared;
252 return 0;
257 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
259 *pref = attr->lockkind;
260 return 0;
265 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
267 if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
268 && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
269 && pref != PTHREAD_RWLOCK_DEFAULT_NP)
270 return EINVAL;
272 attr->lockkind = pref;
274 return 0;