1 /* $NetBSD: pthread_lock.c,v 1.33 2008/01/05 01:37:35 ad Exp $ */
4 * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * libpthread internal spinlock routines.
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: pthread_lock.c,v 1.33 2008/01/05 01:37:35 ad Exp $");
39 #include <sys/types.h>
42 #include <machine/lock.h>
50 #include "pthread_int.h"
52 /* How many times to try acquiring spin locks on MP systems. */
53 #define PTHREAD__NSPINS 64
55 RAS_DECL(pthread__lock
);
57 static void pthread__spinlock_slow(pthread_spin_t
*);
59 #ifdef PTHREAD__ASM_RASOPS
61 void pthread__ras_simple_lock_init(__cpu_simple_lock_t
*);
62 int pthread__ras_simple_lock_try(__cpu_simple_lock_t
*);
63 void pthread__ras_simple_unlock(__cpu_simple_lock_t
*);
68 pthread__ras_simple_lock_init(__cpu_simple_lock_t
*alp
)
71 __cpu_simple_lock_clear(alp
);
75 pthread__ras_simple_lock_try(__cpu_simple_lock_t
*alp
)
79 RAS_START(pthread__lock
);
80 locked
= __SIMPLELOCK_LOCKED_P(alp
);
81 __cpu_simple_lock_set(alp
);
82 RAS_END(pthread__lock
);
88 pthread__ras_simple_unlock(__cpu_simple_lock_t
*alp
)
91 __cpu_simple_lock_clear(alp
);
94 #endif /* PTHREAD__ASM_RASOPS */
96 static const struct pthread_lock_ops pthread__lock_ops_ras
= {
97 pthread__ras_simple_lock_init
,
98 pthread__ras_simple_lock_try
,
99 pthread__ras_simple_unlock
,
100 pthread__spinlock_slow
,
104 pthread__atomic_simple_lock_init(__cpu_simple_lock_t
*alp
)
107 __cpu_simple_lock_init(alp
);
111 pthread__atomic_simple_lock_try(__cpu_simple_lock_t
*alp
)
114 return (__cpu_simple_lock_try(alp
));
118 pthread__atomic_simple_unlock(__cpu_simple_lock_t
*alp
)
121 __cpu_simple_unlock(alp
);
124 static const struct pthread_lock_ops pthread__lock_ops_atomic
= {
125 pthread__atomic_simple_lock_init
,
126 pthread__atomic_simple_lock_try
,
127 pthread__atomic_simple_unlock
,
128 pthread__spinlock_slow
,
132 * We default to pointing to the RAS primitives; we might need to use
133 * locks early, but before main() starts. This is safe, since no other
134 * threads will be active for the process, so atomicity will not be
137 const struct pthread_lock_ops
*pthread__lock_ops
= &pthread__lock_ops_ras
;
140 * Prevent this routine from being inlined. The common case is no
141 * contention and it's better to not burden the instruction decoder.
144 pthread__spinlock_slow(pthread_spin_t
*lock
)
149 self
= pthread__self();
152 count
= pthread__nspins
;
153 while (__SIMPLELOCK_LOCKED_P(lock
) && --count
> 0)
154 pthread__smt_pause();
156 if ((*self
->pt_lockops
.plo_try
)(lock
))
161 } while (/*CONSTCOND*/ 1);
165 * Initialize the locking primitives. On uniprocessors, we always
166 * use Restartable Atomic Sequences if they are available. Otherwise,
167 * we fall back onto machine-dependent atomic lock primitives.
170 pthread__lockprim_init(void)
174 if ((p
= pthread__getenv("PTHREAD_NSPINS")) != NULL
)
175 pthread__nspins
= atoi(p
);
176 else if (pthread__concurrency
!= 1)
177 pthread__nspins
= PTHREAD__NSPINS
;
181 if (pthread__concurrency
!= 1) {
182 pthread__lock_ops
= &pthread__lock_ops_atomic
;
186 if (rasctl(RAS_ADDR(pthread__lock
), RAS_SIZE(pthread__lock
),
188 pthread__lock_ops
= &pthread__lock_ops_atomic
;
194 pthread_lockinit(pthread_spin_t
*lock
)
197 pthread__simple_lock_init(lock
);