pthread_lock.c revision 1.1.2.7 1 1.1.2.7 nathanw /* $NetBSD: pthread_lock.c,v 1.1.2.7 2002/02/08 22:26:55 nathanw Exp $ */
2 1.1.2.3 nathanw
3 1.1.2.3 nathanw /*-
4 1.1.2.3 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.2.3 nathanw * All rights reserved.
6 1.1.2.3 nathanw *
7 1.1.2.3 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.3 nathanw * by Nathan J. Williams.
9 1.1.2.3 nathanw *
10 1.1.2.3 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1.2.3 nathanw * modification, are permitted provided that the following conditions
12 1.1.2.3 nathanw * are met:
13 1.1.2.3 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1.2.3 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.3 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1.2.3 nathanw * documentation and/or other materials provided with the distribution.
18 1.1.2.3 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1.2.3 nathanw * must display the following acknowledgement:
20 1.1.2.3 nathanw * This product includes software developed by the NetBSD
21 1.1.2.3 nathanw * Foundation, Inc. and its contributors.
22 1.1.2.3 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.2.3 nathanw * contributors may be used to endorse or promote products derived
24 1.1.2.3 nathanw * from this software without specific prior written permission.
25 1.1.2.3 nathanw *
26 1.1.2.3 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.2.3 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.2.3 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.2.3 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.2.3 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.2.3 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.2.3 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.2.3 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.2.3 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.2.3 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.2.3 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1.2.3 nathanw */
38 1.1.2.1 nathanw
39 1.1.2.6 nathanw #include <assert.h>
40 1.1.2.6 nathanw
41 1.1.2.1 nathanw #include "pthread.h"
42 1.1.2.1 nathanw #include "pthread_int.h"
43 1.1.2.1 nathanw
44 1.1.2.6 nathanw #undef PTHREAD_SA_DEBUG
45 1.1.2.6 nathanw
46 1.1.2.6 nathanw #ifdef PTHREAD_SA_DEBUG
47 1.1.2.6 nathanw #define SDPRINTF(x) DPRINTF(x)
48 1.1.2.6 nathanw #else
49 1.1.2.6 nathanw #define SDPRINTF(x)
50 1.1.2.6 nathanw #endif
51 1.1.2.6 nathanw
52 1.1.2.1 nathanw /* How many times to try before checking whether we've been continued. */
53 1.1.2.1 nathanw #define NSPINS 20 /* XXX arbitrary */
54 1.1.2.1 nathanw
55 1.1.2.1 nathanw static int nspins = NSPINS;
56 1.1.2.1 nathanw
57 1.1.2.1 nathanw void
58 1.1.2.6 nathanw pthread_lockinit(pthread_spin_t *lock)
59 1.1.2.2 nathanw {
60 1.1.2.2 nathanw
61 1.1.2.2 nathanw __cpu_simple_lock_init(lock);
62 1.1.2.2 nathanw }
63 1.1.2.2 nathanw
64 1.1.2.2 nathanw void
65 1.1.2.6 nathanw pthread_spinlock(pthread_t thread, pthread_spin_t *lock)
66 1.1.2.1 nathanw {
67 1.1.2.1 nathanw int count, ret;
68 1.1.2.1 nathanw
69 1.1.2.1 nathanw count = nspins;
70 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
71 1.1.2.6 nathanw thread, thread->pt_spinlocks));
72 1.1.2.1 nathanw ++thread->pt_spinlocks;
73 1.1.2.1 nathanw
74 1.1.2.1 nathanw do {
75 1.1.2.1 nathanw while (((ret = __cpu_simple_lock_try(lock)) == 0) && --count)
76 1.1.2.1 nathanw ;
77 1.1.2.1 nathanw
78 1.1.2.1 nathanw if (ret == 1)
79 1.1.2.1 nathanw break;
80 1.1.2.6 nathanw
81 1.1.2.6 nathanw /* As long as this is uniprocessor, encountering a
82 1.1.2.6 nathanw * locked spinlock is a bug.
83 1.1.2.6 nathanw */
84 1.1.2.6 nathanw assert (ret == 1);
85 1.1.2.6 nathanw
86 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) decrementing spinlock from %d\n",
87 1.1.2.6 nathanw thread, thread->pt_spinlocks));
88 1.1.2.1 nathanw --thread->pt_spinlocks;
89 1.1.2.1 nathanw
90 1.1.2.1 nathanw /* We may be preempted while spinning. If so, we will
91 1.1.2.1 nathanw * be restarted here if thread->pt_spinlocks is
92 1.1.2.1 nathanw * nonzero, which can happen if:
93 1.1.2.1 nathanw * a) we just got the lock
94 1.1.2.1 nathanw * b) we haven't yet decremented the lock count.
95 1.1.2.1 nathanw * If we're at this point, (b) applies. Therefore,
96 1.1.2.1 nathanw * check if we're being continued, and if so, bail.
97 1.1.2.1 nathanw * (in case (a), we should let the code finish and
98 1.1.2.1 nathanw * we will bail out in pthread_spinunlock()).
99 1.1.2.1 nathanw */
100 1.1.2.7 nathanw if ((thread->pt_next != NULL) &&
101 1.1.2.7 nathanw (thread->pt_type != PT_THREAD_UPCALL)) {
102 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
103 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
104 1.1.2.1 nathanw }
105 1.1.2.1 nathanw /* try again */
106 1.1.2.1 nathanw count = nspins;
107 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
108 1.1.2.6 nathanw thread, thread->pt_spinlocks));
109 1.1.2.1 nathanw ++thread->pt_spinlocks;
110 1.1.2.1 nathanw } while (/*CONSTCOND*/1);
111 1.1.2.1 nathanw
112 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINLOCKS);
113 1.1.2.1 nathanw /* Got it! We're out of here. */
114 1.1.2.1 nathanw }
115 1.1.2.1 nathanw
116 1.1.2.1 nathanw
117 1.1.2.1 nathanw int
118 1.1.2.6 nathanw pthread_spintrylock(pthread_t thread, pthread_spin_t *lock)
119 1.1.2.1 nathanw {
120 1.1.2.1 nathanw int ret;
121 1.1.2.1 nathanw
122 1.1.2.6 nathanw SDPRINTF(("(pthread_spinlock %p) incrementing spinlock from %d\n",
123 1.1.2.6 nathanw thread, thread->pt_spinlocks));
124 1.1.2.1 nathanw ++thread->pt_spinlocks;
125 1.1.2.1 nathanw
126 1.1.2.1 nathanw ret = __cpu_simple_lock_try(lock);
127 1.1.2.1 nathanw
128 1.1.2.1 nathanw if (ret == 0) {
129 1.1.2.6 nathanw SDPRINTF(("(pthread_spintrylock %p) decrementing spinlock from %d\n",
130 1.1.2.6 nathanw thread, thread->pt_spinlocks));
131 1.1.2.1 nathanw --thread->pt_spinlocks;
132 1.1.2.1 nathanw /* See above. */
133 1.1.2.7 nathanw if ((thread->pt_next != NULL) &&
134 1.1.2.7 nathanw (thread->pt_type != PT_THREAD_UPCALL)) {
135 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
136 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
137 1.1.2.1 nathanw }
138 1.1.2.1 nathanw }
139 1.1.2.1 nathanw
140 1.1.2.1 nathanw return ret;
141 1.1.2.1 nathanw }
142 1.1.2.1 nathanw
143 1.1.2.1 nathanw
144 1.1.2.1 nathanw void
145 1.1.2.6 nathanw pthread_spinunlock(pthread_t thread, pthread_spin_t *lock)
146 1.1.2.1 nathanw {
147 1.1.2.1 nathanw __cpu_simple_unlock(lock);
148 1.1.2.6 nathanw SDPRINTF(("(pthread_spinunlock %p) decrementing spinlock from %d\n",
149 1.1.2.6 nathanw thread, thread->pt_spinlocks));
150 1.1.2.1 nathanw --thread->pt_spinlocks;
151 1.1.2.1 nathanw
152 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINUNLOCKS);
153 1.1.2.1 nathanw
154 1.1.2.1 nathanw /* If we were preempted while holding a spinlock, the
155 1.1.2.1 nathanw * scheduler will notice this and continue us. To be good
156 1.1.2.1 nathanw * citzens, we must now get out of here if that was our
157 1.1.2.1 nathanw * last spinlock.
158 1.1.2.1 nathanw * XXX when will we ever have more than one?
159 1.1.2.1 nathanw */
160 1.1.2.1 nathanw
161 1.1.2.7 nathanw if ((thread->pt_spinlocks == 0) && (thread->pt_next != NULL)
162 1.1.2.7 nathanw && (thread->pt_type != PT_THREAD_UPCALL)) {
163 1.1.2.1 nathanw PTHREADD_ADD(PTHREADD_SPINPREEMPT);
164 1.1.2.5 nathanw pthread__switch(thread, thread->pt_next);
165 1.1.2.1 nathanw }
166 1.1.2.1 nathanw }
167 1.1.2.1 nathanw
168