xref: /netbsd-src/lib/libpthread/pthread_lock.c (revision 568eb77ef2a2b1fbe2457d69c7aa6e1dd3d8de8b)
1*568eb77eSriastradh /*	$NetBSD: pthread_lock.c,v 1.36 2022/04/10 10:38:33 riastradh Exp $	*/
2c62a74e6Sthorpej 
3c62a74e6Sthorpej /*-
4da3cc0ffSad  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5c62a74e6Sthorpej  * All rights reserved.
6c62a74e6Sthorpej  *
7c62a74e6Sthorpej  * This code is derived from software contributed to The NetBSD Foundation
8da3cc0ffSad  * by Nathan J. Williams and Andrew Doran.
9c62a74e6Sthorpej  *
10c62a74e6Sthorpej  * Redistribution and use in source and binary forms, with or without
11c62a74e6Sthorpej  * modification, are permitted provided that the following conditions
12c62a74e6Sthorpej  * are met:
13c62a74e6Sthorpej  * 1. Redistributions of source code must retain the above copyright
14c62a74e6Sthorpej  *    notice, this list of conditions and the following disclaimer.
15c62a74e6Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
16c62a74e6Sthorpej  *    notice, this list of conditions and the following disclaimer in the
17c62a74e6Sthorpej  *    documentation and/or other materials provided with the distribution.
18c62a74e6Sthorpej  *
19c62a74e6Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c62a74e6Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c62a74e6Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c62a74e6Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c62a74e6Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c62a74e6Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c62a74e6Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c62a74e6Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c62a74e6Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c62a74e6Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c62a74e6Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
30c62a74e6Sthorpej  */
31c62a74e6Sthorpej 
32d9adedd7Sad /*
33d9adedd7Sad  * libpthread internal spinlock routines.
34d9adedd7Sad  */
35d9adedd7Sad 
36f043c0fbSlukem #include <sys/cdefs.h>
37*568eb77eSriastradh __RCSID("$NetBSD: pthread_lock.c,v 1.36 2022/04/10 10:38:33 riastradh Exp $");
387adb4107Sriastradh 
397adb4107Sriastradh /* Need to use libc-private names for atomic operations. */
407adb4107Sriastradh #include "../../common/lib/libc/atomic/atomic_op_namespace.h"
41f043c0fbSlukem 
4299f69b6cShe #include <sys/types.h>
43c62a74e6Sthorpej #include <sys/ras.h>
44c62a74e6Sthorpej 
45b43749fdSad #include <machine/lock.h>
46b43749fdSad 
47c62a74e6Sthorpej #include <errno.h>
48c62a74e6Sthorpej #include <unistd.h>
49da3cc0ffSad #include <stdio.h>
50b8833ff5Sad #include <stdlib.h>
51c62a74e6Sthorpej 
52c62a74e6Sthorpej #include "pthread.h"
53c62a74e6Sthorpej #include "pthread_int.h"
54c62a74e6Sthorpej 
55b8833ff5Sad /* How many times to try acquiring spin locks on MP systems. */
5650590878Sad #define	PTHREAD__NSPINS         64
57b8833ff5Sad 
581cda93b4Sthorpej RAS_DECL(pthread__lock);
59c62a74e6Sthorpej 
6015e9cec1Sad static void 	pthread__spinlock_slow(pthread_spin_t *);
619fdaf800Sskrll 
62d32ed989Sskrll #ifdef PTHREAD__ASM_RASOPS
63c62a74e6Sthorpej 
64d32ed989Sskrll void pthread__ras_simple_lock_init(__cpu_simple_lock_t *);
65d32ed989Sskrll int pthread__ras_simple_lock_try(__cpu_simple_lock_t *);
66d32ed989Sskrll void pthread__ras_simple_unlock(__cpu_simple_lock_t *);
67d32ed989Sskrll 
68d32ed989Sskrll #else
69d32ed989Sskrll 
70d32ed989Sskrll static void
pthread__ras_simple_lock_init(__cpu_simple_lock_t * alp)71d32ed989Sskrll pthread__ras_simple_lock_init(__cpu_simple_lock_t *alp)
72d32ed989Sskrll {
73a5070151Sad 
749fdaf800Sskrll 	__cpu_simple_lock_clear(alp);
75c62a74e6Sthorpej }
76c62a74e6Sthorpej 
77d32ed989Sskrll static int
pthread__ras_simple_lock_try(__cpu_simple_lock_t * alp)78d32ed989Sskrll pthread__ras_simple_lock_try(__cpu_simple_lock_t *alp)
79c62a74e6Sthorpej {
80f9577d3aSskrll 	int locked;
81c62a74e6Sthorpej 
821cda93b4Sthorpej 	RAS_START(pthread__lock);
83f9577d3aSskrll 	locked = __SIMPLELOCK_LOCKED_P(alp);
849fdaf800Sskrll 	__cpu_simple_lock_set(alp);
851cda93b4Sthorpej 	RAS_END(pthread__lock);
86c62a74e6Sthorpej 
87f9577d3aSskrll 	return !locked;
88c62a74e6Sthorpej }
89c62a74e6Sthorpej 
90d32ed989Sskrll static void
pthread__ras_simple_unlock(__cpu_simple_lock_t * alp)91d32ed989Sskrll pthread__ras_simple_unlock(__cpu_simple_lock_t *alp)
92c62a74e6Sthorpej {
93c62a74e6Sthorpej 
94d32ed989Sskrll 	__cpu_simple_lock_clear(alp);
95a5070151Sad }
969fdaf800Sskrll 
97d32ed989Sskrll #endif /* PTHREAD__ASM_RASOPS */
98d32ed989Sskrll 
99d32ed989Sskrll static const struct pthread_lock_ops pthread__lock_ops_ras = {
100d32ed989Sskrll 	pthread__ras_simple_lock_init,
101d32ed989Sskrll 	pthread__ras_simple_lock_try,
102d32ed989Sskrll 	pthread__ras_simple_unlock,
10315e9cec1Sad 	pthread__spinlock_slow,
104d32ed989Sskrll };
105d32ed989Sskrll 
106d32ed989Sskrll static void
pthread__atomic_simple_lock_init(__cpu_simple_lock_t * alp)107d32ed989Sskrll pthread__atomic_simple_lock_init(__cpu_simple_lock_t *alp)
108d32ed989Sskrll {
109d32ed989Sskrll 
110d32ed989Sskrll 	__cpu_simple_lock_init(alp);
111d9adedd7Sad }
112d9adedd7Sad 
113d32ed989Sskrll static int
pthread__atomic_simple_lock_try(__cpu_simple_lock_t * alp)114d32ed989Sskrll pthread__atomic_simple_lock_try(__cpu_simple_lock_t *alp)
115d32ed989Sskrll {
116d32ed989Sskrll 
117d32ed989Sskrll 	return (__cpu_simple_lock_try(alp));
118d32ed989Sskrll }
119d32ed989Sskrll 
120d32ed989Sskrll static void
pthread__atomic_simple_unlock(__cpu_simple_lock_t * alp)121d32ed989Sskrll pthread__atomic_simple_unlock(__cpu_simple_lock_t *alp)
122d32ed989Sskrll {
123d32ed989Sskrll 
124d32ed989Sskrll 	__cpu_simple_unlock(alp);
125d32ed989Sskrll }
126d32ed989Sskrll 
127d32ed989Sskrll static const struct pthread_lock_ops pthread__lock_ops_atomic = {
128d32ed989Sskrll 	pthread__atomic_simple_lock_init,
129d32ed989Sskrll 	pthread__atomic_simple_lock_try,
130d32ed989Sskrll 	pthread__atomic_simple_unlock,
13115e9cec1Sad 	pthread__spinlock_slow,
132d32ed989Sskrll };
133d32ed989Sskrll 
134d32ed989Sskrll /*
135d32ed989Sskrll  * We default to pointing to the RAS primitives; we might need to use
136d32ed989Sskrll  * locks early, but before main() starts.  This is safe, since no other
137d32ed989Sskrll  * threads will be active for the process, so atomicity will not be
138d32ed989Sskrll  * required.
139d32ed989Sskrll  */
140d32ed989Sskrll const struct pthread_lock_ops *pthread__lock_ops = &pthread__lock_ops_ras;
141d32ed989Sskrll 
142d9adedd7Sad /*
143d9adedd7Sad  * Prevent this routine from being inlined.  The common case is no
144d9adedd7Sad  * contention and it's better to not burden the instruction decoder.
145d9adedd7Sad  */
146d9adedd7Sad static void
pthread__spinlock_slow(pthread_spin_t * lock)14715e9cec1Sad pthread__spinlock_slow(pthread_spin_t *lock)
148d9adedd7Sad {
14915e9cec1Sad 	pthread_t self;
150d9adedd7Sad 	int count;
151d9adedd7Sad 
15215e9cec1Sad 	self = pthread__self();
15315e9cec1Sad 
154d9adedd7Sad 	do {
155d9adedd7Sad 		count = pthread__nspins;
15615e9cec1Sad 		while (__SIMPLELOCK_LOCKED_P(lock) && --count > 0)
157d9adedd7Sad 			pthread__smt_pause();
158d9adedd7Sad 		if (count > 0) {
15915e9cec1Sad 			if ((*self->pt_lockops.plo_try)(lock))
160d9adedd7Sad 				break;
161d9adedd7Sad 			continue;
162d9adedd7Sad 		}
1638ccc6e06Sad 		sched_yield();
164d9adedd7Sad 	} while (/*CONSTCOND*/ 1);
165d9adedd7Sad }
166d9adedd7Sad 
167c62a74e6Sthorpej /*
168c62a74e6Sthorpej  * Initialize the locking primitives.  On uniprocessors, we always
169c62a74e6Sthorpej  * use Restartable Atomic Sequences if they are available.  Otherwise,
170c62a74e6Sthorpej  * we fall back onto machine-dependent atomic lock primitives.
171c62a74e6Sthorpej  */
172c62a74e6Sthorpej void
pthread__lockprim_init(void)173b8833ff5Sad pthread__lockprim_init(void)
174c62a74e6Sthorpej {
175b8833ff5Sad 	char *p;
176c62a74e6Sthorpej 
17715e9cec1Sad 	if ((p = pthread__getenv("PTHREAD_NSPINS")) != NULL)
178b8833ff5Sad 		pthread__nspins = atoi(p);
179b8833ff5Sad 	else if (pthread__concurrency != 1)
180b8833ff5Sad 		pthread__nspins = PTHREAD__NSPINS;
181b8833ff5Sad 	else
182b8833ff5Sad 		pthread__nspins = 1;
183b8833ff5Sad 
184b8833ff5Sad 	if (pthread__concurrency != 1) {
185d32ed989Sskrll 		pthread__lock_ops = &pthread__lock_ops_atomic;
186c62a74e6Sthorpej 		return;
187c62a74e6Sthorpej 	}
188c62a74e6Sthorpej 
189a5070151Sad 	if (rasctl(RAS_ADDR(pthread__lock), RAS_SIZE(pthread__lock),
190a5070151Sad 	    RAS_INSTALL) != 0) {
191d32ed989Sskrll 		pthread__lock_ops = &pthread__lock_ops_atomic;
192d9adedd7Sad 		return;
193d9adedd7Sad 	}
194c62a74e6Sthorpej }
195c62a74e6Sthorpej 
196c62a74e6Sthorpej void
pthread_lockinit(pthread_spin_t * lock)197c62a74e6Sthorpej pthread_lockinit(pthread_spin_t *lock)
198c62a74e6Sthorpej {
199c62a74e6Sthorpej 
200c62a74e6Sthorpej 	pthread__simple_lock_init(lock);
201c62a74e6Sthorpej }
202