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