Subversion Repositories HelenOS

Rev

Rev 2022 | Rev 2042 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2022 Rev 2028
Line 42... Line 42...
42
 
42
 
43
static waitq_t can_start;
43
static waitq_t can_start;
44
static atomic_t items_read;
44
static atomic_t items_read;
45
static atomic_t items_written;
45
static atomic_t items_written;
46
 
46
 
47
static void writer(void *arg);
47
static void writer(void *arg)
48
static void reader(void *arg);
-
 
49
static void failed(void);
-
 
50
 
-
 
51
void writer(void *arg)
-
 
52
{
48
{
53
    thread_detach(THREAD);
49
    thread_detach(THREAD);
54
 
50
 
55
    waitq_sleep(&can_start);
51
    waitq_sleep(&can_start);
56
 
52
 
57
    rwlock_write_lock(&rwlock);
53
    rwlock_write_lock(&rwlock);
58
    atomic_inc(&items_written);
54
    atomic_inc(&items_written);
59
    rwlock_write_unlock(&rwlock);
55
    rwlock_write_unlock(&rwlock);
60
}
56
}
61
 
57
 
62
void reader(void *arg)
58
static void reader(void *arg)
63
{
59
{
64
    thread_detach(THREAD);
60
    thread_detach(THREAD);
65
 
61
 
66
    waitq_sleep(&can_start);
62
    waitq_sleep(&can_start);
67
   
63
   
68
    rwlock_read_lock(&rwlock);
64
    rwlock_read_lock(&rwlock);
69
    atomic_inc(&items_read);
65
    atomic_inc(&items_read);
70
    rwlock_read_unlock(&rwlock);
66
    rwlock_read_unlock(&rwlock);
71
}
67
}
72
 
68
 
73
void failed(void)
-
 
74
{
-
 
75
    printf("Test failed prematurely.\n");
-
 
76
    thread_exit();
-
 
77
}
-
 
78
 
-
 
79
void test_rwlock5(void)
69
char * test_rwlock5(void)
80
{
70
{
81
    int i, j, k;
71
    int i, j, k;
82
    count_t readers, writers;
72
    count_t readers, writers;
83
   
73
   
84
    printf("Read/write locks test #5\n");
-
 
85
   
-
 
86
    waitq_initialize(&can_start);
74
    waitq_initialize(&can_start);
87
    rwlock_initialize(&rwlock);
75
    rwlock_initialize(&rwlock);
88
   
76
   
89
    for (i=1; i<=3; i++) {
77
    for (i = 1; i <= 3; i++) {
90
        thread_t *thrd;
78
        thread_t *thrd;
91
 
79
 
92
        atomic_set(&items_read, 0);
80
        atomic_set(&items_read, 0);
93
        atomic_set(&items_written, 0);
81
        atomic_set(&items_written, 0);
94
 
82
 
95
        readers = i*READERS;
83
        readers = i * READERS;
96
        writers = (4-i)*WRITERS;
84
        writers = (4 - i) * WRITERS;
97
 
85
 
98
        printf("Creating %ld readers and %ld writers...", readers, writers);
86
        printf("Creating %ld readers and %ld writers...", readers, writers);
99
       
87
       
100
        for (j=0; j<(READERS+WRITERS)/2; j++) {
88
        for (j = 0; j < (READERS + WRITERS) / 2; j++) {
101
            for (k=0; k<i; k++) {
89
            for (k = 0; k < i; k++) {
102
                thrd = thread_create(reader, NULL, TASK, 0, "reader");
90
                thrd = thread_create(reader, NULL, TASK, 0, "reader");
103
                if (thrd)
91
                if (thrd)
104
                    thread_ready(thrd);
92
                    thread_ready(thrd);
105
                else
93
                else
106
                    failed();
94
                    printf("Could not create reader %d\n", k);
107
            }
95
            }
108
            for (k=0; k<(4-i); k++) {
96
            for (k = 0; k < (4 - i); k++) {
109
                thrd = thread_create(writer, NULL, TASK, 0, "writer");
97
                thrd = thread_create(writer, NULL, TASK, 0, "writer");
110
                if (thrd)
98
                if (thrd)
111
                    thread_ready(thrd);
99
                    thread_ready(thrd);
112
                else
100
                else
113
                    failed();
101
                    printf("Could not create writer %d\n", k);
114
            }
102
            }
115
        }
103
        }
116
 
104
 
117
        printf("ok\n");
105
        printf("ok\n");
118
 
106
 
119
        thread_sleep(1);
107
        thread_sleep(1);
120
        waitq_wakeup(&can_start, WAKEUP_ALL);
108
        waitq_wakeup(&can_start, WAKEUP_ALL);
121
   
109
   
122
        while (items_read.count != readers || items_written.count != writers) {
110
        while ((items_read.count != readers) || (items_written.count != writers)) {
123
            printf("%zd readers remaining, %zd writers remaining, readers_in=%zd\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in);
111
            printf("%zd readers remaining, %zd writers remaining, readers_in=%zd\n", readers - items_read.count, writers - items_written.count, rwlock.readers_in);
124
            thread_usleep(100000);
112
            thread_usleep(100000);
125
        }
113
        }
126
    }
114
    }
-
 
115
   
127
    printf("Test passed.\n");
116
    return NULL;
128
}
117
}