[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <test.h>
|
---|
| 30 | #include <arch.h>
|
---|
[23684b7] | 31 | #include <atomic.h>
|
---|
[f761f1eb] | 32 | #include <print.h>
|
---|
| 33 | #include <proc/thread.h>
|
---|
| 34 | #include <arch/types.h>
|
---|
| 35 | #include <arch/context.h>
|
---|
[9756131] | 36 | #include <context.h>
|
---|
[f761f1eb] | 37 |
|
---|
| 38 | #include <synch/waitq.h>
|
---|
| 39 | #include <synch/rwlock.h>
|
---|
| 40 | #include <synch/synch.h>
|
---|
[dc747e3] | 41 | #include <synch/spinlock.h>
|
---|
[f761f1eb] | 42 |
|
---|
| 43 | #define READERS 50
|
---|
| 44 | #define WRITERS 50
|
---|
| 45 |
|
---|
[deada67] | 46 | static atomic_t thread_count;
|
---|
[f761f1eb] | 47 | static rwlock_t rwlock;
|
---|
[7e13972] | 48 | static atomic_t threads_fault;
|
---|
[deada67] | 49 | static bool sh_quiet;
|
---|
[f761f1eb] | 50 |
|
---|
[dff0a94] | 51 | SPINLOCK_INITIALIZE(rw_lock);
|
---|
[f761f1eb] | 52 |
|
---|
| 53 | static waitq_t can_start;
|
---|
| 54 |
|
---|
[70b6de1] | 55 | static uint32_t seed = 0xdeadbeef;
|
---|
[f761f1eb] | 56 |
|
---|
[70b6de1] | 57 | static uint32_t random(uint32_t max)
|
---|
[f761f1eb] | 58 | {
|
---|
[7f1c620] | 59 | uint32_t rc;
|
---|
[f761f1eb] | 60 |
|
---|
[dff0a94] | 61 | spinlock_lock(&rw_lock);
|
---|
[f761f1eb] | 62 | rc = seed % max;
|
---|
| 63 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
|
---|
[dff0a94] | 64 | spinlock_unlock(&rw_lock);
|
---|
[f761f1eb] | 65 | return rc;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[70b6de1] | 68 | static void writer(void *arg)
|
---|
[f761f1eb] | 69 | {
|
---|
| 70 | int rc, to;
|
---|
[8d6d76a] | 71 | thread_detach(THREAD);
|
---|
[f761f1eb] | 72 | waitq_sleep(&can_start);
|
---|
| 73 |
|
---|
| 74 | to = random(40000);
|
---|
[deada67] | 75 |
|
---|
| 76 | if (!sh_quiet)
|
---|
[201abde] | 77 | printf("cpu%d, tid %llu w+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[deada67] | 78 |
|
---|
[f761f1eb] | 79 | rc = rwlock_write_lock_timeout(&rwlock, to);
|
---|
| 80 | if (SYNCH_FAILED(rc)) {
|
---|
[deada67] | 81 | if (!sh_quiet)
|
---|
[201abde] | 82 | printf("cpu%d, tid %llu w!\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 83 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 84 | return;
|
---|
[96348adc] | 85 | }
|
---|
[deada67] | 86 |
|
---|
| 87 | if (!sh_quiet)
|
---|
[201abde] | 88 | printf("cpu%d, tid %llu w=\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 89 |
|
---|
[7e13972] | 90 | if (rwlock.readers_in) {
|
---|
[deada67] | 91 | if (!sh_quiet)
|
---|
| 92 | printf("Oops.");
|
---|
[7e13972] | 93 | atomic_inc(&threads_fault);
|
---|
[deada67] | 94 | atomic_dec(&thread_count);
|
---|
[7e13972] | 95 | return;
|
---|
| 96 | }
|
---|
[f761f1eb] | 97 | thread_usleep(random(1000000));
|
---|
[7e13972] | 98 | if (rwlock.readers_in) {
|
---|
[deada67] | 99 | if (!sh_quiet)
|
---|
| 100 | printf("Oops.");
|
---|
[7e13972] | 101 | atomic_inc(&threads_fault);
|
---|
[deada67] | 102 | atomic_dec(&thread_count);
|
---|
[7e13972] | 103 | return;
|
---|
| 104 | }
|
---|
[f761f1eb] | 105 |
|
---|
| 106 | rwlock_write_unlock(&rwlock);
|
---|
[deada67] | 107 |
|
---|
| 108 | if (!sh_quiet)
|
---|
[201abde] | 109 | printf("cpu%d, tid %llu w-\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 110 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 111 | }
|
---|
| 112 |
|
---|
[70b6de1] | 113 | static void reader(void *arg)
|
---|
[f761f1eb] | 114 | {
|
---|
| 115 | int rc, to;
|
---|
[8d6d76a] | 116 | thread_detach(THREAD);
|
---|
[f761f1eb] | 117 | waitq_sleep(&can_start);
|
---|
| 118 |
|
---|
| 119 | to = random(2000);
|
---|
[deada67] | 120 |
|
---|
| 121 | if (!sh_quiet)
|
---|
[201abde] | 122 | printf("cpu%d, tid %llu r+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[deada67] | 123 |
|
---|
[f761f1eb] | 124 | rc = rwlock_read_lock_timeout(&rwlock, to);
|
---|
| 125 | if (SYNCH_FAILED(rc)) {
|
---|
[deada67] | 126 | if (!sh_quiet)
|
---|
[201abde] | 127 | printf("cpu%d, tid %llu r!\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 128 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 129 | return;
|
---|
| 130 | }
|
---|
[deada67] | 131 |
|
---|
| 132 | if (!sh_quiet)
|
---|
[201abde] | 133 | printf("cpu%d, tid %llu r=\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 134 |
|
---|
[f761f1eb] | 135 | thread_usleep(30000);
|
---|
| 136 | rwlock_read_unlock(&rwlock);
|
---|
[deada67] | 137 |
|
---|
| 138 | if (!sh_quiet)
|
---|
[201abde] | 139 | printf("cpu%d, tid %llu r-\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 140 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 141 | }
|
---|
| 142 |
|
---|
[95155b0c] | 143 | char * test_rwlock4(bool quiet)
|
---|
[f761f1eb] | 144 | {
|
---|
| 145 | context_t ctx;
|
---|
[deada67] | 146 | uint32_t i;
|
---|
| 147 | sh_quiet = quiet;
|
---|
[f761f1eb] | 148 |
|
---|
| 149 | waitq_initialize(&can_start);
|
---|
| 150 | rwlock_initialize(&rwlock);
|
---|
[7e13972] | 151 | atomic_set(&threads_fault, 0);
|
---|
[f761f1eb] | 152 |
|
---|
[deada67] | 153 | uint32_t rd = random(7) + 1;
|
---|
| 154 | uint32_t wr = random(5) + 1;
|
---|
| 155 |
|
---|
| 156 | atomic_set(&thread_count, rd + wr);
|
---|
| 157 |
|
---|
[96348adc] | 158 | thread_t *thrd;
|
---|
| 159 |
|
---|
| 160 | context_save(&ctx);
|
---|
[deada67] | 161 | if (!quiet) {
|
---|
| 162 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
|
---|
| 163 | printf("Creating %d readers\n", rd);
|
---|
| 164 | }
|
---|
[96348adc] | 165 |
|
---|
[deada67] | 166 | for (i = 0; i < rd; i++) {
|
---|
[62b6d17] | 167 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
|
---|
[96348adc] | 168 | if (thrd)
|
---|
| 169 | thread_ready(thrd);
|
---|
[deada67] | 170 | else if (!quiet)
|
---|
[96348adc] | 171 | printf("Could not create reader %d\n", i);
|
---|
| 172 | }
|
---|
[f761f1eb] | 173 |
|
---|
[deada67] | 174 | if (!quiet)
|
---|
| 175 | printf("Creating %d writers\n", wr);
|
---|
| 176 |
|
---|
| 177 | for (i = 0; i < wr; i++) {
|
---|
[62b6d17] | 178 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
|
---|
[96348adc] | 179 | if (thrd)
|
---|
| 180 | thread_ready(thrd);
|
---|
[deada67] | 181 | else if (!quiet)
|
---|
[96348adc] | 182 | printf("Could not create writer %d\n", i);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | thread_usleep(20000);
|
---|
| 186 | waitq_wakeup(&can_start, WAKEUP_ALL);
|
---|
| 187 |
|
---|
[deada67] | 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 |
|
---|
[7e13972] | 194 | if (atomic_get(&threads_fault) == 0)
|
---|
| 195 | return NULL;
|
---|
| 196 |
|
---|
| 197 | return "Test failed";
|
---|
[f761f1eb] | 198 | }
|
---|