xref: /netbsd-src/lib/libpthread/pthread_spin.c (revision ed75d7a867996c84cfa88e3b8906816277e957f7)
1 /*	$NetBSD: pthread_spin.c,v 1.8 2020/02/05 11:05:10 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nathan J. Williams and Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Public (POSIX-specified) spinlocks.
34  */
35 
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: pthread_spin.c,v 1.8 2020/02/05 11:05:10 kamil Exp $");
38 
39 #include <sys/types.h>
40 #include <sys/ras.h>
41 
42 #include <machine/lock.h>
43 
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include "pthread.h"
50 #include "pthread_int.h"
51 
52 int
53 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
54 {
55 
56 	pthread__error(EINVAL, "Invalid pshared",
57 	    pshared == PTHREAD_PROCESS_PRIVATE ||
58 	    pshared == PTHREAD_PROCESS_SHARED);
59 
60 	lock->pts_magic = _PT_SPINLOCK_MAGIC;
61 
62 	/*
63 	 * We don't actually use the pshared flag for anything;
64 	 * CPU simple locks have all the process-shared properties
65 	 * that we want anyway.
66 	 */
67 	lock->pts_flags = pshared;
68 	pthread_lockinit(&lock->pts_spin);
69 
70 	return 0;
71 }
72 
73 int
74 pthread_spin_destroy(pthread_spinlock_t *lock)
75 {
76 
77 	pthread__error(EINVAL, "Invalid spinlock",
78 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
79 
80 	if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
81 		return EBUSY;
82 
83 	lock->pts_magic = _PT_SPINLOCK_DEAD;
84 
85 	return 0;
86 }
87 
88 int
89 pthread_spin_lock(pthread_spinlock_t *lock)
90 {
91 	pthread_t self;
92 
93 	pthread__error(EINVAL, "Invalid spinlock",
94 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
95 
96 	self = pthread__self();
97 	while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
98 		pthread__smt_pause();
99 	}
100 
101 	return 0;
102 }
103 
104 int
105 pthread_spin_trylock(pthread_spinlock_t *lock)
106 {
107 	pthread_t self;
108 
109 	pthread__error(EINVAL, "Invalid spinlock",
110 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
111 
112 	self = pthread__self();
113 	if (pthread__spintrylock(self, &lock->pts_spin) == 0)
114 		return EBUSY;
115 	return 0;
116 }
117 
118 int
119 pthread_spin_unlock(pthread_spinlock_t *lock)
120 {
121 	pthread_t self;
122 
123 	pthread__error(EINVAL, "Invalid spinlock",
124 	    lock->pts_magic == _PT_SPINLOCK_MAGIC);
125 
126 	self = pthread__self();
127 	pthread__spinunlock(self, &lock->pts_spin);
128 	pthread__smt_wake();
129 
130 	return 0;
131 }
132