10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * 3*3857Sstevel * Copyright 1998 Sun Microsystems, Inc. All rights reserved. 4*3857Sstevel * Use is subject to license terms. 50Sstevel@tonic-gate * 60Sstevel@tonic-gate */ 70Sstevel@tonic-gate 80Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 90Sstevel@tonic-gate 100Sstevel@tonic-gate #include <pthread.h> 110Sstevel@tonic-gate 120Sstevel@tonic-gate typedef void * (*PFP)(void *); 130Sstevel@tonic-gate 140Sstevel@tonic-gate typedef void * (*PFP2)(void *, void *); 150Sstevel@tonic-gate 160Sstevel@tonic-gate typedef struct tq_listS { 170Sstevel@tonic-gate void * arg; 180Sstevel@tonic-gate struct tq_listS * next; 190Sstevel@tonic-gate } tq_listT, * tq_listTp; 200Sstevel@tonic-gate 210Sstevel@tonic-gate typedef struct { 220Sstevel@tonic-gate tq_listTp first; /* first element in the queue */ 230Sstevel@tonic-gate tq_listTp last; /* last element in the queue */ 240Sstevel@tonic-gate pthread_mutex_t lock; /* queue mutex */ 250Sstevel@tonic-gate pthread_cond_t cond; /* queue condition to signal new elements */ 260Sstevel@tonic-gate int * shutdown; /* variable to test to see shutdown condition*/ 270Sstevel@tonic-gate int shutalloc; /* is the shutdown variable allocated locally*/ 280Sstevel@tonic-gate int stopping; /* queue is currently stopping */ 290Sstevel@tonic-gate int queue_size; /* current size of the queue */ 300Sstevel@tonic-gate int nb_thr; /* current nb of threads pocessing the queue */ 310Sstevel@tonic-gate int thr_waiting; /* current nb of threads waiting */ 320Sstevel@tonic-gate int max_thr; /* max allowed threads to process the queue */ 330Sstevel@tonic-gate int min_thr; /* min nb of threads to keep alive */ 340Sstevel@tonic-gate PFP doit; /* function to call to process the queue */ 350Sstevel@tonic-gate PFP2 endit; /* function called before to end the thread */ 360Sstevel@tonic-gate void * arg; /* argument to pass to the doit/endit func. */ 370Sstevel@tonic-gate pthread_t * tids; /* array of thread ids for watchdog */ 380Sstevel@tonic-gate } tqT, * tqTp; 390Sstevel@tonic-gate 400Sstevel@tonic-gate extern tqTp tq_alloc(PFP process_func, /* function to process the queue */ 410Sstevel@tonic-gate PFP2 shutdown_func, /* function called before to end */ 420Sstevel@tonic-gate void * func_arg, /* arg passed to both functions */ 430Sstevel@tonic-gate int * shutdown, /* shutdown variable to test */ 440Sstevel@tonic-gate int max, /* max allowed threads */ 450Sstevel@tonic-gate int min); /* min allowed threads */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate extern int tq_queue(tqTp queue, /* pointer to the queue */ 480Sstevel@tonic-gate void * arg); /* element to be queued */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate /* tq_dequeue returns the first "arg" passed to tq_queue */ 510Sstevel@tonic-gate extern void * tq_dequeue(tqTp queue, /* pointer to the queue */ 520Sstevel@tonic-gate void * endit_arg); /* pointer to "shutdown" arguments */ 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * tq_end_thread terminates the current 550Sstevel@tonic-gate * thread and restarts a new one if necessary 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate extern void tq_end_thread (tqTp queue, /* pointer to the queue */ 580Sstevel@tonic-gate void * endit_arg); /* pointer to "shutdown" arguments */ 590Sstevel@tonic-gate 600Sstevel@tonic-gate /* 610Sstevel@tonic-gate * tq_shutdown, shutdown the queue (alternate way to shutdown if you don't 620Sstevel@tonic-gate * have a global shutdown integer 630Sstevel@tonic-gate * 640Sstevel@tonic-gate * shutdown can be immediate (1) or delayed until there is nothing more 650Sstevel@tonic-gate * in the queue (immadiate = 0) 660Sstevel@tonic-gate * 670Sstevel@tonic-gate * when you call this function, no more argument can be queued using 680Sstevel@tonic-gate * tq_queue. 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * when tq_dequeue returns, the queue pointer is not allocated anymore 710Sstevel@tonic-gate * 720Sstevel@tonic-gate */ 730Sstevel@tonic-gate extern void tq_shutdown(tqTp queue, /* pointer to the queue */ 740Sstevel@tonic-gate int immediate); /* 1: don't wait, 0: wait for queue */ 750Sstevel@tonic-gate /* to be empty */ 76