1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "bounded-queue.h"
struct int_entry {
struct queue_entry link;
int val;
};
struct test_state {
struct bounded_queue *q;
pthread_mutex_t mutex;
pthread_cond_t cond;
int successful_pushes;
int failed_pushes;
int successful_pops;
int failed_pops;
};
static void *push_thread (void *arg) {
struct test_state *state = arg;
struct int_entry *e = malloc (sizeof (*e));
int rc;
e->val = 42;
rc = bounded_queue_push (state->q, &e->link);
pthread_mutex_lock (&state->mutex);
if (!rc) {
state->successful_pushes++;
} else {
free (e);
state->failed_pushes++;
}
pthread_mutex_unlock (&state->mutex);
pthread_cond_signal (&state->cond);
return NULL;
}
static void *pop_thread (void *arg) {
struct test_state *state = arg;
struct int_entry *e;
e = (struct int_entry *)bounded_queue_pop (state->q);
pthread_mutex_lock (&state->mutex);
if (e) {
assert (e->val == 42);
state->successful_pops++;
free (e);
} else {
state->failed_pops++;
}
pthread_mutex_unlock (&state->mutex);
pthread_cond_signal (&state->cond);
return NULL;
}
int main (void) {
struct bounded_queue q;
struct int_entry *e;
struct test_state state;
int i, rc;
rc = bounded_queue_init (&q, 10);
for (i = 0; i < 10; ++i) {
e = malloc (sizeof (*e));
e->val = i;
rc = bounded_queue_push (&q, &e->link);
assert (rc == 0);
}
for (i = 0; i < 10; ++i) {
e = (struct int_entry *)bounded_queue_pop (&q);
assert (e);
assert (e->val == i);
free (e);
}
puts ("Single-threaded test passed.");
memset (&state, 0, sizeof (state));
pthread_mutex_init (&state.mutex, NULL);
pthread_cond_init (&state.cond, NULL);
state.q = &q;
puts ("Starting 20 pusher threads.");
for (i = 0; i < 20; ++i) {
pthread_t t;
pthread_create (&t, NULL, push_thread, (void *)&state);
pthread_detach (t);
}
// Since queue limit is 10, 10 and only 10 pushers should succeed.
puts ("Waiting for winners...");
pthread_mutex_lock (&state.mutex);
while (state.successful_pushes < 10) {
pthread_cond_wait (&state.cond, &state.mutex);
}
pthread_mutex_unlock (&state.mutex);
sleep (1);
pthread_mutex_lock (&state.mutex);
assert (state.successful_pushes == 10);
assert (state.failed_pushes == 0);
pthread_mutex_unlock (&state.mutex);
// Kill the queue: all 10 blocked pushers should fail.
bounded_queue_kill (&q);
puts ("Waiting for losers...");
pthread_mutex_lock (&state.mutex);
while (state.failed_pushes != 10) {
pthread_cond_wait (&state.cond, &state.mutex);
}
pthread_mutex_unlock (&state.mutex);
pthread_mutex_lock (&state.mutex);
assert (state.successful_pushes == 10);
assert (state.failed_pushes == 10);
pthread_mutex_unlock (&state.mutex);
q.killed = 0;
puts ("Starting 20 popper threads...");
for (i = 0; i < 20; ++i) {
pthread_t t;
pthread_create (&t, NULL, pop_thread, (void *)&state);
pthread_detach (t);
}
// We have 10 entries on the queue, so 10 pops should succeed.
puts ("Waiting for winners...");
pthread_mutex_lock (&state.mutex);
while (state.successful_pops != 10) {
pthread_cond_wait (&state.cond, &state.mutex);
}
pthread_mutex_unlock (&state.mutex);
sleep (1);
pthread_mutex_lock (&state.mutex);
assert (state.successful_pops == 10);
assert (state.failed_pops == 0);
pthread_mutex_unlock (&state.mutex);
bounded_queue_kill (&q);
puts ("Waiting for losers...");
pthread_mutex_lock (&state.mutex);
while (state.failed_pops != 10) {
pthread_cond_wait (&state.cond, &state.mutex);
}
pthread_mutex_unlock (&state.mutex);
sleep (1);
pthread_mutex_lock (&state.mutex);
assert (state.successful_pops == 10);
assert (state.failed_pops == 10);
pthread_mutex_unlock (&state.mutex);
puts ("Multi-threaded tests passed.");
pthread_cond_destroy (&state.cond);
pthread_mutex_destroy (&state.mutex);
return 0;
}
|