2 * Copyright 1999, 2000 John D. Polstra.
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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * $FreeBSD: src/libexec/rtld-elf/i386/lockdflt.c,v 1.5.2.4 2002/07/11 23:52:32 jdp Exp $
26 * $DragonFly: src/libexec/rtld-elf/i386/lockdflt.c,v 1.3 2007/11/09 19:38:50 hasso Exp $
30 * Thread locking implementation for the dynamic linker.
32 * We use the "simple, non-scalable reader-preference lock" from:
34 * J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
35 * Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
36 * Principles and Practice of Parallel Programming, April 1991.
38 * In this algorithm the lock is a single word. Its low-order bit is
39 * set when a writer holds the lock. The remaining high-order bits
40 * contain a count of readers desiring the lock. The algorithm requires
41 * atomic "compare_and_store" and "add" operations.
53 #define CACHE_LINE_SIZE 32
55 #define WAFLAG 0x1 /* A writer holds the lock */
56 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
58 typedef struct Struct_Lock
{
63 static sigset_t fullsigmask
, oldsigmask
;
66 cmpxchgl(int old
, int new, volatile int *m
)
70 __asm
__volatile ("lock; cmpxchgl %2, %0"
71 : "+m"(*m
), "=a"(result
)
79 lock_create(void *context
)
87 * Arrange for the lock to occupy its own cache line. First, we
88 * optimistically allocate just a cache line, hoping that malloc
89 * will give us a well-aligned block of memory. If that doesn't
90 * work, we allocate a larger block and take a well-aligned cache
93 base
= xmalloc(CACHE_LINE_SIZE
);
95 if ((uintptr_t)p
% CACHE_LINE_SIZE
!= 0) {
97 base
= xmalloc(2 * CACHE_LINE_SIZE
);
99 if ((r
= (uintptr_t)p
% CACHE_LINE_SIZE
) != 0)
100 p
+= CACHE_LINE_SIZE
- r
;
109 lock_destroy(void *lock
)
111 Lock
*l
= (Lock
*)lock
;
117 * Reader/writer locks.
120 rlock_acquire(void *lock
)
122 Lock
*l
= (Lock
*)lock
;
124 atomic_add_int(&l
->lock
, RC_INCR
);
125 while (l
->lock
& WAFLAG
)
130 wlock_acquire(void *lock
)
132 Lock
*l
= (Lock
*)lock
;
133 sigset_t tmp_oldsigmask
;
136 sigprocmask(SIG_BLOCK
, &fullsigmask
, &tmp_oldsigmask
);
137 if (cmpxchgl(0, WAFLAG
, &l
->lock
) == 0)
139 sigprocmask(SIG_SETMASK
, &tmp_oldsigmask
, NULL
);
141 oldsigmask
= tmp_oldsigmask
;
145 rlock_release(void *lock
)
147 Lock
*l
= (Lock
*)lock
;
149 atomic_add_int(&l
->lock
, -RC_INCR
);
153 wlock_release(void *lock
)
155 Lock
*l
= (Lock
*)lock
;
157 atomic_add_int(&l
->lock
, -WAFLAG
);
158 sigprocmask(SIG_SETMASK
, &oldsigmask
, NULL
);
162 lockdflt_init(LockInfo
*li
)
165 li
->context_destroy
= NULL
;
166 li
->lock_create
= lock_create
;
167 li
->lock_destroy
= lock_destroy
;
168 li
->rlock_acquire
= rlock_acquire
;
169 li
->wlock_acquire
= wlock_acquire
;
170 li
->rlock_release
= rlock_release
;
171 li
->wlock_release
= wlock_release
;
173 * Construct a mask to block all signals except traps which might
174 * conceivably be generated within the dynamic linker itself.
176 sigfillset(&fullsigmask
);
177 sigdelset(&fullsigmask
, SIGILL
);
178 sigdelset(&fullsigmask
, SIGTRAP
);
179 sigdelset(&fullsigmask
, SIGABRT
);
180 sigdelset(&fullsigmask
, SIGEMT
);
181 sigdelset(&fullsigmask
, SIGFPE
);
182 sigdelset(&fullsigmask
, SIGBUS
);
183 sigdelset(&fullsigmask
, SIGSEGV
);
184 sigdelset(&fullsigmask
, SIGSYS
);