Subversion Repositories HelenOS-historic

Rev

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

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