xref: /freebsd/lib/libthr/thread/thr_pspinlock.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
5  * Copyright (c) 2016 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Konstantin Belousov
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #include "namespace.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include "un-namespace.h"
39 
40 #include "thr_private.h"
41 
42 _Static_assert(sizeof(struct pthread_spinlock) <= THR_PAGE_SIZE_MIN,
43     "pthread_spinlock is too large for off-page");
44 
45 #define SPIN_COUNT 100000
46 
47 __weak_reference(_pthread_spin_init, pthread_spin_init);
48 __weak_reference(_pthread_spin_destroy, pthread_spin_destroy);
49 __weak_reference(_pthread_spin_trylock, pthread_spin_trylock);
50 __weak_reference(_pthread_spin_lock, pthread_spin_lock);
51 __weak_reference(_pthread_spin_unlock, pthread_spin_unlock);
52 
53 int
54 _pthread_spin_init(pthread_spinlock_t *lock, int pshared)
55 {
56 	struct pthread_spinlock	*lck;
57 
58 	if (lock == NULL)
59 		return (EINVAL);
60 	if (pshared == PTHREAD_PROCESS_PRIVATE) {
61 		lck = aligned_alloc(CACHE_LINE_SIZE,
62 		    roundup(sizeof(struct pthread_spinlock), CACHE_LINE_SIZE));
63 		if (lck == NULL)
64 			return (ENOMEM);
65 		*lock = lck;
66 	} else if (pshared == PTHREAD_PROCESS_SHARED) {
67 		lck = __thr_pshared_offpage(lock, 1);
68 		if (lck == NULL)
69 			return (EFAULT);
70 		*lock = THR_PSHARED_PTR;
71 	} else {
72 		return (EINVAL);
73 	}
74 	_thr_umutex_init(&lck->s_lock);
75 	return (0);
76 }
77 
78 int
79 _pthread_spin_destroy(pthread_spinlock_t *lock)
80 {
81 	void *l;
82 	int ret;
83 
84 	if (lock == NULL || *lock == NULL) {
85 		ret = EINVAL;
86 	} else if (*lock == THR_PSHARED_PTR) {
87 		l = __thr_pshared_offpage(lock, 0);
88 		if (l != NULL)
89 			__thr_pshared_destroy(l);
90 		ret = 0;
91 	} else {
92 		free(*lock);
93 		*lock = NULL;
94 		ret = 0;
95 	}
96 	return (ret);
97 }
98 
99 int
100 _pthread_spin_trylock(pthread_spinlock_t *lock)
101 {
102 	struct pthread_spinlock	*lck;
103 
104 	if (lock == NULL || *lock == NULL)
105 		return (EINVAL);
106 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
107 	if (lck == NULL)
108 		return (EINVAL);
109 	return (THR_UMUTEX_TRYLOCK(_get_curthread(), &lck->s_lock));
110 }
111 
112 int
113 _pthread_spin_lock(pthread_spinlock_t *lock)
114 {
115 	struct pthread *curthread;
116 	struct pthread_spinlock	*lck;
117 	int count;
118 
119 	if (lock == NULL)
120 		return (EINVAL);
121 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
122 	if (lck == NULL)
123 		return (EINVAL);
124 
125 	curthread = _get_curthread();
126 	count = SPIN_COUNT;
127 	while (THR_UMUTEX_TRYLOCK(curthread, &lck->s_lock) != 0) {
128 		while (lck->s_lock.m_owner) {
129 			if (!_thr_is_smp) {
130 				_pthread_yield();
131 			} else {
132 				CPU_SPINWAIT;
133 				if (--count <= 0) {
134 					count = SPIN_COUNT;
135 					_pthread_yield();
136 				}
137 			}
138 		}
139 	}
140 	return (0);
141 }
142 
143 int
144 _pthread_spin_unlock(pthread_spinlock_t *lock)
145 {
146 	struct pthread_spinlock	*lck;
147 
148 	if (lock == NULL)
149 		return (EINVAL);
150 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
151 	if (lck == NULL)
152 		return (EINVAL);
153 	return (THR_UMUTEX_UNLOCK(_get_curthread(), &lck->s_lock));
154 }
155