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. */
24 #include "internals.h"
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
;
41 rwlock
->rw_kind
= PTHREAD_RWLOCK_DEFAULT_NP
;
42 rwlock
->rw_pshared
= PTHREAD_PROCESS_PRIVATE
;
46 rwlock
->rw_kind
= attr
->lockkind
;
47 rwlock
->rw_pshared
= attr
->pshared
;
55 pthread_rwlock_destroy (pthread_rwlock_t
*rwlock
)
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
)
73 pthread_rwlock_rdlock (pthread_rwlock_t
*rwlock
)
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. */
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 */
94 __pthread_unlock (&rwlock
->rw_lock
);
101 pthread_rwlock_tryrdlock (pthread_rwlock_t
*rwlock
)
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
;
113 __pthread_unlock (&rwlock
->rw_lock
);
120 pthread_rwlock_wrlock (pthread_rwlock_t
*rwlock
)
122 pthread_descr self
= thread_self ();
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
);
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
)
147 __pthread_lock (&rwlock
->rw_lock
);
148 if (rwlock
->rw_readers
== 0 && rwlock
->rw_writer
== NULL
)
150 rwlock
->rw_writer
= thread_self ();
153 __pthread_unlock (&rwlock
->rw_lock
);
160 pthread_rwlock_unlock (pthread_rwlock_t
*rwlock
)
162 pthread_descr torestart
;
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
);
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
)
188 /* Restart one waiting writer. */
189 __pthread_unlock (&rwlock
->rw_lock
);
195 /* Unlocking a read lock. */
196 if (rwlock
->rw_readers
== 0)
198 __pthread_unlock (&rwlock
->rw_lock
);
202 --rwlock
->rw_readers
;
203 if (rwlock
->rw_readers
== 0)
204 /* Restart one waiting writer, if any. */
205 th
= dequeue (&rwlock
->rw_write_waiting
);
209 __pthread_unlock (&rwlock
->rw_lock
);
220 pthread_rwlockattr_init (pthread_rwlockattr_t
*attr
)
230 pthread_rwlockattr_destroy (pthread_rwlockattr_t
*attr
)
237 pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
*attr
, int *pshared
)
239 *pshared
= attr
->pshared
;
245 pthread_rwlockattr_setpshared (pthread_rwlockattr_t
*attr
, int pshared
)
247 if (pshared
!= PTHREAD_PROCESS_PRIVATE
&& pshared
!= PTHREAD_PROCESS_SHARED
)
250 attr
->pshared
= pshared
;
257 pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t
*attr
, int *pref
)
259 *pref
= attr
->lockkind
;
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
)
272 attr
->lockkind
= pref
;