Subversion Repositories HelenOS

Rev

Rev 2024 | Rev 2029 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jermar 1
/*
2
 * Copyright (C) 2001-2004 Jakub 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>
34
#include <arch/types.h>
35
#include <arch/context.h>
205 jermar 36
#include <context.h>
81 jermar 37
#include <panic.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
 
47
static rwlock_t rwlock;
48
 
2024 decky 49
SPINLOCK_INITIALIZE(rw_lock);
1 jermar 50
 
51
static waitq_t can_start;
52
 
2022 decky 53
static uint32_t seed = 0xdeadbeef;
1 jermar 54
 
2022 decky 55
static uint32_t random(uint32_t max)
1 jermar 56
{
1780 jermar 57
	uint32_t rc;
1 jermar 58
 
2024 decky 59
	spinlock_lock(&rw_lock);	
1 jermar 60
	rc = seed % max;
61
	seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
2024 decky 62
	spinlock_unlock(&rw_lock);
1 jermar 63
	return rc;
64
}
65
 
2022 decky 66
static void writer(void *arg)
1 jermar 67
{
68
	int rc, to;
1658 vana 69
	thread_detach(THREAD);
1 jermar 70
	waitq_sleep(&can_start);
71
 
72
	to = random(40000);
15 jermar 73
	printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
1 jermar 74
	rc = rwlock_write_lock_timeout(&rwlock, to);
75
	if (SYNCH_FAILED(rc)) {
15 jermar 76
		printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
1 jermar 77
		return;
2028 decky 78
	}
15 jermar 79
	printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
1 jermar 80
 
81
	if (rwlock.readers_in) panic("Oops.");
82
	thread_usleep(random(1000000));
83
	if (rwlock.readers_in) panic("Oops.");	
84
 
85
	rwlock_write_unlock(&rwlock);
15 jermar 86
	printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);	
1 jermar 87
}
88
 
2022 decky 89
static void reader(void *arg)
1 jermar 90
{
91
	int rc, to;
1658 vana 92
	thread_detach(THREAD);
1 jermar 93
	waitq_sleep(&can_start);
94
 
95
	to = random(2000);
15 jermar 96
	printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
1 jermar 97
	rc = rwlock_read_lock_timeout(&rwlock, to);
98
	if (SYNCH_FAILED(rc)) {
15 jermar 99
		printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
1 jermar 100
		return;
101
	}
15 jermar 102
	printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
1 jermar 103
	thread_usleep(30000);
104
	rwlock_read_unlock(&rwlock);
15 jermar 105
	printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);		
1 jermar 106
}
107
 
2028 decky 108
char * test_rwlock4(void)
1 jermar 109
{
110
	context_t ctx;
1780 jermar 111
	uint32_t i, k;
1 jermar 112
 
113
	waitq_initialize(&can_start);
114
	rwlock_initialize(&rwlock);
115
 
2028 decky 116
	thread_t *thrd;
117
 
118
	context_save(&ctx);
119
	printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
120
 
121
	k = random(7) + 1;
122
	printf("Creating %d readers\n", k);
123
	for (i = 0; i < k; i++) {
124
		thrd = thread_create(reader, NULL, TASK, 0, "reader");
125
		if (thrd)
126
			thread_ready(thrd);
127
		else
128
			printf("Could not create reader %d\n", i);
129
	}
1 jermar 130
 
2028 decky 131
	k = random(5) + 1;
132
	printf("Creating %d writers\n", k);
133
	for (i=0; i<k; i++) {
134
		thrd = thread_create(writer, NULL, TASK, 0, "writer");
135
		if (thrd)
136
			thread_ready(thrd);
137
		else
138
			printf("Could not create writer %d\n", i);
139
	}
140
 
141
	thread_usleep(20000);
142
	waitq_wakeup(&can_start, WAKEUP_ALL);
143
 
144
	return NULL;
1 jermar 145
}