this repo has no description
at fixPythonPipStalling 108 lines 3.4 kB view raw
1/*- 2 * Copyright (c) 2011, Mark Heily <mark@heily.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 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. 14 * 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. 25 * 26 */ 27 28#include <err.h> 29#include <stdio.h> 30#include <stdlib.h> 31#include <unistd.h> 32 33#include <pthread_workqueue.h> 34 35void f(void *arg) 36{ 37 long x = (long) arg; 38 39 printf("worker %ld running\n", x); 40 sleep(1); 41 printf("worker %ld finished\n", x); 42} 43 44void run_idle_test(pthread_workqueue_t wq) 45{ 46 long i; 47 int rv; 48 49 for (i = 0; i < 100; i++) { 50 rv = pthread_workqueue_additem_np(wq, f, (void *) i, NULL, NULL); 51 if (rv != 0) abort(); 52 } 53 54 sleep(2); 55 56 int rounds = 0; 57 for (;;) { 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)) 64 break; 65 66 sleep(1); 67 if (rounds++ > 240) { 68 printf("\n*** ERROR: idle threads were not reaped properly\n"); 69 exit(1); 70 } 71 } 72} 73 74/* 75 * Enqueue a large number of short-lived workitems, to allow observation 76 * of how idle threads are terminated. 77 */ 78int main(int argc, char *argv[]) 79{ 80 pthread_workqueue_t wq; 81 pthread_workqueue_t ocwq; 82 pthread_workqueue_attr_t attr; 83 pthread_workqueue_attr_t ocattr; 84 int i, rounds; 85 int rv; 86 87 if (argc == 2) 88 rounds = atoi(argv[1]); 89 else 90 rounds = 1; 91 92 pthread_workqueue_attr_init_np(&attr); 93 pthread_workqueue_attr_setovercommit_np(&attr, 0); 94 rv = pthread_workqueue_create_np(&wq, &attr); 95 if (rv != 0) abort(); 96 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(); 101 102 for (i = 0; i < rounds; i++) { 103 run_idle_test(wq); 104 run_idle_test(ocwq); 105 } 106 printf("\n---\nOK: all excess idle threads have been terminated after %d rounds.\n", rounds); 107 exit(0); 108}