Rev 2050 | Rev 2071 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2050 | Rev 2053 | ||
|---|---|---|---|
| Line 41... | Line 41... | ||
| 41 | #include <synch/spinlock.h> |
41 | #include <synch/spinlock.h> |
| 42 | 42 | ||
| 43 | #define READERS 50 |
43 | #define READERS 50 |
| 44 | #define WRITERS 50 |
44 | #define WRITERS 50 |
| 45 | 45 | ||
| - | 46 | static atomic_t thread_count; |
|
| 46 | static rwlock_t rwlock; |
47 | static rwlock_t rwlock; |
| 47 | static atomic_t threads_fault; |
48 | static atomic_t threads_fault; |
| - | 49 | static bool sh_quiet; |
|
| 48 | 50 | ||
| 49 | SPINLOCK_INITIALIZE(rw_lock); |
51 | SPINLOCK_INITIALIZE(rw_lock); |
| 50 | 52 | ||
| 51 | static waitq_t can_start; |
53 | static waitq_t can_start; |
| 52 | 54 | ||
| Line 68... | Line 70... | ||
| 68 | int rc, to; |
70 | int rc, to; |
| 69 | thread_detach(THREAD); |
71 | thread_detach(THREAD); |
| 70 | waitq_sleep(&can_start); |
72 | waitq_sleep(&can_start); |
| 71 | 73 | ||
| 72 | to = random(40000); |
74 | to = random(40000); |
| - | 75 | ||
| - | 76 | if (!sh_quiet) |
|
| 73 | printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to); |
77 | printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to); |
| - | 78 | ||
| 74 | rc = rwlock_write_lock_timeout(&rwlock, to); |
79 | rc = rwlock_write_lock_timeout(&rwlock, to); |
| 75 | if (SYNCH_FAILED(rc)) { |
80 | if (SYNCH_FAILED(rc)) { |
| - | 81 | if (!sh_quiet) |
|
| 76 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
82 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
| - | 83 | atomic_dec(&thread_count); |
|
| 77 | return; |
84 | return; |
| 78 | } |
85 | } |
| - | 86 | ||
| - | 87 | if (!sh_quiet) |
|
| 79 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
88 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
| 80 | 89 | ||
| 81 | if (rwlock.readers_in) { |
90 | if (rwlock.readers_in) { |
| - | 91 | if (!sh_quiet) |
|
| 82 | printf("Oops."); |
92 | printf("Oops."); |
| 83 | atomic_inc(&threads_fault); |
93 | atomic_inc(&threads_fault); |
| - | 94 | atomic_dec(&thread_count); |
|
| 84 | return; |
95 | return; |
| 85 | } |
96 | } |
| 86 | thread_usleep(random(1000000)); |
97 | thread_usleep(random(1000000)); |
| 87 | if (rwlock.readers_in) { |
98 | if (rwlock.readers_in) { |
| - | 99 | if (!sh_quiet) |
|
| 88 | printf("Oops."); |
100 | printf("Oops."); |
| 89 | atomic_inc(&threads_fault); |
101 | atomic_inc(&threads_fault); |
| - | 102 | atomic_dec(&thread_count); |
|
| 90 | return; |
103 | return; |
| 91 | } |
104 | } |
| 92 | 105 | ||
| 93 | rwlock_write_unlock(&rwlock); |
106 | rwlock_write_unlock(&rwlock); |
| - | 107 | ||
| - | 108 | if (!sh_quiet) |
|
| 94 | printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid); |
109 | printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid); |
| - | 110 | atomic_dec(&thread_count); |
|
| 95 | } |
111 | } |
| 96 | 112 | ||
| 97 | static void reader(void *arg) |
113 | static void reader(void *arg) |
| 98 | { |
114 | { |
| 99 | int rc, to; |
115 | int rc, to; |
| 100 | thread_detach(THREAD); |
116 | thread_detach(THREAD); |
| 101 | waitq_sleep(&can_start); |
117 | waitq_sleep(&can_start); |
| 102 | 118 | ||
| 103 | to = random(2000); |
119 | to = random(2000); |
| - | 120 | ||
| - | 121 | if (!sh_quiet) |
|
| 104 | printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to); |
122 | printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to); |
| - | 123 | ||
| 105 | rc = rwlock_read_lock_timeout(&rwlock, to); |
124 | rc = rwlock_read_lock_timeout(&rwlock, to); |
| 106 | if (SYNCH_FAILED(rc)) { |
125 | if (SYNCH_FAILED(rc)) { |
| - | 126 | if (!sh_quiet) |
|
| 107 | printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid); |
127 | printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid); |
| - | 128 | atomic_dec(&thread_count); |
|
| 108 | return; |
129 | return; |
| 109 | } |
130 | } |
| - | 131 | ||
| - | 132 | if (!sh_quiet) |
|
| 110 | printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid); |
133 | printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid); |
| - | 134 | ||
| 111 | thread_usleep(30000); |
135 | thread_usleep(30000); |
| 112 | rwlock_read_unlock(&rwlock); |
136 | rwlock_read_unlock(&rwlock); |
| - | 137 | ||
| - | 138 | if (!sh_quiet) |
|
| 113 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
139 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
| - | 140 | atomic_dec(&thread_count); |
|
| 114 | } |
141 | } |
| 115 | 142 | ||
| 116 | char * test_rwlock4(bool quiet) |
143 | char * test_rwlock4(bool quiet) |
| 117 | { |
144 | { |
| 118 | context_t ctx; |
145 | context_t ctx; |
| 119 | uint32_t i, k; |
146 | uint32_t i; |
| - | 147 | sh_quiet = quiet; |
|
| 120 | 148 | ||
| 121 | waitq_initialize(&can_start); |
149 | waitq_initialize(&can_start); |
| 122 | rwlock_initialize(&rwlock); |
150 | rwlock_initialize(&rwlock); |
| 123 | atomic_set(&threads_fault, 0); |
151 | atomic_set(&threads_fault, 0); |
| 124 | 152 | ||
| - | 153 | uint32_t rd = random(7) + 1; |
|
| - | 154 | uint32_t wr = random(5) + 1; |
|
| - | 155 | ||
| - | 156 | atomic_set(&thread_count, rd + wr); |
|
| - | 157 | ||
| 125 | thread_t *thrd; |
158 | thread_t *thrd; |
| 126 | 159 | ||
| 127 | context_save(&ctx); |
160 | context_save(&ctx); |
| - | 161 | if (!quiet) { |
|
| 128 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
162 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
| - | 163 | printf("Creating %d readers\n", rd); |
|
| - | 164 | } |
|
| 129 | 165 | ||
| 130 | k = random(7) + 1; |
- | |
| 131 | printf("Creating %d readers\n", k); |
- | |
| 132 | for (i = 0; i < k; i++) { |
166 | for (i = 0; i < rd; i++) { |
| 133 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
167 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false); |
| 134 | if (thrd) |
168 | if (thrd) |
| 135 | thread_ready(thrd); |
169 | thread_ready(thrd); |
| 136 | else |
170 | else if (!quiet) |
| 137 | printf("Could not create reader %d\n", i); |
171 | printf("Could not create reader %d\n", i); |
| 138 | } |
172 | } |
| 139 | 173 | ||
| 140 | k = random(5) + 1; |
174 | if (!quiet) |
| 141 | printf("Creating %d writers\n", k); |
175 | printf("Creating %d writers\n", wr); |
| - | 176 | ||
| 142 | for (i = 0; i < k; i++) { |
177 | for (i = 0; i < wr; i++) { |
| 143 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
178 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false); |
| 144 | if (thrd) |
179 | if (thrd) |
| 145 | thread_ready(thrd); |
180 | thread_ready(thrd); |
| 146 | else |
181 | else if (!quiet) |
| 147 | printf("Could not create writer %d\n", i); |
182 | printf("Could not create writer %d\n", i); |
| 148 | } |
183 | } |
| 149 | 184 | ||
| 150 | thread_usleep(20000); |
185 | thread_usleep(20000); |
| 151 | waitq_wakeup(&can_start, WAKEUP_ALL); |
186 | waitq_wakeup(&can_start, WAKEUP_ALL); |
| 152 | 187 | ||
| - | 188 | while (atomic_get(&thread_count) > 0) { |
|
| - | 189 | if (!quiet) |
|
| - | 190 | printf("Threads left: %d\n", atomic_get(&thread_count)); |
|
| - | 191 | thread_sleep(1); |
|
| - | 192 | } |
|
| - | 193 | ||
| 153 | if (atomic_get(&threads_fault) == 0) |
194 | if (atomic_get(&threads_fault) == 0) |
| 154 | return NULL; |
195 | return NULL; |
| 155 | 196 | ||
| 156 | return "Test failed"; |
197 | return "Test failed"; |
| 157 | } |
198 | } |