Rev 557 | Rev 622 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 557 | Rev 581 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <synch/spinlock.h> |
29 | #include <synch/spinlock.h> |
30 | #include <arch/atomic.h> |
30 | #include <arch/atomic.h> |
31 | #include <arch/barrier.h> |
31 | #include <arch/barrier.h> |
32 | #include <arch.h> |
32 | #include <arch.h> |
33 | #include <preemption.h> |
33 | #include <preemption.h> |
34 | #include <print.h> |
34 | #include <print.h> |
35 | #include <debug.h> |
35 | #include <debug.h> |
36 | #include <symtab.h> |
36 | #include <symtab.h> |
37 | 37 | ||
38 | #ifdef CONFIG_SMP |
38 | #ifdef CONFIG_SMP |
39 | 39 | ||
40 | /** Initialize spinlock |
40 | /** Initialize spinlock |
41 | * |
41 | * |
42 | * Initialize spinlock. |
42 | * Initialize spinlock. |
43 | * |
43 | * |
44 | * @param sl Pointer to spinlock_t structure. |
44 | * @param sl Pointer to spinlock_t structure. |
45 | */ |
45 | */ |
46 | void spinlock_initialize(spinlock_t *sl, char *name) |
46 | void spinlock_initialize(spinlock_t *sl, char *name) |
47 | { |
47 | { |
48 | sl->val = 0; |
48 | sl->val = 0; |
49 | #ifdef CONFIG_DEBUG_SPINLOCK |
49 | #ifdef CONFIG_DEBUG_SPINLOCK |
50 | sl->name = name; |
50 | sl->name = name; |
51 | #endif |
51 | #endif |
52 | } |
52 | } |
53 | 53 | ||
54 | #ifdef CONFIG_DEBUG_SPINLOCK |
54 | #ifdef CONFIG_DEBUG_SPINLOCK |
55 | /** Lock spinlock |
55 | /** Lock spinlock |
56 | * |
56 | * |
57 | * Lock spinlock. |
57 | * Lock spinlock. |
58 | * This version has limitted ability to report |
58 | * This version has limitted ability to report |
59 | * possible occurence of deadlock. |
59 | * possible occurence of deadlock. |
60 | * |
60 | * |
61 | * @param sl Pointer to spinlock_t structure. |
61 | * @param sl Pointer to spinlock_t structure. |
62 | */ |
62 | */ |
63 | void spinlock_lock(spinlock_t *sl) |
63 | void spinlock_lock(spinlock_t *sl) |
64 | { |
64 | { |
65 | count_t i = 0; |
65 | count_t i = 0; |
66 | __address caller = ((__address *) &sl)[-1]; |
66 | __address caller = CALLER(sl); |
67 | char *symbol; |
67 | char *symbol; |
68 | bool deadlock_reported = false; |
68 | bool deadlock_reported = false; |
69 | 69 | ||
70 | preemption_disable(); |
70 | preemption_disable(); |
71 | while (test_and_set(&sl->val)) { |
71 | while (test_and_set(&sl->val)) { |
72 | if (i++ > 300000) { |
72 | if (i++ > 300000) { |
73 | printf("cpu%d: looping on spinlock %p:%s, caller=%p", |
73 | printf("cpu%d: looping on spinlock %p:%s, caller=%p", |
74 | CPU->id, sl, sl->name, caller); |
74 | CPU->id, sl, sl->name, caller); |
75 | symbol = get_symtab_entry(caller); |
75 | symbol = get_symtab_entry(caller); |
76 | if (symbol) |
76 | if (symbol) |
77 | printf("(%s)", symbol); |
77 | printf("(%s)", symbol); |
78 | printf("\n"); |
78 | printf("\n"); |
79 | i = 0; |
79 | i = 0; |
80 | deadlock_reported = true; |
80 | deadlock_reported = true; |
81 | } |
81 | } |
82 | } |
82 | } |
83 | 83 | ||
84 | if (deadlock_reported) |
84 | if (deadlock_reported) |
85 | printf("cpu%d: not deadlocked\n", CPU->id); |
85 | printf("cpu%d: not deadlocked\n", CPU->id); |
86 | 86 | ||
87 | /* |
87 | /* |
88 | * Prevent critical section code from bleeding out this way up. |
88 | * Prevent critical section code from bleeding out this way up. |
89 | */ |
89 | */ |
90 | CS_ENTER_BARRIER(); |
90 | CS_ENTER_BARRIER(); |
91 | 91 | ||
92 | } |
92 | } |
93 | 93 | ||
94 | #else |
94 | #else |
95 | 95 | ||
96 | /** Lock spinlock |
96 | /** Lock spinlock |
97 | * |
97 | * |
98 | * Lock spinlock. |
98 | * Lock spinlock. |
99 | * |
99 | * |
100 | * @param sl Pointer to spinlock_t structure. |
100 | * @param sl Pointer to spinlock_t structure. |
101 | */ |
101 | */ |
102 | void spinlock_lock(spinlock_t *sl) |
102 | void spinlock_lock(spinlock_t *sl) |
103 | { |
103 | { |
104 | preemption_disable(); |
104 | preemption_disable(); |
105 | 105 | ||
106 | /* |
106 | /* |
107 | * Each architecture has its own efficient/recommended |
107 | * Each architecture has its own efficient/recommended |
108 | * implementation of spinlock. |
108 | * implementation of spinlock. |
109 | */ |
109 | */ |
110 | spinlock_arch(&sl->val); |
110 | spinlock_arch(&sl->val); |
111 | 111 | ||
112 | /* |
112 | /* |
113 | * Prevent critical section code from bleeding out this way up. |
113 | * Prevent critical section code from bleeding out this way up. |
114 | */ |
114 | */ |
115 | CS_ENTER_BARRIER(); |
115 | CS_ENTER_BARRIER(); |
116 | } |
116 | } |
117 | #endif |
117 | #endif |
118 | 118 | ||
119 | /** Lock spinlock conditionally |
119 | /** Lock spinlock conditionally |
120 | * |
120 | * |
121 | * Lock spinlock conditionally. |
121 | * Lock spinlock conditionally. |
122 | * If the spinlock is not available at the moment, |
122 | * If the spinlock is not available at the moment, |
123 | * signal failure. |
123 | * signal failure. |
124 | * |
124 | * |
125 | * @param sl Pointer to spinlock_t structure. |
125 | * @param sl Pointer to spinlock_t structure. |
126 | * |
126 | * |
127 | * @return Zero on failure, non-zero otherwise. |
127 | * @return Zero on failure, non-zero otherwise. |
128 | */ |
128 | */ |
129 | int spinlock_trylock(spinlock_t *sl) |
129 | int spinlock_trylock(spinlock_t *sl) |
130 | { |
130 | { |
131 | int rc; |
131 | int rc; |
132 | 132 | ||
133 | preemption_disable(); |
133 | preemption_disable(); |
134 | rc = !test_and_set(&sl->val); |
134 | rc = !test_and_set(&sl->val); |
135 | 135 | ||
136 | /* |
136 | /* |
137 | * Prevent critical section code from bleeding out this way up. |
137 | * Prevent critical section code from bleeding out this way up. |
138 | */ |
138 | */ |
139 | CS_ENTER_BARRIER(); |
139 | CS_ENTER_BARRIER(); |
140 | 140 | ||
141 | if (!rc) |
141 | if (!rc) |
142 | preemption_enable(); |
142 | preemption_enable(); |
143 | 143 | ||
144 | return rc; |
144 | return rc; |
145 | } |
145 | } |
146 | 146 | ||
147 | /** Unlock spinlock |
147 | /** Unlock spinlock |
148 | * |
148 | * |
149 | * Unlock spinlock. |
149 | * Unlock spinlock. |
150 | * |
150 | * |
151 | * @param sl Pointer to spinlock_t structure. |
151 | * @param sl Pointer to spinlock_t structure. |
152 | */ |
152 | */ |
153 | void spinlock_unlock(spinlock_t *sl) |
153 | void spinlock_unlock(spinlock_t *sl) |
154 | { |
154 | { |
155 | ASSERT(sl->val != 0); |
155 | ASSERT(sl->val != 0); |
156 | 156 | ||
157 | /* |
157 | /* |
158 | * Prevent critical section code from bleeding out this way down. |
158 | * Prevent critical section code from bleeding out this way down. |
159 | */ |
159 | */ |
160 | CS_LEAVE_BARRIER(); |
160 | CS_LEAVE_BARRIER(); |
161 | 161 | ||
162 | sl->val = 0; |
162 | sl->val = 0; |
163 | preemption_enable(); |
163 | preemption_enable(); |
164 | } |
164 | } |
165 | 165 | ||
166 | #endif |
166 | #endif |
167 | 167 |