Subversion Repositories HelenOS-historic

Rev

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

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