2 * Copyright (c) 2006, David Xu <davidxu@freebsd.org>
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 unmodified, this list of conditions, and the following
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * A lockless rwlock for rtld.
33 #include <sys/cdefs.h>
36 #include "rtld_lock.h"
37 #include "thr_private.h"
42 static int _thr_rtld_clr_flag(int);
43 static void *_thr_rtld_lock_create(void);
44 static void _thr_rtld_lock_destroy(void *);
45 static void _thr_rtld_lock_release(void *);
46 static void _thr_rtld_rlock_acquire(void *);
47 static int _thr_rtld_set_flag(int);
48 static void _thr_rtld_wlock_acquire(void *);
52 char _pad[CACHE_LINE_SIZE - sizeof(struct urwlock)];
55 static struct rtld_lock lock_place[MAX_RTLD_LOCKS] __aligned(CACHE_LINE_SIZE);
56 static int busy_places;
59 _thr_rtld_lock_create(void)
63 static const char fail[] = "_thr_rtld_lock_create failed\n";
65 for (locki = 0; locki < MAX_RTLD_LOCKS; locki++) {
66 if ((busy_places & (1 << locki)) == 0)
69 if (locki == MAX_RTLD_LOCKS) {
70 write(2, fail, sizeof(fail) - 1);
73 busy_places |= (1 << locki);
75 l = &lock_place[locki];
76 l->lock.rw_flags = URWLOCK_PREFER_READER;
81 _thr_rtld_lock_destroy(void *lock)
85 locki = (struct rtld_lock *)lock - &lock_place[0];
86 busy_places &= ~(1 << locki);
89 #define SAVE_ERRNO() { \
90 if (curthread != _thr_initial) \
91 errsave = curthread->error; \
96 #define RESTORE_ERRNO() { \
97 if (curthread != _thr_initial) \
98 curthread->error = errsave; \
104 _thr_rtld_rlock_acquire(void *lock)
106 struct pthread *curthread;
110 curthread = _get_curthread();
112 l = (struct rtld_lock *)lock;
114 THR_CRITICAL_ENTER(curthread);
115 while (_thr_rwlock_rdlock(&l->lock, 0, NULL) != 0)
117 curthread->rdlock_count++;
122 _thr_rtld_wlock_acquire(void *lock)
124 struct pthread *curthread;
128 curthread = _get_curthread();
130 l = (struct rtld_lock *)lock;
132 _thr_signal_block(curthread);
133 while (_thr_rwlock_wrlock(&l->lock, NULL) != 0)
139 _thr_rtld_lock_release(void *lock)
141 struct pthread *curthread;
146 curthread = _get_curthread();
148 l = (struct rtld_lock *)lock;
150 state = l->lock.rw_state;
151 if (_thr_rwlock_unlock(&l->lock) == 0) {
152 curthread->rdlock_count--;
153 if ((state & URWLOCK_WRITE_OWNER) == 0) {
154 THR_CRITICAL_LEAVE(curthread);
156 _thr_signal_unblock(curthread);
163 _thr_rtld_set_flag(int mask __unused)
166 * The caller's code in rtld-elf is broken, it is not signal safe,
167 * just return zero to fool it.
173 _thr_rtld_clr_flag(int mask __unused)
181 struct RtldLockInfo li;
182 struct pthread *curthread;
185 curthread = _get_curthread();
187 /* force to resolve _umtx_op PLT */
188 _umtx_op_err((struct umtx *)&dummy, UMTX_OP_WAKE, 1, 0, 0);
190 /* force to resolve errno() PLT */
193 li.lock_create = _thr_rtld_lock_create;
194 li.lock_destroy = _thr_rtld_lock_destroy;
195 li.rlock_acquire = _thr_rtld_rlock_acquire;
196 li.wlock_acquire = _thr_rtld_wlock_acquire;
197 li.lock_release = _thr_rtld_lock_release;
198 li.thread_set_flag = _thr_rtld_set_flag;
199 li.thread_clr_flag = _thr_rtld_clr_flag;
202 /* mask signals, also force to resolve __sys_sigprocmask PLT */
203 _thr_signal_block(curthread);
204 _rtld_thread_init(&li);
205 _thr_signal_unblock(curthread);
211 struct pthread *curthread;
213 curthread = _get_curthread();
214 _thr_signal_block(curthread);
215 _rtld_thread_init(NULL);
216 _thr_signal_unblock(curthread);