Rev 3343 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3343 | Rev 3862 | ||
---|---|---|---|
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 <test.h> |
29 | #include <test.h> |
30 | #include <arch.h> |
30 | #include <arch.h> |
31 | #include <atomic.h> |
31 | #include <atomic.h> |
32 | #include <print.h> |
32 | #include <print.h> |
33 | #include <proc/thread.h> |
33 | #include <proc/thread.h> |
- | 34 | #include <arch/asm.h> |
|
34 | #include <arch/types.h> |
35 | #include <arch/types.h> |
35 | #include <arch/context.h> |
36 | #include <arch/context.h> |
36 | #include <context.h> |
37 | #include <context.h> |
37 | 38 | ||
38 | #include <synch/waitq.h> |
39 | #include <synch/waitq.h> |
39 | #include <synch/rwlock.h> |
40 | #include <synch/rwlock.h> |
40 | #include <synch/synch.h> |
41 | #include <synch/synch.h> |
41 | #include <synch/spinlock.h> |
42 | #include <synch/spinlock.h> |
42 | 43 | ||
43 | #define READERS 50 |
44 | #define READERS 50 |
44 | #define WRITERS 50 |
45 | #define WRITERS 50 |
45 | 46 | ||
46 | static atomic_t thread_count; |
47 | static atomic_t thread_count; |
47 | static rwlock_t rwlock; |
48 | static rwlock_t rwlock; |
48 | static atomic_t threads_fault; |
49 | static atomic_t threads_fault; |
49 | static bool sh_quiet; |
50 | static bool sh_quiet; |
50 | 51 | ||
51 | SPINLOCK_INITIALIZE(rw_lock); |
52 | SPINLOCK_INITIALIZE(rw_lock); |
52 | 53 | ||
53 | static waitq_t can_start; |
54 | static waitq_t can_start; |
54 | 55 | ||
55 | static uint32_t seed = 0xdeadbeef; |
56 | static uint32_t seed = 0xdeadbeef; |
56 | 57 | ||
57 | static uint32_t random(uint32_t max) |
58 | static uint32_t random(uint32_t max) |
58 | { |
59 | { |
59 | uint32_t rc; |
60 | uint32_t rc; |
60 | 61 | ||
61 | spinlock_lock(&rw_lock); |
62 | spinlock_lock(&rw_lock); |
62 | rc = seed % max; |
63 | rc = seed % max; |
63 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
64 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
64 | spinlock_unlock(&rw_lock); |
65 | spinlock_unlock(&rw_lock); |
65 | return rc; |
66 | return rc; |
66 | } |
67 | } |
67 | 68 | ||
68 | static void writer(void *arg) |
69 | static void writer(void *arg) |
69 | { |
70 | { |
70 | int rc, to; |
71 | int rc, to; |
71 | thread_detach(THREAD); |
72 | thread_detach(THREAD); |
72 | waitq_sleep(&can_start); |
73 | waitq_sleep(&can_start); |
73 | 74 | ||
74 | to = random(40000); |
75 | to = random(40000); |
75 | 76 | ||
76 | if (!sh_quiet) |
77 | if (!sh_quiet) |
77 | printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); |
78 | printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to); |
78 | 79 | ||
79 | rc = rwlock_write_lock_timeout(&rwlock, to); |
80 | rc = rwlock_write_lock_timeout(&rwlock, to); |
80 | if (SYNCH_FAILED(rc)) { |
81 | if (SYNCH_FAILED(rc)) { |
81 | if (!sh_quiet) |
82 | if (!sh_quiet) |
82 | printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); |
83 | printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid); |
83 | atomic_dec(&thread_count); |
84 | atomic_dec(&thread_count); |
84 | return; |
85 | return; |
85 | } |
86 | } |
86 | 87 | ||
87 | if (!sh_quiet) |
88 | if (!sh_quiet) |
88 | printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); |
89 | printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid); |
89 | 90 | ||
90 | if (rwlock.readers_in) { |
91 | if (rwlock.readers_in) { |
91 | if (!sh_quiet) |
92 | if (!sh_quiet) |
92 | printf("Oops."); |
93 | printf("Oops."); |
93 | atomic_inc(&threads_fault); |
94 | atomic_inc(&threads_fault); |
94 | atomic_dec(&thread_count); |
95 | atomic_dec(&thread_count); |
95 | return; |
96 | return; |
96 | } |
97 | } |
97 | thread_usleep(random(1000000)); |
98 | thread_usleep(random(1000000)); |
98 | if (rwlock.readers_in) { |
99 | if (rwlock.readers_in) { |
99 | if (!sh_quiet) |
100 | if (!sh_quiet) |
100 | printf("Oops."); |
101 | printf("Oops."); |
101 | atomic_inc(&threads_fault); |
102 | atomic_inc(&threads_fault); |
102 | atomic_dec(&thread_count); |
103 | atomic_dec(&thread_count); |
103 | return; |
104 | return; |
104 | } |
105 | } |
105 | 106 | ||
106 | rwlock_write_unlock(&rwlock); |
107 | rwlock_write_unlock(&rwlock); |
107 | 108 | ||
108 | if (!sh_quiet) |
109 | if (!sh_quiet) |
109 | printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); |
110 | printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid); |
110 | atomic_dec(&thread_count); |
111 | atomic_dec(&thread_count); |
111 | } |
112 | } |
112 | 113 | ||
113 | static void reader(void *arg) |
114 | static void reader(void *arg) |
114 | { |
115 | { |
115 | int rc, to; |
116 | int rc, to; |
116 | thread_detach(THREAD); |
117 | thread_detach(THREAD); |
117 | waitq_sleep(&can_start); |
118 | waitq_sleep(&can_start); |
118 | 119 | ||
119 | to = random(2000); |
120 | to = random(2000); |
120 | 121 | ||
121 | if (!sh_quiet) |
122 | if (!sh_quiet) |
122 | printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); |
123 | printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to); |
123 | 124 | ||
124 | rc = rwlock_read_lock_timeout(&rwlock, to); |
125 | rc = rwlock_read_lock_timeout(&rwlock, to); |
125 | if (SYNCH_FAILED(rc)) { |
126 | if (SYNCH_FAILED(rc)) { |
126 | if (!sh_quiet) |
127 | if (!sh_quiet) |
127 | printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); |
128 | printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid); |
128 | atomic_dec(&thread_count); |
129 | atomic_dec(&thread_count); |
129 | return; |
130 | return; |
130 | } |
131 | } |
131 | 132 | ||
132 | if (!sh_quiet) |
133 | if (!sh_quiet) |
133 | printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); |
134 | printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid); |
134 | 135 | ||
135 | thread_usleep(30000); |
136 | thread_usleep(30000); |
136 | rwlock_read_unlock(&rwlock); |
137 | rwlock_read_unlock(&rwlock); |
137 | 138 | ||
138 | if (!sh_quiet) |
139 | if (!sh_quiet) |
139 | printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); |
140 | printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid); |
140 | atomic_dec(&thread_count); |
141 | atomic_dec(&thread_count); |
141 | } |
142 | } |
142 | 143 | ||
143 | char * test_rwlock4(bool quiet) |
144 | char * test_rwlock4(bool quiet) |
144 | { |
145 | { |
145 | context_t ctx; |
146 | context_t ctx; |
146 | uint32_t i; |
147 | uint32_t i; |
147 | sh_quiet = quiet; |
148 | sh_quiet = quiet; |
148 | 149 | ||
149 | waitq_initialize(&can_start); |
150 | waitq_initialize(&can_start); |
150 | rwlock_initialize(&rwlock); |
151 | rwlock_initialize(&rwlock); |
151 | atomic_set(&threads_fault, 0); |
152 | atomic_set(&threads_fault, 0); |
152 | 153 | ||
153 | uint32_t rd = random(7) + 1; |
154 | uint32_t rd = random(7) + 1; |
154 | uint32_t wr = random(5) + 1; |
155 | uint32_t wr = random(5) + 1; |
155 | 156 | ||
156 | atomic_set(&thread_count, rd + wr); |
157 | atomic_set(&thread_count, rd + wr); |
157 | 158 | ||
158 | thread_t *thrd; |
159 | thread_t *thrd; |
159 | 160 | ||
160 | context_save(&ctx); |
161 | context_save(&ctx); |
161 | if (!quiet) { |
162 | if (!quiet) { |
162 | printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); |
163 | printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in); |
163 | printf("Creating %" PRIu32 " readers\n", rd); |
164 | printf("Creating %" PRIu32 " readers\n", rd); |
164 | } |
165 | } |
165 | 166 | ||
166 | for (i = 0; i < rd; i++) { |
167 | for (i = 0; i < rd; i++) { |
167 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
168 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
168 | if (thrd) |
169 | if (thrd) |
169 | thread_ready(thrd); |
170 | thread_ready(thrd); |
170 | else if (!quiet) |
171 | else if (!quiet) |
171 | printf("Could not create reader %" PRIu32 "\n", i); |
172 | printf("Could not create reader %" PRIu32 "\n", i); |
172 | } |
173 | } |
173 | 174 | ||
174 | if (!quiet) |
175 | if (!quiet) |
175 | printf("Creating %" PRIu32 " writers\n", wr); |
176 | printf("Creating %" PRIu32 " writers\n", wr); |
176 | 177 | ||
177 | for (i = 0; i < wr; i++) { |
178 | for (i = 0; i < wr; i++) { |
178 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
179 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
179 | if (thrd) |
180 | if (thrd) |
180 | thread_ready(thrd); |
181 | thread_ready(thrd); |
181 | else if (!quiet) |
182 | else if (!quiet) |
182 | printf("Could not create writer %" PRIu32 "\n", i); |
183 | printf("Could not create writer %" PRIu32 "\n", i); |
183 | } |
184 | } |
184 | 185 | ||
185 | thread_usleep(20000); |
186 | thread_usleep(20000); |
186 | waitq_wakeup(&can_start, WAKEUP_ALL); |
187 | waitq_wakeup(&can_start, WAKEUP_ALL); |
187 | 188 | ||
188 | while (atomic_get(&thread_count) > 0) { |
189 | while (atomic_get(&thread_count) > 0) { |
189 | if (!quiet) |
190 | if (!quiet) |
190 | printf("Threads left: %ld\n", atomic_get(&thread_count)); |
191 | printf("Threads left: %ld\n", atomic_get(&thread_count)); |
191 | thread_sleep(1); |
192 | thread_sleep(1); |
192 | } |
193 | } |
193 | 194 | ||
194 | if (atomic_get(&threads_fault) == 0) |
195 | if (atomic_get(&threads_fault) == 0) |
195 | return NULL; |
196 | return NULL; |
196 | 197 | ||
197 | return "Test failed"; |
198 | return "Test failed"; |
198 | } |
199 | } |
199 | 200 |