Subversion Repositories HelenOS

Rev

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

Rev 4341 Rev 4348
Line 38... Line 38...
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
#include <synch/spinlock.h>
41
#include <synch/spinlock.h>
42
 
42
 
43
#define READERS     50
43
#define READERS  50
44
#define WRITERS     50
44
#define WRITERS  50
45
 
45
 
46
static atomic_t thread_count;
46
static atomic_t thread_count;
47
static rwlock_t rwlock;
47
static rwlock_t rwlock;
48
static atomic_t threads_fault;
48
static atomic_t threads_fault;
49
static bool sh_quiet;
-
 
50
 
49
 
51
SPINLOCK_INITIALIZE(rw_lock);
50
SPINLOCK_INITIALIZE(rw_lock);
52
 
51
 
53
static waitq_t can_start;
52
static waitq_t can_start;
54
 
53
 
55
static uint32_t seed = 0xdeadbeef;
54
static uint32_t seed = 0xdeadbeef;
56
 
55
 
57
static uint32_t random(uint32_t max)
56
static uint32_t random(uint32_t max)
58
{
57
{
59
    uint32_t rc;
58
    uint32_t rc;
60
 
59
   
61
    spinlock_lock(&rw_lock);   
60
    spinlock_lock(&rw_lock);
62
    rc = seed % max;
61
    rc = seed % max;
63
    seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc;
62
    seed = (((seed << 2) ^ (seed >> 2)) * 487) + rc;
64
    spinlock_unlock(&rw_lock);
63
    spinlock_unlock(&rw_lock);
65
    return rc;
64
    return rc;
66
}
65
}
Line 68... Line 67...
68
static void writer(void *arg)
67
static void writer(void *arg)
69
{
68
{
70
    int rc, to;
69
    int rc, to;
71
    thread_detach(THREAD);
70
    thread_detach(THREAD);
72
    waitq_sleep(&can_start);
71
    waitq_sleep(&can_start);
73
 
72
   
74
    to = random(40000);
73
    to = random(40000);
75
   
74
   
76
    if (!sh_quiet)
-
 
77
        printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to);
75
    TPRINTF("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to);
78
   
76
   
79
    rc = rwlock_write_lock_timeout(&rwlock, to);
77
    rc = rwlock_write_lock_timeout(&rwlock, to);
80
    if (SYNCH_FAILED(rc)) {
78
    if (SYNCH_FAILED(rc)) {
81
        if (!sh_quiet)
-
 
82
            printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid);
79
        TPRINTF("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid);
83
        atomic_dec(&thread_count);
80
        atomic_dec(&thread_count);
84
        return;
81
        return;
85
    }
82
    }
86
   
83
   
87
    if (!sh_quiet)
-
 
88
        printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid);
84
    TPRINTF("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid);
89
 
85
   
90
    if (rwlock.readers_in) {
86
    if (rwlock.readers_in) {
91
        if (!sh_quiet)
-
 
92
            printf("Oops.");
87
        TPRINTF("Oops.\n");
93
        atomic_inc(&threads_fault);
88
        atomic_inc(&threads_fault);
94
        atomic_dec(&thread_count);
89
        atomic_dec(&thread_count);
95
        return;
90
        return;
96
    }
91
    }
-
 
92
   
97
    thread_usleep(random(1000000));
93
    thread_usleep(random(1000000));
-
 
94
   
98
    if (rwlock.readers_in) {
95
    if (rwlock.readers_in) {
99
        if (!sh_quiet)
-
 
100
            printf("Oops.");   
96
        TPRINTF("Oops.\n");
101
        atomic_inc(&threads_fault);
97
        atomic_inc(&threads_fault);
102
        atomic_dec(&thread_count);
98
        atomic_dec(&thread_count);
103
        return;
99
        return;
104
    }
100
    }
105
 
101
   
106
    rwlock_write_unlock(&rwlock);
102
    rwlock_write_unlock(&rwlock);
107
   
103
   
108
    if (!sh_quiet)
-
 
109
        printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid);
104
    TPRINTF("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid);
110
    atomic_dec(&thread_count);
105
    atomic_dec(&thread_count);
111
}
106
}
112
 
107
 
