Subversion Repositories HelenOS

Rev

Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2001-2004 Jakub Jermar
1 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>
1104 jermar 31
#include <atomic.h>
1 jermar 32
#include <print.h>
33
#include <proc/thread.h>
3862 rimsky 34
#include <arch/asm.h>
1 jermar 35
#include <arch/types.h>
36
#include <arch/context.h>
205 jermar 37
#include <context.h>
1 jermar 38
 
39
#include <synch/waitq.h>
40
#include <synch/rwlock.h>
41
#include <synch/synch.h>
623 jermar 42
#include <synch/spinlock.h>
1 jermar 43
 
44
#define READERS     50
45
#define WRITERS     50
46
 
2053 decky 47
static atomic_t thread_count;
1 jermar 48
static rwlock_t rwlock;
2029 decky 49
static atomic_t threads_fault;
2053 decky 50
static bool sh_quiet;
1 jermar 51
 
2024 decky 52
SPINLOCK_INITIALIZE(rw_lock);
1 jermar 53
 
54
static waitq_t can_start;
55
 
2022 decky 56
static uint32_t seed = 0xdeadbeef;
1 jermar 57
 
2022 decky 58
static uint32_t random(uint32_t max)
1 jermar 59
{
1780 jermar 60
    uint32_t rc;
1 jermar 61
 
2024 decky 62
    spinlock_lock(&rw_lock);   
1 jermar 63
    rc = seed % max;
64
    seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
2024 decky 65
    spinlock_unlock(&rw_lock);
1 jermar 66
    return rc;
67
}
68
 
2022 decky 69
static void writer(void *arg)
1 jermar 70
{
71
    int rc, to;
1658 vana 72
    thread_detach(THREAD);
1 jermar 73
    waitq_sleep(&can_start);
74
 
75
    to = random(40000);
2053 decky 76
 
77
    if (!sh_quiet)
3069 decky 78
        printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to);
2053 decky 79
 
1 jermar 80
    rc = rwlock_write_lock_timeout(&rwlock, to);
81
    if (SYNCH_FAILED(rc)) {
2053 decky 82
        if (!sh_quiet)
3069 decky 83
            printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid);
2053 decky 84
        atomic_dec(&thread_count);
1 jermar 85
        return;
2028 decky 86
    }
2053 decky 87
 
88
    if (!sh_quiet)
3069 decky 89
        printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid);
1 jermar 90
 
2029 decky 91
    if (rwlock.readers_in) {
2053 decky 92
        if (!sh_quiet)
93
            printf("Oops.");
2029 decky 94
        atomic_inc(&threads_fault);
2053 decky 95
        atomic_dec(&thread_count);
2029 decky 96
        return;
97
    }
1 jermar 98
    thread_usleep(random(1000000));
2029 decky 99
    if (rwlock.readers_in) {
2053 decky 100
        if (!sh_quiet)
101
            printf("Oops.");   
2029 decky 102
        atomic_inc(&threads_fault);
2053 decky 103
        atomic_dec(&thread_count);
2029 decky 104
        return;
105
    }
1 jermar 106
 
107
    rwlock_write_unlock(&rwlock);
2053 decky 108
 
109
    if (!sh_quiet)
3069 decky 110
        printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid);
2053 decky 111
    atomic_dec(&thread_count);
1 jermar 112
}
113
 
2022 decky 114
static void reader(void *arg)
1 jermar 115
{
116
    int rc, to;
1658 vana 117
    thread_detach(THREAD);
1 jermar 118
    waitq_sleep(&can_start);
119
 
120
    to = random(2000);
2053 decky 121
 
122
    if (!sh_quiet)
3069 decky 123
        printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to);
2053 decky 124
 
1 jermar 125
    rc = rwlock_read_lock_timeout(&rwlock, to);
126
    if (SYNCH_FAILED(rc)) {
2053 decky 127
        if (!sh_quiet)
3069 decky 128
            printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid);
2053 decky 129
        atomic_dec(&thread_count);
1 jermar 130
        return;
131
    }
2053 decky 132
 
133
    if (!sh_quiet)
3069 decky 134
        printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid);
2053 decky 135
 
1 jermar 136
    thread_usleep(30000);
137
    rwlock_read_unlock(&rwlock);
2053 decky 138
 
139
    if (!sh_quiet)
3069 decky 140
        printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid);
2053 decky 141
    atomic_dec(&thread_count);
1 jermar 142
}
143
 
2050 decky 144
char * test_rwlock4(bool quiet)
1 jermar 145
{
146
    context_t ctx;
2053 decky 147
    uint32_t i;
148
    sh_quiet = quiet;
1 jermar 149
 
150
    waitq_initialize(&can_start);
151
    rwlock_initialize(&rwlock);
2029 decky 152
    atomic_set(&threads_fault, 0);
1 jermar 153
 
2053 decky 154
    uint32_t rd = random(7) + 1;
155
    uint32_t wr = random(5) + 1;
156
 
157
    atomic_set(&thread_count, rd + wr);
158
 
2028 decky 159
    thread_t *thrd;
160
 
161
    context_save(&ctx);
2053 decky 162
    if (!quiet) {
3069 decky 163
        printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in);
164
        printf("Creating %" PRIu32 " readers\n", rd);
2053 decky 165
    }
2028 decky 166
 
2053 decky 167
    for (i = 0; i < rd; i++) {
2042 decky 168
        thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
2028 decky 169
        if (thrd)
170
            thread_ready(thrd);
2053 decky 171
        else if (!quiet)
3069 decky 172
            printf("Could not create reader %" PRIu32 "\n", i);
2028 decky 173
    }
1 jermar 174
 
2053 decky 175
    if (!quiet)
3069 decky 176
        printf("Creating %" PRIu32 " writers\n", wr);
2053 decky 177
 
178
    for (i = 0; i < wr; i++) {
2042 decky 179
        thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
2028 decky 180
        if (thrd)
181
            thread_ready(thrd);
2053 decky 182
        else if (!quiet)
3069 decky 183
            printf("Could not create writer %" PRIu32 "\n", i);
2028 decky 184
    }
185
 
186
    thread_usleep(20000);
187
    waitq_wakeup(&can_start, WAKEUP_ALL);
188
 
2053 decky 189
    while (atomic_get(&thread_count) > 0) {
190
        if (!quiet)
3069 decky 191
            printf("Threads left: %ld\n", atomic_get(&thread_count));
2053 decky 192
        thread_sleep(1);
193
    }
194
 
2029 decky 195
    if (atomic_get(&threads_fault) == 0)
196
        return NULL;
197
 
198
    return "Test failed";
1 jermar 199
}