2 * Copyright (c) 2001 Alexander Kabaev
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 #include "rtld_lock.h"
34 #include "thr_private.h"
36 static int _thr_rtld_clr_flag(int);
37 static void *_thr_rtld_lock_create(void);
38 static void _thr_rtld_lock_destroy(void *);
39 static void _thr_rtld_lock_release(void *);
40 static void _thr_rtld_rlock_acquire(void *);
41 static int _thr_rtld_set_flag(int);
42 static void _thr_rtld_wlock_acquire(void *);
46 _thr_rtld_lock_create(void)
48 pthread_rwlock_t prwlock;
49 if (_pthread_rwlock_init(&prwlock, NULL))
55 _thr_rtld_lock_destroy(void *lock)
57 pthread_rwlock_t prwlock;
59 prwlock = (pthread_rwlock_t)lock;
61 _pthread_rwlock_destroy(&prwlock);
65 _thr_rtld_rlock_acquire(void *lock)
67 pthread_rwlock_t prwlock;
69 prwlock = (pthread_rwlock_t)lock;
70 _thr_rwlock_rdlock(&prwlock);
74 _thr_rtld_wlock_acquire(void *lock)
76 pthread_rwlock_t prwlock;
78 prwlock = (pthread_rwlock_t)lock;
79 _thr_rwlock_wrlock(&prwlock);
83 _thr_rtld_lock_release(void *lock)
85 pthread_rwlock_t prwlock;
87 prwlock = (pthread_rwlock_t)lock;
88 _thr_rwlock_unlock(&prwlock);
93 _thr_rtld_set_flag(int mask)
95 struct pthread *curthread;
98 curthread = _get_curthread();
99 if (curthread != NULL) {
100 bits = curthread->rtld_bits;
101 curthread->rtld_bits |= mask;
104 PANIC("No current thread in rtld call");
111 _thr_rtld_clr_flag(int mask)
113 struct pthread *curthread;
116 curthread = _get_curthread();
117 if (curthread != NULL) {
118 bits = curthread->rtld_bits;
119 curthread->rtld_bits &= ~mask;
122 PANIC("No current thread in rtld call");
130 struct RtldLockInfo li;
132 li.lock_create = _thr_rtld_lock_create;
133 li.lock_destroy = _thr_rtld_lock_destroy;
134 li.rlock_acquire = _thr_rtld_rlock_acquire;
135 li.wlock_acquire = _thr_rtld_wlock_acquire;
136 li.lock_release = _thr_rtld_lock_release;
137 li.thread_set_flag = _thr_rtld_set_flag;
138 li.thread_clr_flag = _thr_rtld_clr_flag;
140 _rtld_thread_init(&li);
146 _rtld_thread_init(NULL);
150 struct rtld_kse_lock {
159 _thr_rtld_lock_create(void)
161 struct rtld_kse_lock *l;
163 if ((l = malloc(sizeof(struct rtld_kse_lock))) != NULL) {
164 _lock_init(&l->lck, LCK_ADAPTIVE, _kse_lock_wait,
165 _kse_lock_wakeup, calloc);
174 _thr_rtld_lock_destroy(void *lock __unused)
176 /* XXX We really can not free memory after a fork() */
178 struct rtld_kse_lock *l;
180 l = (struct rtld_kse_lock *)lock;
181 _lock_destroy(&l->lck);
188 _thr_rtld_rlock_acquire(void *lock)
190 struct rtld_kse_lock *l;
194 l = (struct rtld_kse_lock *)lock;
195 crit = _kse_critical_enter();
196 curkse = _get_curkse();
197 if (l->owner == curkse) {
199 _kse_critical_leave(crit); /* probably not necessary */
201 KSE_LOCK_ACQUIRE(curkse, &l->lck);
210 _thr_rtld_wlock_acquire(void *lock)
212 struct rtld_kse_lock *l;
216 l = (struct rtld_kse_lock *)lock;
217 crit = _kse_critical_enter();
218 curkse = _get_curkse();
219 if (l->owner == curkse) {
220 _kse_critical_leave(crit);
221 PANIC("Recursive write lock attempt on rtld lock");
223 KSE_LOCK_ACQUIRE(curkse, &l->lck);
232 _thr_rtld_lock_release(void *lock)
234 struct rtld_kse_lock *l;
238 l = (struct rtld_kse_lock *)lock;
239 crit = _kse_critical_enter();
240 curkse = _get_curkse();
241 if (l->owner != curkse) {
243 * We might want to forcibly unlock the rtld lock
244 * and/or disable threaded mode so there is better
245 * chance that the panic will work. Otherwise,
246 * we could end up trying to take the rtld lock
249 _kse_critical_leave(crit);
250 PANIC("Attempt to unlock rtld lock when not owner.");
255 * If there ever is a count associated with
256 * _kse_critical_leave(), we'll need to add
257 * another call to it here with the crit
263 KSE_LOCK_RELEASE(curkse, &l->lck);
265 _kse_critical_leave(crit);
271 _thr_rtld_set_flag(int mask __unused)
277 _thr_rtld_clr_flag(int mask __unused)
285 struct RtldLockInfo li;
287 li.lock_create = _thr_rtld_lock_create;
288 li.lock_destroy = _thr_rtld_lock_destroy;
289 li.rlock_acquire = _thr_rtld_rlock_acquire;
290 li.wlock_acquire = _thr_rtld_wlock_acquire;
291 li.lock_release = _thr_rtld_lock_release;
292 li.thread_set_flag = _thr_rtld_set_flag;
293 li.thread_clr_flag = _thr_rtld_clr_flag;
295 _rtld_thread_init(&li);
301 _rtld_thread_init(NULL);