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 $
29 * Thread locking implementation for the dynamic linker.
31 * J. M. Mellor-Crummey and M. L. Scott. "Scalable Reader-Writer
32 * Synchronization for Shared-Memory Multiprocessors." 3rd ACM Symp. on
33 * Principles and Practice of Parallel Programming, April 1991.
35 * In this algorithm the lock is a single word. Its low-order bit is
36 * set when a writer holds the lock. The remaining high-order bits
37 * contain a count of readers desiring the lock. The algorithm requires
38 * atomic "compare_and_store" and "add" operations.
49 #define WAFLAG 0x1 /* A writer holds the lock */
50 #define RC_INCR 0x2 /* Adjusts count of readers desiring lock */
52 typedef struct Struct_Lock
{
57 static sigset_t fullsigmask
, oldsigmask
;
60 cmpxchgl(int old
, int new, volatile int *m
)
64 __asm
__volatile ("lock; cmpxchgl %2, %0"
65 : "+m"(*m
), "=a"(result
)
73 lock_create(void *context
)
81 * Arrange for the lock to occupy its own cache line. First, we
82 * optimistically allocate just a cache line, hoping that malloc
83 * will give us a well-aligned block of memory. If that doesn't
84 * work, we allocate a larger block and take a well-aligned cache
87 base
= xmalloc(CACHE_LINE_SIZE
);
89 if ((uintptr_t)p
% CACHE_LINE_SIZE
!= 0) {
91 base
= xmalloc(2 * CACHE_LINE_SIZE
);
93 if ((r
= (uintptr_t)p
% CACHE_LINE_SIZE
) != 0)
94 p
+= CACHE_LINE_SIZE
- r
;
103 lock_destroy(void *lock
)
105 Lock
*l
= (Lock
*)lock
;
111 * Better reader/writer locks for the 80486 and later CPUs.
114 rlock_acquire(void *lock
)
116 Lock
*l
= (Lock
*)lock
;
118 atomic_add_int(&l
->lock
, RC_INCR
);
119 while (l
->lock
& WAFLAG
)
124 wlock_acquire(void *lock
)
126 Lock
*l
= (Lock
*)lock
;
127 sigset_t tmp_oldsigmask
;
130 sigprocmask(SIG_BLOCK
, &fullsigmask
, &tmp_oldsigmask
);
131 if (cmpxchgl(0, WAFLAG
, &l
->lock
) == 0)
133 sigprocmask(SIG_SETMASK
, &tmp_oldsigmask
, NULL
);
135 oldsigmask
= tmp_oldsigmask
;
139 rlock_release(void *lock
)
141 Lock
*l
= (Lock
*)lock
;
143 atomic_add_int(&l
->lock
, -RC_INCR
);
147 wlock_release(void *lock
)
149 Lock
*l
= (Lock
*)lock
;
151 atomic_add_int(&l
->lock
, -WAFLAG
);
152 sigprocmask(SIG_SETMASK
, &oldsigmask
, NULL
);
156 lockdflt_init(LockInfo
*li
)
159 li
->context_destroy
= NULL
;
160 li
->lock_create
= lock_create
;
161 li
->lock_destroy
= lock_destroy
;
162 li
->rlock_acquire
= rlock_acquire
;
163 li
->wlock_acquire
= wlock_acquire
;
164 li
->rlock_release
= rlock_release
;
165 li
->wlock_release
= wlock_release
;
167 * Construct a mask to block all signals except traps which might
168 * conceivably be generated within the dynamic linker itself.
170 sigfillset(&fullsigmask
);
171 sigdelset(&fullsigmask
, SIGILL
);
172 sigdelset(&fullsigmask
, SIGTRAP
);
173 sigdelset(&fullsigmask
, SIGABRT
);
174 sigdelset(&fullsigmask
, SIGEMT
);
175 sigdelset(&fullsigmask
, SIGFPE
);
176 sigdelset(&fullsigmask
, SIGBUS
);
177 sigdelset(&fullsigmask
, SIGSEGV
);
178 sigdelset(&fullsigmask
, SIGSYS
);