Rev 501 | Rev 534 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 501 | Rev 532 | ||
---|---|---|---|
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 <arch/atomic.h> |
31 | #include <arch/atomic.h> |
32 | #include <print.h> |
32 | #include <print.h> |
33 | #include <proc/thread.h> |
33 | #include <proc/thread.h> |
34 | #include <arch/types.h> |
34 | #include <arch/types.h> |
35 | #include <arch/context.h> |
35 | #include <arch/context.h> |
36 | #include <context.h> |
36 | #include <context.h> |
37 | #include <panic.h> |
37 | #include <panic.h> |
38 | 38 | ||
39 | #include <synch/waitq.h> |
39 | #include <synch/waitq.h> |
40 | #include <synch/rwlock.h> |
40 | #include <synch/rwlock.h> |
41 | #include <synch/synch.h> |
41 | #include <synch/synch.h> |
42 | 42 | ||
43 | #define READERS 50 |
43 | #define READERS 50 |
44 | #define WRITERS 50 |
44 | #define WRITERS 50 |
45 | 45 | ||
46 | static rwlock_t rwlock; |
46 | static rwlock_t rwlock; |
47 | 47 | ||
48 | static spinlock_t lock; |
48 | static spinlock_t lock; |
49 | 49 | ||
50 | static waitq_t can_start; |
50 | static waitq_t can_start; |
51 | 51 | ||
52 | __u32 seed = 0xdeadbeef; |
52 | __u32 seed = 0xdeadbeef; |
53 | 53 | ||
54 | static __u32 random(__u32 max); |
54 | static __u32 random(__u32 max); |
55 | 55 | ||
56 | static void writer(void *arg); |
56 | static void writer(void *arg); |
57 | static void reader(void *arg); |
57 | static void reader(void *arg); |
58 | static void failed(void); |
58 | static void failed(void); |
59 | 59 | ||
60 | __u32 random(__u32 max) |
60 | __u32 random(__u32 max) |
61 | { |
61 | { |
62 | __u32 rc; |
62 | __u32 rc; |
63 | ipl_t ipl; |
63 | ipl_t ipl; |
64 | 64 | ||
65 | spinlock_lock(&lock); |
65 | spinlock_lock(&lock); |
66 | rc = seed % max; |
66 | rc = seed % max; |
67 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
67 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
68 | spinlock_unlock(&lock); |
68 | spinlock_unlock(&lock); |
69 | return rc; |
69 | return rc; |
70 | } |
70 | } |
71 | 71 | ||
72 | 72 | ||
73 | void writer(void *arg) |
73 | void writer(void *arg) |
74 | { |
74 | { |
75 | int rc, to; |
75 | int rc, to; |
76 | waitq_sleep(&can_start); |
76 | waitq_sleep(&can_start); |
77 | 77 | ||
78 | to = random(40000); |
78 | to = random(40000); |
79 | printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to); |
79 | printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to); |
80 | rc = rwlock_write_lock_timeout(&rwlock, to); |
80 | rc = rwlock_write_lock_timeout(&rwlock, to); |
81 | if (SYNCH_FAILED(rc)) { |
81 | if (SYNCH_FAILED(rc)) { |
82 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
82 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
83 | return; |
83 | return; |
84 | }; |
84 | }; |
85 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
85 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
86 | 86 | ||
87 | if (rwlock.readers_in) panic("Oops."); |
87 | if (rwlock.readers_in) panic("Oops."); |
88 | thread_usleep(random(1000000)); |
88 | thread_usleep(random(1000000)); |
89 | if (rwlock.readers_in) panic("Oops."); |
89 | if (rwlock.readers_in) panic("Oops."); |
90 | 90 | ||
91 | rwlock_write_unlock(&rwlock); |
91 | rwlock_write_unlock(&rwlock); |
92 | printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid); |
92 | printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid); |
93 | } |
93 | } |
94 | 94 | ||
95 | void reader(void *arg) |
95 | void reader(void *arg) |
96 | { |
96 | { |
97 | int rc, to; |
97 | int rc, to; |
98 | waitq_sleep(&can_start); |
98 | waitq_sleep(&can_start); |
99 | 99 | ||
100 | to = random(2000); |
100 | to = random(2000); |
101 | printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to); |
101 | printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to); |
102 | rc = rwlock_read_lock_timeout(&rwlock, to); |
102 | rc = rwlock_read_lock_timeout(&rwlock, to); |
103 | if (SYNCH_FAILED(rc)) { |
103 | if (SYNCH_FAILED(rc)) { |
104 | printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid); |
104 | printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid); |
105 | return; |
105 | return; |
106 | } |
106 | } |
107 | printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid); |
107 | printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid); |
108 | thread_usleep(30000); |
108 | thread_usleep(30000); |
109 | rwlock_read_unlock(&rwlock); |
109 | rwlock_read_unlock(&rwlock); |
110 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
110 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
111 | } |
111 | } |
112 | 112 | ||
113 | void failed(void) |
113 | void failed(void) |
114 | { |
114 | { |
115 | printf("Test failed prematurely.\n"); |
115 | printf("Test failed prematurely.\n"); |
116 | thread_exit(); |
116 | thread_exit(); |
117 | } |
117 | } |
118 | 118 | ||
119 | void test(void) |
119 | void test(void) |
120 | { |
120 | { |
121 | context_t ctx; |
121 | context_t ctx; |
122 | __u32 i, k; |
122 | __u32 i, k; |
123 | 123 | ||
124 | printf("Read/write locks test #4\n"); |
124 | printf("Read/write locks test #4\n"); |
125 | 125 | ||
126 | waitq_initialize(&can_start); |
126 | waitq_initialize(&can_start); |
127 | rwlock_initialize(&rwlock); |
127 | rwlock_initialize(&rwlock); |
128 | 128 | ||
129 | 129 | ||
130 | 130 | ||
131 | for (; ;) { |
131 | for (; ;) { |
132 | thread_t *thrd; |
132 | thread_t *thrd; |
133 | 133 | ||
134 | context_save(&ctx); |
134 | context_save(&ctx); |
135 | printf("sp=%X, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
135 | printf("sp=%X, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
136 | 136 | ||
137 | k = random(7) + 1; |
137 | k = random(7) + 1; |
138 | printf("Creating %d readers\n", k); |
138 | printf("Creating %d readers\n", k); |
139 | for (i=0; i<k; i++) { |
139 | for (i=0; i<k; i++) { |
140 | thrd = thread_create(reader, NULL, TASK, 0); |
140 | thrd = thread_create(reader, NULL, TASK, 0); |
141 | if (thrd) |
141 | if (thrd) |
142 | thread_ready(thrd); |
142 | thread_ready(thrd); |
143 | else |
143 | else |
144 | failed(); |
144 | failed(); |
145 | } |
145 | } |
146 | 146 | ||
147 | k = random(5) + 1; |
147 | k = random(5) + 1; |
148 | printf("Creating %d writers\n", k); |
148 | printf("Creating %d writers\n", k); |
149 | for (i=0; i<k; i++) { |
149 | for (i=0; i<k; i++) { |
150 | thrd = thread_create(writer, NULL, TASK, 0); |
150 | thrd = thread_create(writer, NULL, TASK, 0); |
151 | if (thrd) |
151 | if (thrd) |
152 | thread_ready(thrd); |
152 | thread_ready(thrd); |
153 | else |
153 | else |
154 | failed(); |
154 | failed(); |
155 | } |
155 | } |
156 | 156 | ||
157 | thread_usleep(20000); |
157 | thread_usleep(20000); |
158 | waitq_wakeup(&can_start, WAKEUP_ALL); |
158 | waitq_wakeup(&can_start, WAKEUP_ALL); |
159 | } |
159 | } |
160 | 160 | ||
161 | } |
161 | } |
162 | 162 |