Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libpthread / pthread_spin.c
blobb4d9b7f2b531ddbe971ee2e7a9e2ac8bb2532a52
1 /* $NetBSD: pthread_spin.c,v 1.4 2008/01/05 01:37:35 ad Exp $ */
3 /*-
4 * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams and Andrew Doran.
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.
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.
32 /*
33 * Public (POSIX-specified) spinlocks.
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: pthread_spin.c,v 1.4 2008/01/05 01:37:35 ad Exp $");
39 #include <sys/types.h>
40 #include <sys/ras.h>
42 #include <machine/lock.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
49 #include "pthread.h"
50 #include "pthread_int.h"
52 int
53 pthread_spin_init(pthread_spinlock_t *lock, int pshared)
56 #ifdef ERRORCHECK
57 if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
58 pshared != PTHREAD_PROCESS_SHARED))
59 return EINVAL;
60 #endif
61 lock->pts_magic = _PT_SPINLOCK_MAGIC;
64 * We don't actually use the pshared flag for anything;
65 * CPU simple locks have all the process-shared properties
66 * that we want anyway.
68 lock->pts_flags = pshared;
69 pthread_lockinit(&lock->pts_spin);
71 return 0;
74 int
75 pthread_spin_destroy(pthread_spinlock_t *lock)
78 #ifdef ERRORCHECK
79 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
80 return EINVAL;
81 if (!__SIMPLELOCK_UNLOCKED_P(&lock->pts_spin))
82 return EBUSY;
83 #endif
85 lock->pts_magic = _PT_SPINLOCK_DEAD;
87 return 0;
90 int
91 pthread_spin_lock(pthread_spinlock_t *lock)
93 pthread_t self;
95 #ifdef ERRORCHECK
96 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
97 return EINVAL;
98 #endif
100 self = pthread__self();
101 while (pthread__spintrylock(self, &lock->pts_spin) == 0) {
102 pthread__smt_pause();
105 return 0;
109 pthread_spin_trylock(pthread_spinlock_t *lock)
111 pthread_t self;
113 #ifdef ERRORCHECK
114 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
115 return EINVAL;
116 #endif
118 self = pthread__self();
119 if (pthread__spintrylock(self, &lock->pts_spin) == 0)
120 return EBUSY;
121 return 0;
125 pthread_spin_unlock(pthread_spinlock_t *lock)
127 pthread_t self;
129 #ifdef ERRORCHECK
130 if (lock == NULL || lock->pts_magic != _PT_SPINLOCK_MAGIC)
131 return EINVAL;
132 #endif
134 self = pthread__self();
135 pthread__spinunlock(self, &lock->pts_spin);
137 return 0;