Rev 2028 | Rev 2042 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2028 | Rev 2029 | ||
|---|---|---|---|
| Line 32... | Line 32... | ||
| 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 | ||
| Line 76... | Line 76... | ||
| 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 | ||
| Line 110... | Line 118... | ||
| 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); |
| Line 128... | Line 137... | ||
| 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 | } |