1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_queue.h"
19 #include "apr_thread_pool.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
33 static apr_queue_t
*queue
;
35 static void * APR_THREAD_FUNC
consumer(apr_thread_t
*thd
, void *data
)
42 sleeprate
= 1000000/CONSUMER_ACTIVITY
;
43 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
46 rv
= apr_queue_pop(queue
, &v
);
48 if (rv
== APR_EINTR
) {
50 } else if (rv
== APR_EOF
) {
54 ABTS_TRUE(tc
, v
== NULL
);
55 ABTS_TRUE(tc
, rv
== APR_SUCCESS
);
57 apr_sleep(sleeprate
); /* sleep this long to acheive our rate */
63 static void * APR_THREAD_FUNC
producer(apr_thread_t
*thd
, void *data
)
68 sleeprate
= 1000000/PRODUCER_ACTIVITY
;
69 apr_sleep((rand() % 4) * 1000000); /* sleep random seconds */
73 rv
= apr_queue_push(queue
, NULL
);
74 } while (rv
== APR_EINTR
);
80 apr_sleep(sleeprate
); /* sleep this long to acheive our rate */
86 static void test_queue_producer_consumer(abts_case
*tc
, void *data
)
90 apr_thread_pool_t
*thrp
;
92 /* XXX: non-portable */
93 srand((unsigned int)apr_time_now());
95 rv
= apr_queue_create(&queue
, QUEUE_SIZE
, p
);
96 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
98 rv
= apr_thread_pool_create(&thrp
, 0, NUMBER_CONSUMERS
+ NUMBER_PRODUCERS
, p
);
99 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
101 for (i
= 0; i
< NUMBER_CONSUMERS
; i
++) {
102 rv
= apr_thread_pool_push(thrp
, consumer
, tc
, 0, NULL
);
103 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
106 for (i
= 0; i
< NUMBER_PRODUCERS
; i
++) {
107 rv
= apr_thread_pool_push(thrp
, producer
, tc
, 0, NULL
);
108 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
111 apr_sleep(SLEEP_TIME
* 1000000); /* sleep 10 seconds */
113 rv
= apr_queue_term(queue
);
114 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
116 rv
= apr_thread_pool_destroy(thrp
);
117 ABTS_INT_EQUAL(tc
, rv
, APR_SUCCESS
);
120 #endif /* APR_HAS_THREADS */
122 abts_suite
*testqueue(abts_suite
*suite
)
124 suite
= ADD_SUITE(suite
);
127 abts_run_test(suite
, test_queue_producer_consumer
, NULL
);
128 #endif /* APR_HAS_THREADS */