xref: /freebsd/lib/libthr/thread/thr_pspinlock.c (revision f8bbbce4)
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 "namespace.h"
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <pthread.h>
37 #include "un-namespace.h"
38 
39 #include "thr_private.h"
40 
41 _Static_assert(sizeof(struct pthread_spinlock) <= THR_PAGE_SIZE_MIN,
42     "pthread_spinlock is too large for off-page");
43 
44 #define SPIN_COUNT 100000
45 
46 __weak_reference(_pthread_spin_init, pthread_spin_init);
47 __weak_reference(_pthread_spin_destroy, pthread_spin_destroy);
48 __weak_reference(_pthread_spin_trylock, pthread_spin_trylock);
49 __weak_reference(_pthread_spin_lock, pthread_spin_lock);
50 __weak_reference(_pthread_spin_unlock, pthread_spin_unlock);
51 
52 int
_pthread_spin_init(pthread_spinlock_t * lock,int pshared)53 _pthread_spin_init(pthread_spinlock_t *lock, int pshared)
54 {
55 	struct pthread_spinlock	*lck;
56 
57 	if (lock == NULL)
58 		return (EINVAL);
59 	if (pshared == PTHREAD_PROCESS_PRIVATE) {
60 		lck = aligned_alloc(CACHE_LINE_SIZE,
61 		    roundup(sizeof(struct pthread_spinlock), CACHE_LINE_SIZE));
62 		if (lck == NULL)
63 			return (ENOMEM);
64 		*lock = lck;
65 	} else if (pshared == PTHREAD_PROCESS_SHARED) {
66 		lck = __thr_pshared_offpage(lock, 1);
67 		if (lck == NULL)
68 			return (EFAULT);
69 		*lock = THR_PSHARED_PTR;
70 	} else {
71 		return (EINVAL);
72 	}
73 	_thr_umutex_init(&lck->s_lock);
74 	return (0);
75 }
76 
77 int
_pthread_spin_destroy(pthread_spinlock_t * lock)78 _pthread_spin_destroy(pthread_spinlock_t *lock)
79 {
80 	void *l;
81 	int ret;
82 
83 	if (lock == NULL || *lock == NULL) {
84 		ret = EINVAL;
85 	} else if (*lock == THR_PSHARED_PTR) {
86 		l = __thr_pshared_offpage(lock, 0);
87 		if (l != NULL)
88 			__thr_pshared_destroy(l);
89 		ret = 0;
90 	} else {
91 		free(*lock);
92 		*lock = NULL;
93 		ret = 0;
94 	}
95 	return (ret);
96 }
97 
98 int
_pthread_spin_trylock(pthread_spinlock_t * lock)99 _pthread_spin_trylock(pthread_spinlock_t *lock)
100 {
101 	struct pthread_spinlock	*lck;
102 
103 	if (lock == NULL || *lock == NULL)
104 		return (EINVAL);
105 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
106 	if (lck == NULL)
107 		return (EINVAL);
108 	return (THR_UMUTEX_TRYLOCK(_get_curthread(), &lck->s_lock));
109 }
110 
111 int
_pthread_spin_lock(pthread_spinlock_t * lock)112 _pthread_spin_lock(pthread_spinlock_t *lock)
113 {
114 	struct pthread *curthread;
115 	struct pthread_spinlock	*lck;
116 	int count;
117 
118 	if (lock == NULL)
119 		return (EINVAL);
120 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
121 	if (lck == NULL)
122 		return (EINVAL);
123 
124 	curthread = _get_curthread();
125 	count = SPIN_COUNT;
126 	while (THR_UMUTEX_TRYLOCK(curthread, &lck->s_lock) != 0) {
127 		while (lck->s_lock.m_owner) {
128 			if (!_thr_is_smp) {
129 				_pthread_yield();
130 			} else {
131 				CPU_SPINWAIT;
132 				if (--count <= 0) {
133 					count = SPIN_COUNT;
134 					_pthread_yield();
135 				}
136 			}
137 		}
138 	}
139 	return (0);
140 }
141 
142 int
_pthread_spin_unlock(pthread_spinlock_t * lock)143 _pthread_spin_unlock(pthread_spinlock_t *lock)
144 {
145 	struct pthread_spinlock	*lck;
146 
147 	if (lock == NULL)
148 		return (EINVAL);
149 	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
150 	if (lck == NULL)
151 		return (EINVAL);
152 	return (THR_UMUTEX_UNLOCK(_get_curthread(), &lck->s_lock));
153 }
154