14
14
* limitations under the License.
17
#include <apr_thread_proc.h>
18
#include <apr_errno.h>
19
#include <apr_general.h>
20
#include <apr_getopt.h>
21
#include <apr_strings.h>
29
#include <apr_portable.h>
30
18
#include "apr_queue.h"
36
"This program won't work on this platform because there is no "
37
"support for threads.\n");
40
#else /* !APR_HAS_THREADS */
43
int consumer_activity=400;
44
int producer_activity=300;
46
static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data);
47
static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data);
48
static void usage(void);
19
#include "apr_thread_proc.h"
26
#define NUMBER_CONSUMERS 3
27
#define CONSUMER_ACTIVITY 4
28
#define NUMBER_PRODUCERS 4
29
#define PRODUCER_ACTIVITY 5
30
#define QUEUE_SIZE 100
32
static apr_queue_t *queue;
50
34
static void * APR_THREAD_FUNC consumer(apr_thread_t *thd, void *data)
53
apr_queue_t *q = (apr_queue_t*)data;
57
char current_thread_str[30];
58
apr_os_thread_t current_thread = apr_os_thread_current();
60
apr_snprintf(current_thread_str, sizeof current_thread_str,
61
"%pT", ¤t_thread);
63
sleeprate = 1000000/consumer_activity;
41
sleeprate = 1000000/CONSUMER_ACTIVITY;
64
42
apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */
67
rv = apr_queue_pop(q, &v);
68
if (rv == APR_EINTR) {
69
fprintf(stderr, "%s\tconsumer intr\n", current_thread_str);
72
} while (rv == APR_EINTR) ;
73
if (rv != APR_SUCCESS) {
75
fprintf(stderr, "%s\tconsumer:queue terminated APR_EOF\n", current_thread_str);
79
fprintf(stderr, "%s\tconsumer thread exit rv %d\n", current_thread_str, rv);
80
apr_thread_exit(thd, rv);
85
fprintf(stderr, "%s\tpop %d\n", current_thread_str, val);
46
rv = apr_queue_pop(queue, &v);
54
ABTS_TRUE(tc, v == NULL);
55
ABTS_TRUE(tc, rv == APR_SUCCESS);
86
57
apr_sleep( sleeprate ); /* sleep this long to acheive our rate */
60
apr_thread_exit(thd, rv);
92
66
static void * APR_THREAD_FUNC producer(apr_thread_t *thd, void *data)
96
apr_queue_t *q = (apr_queue_t*)data;
99
char current_thread_str[30];
100
apr_os_thread_t current_thread = apr_os_thread_current();
102
apr_snprintf(current_thread_str, sizeof current_thread_str,
103
"%pT", ¤t_thread);
105
sleeprate = 1000000/producer_activity;
72
sleeprate = 1000000/PRODUCER_ACTIVITY;
106
73
apr_sleep( (rand() % 4 ) * 1000000 ); /* sleep random seconds */
77
rv = apr_queue_push(queue, NULL);
109
val = apr_palloc(context, sizeof(int));
112
fprintf(stderr, "%s\tpush %d\n", current_thread_str, *val);
114
rv = apr_queue_push(q, val);
116
fprintf(stderr, "%s\tproducer intr\n", current_thread_str);
117
} while (rv == APR_EINTR);
119
if (rv != APR_SUCCESS) {
121
fprintf(stderr, "%s\tproducer: queue terminated APR_EOF\n", current_thread_str);
125
fprintf(stderr, "%s\tproducer thread exit rv %d\n", current_thread_str, rv);
126
apr_thread_exit(thd, rv);
85
ABTS_TRUE(tc, rv == APR_SUCCESS);
130
87
apr_sleep( sleeprate ); /* sleep this long to acheive our rate */
90
apr_thread_exit(thd, rv);
136
static void usage(void)
138
fprintf(stderr,"usage: testqueue -p n -P n -c n -C n -q n -s n\n");
139
fprintf(stderr,"-c # of consumer\n");
140
fprintf(stderr,"-C amount they consumer before dying\n");
141
fprintf(stderr,"-p # of producers\n");
142
fprintf(stderr,"-P amount they produce before dying\n");
143
fprintf(stderr,"-q queue size\n");
144
fprintf(stderr,"-s amount of time to sleep before killing it\n");
145
fprintf(stderr,"-v verbose\n");
148
int main(int argc, const char* const argv[])
96
static void test_queue_producer_consumer(abts_case *tc, void *data)
150
100
apr_thread_t **t;
102
/* XXX: non-portable */
164
103
srand((unsigned int)apr_time_now());
165
printf("APR Queue Test\n======================\n\n");
167
printf("%-60s", "Initializing the context");
168
if (apr_pool_create(&context, NULL) != APR_SUCCESS) {
170
fprintf(stderr, "Failed.\nCould not initialize\n");
175
apr_getopt_init(&opt, context, argc, argv);
176
while ((rv = apr_getopt(opt, "p:c:P:C:q:s:v", &c, &optarg))
180
numconsumers = atoi( optarg);
183
numproducers = atoi( optarg);
186
consumer_activity = atoi( optarg);
189
producer_activity = atoi( optarg);
192
sleeptime= atoi(optarg);
195
queuesize = atoi(optarg);
205
/* bad cmdline option? then we die */
206
if (rv != APR_EOF || opt->ind < opt->argc) {
213
printf("test stats %d consumers (rate %d/sec) %d producers (rate %d/sec) queue size %d sleep %d\n",
214
numconsumers,consumer_activity, numproducers, producer_activity, queuesize,sleeptime);
215
printf("%-60s", "Initializing the queue");
216
rv = apr_queue_create(&queue, queuesize, context);
218
if (rv != APR_SUCCESS) {
220
fprintf(stderr, "Failed\nCould not create queue %d\n",rv);
221
apr_strerror(rv, errorbuf,200);
222
fprintf(stderr,"%s\n",errorbuf);
227
t = apr_palloc( context, sizeof(apr_thread_t*) * (numconsumers+numproducers));
228
printf("%-60s", "Starting consumers");
229
for (i=0;i<numconsumers;i++) {
230
rv = apr_thread_create(&t[i], NULL, consumer, queue, context);
231
if (rv != APR_SUCCESS) {
232
apr_strerror(rv, errorbuf,200);
233
fprintf(stderr, "Failed\nError starting consumer thread (%d) rv=%d:%s\n",i, rv,errorbuf);
238
for (i=numconsumers;i<(numconsumers+numproducers);i++) {
239
rv = apr_thread_create(&t[i], NULL, producer, queue, context);
240
if (rv != APR_SUCCESS) {
241
apr_strerror(rv, errorbuf,200);
242
fprintf(stderr, "Failed\nError starting producer thread (%d) rv=%d:%s\n",i, rv,errorbuf);
249
printf("%-60s", "Sleeping\n");
250
apr_sleep( sleeptime * 1000000 ); /* sleep 10 seconds */
253
printf("%-60s", "Terminating queue");
105
rv = apr_queue_create(&queue, QUEUE_SIZE, p);
106
ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
108
t = apr_palloc(p, sizeof(apr_thread_t*) * (NUMBER_CONSUMERS
109
+ NUMBER_PRODUCERS));
110
for (i = 0; i < NUMBER_CONSUMERS; ++i) {
111
rv = apr_thread_create(&t[i], NULL, consumer, tc, p);
112
ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
114
for (i = NUMBER_CONSUMERS; i < NUMBER_CONSUMERS + NUMBER_PRODUCERS; ++i) {
115
rv = apr_thread_create(&t[i], NULL, producer, tc, p);
116
ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
119
apr_sleep(5000000); /* sleep 5 seconds */
254
121
rv = apr_queue_term(queue);
255
if (rv != APR_SUCCESS) {
256
apr_strerror(rv, errorbuf,200);
257
fprintf( stderr, "apr_queue_term failed %d:%s\n",rv,errorbuf);
262
printf("%-60s", "Waiting for threads to exit\n");
264
for (i=0;i<numconsumers+numproducers;i++) {
122
ABTS_INT_EQUAL(tc, rv, APR_SUCCESS);
124
for (i = 0; i < NUMBER_CONSUMERS + NUMBER_PRODUCERS; ++i) {
265
125
apr_thread_join(&rv, t[i]);
267
apr_strerror(rv, errorbuf,200);
269
fprintf( stderr, "consumer thread %d failed rv %d:%s\n",i,rv,errorbuf);
271
fprintf( stderr, "producer thread %d failed rv %d:%s\n",i,rv,errorbuf);
126
ABTS_INT_EQUAL(tc, rv, APR_EOF);
282
#endif /* !APR_HAS_THREADS */
130
#endif /* APR_HAS_THREADS */
132
abts_suite *testqueue(abts_suite *suite)
134
suite = ADD_SUITE(suite);
137
abts_run_test(suite, test_queue_producer_consumer, NULL);
138
#endif /* APR_HAS_THREADS */