Rev 2024 | Rev 2029 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2024 | Rev 2028 | ||
|---|---|---|---|
| Line 50... | Line 50... | ||
| 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); |
- | |
| 56 | - | ||
| 57 | static void writer(void *arg); |
- | |
| 58 | static void reader(void *arg); |
- | |
| 59 | static void failed(void); |
- | |
| 60 | - | ||
| 61 | static uint32_t random(uint32_t max) |
55 | static uint32_t random(uint32_t max) |
| 62 | { |
56 | { |
| 63 | uint32_t rc; |
57 | uint32_t rc; |
| 64 | 58 | ||
| 65 | spinlock_lock(&rw_lock); |
59 | spinlock_lock(&rw_lock); |
| Line 67... | Line 61... | ||
| 67 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
61 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc; |
| 68 | spinlock_unlock(&rw_lock); |
62 | spinlock_unlock(&rw_lock); |
| 69 | return rc; |
63 | return rc; |
| 70 | } |
64 | } |
| 71 | 65 | ||
| 72 | - | ||
| 73 | static void writer(void *arg) |
66 | static void writer(void *arg) |
| 74 | { |
67 | { |
| 75 | int rc, to; |
68 | int rc, to; |
| 76 | thread_detach(THREAD); |
69 | thread_detach(THREAD); |
| 77 | waitq_sleep(&can_start); |
70 | waitq_sleep(&can_start); |
| Line 80... | Line 73... | ||
| 80 | 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); |
| 81 | rc = rwlock_write_lock_timeout(&rwlock, to); |
74 | rc = rwlock_write_lock_timeout(&rwlock, to); |
| 82 | if (SYNCH_FAILED(rc)) { |
75 | if (SYNCH_FAILED(rc)) { |
| 83 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
76 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid); |
| 84 | return; |
77 | return; |
| 85 | }; |
78 | } |
| 86 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
79 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid); |
| 87 | 80 | ||
| 88 | if (rwlock.readers_in) panic("Oops."); |
81 | if (rwlock.readers_in) panic("Oops."); |
| 89 | thread_usleep(random(1000000)); |
82 | thread_usleep(random(1000000)); |
| 90 | if (rwlock.readers_in) panic("Oops."); |
83 | if (rwlock.readers_in) panic("Oops."); |
| Line 110... | Line 103... | ||
| 110 | thread_usleep(30000); |
103 | thread_usleep(30000); |
| 111 | rwlock_read_unlock(&rwlock); |
104 | rwlock_read_unlock(&rwlock); |
| 112 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
105 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid); |
| 113 | } |
106 | } |
| 114 | 107 | ||
| 115 | static void failed(void) |
- | |
| 116 | { |
- | |
| 117 | printf("Test failed prematurely.\n"); |
- | |
| 118 | thread_exit(); |
- | |
| 119 | } |
- | |
| 120 | - | ||
| 121 | void test_rwlock4(void) |
108 | char * test_rwlock4(void) |
| 122 | { |
109 | { |
| 123 | context_t ctx; |
110 | context_t ctx; |
| 124 | uint32_t i, k; |
111 | uint32_t i, k; |
| 125 | 112 | ||
| 126 | printf("Read/write locks test #4\n"); |
- | |
| 127 | - | ||
| 128 | waitq_initialize(&can_start); |
113 | waitq_initialize(&can_start); |
| 129 | rwlock_initialize(&rwlock); |
114 | rwlock_initialize(&rwlock); |
| 130 | 115 | ||
| 131 | for (;;) { |
- | |
| 132 | thread_t *thrd; |
116 | thread_t *thrd; |
| 133 | 117 | ||
| 134 | context_save(&ctx); |
118 | context_save(&ctx); |
| 135 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
119 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in); |
| 136 | 120 | ||
| 137 | k = random(7) + 1; |
121 | k = random(7) + 1; |
| 138 | printf("Creating %d readers\n", k); |
122 | printf("Creating %d readers\n", k); |
| 139 | for (i=0; i<k; i++) { |
123 | for (i = 0; i < k; i++) { |
| 140 | thrd = thread_create(reader, NULL, TASK, 0, "reader"); |
124 | thrd = thread_create(reader, NULL, TASK, 0, "reader"); |
| 141 | if (thrd) |
125 | if (thrd) |
| 142 | thread_ready(thrd); |
126 | thread_ready(thrd); |
| 143 | else |
127 | else |
| 144 | failed(); |
- | |
| 145 | } |
- | |
| 146 | - | ||
| 147 | k = random(5) + 1; |
- | |
| 148 | printf("Creating %d writers\n", k); |
128 | printf("Could not create reader %d\n", i); |
| 149 | for (i=0; i<k; i++) { |
- | |
| 150 | thrd = thread_create(writer, NULL, TASK, 0, "writer"); |
- | |
| 151 | if (thrd) |
- | |
| 152 | thread_ready(thrd); |
- | |
| 153 | else |
- | |
| 154 | failed(); |
- | |
| 155 | } |
129 | } |
| 156 | - | ||
| 157 | thread_usleep(20000); |
- | |
| 158 | waitq_wakeup(&can_start, WAKEUP_ALL); |
- | |
| 159 | } |
- | |
| 160 | 130 | ||
| - | 131 | k = random(5) + 1; |
|
| - | 132 | printf("Creating %d writers\n", k); |
|
| - | 133 | for (i=0; i<k; i++) { |
|
| - | 134 | thrd = thread_create(writer, NULL, TASK, 0, "writer"); |
|
| - | 135 | if (thrd) |
|
| - | 136 | thread_ready(thrd); |
|
| - | 137 | else |
|
| - | 138 | printf("Could not create writer %d\n", i); |
|
| - | 139 | } |
|
| - | 140 | ||
| - | 141 | thread_usleep(20000); |
|
| - | 142 | waitq_wakeup(&can_start, WAKEUP_ALL); |
|
| - | 143 | ||
| - | 144 | return NULL; |
|
| 161 | } |
145 | } |