* sysdeps/m68k/bits/setjmp.h (_JMPBUF_UNWINDS): Added.
[glibc.git] / linuxthreads / rwlock.c
blobc6b281551a9e5202cd61aa76adc50fb2e8345555
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 rwlock->rw_spinlock = 0;
34 rwlock->rw_readers = 0;
35 rwlock->rw_writer = NULL;
37 queue_init(&rwlock->rw_read_waiting);
38 queue_init(&rwlock->rw_write_waiting);
40 if (attr == NULL)
42 rwlock->rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
43 rwlock->rw_pshared = PTHREAD_PROCESS_PRIVATE;
45 else
47 rwlock->rw_kind = attr->lockkind;
48 rwlock->rw_pshared = attr->pshared;
51 return 0;
55 int
56 pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
58 int readers;
59 _pthread_descr writer;
61 acquire (&rwlock->rw_spinlock);
62 readers = rwlock->rw_readers;
63 writer = rwlock->rw_writer;
64 release (&rwlock->rw_spinlock);
66 if (readers > 0 || writer != NULL)
67 return EBUSY;
69 return 0;
73 int
74 pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
76 pthread_descr self;
78 while (1)
80 acquire (&rwlock->rw_spinlock);
81 if (rwlock->rw_writer == NULL
82 || (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
83 && rwlock->rw_readers != 0))
84 /* We can add a reader lock. */
85 break;
87 /* Suspend ourselves, then try again */
88 self = thread_self ();
89 enqueue (&rwlock->rw_read_waiting, self);
90 release (&rwlock->rw_spinlock);
91 suspend (self); /* This is not a cancellation point */
94 ++rwlock->rw_readers;
95 release (&rwlock->rw_spinlock);
97 return 0;
102 pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
104 int result = EBUSY;
106 acquire (&rwlock->rw_spinlock);
107 if (rwlock->rw_writer == NULL
108 || (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
109 && rwlock->rw_readers != 0))
111 ++rwlock->rw_readers;
112 result = 0;
114 release (&rwlock->rw_spinlock);
116 return result;
121 pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
123 pthread_descr self = thread_self ();
125 while(1)
127 acquire (&rwlock->rw_spinlock);
128 if (rwlock->rw_readers == 0 && rwlock->rw_writer == NULL)
130 rwlock->rw_writer = self;
131 release (&rwlock->rw_spinlock);
132 return 0;
135 /* Suspend ourselves, then try again */
136 enqueue (&rwlock->rw_write_waiting, self);
137 release (&rwlock->rw_spinlock);
138 suspend (self); /* This is not a cancellation point */
144 pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
146 int result = EBUSY;
148 acquire (&rwlock->rw_spinlock);
149 if (rwlock->rw_readers == 0 && rwlock->rw_writer == NULL)
151 rwlock->rw_writer = thread_self ();
152 result = 0;
154 release (&rwlock->rw_spinlock);
156 return result;
161 pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
163 struct _pthread_queue torestart;
164 pthread_descr th;
166 acquire (&rwlock->rw_spinlock);
167 if (rwlock->rw_writer != NULL)
169 /* Unlocking a write lock. */
170 if (rwlock->rw_writer != thread_self ())
172 release (&rwlock->rw_spinlock);
173 return EPERM;
175 rwlock->rw_writer = NULL;
177 if (rwlock->rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
178 || (th = dequeue (&rwlock->rw_write_waiting)) == NULL)
180 /* Restart all waiting readers. */
181 torestart = rwlock->rw_read_waiting;
182 queue_init (&rwlock->rw_read_waiting);
183 release (&rwlock->rw_spinlock);
184 while ((th = dequeue (&torestart)) != NULL)
185 restart (th);
187 else
189 /* Restart one waiting writer. */
190 release (&rwlock->rw_spinlock);
191 restart (th);
194 else
196 /* Unlocking a read lock. */
197 if (rwlock->rw_readers == 0)
199 release (&rwlock->rw_spinlock);
200 return EPERM;
203 --rwlock->rw_readers;
204 if (rwlock->rw_readers == 0)
205 /* Restart one waiting writer, if any. */
206 th = dequeue (&rwlock->rw_write_waiting);
207 else
208 th = NULL;
210 release (&rwlock->rw_spinlock);
211 if (th != NULL)
212 restart (th);
215 return 0;
221 pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
223 attr->lockkind = 0;
224 attr->pshared = 0;
226 return 0;
231 pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
233 return 0;
238 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
240 *pshared = attr->pshared;
241 return 0;
246 pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
248 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
249 return EINVAL;
251 attr->pshared = pshared;
253 return 0;
258 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
260 *pref = attr->lockkind;
261 return 0;
266 pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
268 if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
269 && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
270 && pref != PTHREAD_RWLOCK_DEFAULT_NP)
271 return EINVAL;
273 attr->lockkind = pref;
275 return 0;