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/amd64/lockdflt.c,v 1.1 2006/07/27 00:40:35 corecode Exp $
30 * Thread locking implementation for the dynamic linker.
32 * J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
33 * Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
34 * Principles and Practice of Parallel Programming, April 1991.
36 * In this algorithm the lock is a single word. Its low-order bit is
37 * set when a writer holds the lock. The remaining high-order bits
38 * contain a count of readers desiring the lock. The algorithm requires
39 * atomic "compare_and_store" and "add" operations.
50 #define WAFLAG 0x1 /* A writer holds the lock */
51 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
53 typedef struct Struct_Lock
{
58 static sigset_t fullsigmask
, oldsigmask
;
61 cmpxchgl(int old
, int new, volatile int *m
)
65 __asm
__volatile ("lock; cmpxchgl %2, %0"
66 : "+m"(*m
), "=a"(result
)
74 lock_create(void *context
)
82 * Arrange for the lock to occupy its own cache line. First, we
83 * optimistically allocate just a cache line, hoping that malloc
84 * will give us a well-aligned block of memory. If that doesn't
85 * work, we allocate a larger block and take a well-aligned cache
88 base
= xmalloc(CACHE_LINE_SIZE
);
90 if ((uintptr_t)p
% CACHE_LINE_SIZE
!= 0) {
92 base
= xmalloc(2 * CACHE_LINE_SIZE
);
94 if ((r
= (uintptr_t)p
% CACHE_LINE_SIZE
) != 0)
95 p
+= CACHE_LINE_SIZE
- r
;
104 lock_destroy(void *lock
)
106 Lock
*l
= (Lock
*)lock
;
112 * Better reader/writer locks for the 80486 and later CPUs.
115 rlock_acquire(void *lock
)
117 Lock
*l
= (Lock
*)lock
;
119 atomic_add_int(&l
->lock
, RC_INCR
);
120 while (l
->lock
& WAFLAG
)
125 wlock_acquire(void *lock
)
127 Lock
*l
= (Lock
*)lock
;
128 sigset_t tmp_oldsigmask
;
131 sigprocmask(SIG_BLOCK
, &fullsigmask
, &tmp_oldsigmask
);
132 if (cmpxchgl(0, WAFLAG
, &l
->lock
) == 0)
134 sigprocmask(SIG_SETMASK
, &tmp_oldsigmask
, NULL
);
136 oldsigmask
= tmp_oldsigmask
;
140 rlock_release(void *lock
)
142 Lock
*l
= (Lock
*)lock
;
144 atomic_add_int(&l
->lock
, -RC_INCR
);
148 wlock_release(void *lock
)
150 Lock
*l
= (Lock
*)lock
;
152 atomic_add_int(&l
->lock
, -WAFLAG
);
153 sigprocmask(SIG_SETMASK
, &oldsigmask
, NULL
);
157 lockdflt_init(LockInfo
*li
)
160 li
->context_destroy
= NULL
;
161 li
->lock_create
= lock_create
;
162 li
->lock_destroy
= lock_destroy
;
163 li
->rlock_acquire
= rlock_acquire
;
164 li
->wlock_acquire
= wlock_acquire
;
165 li
->rlock_release
= rlock_release
;
166 li
->wlock_release
= wlock_release
;
168 * Construct a mask to block all signals except traps which might
169 * conceivably be generated within the dynamic linker itself.
171 sigfillset(&fullsigmask
);
172 sigdelset(&fullsigmask
, SIGILL
);
173 sigdelset(&fullsigmask
, SIGTRAP
);
174 sigdelset(&fullsigmask
, SIGABRT
);
175 sigdelset(&fullsigmask
, SIGEMT
);
176 sigdelset(&fullsigmask
, SIGFPE
);
177 sigdelset(&fullsigmask
, SIGBUS
);
178 sigdelset(&fullsigmask
, SIGSEGV
);
179 sigdelset(&fullsigmask
, SIGSYS
);