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