113
static void reader(void *arg)
108
static void reader(void *arg)
114
{
109
{
Line 116... Line 111...
116
    thread_detach(THREAD);
111
    thread_detach(THREAD);
117
    waitq_sleep(&can_start);
112
    waitq_sleep(&can_start);
118
   
113
   
119
    to = random(2000);
114
    to = random(2000);
120
   
115
   
121
    if (!sh_quiet)
-
 
122
        printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to);
116
    TPRINTF("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to);
123
   
117
   
124
    rc = rwlock_read_lock_timeout(&rwlock, to);
118
    rc = rwlock_read_lock_timeout(&rwlock, to);
125
    if (SYNCH_FAILED(rc)) {
119
    if (SYNCH_FAILED(rc)) {
126
        if (!sh_quiet)
-
 
127
            printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid);
120
        TPRINTF("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid);
128
        atomic_dec(&thread_count);
121
        atomic_dec(&thread_count);
129
        return;
122
        return;
130
    }
123
    }
131
   
124
   
132
    if (!sh_quiet)
-
 
133
        printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid);
125
    TPRINTF("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid);
134
   
126
   
135
    thread_usleep(30000);
127
    thread_usleep(30000);
136
    rwlock_read_unlock(&rwlock);
128
    rwlock_read_unlock(&rwlock);
137
   
129
   
138
    if (!sh_quiet)
-
 
139
        printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid);
130
    TPRINTF("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid);
140
    atomic_dec(&thread_count);
131
    atomic_dec(&thread_count);
141
}
132
}
142
 
133
 
143
char * test_rwlock4(bool quiet)
134
char *test_rwlock4(void)
144
{
135
{
145
    context_t ctx;
136
    context_t ctx;
146
    uint32_t i;
137
    uint32_t i;
147
    sh_quiet = quiet;
-
 
148
   
138
   
149
    waitq_initialize(&can_start);
139
    waitq_initialize(&can_start);
150
    rwlock_initialize(&rwlock);
140
    rwlock_initialize(&rwlock);
151
    atomic_set(&threads_fault, 0);
141
    atomic_set(&threads_fault, 0);
152
   
142
   
Line 156... Line 146...
156
    atomic_set(&thread_count, rd + wr);
146
    atomic_set(&thread_count, rd + wr);
157
   
147
   
158
    thread_t *thrd;
148
    thread_t *thrd;
159
   
149
   
160
    context_save(&ctx);
150
    context_save(&ctx);
161
    if (!quiet) {
-
 
162
        printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in);
151
    TPRINTF("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in);
163
        printf("Creating %" PRIu32 " readers\n", rd);
152
    TPRINTF("Creating %" PRIu32 " readers\n", rd);
164
    }
-
 
165
   
153
   
166
    for (i = 0; i < rd; i++) {
154
    for (i = 0; i < rd; i++) {
167
        thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
155
        thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
168
        if (thrd)
156
        if (thrd)
169
            thread_ready(thrd);
157
            thread_ready(thrd);
170
        else if (!quiet)
158
        else
171
            printf("Could not create reader %" PRIu32 "\n", i);
159
            TPRINTF("Could not create reader %" PRIu32 "\n", i);
172
    }
160
    }
173
 
161
   
174
    if (!quiet)
-
 
175
        printf("Creating %" PRIu32 " writers\n", wr);
162
    TPRINTF("Creating %" PRIu32 " writers\n", wr);
176
   
163
   
177
    for (i = 0; i < wr; i++) {
164
    for (i = 0; i < wr; i++) {
178
        thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
165
        thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
179
        if (thrd)
166
        if (thrd)
180
            thread_ready(thrd);
167
            thread_ready(thrd);
181
        else if (!quiet)
168
        else
182
            printf("Could not create writer %" PRIu32 "\n", i);
169
            TPRINTF("Could not create writer %" PRIu32 "\n", i);
183
    }
170
    }
184
   
171
   
185
    thread_usleep(20000);
172
    thread_usleep(20000);
186
    waitq_wakeup(&can_start, WAKEUP_ALL);
173
    waitq_wakeup(&can_start, WAKEUP_ALL);
187
   
174
   
188
    while (atomic_get(&thread_count) > 0) {
175
    while (atomic_get(&thread_count) > 0) {
189
        if (!quiet)
-
 
190
            printf("Threads left: %ld\n", atomic_get(&thread_count));
176
        TPRINTF("Threads left: %ld\n", atomic_get(&thread_count));
191
        thread_sleep(1);
177
        thread_sleep(1);
192
    }
178
    }
193
   
179
   
194
    if (atomic_get(&threads_fault) == 0)
180
    if (atomic_get(&threads_fault) == 0)
195
        return NULL;
181
        return NULL;