source: mainline/kernel/test/synch/rwlock5.c@ 8a72a9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a72a9a was cd8ad52, checked in by Martin Decky <martin@…>, 17 years ago

proper printf formatting

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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>
[23684b7]31#include <atomic.h>
[f761f1eb]32#include <print.h>
33#include <proc/thread.h>
34
35#include <synch/waitq.h>
36#include <synch/rwlock.h>
37
38#define READERS 50
39#define WRITERS 50
40
41static rwlock_t rwlock;
42
43static waitq_t can_start;
[59e07c91]44static atomic_t items_read;
45static atomic_t items_written;
[f761f1eb]46
[96348adc]47static void writer(void *arg)
[f761f1eb]48{
[8d6d76a]49 thread_detach(THREAD);
50
[f761f1eb]51 waitq_sleep(&can_start);
52
53 rwlock_write_lock(&rwlock);
54 atomic_inc(&items_written);
55 rwlock_write_unlock(&rwlock);
56}
57
[96348adc]58static void reader(void *arg)
[f761f1eb]59{
[8d6d76a]60 thread_detach(THREAD);
61
[f761f1eb]62 waitq_sleep(&can_start);
63
64 rwlock_read_lock(&rwlock);
65 atomic_inc(&items_read);
66 rwlock_read_unlock(&rwlock);
67}
68
[95155b0c]69char * test_rwlock5(bool quiet)
[f761f1eb]70{
71 int i, j, k;
[6c441cf8]72 long readers, writers;
[f761f1eb]73
74 waitq_initialize(&can_start);
75 rwlock_initialize(&rwlock);
76
[96348adc]77 for (i = 1; i <= 3; i++) {
[f761f1eb]78 thread_t *thrd;
79
[10c071e]80 atomic_set(&items_read, 0);
81 atomic_set(&items_written, 0);
[f761f1eb]82
[96348adc]83 readers = i * READERS;
84 writers = (4 - i) * WRITERS;
[f761f1eb]85
[280a27e]86 printf("Creating %ld readers and %ld writers...", readers, writers);
[f761f1eb]87
[96348adc]88 for (j = 0; j < (READERS + WRITERS) / 2; j++) {
89 for (k = 0; k < i; k++) {
[62b6d17]90 thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
[f761f1eb]91 if (thrd)
92 thread_ready(thrd);
93 else
[96348adc]94 printf("Could not create reader %d\n", k);
[f761f1eb]95 }
[96348adc]96 for (k = 0; k < (4 - i); k++) {
[62b6d17]97 thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
[f761f1eb]98 if (thrd)
99 thread_ready(thrd);
100 else
[96348adc]101 printf("Could not create writer %d\n", k);
[f761f1eb]102 }
103 }
104
105 printf("ok\n");
106
107 thread_sleep(1);
108 waitq_wakeup(&can_start, WAKEUP_ALL);
109
[96348adc]110 while ((items_read.count != readers) || (items_written.count != writers)) {
[cd8ad52]111 printf("%d readers remaining, %d writers remaining, readers_in=%d\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in);
[f761f1eb]112 thread_usleep(100000);
113 }
114 }
[96348adc]115
116 return NULL;
[f761f1eb]117}
Note: See TracBrowser for help on using the repository browser.