[f761f1eb] | 1 | /*
|
---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
| 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 |
|
---|
| 46 | static rwlock_t rwlock;
|
---|
[7e13972] | 47 | static atomic_t threads_fault;
|
---|
[f761f1eb] | 48 |
|
---|
[dff0a94] | 49 | SPINLOCK_INITIALIZE(rw_lock);
|
---|
[f761f1eb] | 50 |
|
---|
| 51 | static waitq_t can_start;
|
---|
| 52 |
|
---|
[70b6de1] | 53 | static uint32_t seed = 0xdeadbeef;
|
---|
[f761f1eb] | 54 |
|
---|
[70b6de1] | 55 | static uint32_t random(uint32_t max)
|
---|
[f761f1eb] | 56 | {
|
---|
[7f1c620] | 57 | uint32_t rc;
|
---|
[f761f1eb] | 58 |
|
---|
[dff0a94] | 59 | spinlock_lock(&rw_lock);
|
---|
[f761f1eb] | 60 | rc = seed % max;
|
---|
| 61 | seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
|
---|
[dff0a94] | 62 | spinlock_unlock(&rw_lock);
|
---|
[f761f1eb] | 63 | return rc;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[70b6de1] | 66 | static void writer(void *arg)
|
---|
[f761f1eb] | 67 | {
|
---|
| 68 | int rc, to;
|
---|
[8d6d76a] | 69 | thread_detach(THREAD);
|
---|
[f761f1eb] | 70 | waitq_sleep(&can_start);
|
---|
| 71 |
|
---|
| 72 | to = random(40000);
|
---|
[43114c5] | 73 | printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[f761f1eb] | 74 | rc = rwlock_write_lock_timeout(&rwlock, to);
|
---|
| 75 | if (SYNCH_FAILED(rc)) {
|
---|
[43114c5] | 76 | printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 77 | return;
|
---|
[96348adc] | 78 | }
|
---|
[43114c5] | 79 | printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 80 |
|
---|
[7e13972] | 81 | if (rwlock.readers_in) {
|
---|
| 82 | printf("Oops.");
|
---|
| 83 | atomic_inc(&threads_fault);
|
---|
| 84 | return;
|
---|
| 85 | }
|
---|
[f761f1eb] | 86 | thread_usleep(random(1000000));
|
---|
[7e13972] | 87 | if (rwlock.readers_in) {
|
---|
| 88 | printf("Oops.");
|
---|
| 89 | atomic_inc(&threads_fault);
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
[f761f1eb] | 92 |
|
---|
| 93 | rwlock_write_unlock(&rwlock);
|
---|
[43114c5] | 94 | printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 95 | }
|
---|
| 96 |
|
---|
[70b6de1] | 97 | static void reader(void *arg)
|
---|
[f761f1eb] | 98 | {
|
---|
| 99 | int rc, to;
|
---|
[8d6d76a] | 100 | thread_detach(THREAD);
|
---|
[f761f1eb] | 101 | waitq_sleep(&can_start);
|
---|
| 102 |
|
---|
| 103 | to = random(2000);
|
---|
[43114c5] | 104 | printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
|
---|
[f761f1eb] | 105 | rc = rwlock_read_lock_timeout(&rwlock, to);
|
---|
| 106 | if (SYNCH_FAILED(rc)) {
|
---|
[43114c5] | 107 | printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 108 | return;
|
---|
| 109 | }
|
---|
[43114c5] | 110 | printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 111 | thread_usleep(30000);
|
---|
| 112 | rwlock_read_unlock(&rwlock);
|
---|
[43114c5] | 113 | printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);
|
---|
[f761f1eb] | 114 | }
|
---|
| 115 |
|
---|
[95155b0c] | 116 | char * test_rwlock4(bool quiet)
|
---|
[f761f1eb] | 117 | {
|
---|
| 118 | context_t ctx;
|
---|
[7f1c620] | 119 | uint32_t i, k;
|
---|
[f761f1eb] | 120 |
|
---|
| 121 | waitq_initialize(&can_start);
|
---|
| 122 | rwlock_initialize(&rwlock);
|
---|
[7e13972] | 123 | atomic_set(&threads_fault, 0);
|
---|
[f761f1eb] | 124 |
|
---|
[96348adc] | 125 | thread_t *thrd;
|
---|
| 126 |
|
---|
| 127 | context_save(&ctx);
|
---|
| 128 | printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
|
---|
| 129 |
|
---|
| 130 | k = random(7) + 1;
|
---|
| 131 | printf("Creating %d readers\n", k);
|
---|
| 132 | for (i = 0; i < k; i++) {
|
---|
[62b6d17] | 133 | thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
|
---|
[96348adc] | 134 | if (thrd)
|
---|
| 135 | thread_ready(thrd);
|
---|
| 136 | else
|
---|
| 137 | printf("Could not create reader %d\n", i);
|
---|
| 138 | }
|
---|
[f761f1eb] | 139 |
|
---|
[96348adc] | 140 | k = random(5) + 1;
|
---|
| 141 | printf("Creating %d writers\n", k);
|
---|
[7e13972] | 142 | for (i = 0; i < k; i++) {
|
---|
[62b6d17] | 143 | thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
|
---|
[96348adc] | 144 | if (thrd)
|
---|
| 145 | thread_ready(thrd);
|
---|
| 146 | else
|
---|
| 147 | printf("Could not create writer %d\n", i);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | thread_usleep(20000);
|
---|
| 151 | waitq_wakeup(&can_start, WAKEUP_ALL);
|
---|
| 152 |
|
---|
[7e13972] | 153 | if (atomic_get(&threads_fault) == 0)
|
---|
| 154 | return NULL;
|
---|
| 155 |
|
---|
| 156 | return "Test failed";
|
---|
[f761f1eb] | 157 | }
|
---|