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