pthread_lock.c revision 1.1.2.3 1 /* $NetBSD: pthread_lock.c,v 1.1.2.3 2001/07/13 02:42:38 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2001 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.
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 #include <assert.h>
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <ucontext.h>
44 #include <sys/queue.h>
45
46 #include "pthread.h"
47 #include "pthread_int.h"
48
49 /* How many times to try before checking whether we've been continued. */
50 #define NSPINS 20 /* XXX arbitrary */
51
52 static int nspins = NSPINS;
53
54 void
55 pthread_lockinit(pt_spin_t *lock)
56 {
57
58 __cpu_simple_lock_init(lock);
59 }
60
61 void
62 pthread_spinlock(pthread_t thread, pt_spin_t *lock)
63 {
64 int count, ret;
65
66 count = nspins;
67 ++thread->pt_spinlocks;
68
69 do {
70 while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
71 ;
72
73 if (ret == 1)
74 break;
75
76 --thread->pt_spinlocks;
77
78 /* We may be preempted while spinning. If so, we will
79 * be restarted here if thread->pt_spinlocks is
80 * nonzero, which can happen if:
81 * a) we just got the lock
82 * b) we haven't yet decremented the lock count.
83 * If we're at this point, (b) applies. Therefore,
84 * check if we're being continued, and if so, bail.
85 * (in case (a), we should let the code finish and
86 * we will bail out in pthread_spinunlock()).
87 */
88 if (thread->pt_next != NULL) {
89 PTHREADD_ADD(PTHREADD_SPINPREEMPT);
90 pthread__switch(thread, thread->pt_next, 0);
91 }
92 /* try again */
93 count = nspins;
94 ++thread->pt_spinlocks;
95 } while (/*CONSTCOND*/1);
96
97 PTHREADD_ADD(PTHREADD_SPINLOCKS);
98 /* Got it! We're out of here. */
99 }
100
101
102 int
103 pthread_spintrylock(pthread_t thread, pt_spin_t *lock)
104 {
105 int ret;
106
107 ++thread->pt_spinlocks;
108
109 ret = __cpu_simple_lock_try(lock);
110
111 if (ret == 0) {
112 --thread->pt_spinlocks;
113 /* See above. */
114 if (thread->pt_next != NULL) {
115 PTHREADD_ADD(PTHREADD_SPINPREEMPT);
116 pthread__switch(thread, thread->pt_next, 0);
117 }
118 }
119
120 return ret;
121 }
122
123
124 void
125 pthread_spinunlock(pthread_t thread, pt_spin_t *lock)
126 {
127 __cpu_simple_unlock(lock);
128 --thread->pt_spinlocks;
129
130 PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
131
132 /* If we were preempted while holding a spinlock, the
133 * scheduler will notice this and continue us. To be good
134 * citzens, we must now get out of here if that was our
135 * last spinlock.
136 * XXX when will we ever have more than one?
137 */
138
139 if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)) {
140 PTHREADD_ADD(PTHREADD_SPINPREEMPT);
141 pthread__switch(thread, thread->pt_next, 0);
142 }
143 }
144
145