2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 David Xu <davidxu@freebsd.org>
5 * Copyright (c) 2016 The FreeBSD Foundation
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
40 #include "un-namespace.h"
42 #include "thr_private.h"
44 _Static_assert(sizeof(struct pthread_spinlock) <= PAGE_SIZE,
45 "pthread_spinlock is too large for off-page");
47 #define SPIN_COUNT 100000
49 __weak_reference(_pthread_spin_init, pthread_spin_init);
50 __weak_reference(_pthread_spin_destroy, pthread_spin_destroy);
51 __weak_reference(_pthread_spin_trylock, pthread_spin_trylock);
52 __weak_reference(_pthread_spin_lock, pthread_spin_lock);
53 __weak_reference(_pthread_spin_unlock, pthread_spin_unlock);
56 _pthread_spin_init(pthread_spinlock_t *lock, int pshared)
58 struct pthread_spinlock *lck;
62 if (pshared == PTHREAD_PROCESS_PRIVATE) {
63 lck = malloc(sizeof(struct pthread_spinlock));
67 } else if (pshared == PTHREAD_PROCESS_SHARED) {
68 lck = __thr_pshared_offpage(lock, 1);
71 *lock = THR_PSHARED_PTR;
75 _thr_umutex_init(&lck->s_lock);
80 _pthread_spin_destroy(pthread_spinlock_t *lock)
85 if (lock == NULL || *lock == NULL) {
87 } else if (*lock == THR_PSHARED_PTR) {
88 l = __thr_pshared_offpage(lock, 0);
90 __thr_pshared_destroy(l);
101 _pthread_spin_trylock(pthread_spinlock_t *lock)
103 struct pthread_spinlock *lck;
105 if (lock == NULL || *lock == NULL)
107 lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
110 return (THR_UMUTEX_TRYLOCK(_get_curthread(), &lck->s_lock));
114 _pthread_spin_lock(pthread_spinlock_t *lock)
116 struct pthread *curthread;
117 struct pthread_spinlock *lck;
122 lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
126 curthread = _get_curthread();
128 while (THR_UMUTEX_TRYLOCK(curthread, &lck->s_lock) != 0) {
129 while (lck->s_lock.m_owner) {
145 _pthread_spin_unlock(pthread_spinlock_t *lock)
147 struct pthread_spinlock *lck;
151 lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
154 return (THR_UMUTEX_UNLOCK(_get_curthread(), &lck->s_lock));