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