2 * Copyright (c) 2011, Mark Heily <mark@heily.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <pthread_workqueue.h>
39 printf("worker %ld running\n", x);
41 printf("worker %ld finished\n", x);
44 void run_idle_test(pthread_workqueue_t wq)
49 for (i = 0; i < 100; i++) {
50 rv = pthread_workqueue_additem_np(wq, f, (void *) i, NULL, NULL);
58 unsigned long idle = pthread_workqueue_peek_np("combined_idle");
59 unsigned long norml_idle = pthread_workqueue_peek_np("idle");
60 unsigned long ocomm_idle = pthread_workqueue_peek_np("ocomm_idle");
61 printf("idle = %lu (overcommit = %lu non-overcommit = %lu)\n",
62 idle, ocomm_idle, norml_idle);
63 if (idle == 0 || (norml_idle == 1 && ocomm_idle == 0))
68 printf("\n*** ERROR: idle threads were not reaped properly\n");
75 * Enqueue a large number of short-lived workitems, to allow observation
76 * of how idle threads are terminated.
78 int main(int argc, char *argv[])
80 pthread_workqueue_t wq;
81 pthread_workqueue_t ocwq;
82 pthread_workqueue_attr_t attr;
83 pthread_workqueue_attr_t ocattr;
88 rounds = atoi(argv[1]);
92 pthread_workqueue_attr_init_np(&attr);
93 pthread_workqueue_attr_setovercommit_np(&attr, 0);
94 rv = pthread_workqueue_create_np(&wq, &attr);
97 pthread_workqueue_attr_init_np(&ocattr);
98 pthread_workqueue_attr_setovercommit_np(&ocattr, 1);
99 rv = pthread_workqueue_create_np(&ocwq, &ocattr);
100 if (rv != 0) abort();
102 for (i = 0; i < rounds; i++) {
106 printf("\n---\nOK: all excess idle threads have been terminated after %d rounds.\n", rounds);