pthread_spin.c revision 1.1 1 /* $NetBSD: pthread_spin.c,v 1.1 2007/08/16 13:54:17 ad 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Public (POSIX-specified) spinlocks.
41 */
42
43 #include <sys/cdefs.h>
44 __RCSID("$NetBSD: pthread_spin.c,v 1.1 2007/08/16 13:54:17 ad Exp $");
45
46 #include <sys/types.h>
47 #include <sys/lock.h>
48 #include <sys/ras.h>
49
50 #include <errno.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54
55 #include "pthread.h"
56 #include "pthread_int.h"
57
58 int
59 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
60 {
61
62 #ifdef ERRORCHECK
63 if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
64 pshared != PTHREAD_PROCESS_SHARED))
65 return EINVAL;
66 #endif
67 lock->pts_magic = _PT_SPINLOCK_MAGIC;
68
69 /*
70 * We don't actually use the pshared flag for anything;
71 * CPU simple locks have all the process-shared properties
72 * that we want anyway.
73 */
74 lock->pts_flags = pshared;
75 pthread_lockinit(&lock->pts_spin);
76
77 return 0;
78 }
79
80 int
81 pthread_spin_destroy(pthread_spinlock_t *lock)
82 {
83
84 #ifdef ERRORCHECK
85 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
86 return EINVAL;
87 if (lock->pts_spin != __SIMPLELOCK_UNLOCKED)
88 return EBUSY;
89 #endif
90
91 lock->pts_magic = _PT_SPINLOCK_DEAD;
92
93 return 0;
94 }
95
96 int
97 pthread_spin_lock(pthread_spinlock_t *lock)
98 {
99
100 #ifdef ERRORCHECK
101 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
102 return EINVAL;
103 #endif
104
105 while (pthread__simple_lock_try(&lock->pts_spin) == 0) {
106 pthread__smt_pause();
107 }
108
109 return 0;
110 }
111
112 int
113 pthread_spin_trylock(pthread_spinlock_t *lock)
114 {
115
116 #ifdef ERRORCHECK
117 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
118 return EINVAL;
119 #endif
120
121 if (pthread__simple_lock_try(&lock->pts_spin) == 0)
122 return EBUSY;
123
124 return 0;
125 }
126
127 int
128 pthread_spin_unlock(pthread_spinlock_t *lock)
129 {
130
131 #ifdef ERRORCHECK
132 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
133 return EINVAL;
134 #endif
135
136 pthread__simple_unlock(&lock->pts_spin);
137
138 return 0;
139 }
140