pthread_spin.c revision 1.4
1/*	$NetBSD: pthread_spin.c,v 1.4 2008/01/05 01:37:35 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.4 2008/01/05 01:37:35 ad Exp $");
45
46#include <sys/types.h>
47#include <sys/ras.h>
48
49#include <machine/lock.h>
50
51#include <errno.h>
52#include <unistd.h>
53#include <stdio.h>
54#include <stdlib.h>
55
56#include "pthread.h"
57#include "pthread_int.h"
58
59int
60pthread_spin_init(pthread_spinlock_t *lock, int pshared)
61{
62
63#ifdef ERRORCHECK
64	if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
65	    pshared != PTHREAD_PROCESS_SHARED))
66		return EINVAL;
67#endif
68	lock->pts_magic = _PT_SPINLOCK_MAGIC;
69
70	/*
71	 * We don't actually use the pshared flag for anything;
72	 * CPU simple locks have all the process-shared properties
73	 * that we want anyway.
74	 */
75	lock->pts_flags = pshared;
76	pthread_lockinit(&lock->pts_spin);
77
78	return 0;
79}
80
81int
82pthread_spin_destroy(pthread_spinlock_t *lock)
83{
84
85#ifdef ERRORCHECK
86	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
87		return EINVAL;
88	if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
89		return EBUSY;
90#endif
91
92	lock->pts_magic = _PT_SPINLOCK_DEAD;
93
94	return 0;
95}
96
97int
98pthread_spin_lock(pthread_spinlock_t *lock)
99{
100	pthread_t self;
101
102#ifdef ERRORCHECK
103	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
104		return EINVAL;
105#endif
106
107	self = pthread__self();
108	while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
109		pthread__smt_pause();
110	}
111
112	return 0;
113}
114
115int
116pthread_spin_trylock(pthread_spinlock_t *lock)
117{
118	pthread_t self;
119
120#ifdef ERRORCHECK
121	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
122		return EINVAL;
123#endif
124
125	self = pthread__self();
126	if (pthread__spintrylock(self, &lock->pts_spin) == 0)
127		return EBUSY;
128	return 0;
129}
130
131int
132pthread_spin_unlock(pthread_spinlock_t *lock)
133{
134	pthread_t self;
135
136#ifdef ERRORCHECK
137	if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
138		return EINVAL;
139#endif
140
141	self = pthread__self();
142	pthread__spinunlock(self, &lock->pts_spin);
143
144	return 0;
145}
146