Subversion Repositories HelenOS-historic

Rev

Rev 171 | Rev 225 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 171 Rev 205
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 <arch/atomic.h>
31
#include <arch/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 <panic.h>
37
#include <panic.h>
37
 
38
 
38
#include <synch/waitq.h>
39
#include <synch/waitq.h>
39
#include <synch/rwlock.h>
40
#include <synch/rwlock.h>
40
#include <synch/synch.h>
41
#include <synch/synch.h>
41
 
42
 
42
#define READERS     50
43
#define READERS     50
43
#define WRITERS     50
44
#define WRITERS     50
44
 
45
 
45
static rwlock_t rwlock;
46
static rwlock_t rwlock;
46
 
47
 
47
static spinlock_t lock;
48
static spinlock_t lock;
48
 
49
 
49
static waitq_t can_start;
50
static waitq_t can_start;
50
 
51
 
51
__u32 seed = 0xdeadbeaf;
52
__u32 seed = 0xdeadbeaf;
52
 
53
 
53
static __u32 random(__u32 max);
54
static __u32 random(__u32 max);
54
 
55
 
55
static void writer(void *arg);
56
static void writer(void *arg);
56
static void reader(void *arg);
57
static void reader(void *arg);
57
static void failed(void);
58
static void failed(void);
58
 
59
 
59
__u32 random(__u32 max)
60
__u32 random(__u32 max)
60
{
61
{
61
    __u32 rc;
62
    __u32 rc;
62
    pri_t pri;
63
    pri_t pri;
63
 
64
 
64
    pri = cpu_priority_high();
65
    pri = cpu_priority_high();
65
    spinlock_lock(&lock);  
66
    spinlock_lock(&lock);  
66
    rc = seed % max;
67
    rc = seed % max;
67
    seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
68
    seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
68
    spinlock_unlock(&lock);
69
    spinlock_unlock(&lock);
69
    cpu_priority_restore(pri);
70
    cpu_priority_restore(pri);
70
    return rc;
71
    return rc;
71
}
72
}
72
 
73
 
73
 
74
 
74
void writer(void *arg)
75
void writer(void *arg)
75
{
76
{
76
    int rc, to;
77
    int rc, to;
77
    waitq_sleep(&can_start);
78
    waitq_sleep(&can_start);
78
 
79
 
79
    to = random(40000);
80
    to = random(40000);
80
    printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
81
    printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
81
    rc = rwlock_write_lock_timeout(&rwlock, to);
82
    rc = rwlock_write_lock_timeout(&rwlock, to);
82
    if (SYNCH_FAILED(rc)) {
83
    if (SYNCH_FAILED(rc)) {
83
        printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
84
        printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
84
        return;
85
        return;
85
    };
86
    };
86
    printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
87
    printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
87
 
88
 
88
    if (rwlock.readers_in) panic("Oops.");
89
    if (rwlock.readers_in) panic("Oops.");
89
    thread_usleep(random(1000000));
90
    thread_usleep(random(1000000));
90
    if (rwlock.readers_in) panic("Oops."); 
91
    if (rwlock.readers_in) panic("Oops."); 
91
 
92
 
92
    rwlock_write_unlock(&rwlock);
93
    rwlock_write_unlock(&rwlock);
93
    printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
94
    printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
94
}
95
}
95
 
96
 
96
void reader(void *arg)
97
void reader(void *arg)
97
{
98
{
98
    int rc, to;
99
    int rc, to;
99
    waitq_sleep(&can_start);
100
    waitq_sleep(&can_start);
100
   
101
   
101
    to = random(2000);
102
    to = random(2000);
102
    printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
103
    printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
103
    rc = rwlock_read_lock_timeout(&rwlock, to);
104
    rc = rwlock_read_lock_timeout(&rwlock, to);
104
    if (SYNCH_FAILED(rc)) {
105
    if (SYNCH_FAILED(rc)) {
105
        printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
106
        printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
106
        return;
107
        return;
107
    }
108
    }
108
    printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
109
    printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
109
    thread_usleep(30000);
110
    thread_usleep(30000);
110
    rwlock_read_unlock(&rwlock);
111
    rwlock_read_unlock(&rwlock);
111
    printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);    
112
    printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);    
112
}
113
}
113
 
114
 
114
void failed(void)
115
void failed(void)
115
{
116
{
116
    printf("Test failed prematurely.\n");
117
    printf("Test failed prematurely.\n");
117
    thread_exit();
118
    thread_exit();
118
}
119
}
119
 
120
 
120
void test(void)
121
void test(void)
121
{
122
{
122
    context_t ctx;
123
    context_t ctx;
123
    __u32 i, k;
124
    __u32 i, k;
124
   
125
   
125
    printf("Read/write locks test #4\n");
126
    printf("Read/write locks test #4\n");
126
   
127
   
127
    waitq_initialize(&can_start);
128
    waitq_initialize(&can_start);
128
    rwlock_initialize(&rwlock);
129
    rwlock_initialize(&rwlock);
129
   
130
   
130
 
131
 
131
   
132
   
132
    for (; ;) {
133
    for (; ;) {
133
        thread_t *thrd;
134
        thread_t *thrd;
134
       
135
       
135
        context_save(&ctx);
136
        context_save(&ctx);
136
        printf("sp=%X, readers_in=%d\n", ctx.sp, rwlock.readers_in);
137
        printf("sp=%X, readers_in=%d\n", ctx.sp, rwlock.readers_in);
137
       
138
       
138
        k = random(7) + 1;
139
        k = random(7) + 1;
139
        printf("Creating %d readers\n", k);
140
        printf("Creating %d readers\n", k);
140
        for (i=0; i<k; i++) {
141
        for (i=0; i<k; i++) {
141
            thrd = thread_create(reader, NULL, TASK, 0);
142
            thrd = thread_create(reader, NULL, TASK, 0);
142
            if (thrd)
143
            if (thrd)
143
                thread_ready(thrd);
144
                thread_ready(thrd);
144
            else
145
            else
145
                failed();
146
                failed();
146
        }
147
        }
147
 
148
 
148
        k = random(5) + 1;
149
        k = random(5) + 1;
149
        printf("Creating %d writers\n", k);
150
        printf("Creating %d writers\n", k);
150
        for (i=0; i<k; i++) {
151
        for (i=0; i<k; i++) {
151
            thrd = thread_create(writer, NULL, TASK, 0);
152
            thrd = thread_create(writer, NULL, TASK, 0);
152
            if (thrd)
153
            if (thrd)
153
                thread_ready(thrd);
154
                thread_ready(thrd);
154
            else
155
            else
155
                failed();
156
                failed();
156
        }
157
        }
157
       
158
       
158
        thread_usleep(20000);
159
        thread_usleep(20000);
159
        waitq_wakeup(&can_start, WAKEUP_ALL);
160
        waitq_wakeup(&can_start, WAKEUP_ALL);
160
    }      
161
    }      
161
 
162
 
162
}
163
}
163
 
164