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 l = malloc(sizeof(struct rtld_kse_lock));
164 _lock_init(&l->lck, LCK_ADAPTIVE, _kse_lock_wait, _kse_lock_wakeup);
172 _thr_rtld_lock_destroy(void *lock)
174 /* XXX We really can not free memory after a fork() */
176 struct rtld_kse_lock *l;
178 l = (struct rtld_kse_lock *)lock;
179 _lock_destroy(&l->lck);
186 _thr_rtld_rlock_acquire(void *lock)
188 struct rtld_kse_lock *l;
192 l = (struct rtld_kse_lock *)lock;
193 crit = _kse_critical_enter();
194 curkse = _get_curkse();
195 if (l->owner == curkse) {
197 _kse_critical_leave(crit); /* probably not necessary */
199 KSE_LOCK_ACQUIRE(curkse, &l->lck);
208 _thr_rtld_wlock_acquire(void *lock)
210 struct rtld_kse_lock *l;
214 l = (struct rtld_kse_lock *)lock;
215 crit = _kse_critical_enter();
216 curkse = _get_curkse();
217 if (l->owner == curkse) {
218 _kse_critical_leave(crit);
219 PANIC("Recursive write lock attempt on rtld lock");
221 KSE_LOCK_ACQUIRE(curkse, &l->lck);
230 _thr_rtld_lock_release(void *lock)
232 struct rtld_kse_lock *l;
236 l = (struct rtld_kse_lock *)lock;
237 crit = _kse_critical_enter();
238 curkse = _get_curkse();
239 if (l->owner != curkse) {
241 * We might want to forcibly unlock the rtld lock
242 * and/or disable threaded mode so there is better
243 * chance that the panic will work. Otherwise,
244 * we could end up trying to take the rtld lock
247 _kse_critical_leave(crit);
248 PANIC("Attempt to unlock rtld lock when not owner.");
253 * If there ever is a count associated with
254 * _kse_critical_leave(), we'll need to add
255 * another call to it here with the crit
261 KSE_LOCK_RELEASE(curkse, &l->lck);
263 _kse_critical_leave(crit);
269 _thr_rtld_set_flag(int mask)
275 _thr_rtld_clr_flag(int mask)
283 struct RtldLockInfo li;
285 li.lock_create = _thr_rtld_lock_create;
286 li.lock_destroy = _thr_rtld_lock_destroy;
287 li.rlock_acquire = _thr_rtld_rlock_acquire;
288 li.wlock_acquire = _thr_rtld_wlock_acquire;
289 li.lock_release = _thr_rtld_lock_release;
290 li.thread_set_flag = _thr_rtld_set_flag;
291 li.thread_clr_flag = _thr_rtld_clr_flag;
293 _rtld_thread_init(&li);
299 _rtld_thread_init(NULL);