Subversion Repositories HelenOS

Rev

Rev 4377 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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