[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 |
|
---|
[cb01e1e] | 43 | #define READERS 50
|
---|
| 44 | #define WRITERS 50
|
---|
[f761f1eb] | 45 |
|
---|
[deada67] | 46 | static atomic_t thread_count;
|
---|
[f761f1eb] | 47 | static rwlock_t rwlock;
|
---|
[7e13972] | 48 | static atomic_t threads_fault;
|
---|
[f761f1eb] | 49 |
|
---|
[dff0a94] | 50 | SPINLOCK_INITIALIZE(rw_lock);
|
---|
[f761f1eb] | 51 |
|
---|
| 52 | static waitq_t can_start;
|
---|
| 53 |
|
---|
[70b6de1] | 54 | static uint32_t seed = 0xdeadbeef;
|
---|
[f761f1eb] | 55 |
|
---|
[70b6de1] | 56 | static uint32_t random(uint32_t max)
|
---|
[f761f1eb] | 57 | {
|
---|
[7f1c620] | 58 | uint32_t rc;
|
---|
[cb01e1e] | 59 |
|
---|
| 60 | spinlock_lock(&rw_lock);
|
---|
[f761f1eb] | 61 | rc = seed % max;
|
---|
[53634f9] | 62 | seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc;
|
---|
[dff0a94] | 63 | spinlock_unlock(&rw_lock);
|
---|
[f761f1eb] | 64 | return rc;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[70b6de1] | 67 | static void writer(void *arg)
|
---|
[f761f1eb] | 68 | {
|
---|
| 69 | int rc, to;
|
---|
[8d6d76a] | 70 | thread_detach(THREAD);
|
---|
[f761f1eb] | 71 | waitq_sleep(&can_start);
|
---|
[cb01e1e] | 72 |
|
---|
[f761f1eb] | 73 | to = random(40000);
|
---|
[deada67] | 74 |
|
---|
[cb01e1e] | 75 | TPRINTF("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[deada67] | 76 |
|
---|
[f761f1eb] | 77 | rc = rwlock_write_lock_timeout(&rwlock, to);
|
---|
| 78 | if (SYNCH_FAILED(rc)) {
|
---|
[cb01e1e] | 79 | TPRINTF("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 80 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 81 | return;
|
---|
[96348adc] | 82 | }
|
---|
[deada67] | 83 |
|
---|
[cb01e1e] | 84 | TPRINTF("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid);
|
---|
| 85 |
|
---|
[7e13972] | 86 | if (rwlock.readers_in) {
|
---|
[cb01e1e] | 87 | TPRINTF("Oops.\n");
|
---|
[7e13972] | 88 | atomic_inc(&threads_fault);
|
---|
[deada67] | 89 | atomic_dec(&thread_count);
|
---|
[7e13972] | 90 | return;
|
---|
| 91 | }
|
---|
[cb01e1e] | 92 |
|
---|
[f761f1eb] | 93 | thread_usleep(random(1000000));
|
---|
[cb01e1e] | 94 |
|
---|
[7e13972] | 95 | if (rwlock.readers_in) {
|
---|
[cb01e1e] | 96 | TPRINTF("Oops.\n");
|
---|
[7e13972] | 97 | atomic_inc(&threads_fault);
|
---|
[deada67] | 98 | atomic_dec(&thread_count);
|
---|
[7e13972] | 99 | return;
|
---|
| 100 | }
|
---|
[cb01e1e] | 101 |
|
---|
[f761f1eb] | 102 | rwlock_write_unlock(&rwlock);
|
---|
[deada67] | 103 |
|
---|
[cb01e1e] | 104 | TPRINTF("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 105 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 106 | }
|
---|
| 107 |
|
---|
[70b6de1] | 108 | static void reader(void *arg)
|
---|
[f761f1eb] | 109 | {
|
---|
| 110 | int rc, to;
|
---|
[8d6d76a] | 111 | thread_detach(THREAD);
|
---|
[f761f1eb] | 112 | waitq_sleep(&can_start);
|
---|
| 113 |
|
---|
| 114 | to = random(2000);
|
---|
[deada67] | 115 |
|
---|
[cb01e1e] | 116 | TPRINTF("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[deada67] | 117 |
|
---|
[f761f1eb] | 118 | rc = rwlock_read_lock_timeout(&rwlock, to);
|
---|
| 119 | if (SYNCH_FAILED(rc)) {
|
---|
[cb01e1e] | 120 | TPRINTF("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 121 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 122 | return;
|
---|
| 123 | }
|
---|
[deada67] | 124 |
|
---|
[cb01e1e] | 125 | TPRINTF("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 126 |
|
---|
[f761f1eb] | 127 | thread_usleep(30000);
|
---|
| 128 | rwlock_read_unlock(&rwlock);
|
---|
[deada67] | 129 |
|
---|
[cb01e1e] | 130 | TPRINTF("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid);
|
---|
[deada67] | 131 | atomic_dec(&thread_count);
|
---|
[f761f1eb] | 132 | }
|
---|
| 133 |
|
---|
[a000878c] | 134 | const char *test_rwlock4(void)
|
---|
[f761f1eb] | 135 | {
|
---|
| 136 | context_t ctx;
|
---|
[deada67] | 137 | uint32_t i;
|
---|
[f761f1eb] | 138 |
|
---|
| 139 | waitq_initialize(&can_start);
|
---|
| 140 | rwlock_initialize(&rwlock);
|
---|
[7e13972] | 141 | atomic_set(&threads_fault, 0);
|
---|
[f761f1eb] | 142 |
|
---|
[deada67] | 143 | uint32_t rd = random(7) + 1;
|
---|
| 144 | uint32_t wr = random(5) + 1;
|
---|
| 145 |
|
---|
| 146 | atomic_set(&thread_count, rd + wr);
|
---|
| 147 |
|
---|
[96348adc] | 148 | thread_t *thrd;
|
---|
| 149 |
|
---|
| 150 | context_save(&ctx);
|
---|
[98000fb] | 151 | TPRINTF("sp=%#x, readers_in=%" PRIs "\n", ctx.sp, rwlock.readers_in);
|
---|
[cb01e1e] | 152 | TPRINTF("Creating %" PRIu32 " readers\n", rd);
|
---|
[96348adc] | 153 |
|
---|
[deada67] | 154 | for (i = 0; i < rd; i++) {
|
---|
[62b6d17] | 155 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
|
---|
[96348adc] | 156 | if (thrd)
|
---|
| 157 | thread_ready(thrd);
|
---|
[cb01e1e] | 158 | else
|
---|
| 159 | TPRINTF("Could not create reader %" PRIu32 "\n", i);
|
---|
[96348adc] | 160 | }
|
---|
[cb01e1e] | 161 |
|
---|
| 162 | TPRINTF("Creating %" PRIu32 " writers\n", wr);
|
---|
[deada67] | 163 |
|
---|
| 164 | for (i = 0; i < wr; i++) {
|
---|
[62b6d17] | 165 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
|
---|
[96348adc] | 166 | if (thrd)
|
---|
| 167 | thread_ready(thrd);
|
---|
[cb01e1e] | 168 | else
|
---|
| 169 | TPRINTF("Could not create writer %" PRIu32 "\n", i);
|
---|
[96348adc] | 170 | }
|
---|
| 171 |
|
---|
| 172 | thread_usleep(20000);
|
---|
| 173 | waitq_wakeup(&can_start, WAKEUP_ALL);
|
---|
| 174 |
|
---|
[deada67] | 175 | while (atomic_get(&thread_count) > 0) {
|
---|
[cb01e1e] | 176 | TPRINTF("Threads left: %ld\n", atomic_get(&thread_count));
|
---|
[deada67] | 177 | thread_sleep(1);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[7e13972] | 180 | if (atomic_get(&threads_fault) == 0)
|
---|
| 181 | return NULL;
|
---|
| 182 |
|
---|
| 183 | return "Test failed";
|
---|
[f761f1eb] | 184 | }
|
---|