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