1 /* $NetBSD: pthread_spin.c,v 1.4 2008/01/05 01:37:35 ad Exp $ */
4 * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
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
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.
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>
42 #include <machine/lock.h>
50 #include "pthread_int.h"
53 pthread_spin_init(pthread_spinlock_t
*lock
, int pshared
)
57 if (lock
== NULL
|| (pshared
!= PTHREAD_PROCESS_PRIVATE
&&
58 pshared
!= PTHREAD_PROCESS_SHARED
))
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
);
75 pthread_spin_destroy(pthread_spinlock_t
*lock
)
79 if (lock
== NULL
|| lock
->pts_magic
!= _PT_SPINLOCK_MAGIC
)
81 if (!__SIMPLELOCK_UNLOCKED_P(&lock
->pts_spin
))
85 lock
->pts_magic
= _PT_SPINLOCK_DEAD
;
91 pthread_spin_lock(pthread_spinlock_t
*lock
)
96 if (lock
== NULL
|| lock
->pts_magic
!= _PT_SPINLOCK_MAGIC
)
100 self
= pthread__self();
101 while (pthread__spintrylock(self
, &lock
->pts_spin
) == 0) {
102 pthread__smt_pause();
109 pthread_spin_trylock(pthread_spinlock_t
*lock
)
114 if (lock
== NULL
|| lock
->pts_magic
!= _PT_SPINLOCK_MAGIC
)
118 self
= pthread__self();
119 if (pthread__spintrylock(self
, &lock
->pts_spin
) == 0)
125 pthread_spin_unlock(pthread_spinlock_t
*lock
)
130 if (lock
== NULL
|| lock
->pts_magic
!= _PT_SPINLOCK_MAGIC
)
134 self
= pthread__self();
135 pthread__spinunlock(self
, &lock
->pts_spin
);