xref: /onnv-gate/usr/src/lib/libldap4/include/thq.h (revision 3857:21b9b714e4ab)
1 /*
2  *
3  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  */
7 
8 #pragma ident	"%Z%%M%	%I%	%E% SMI"
9 
10 #include <pthread.h>
11 
12 typedef void * (*PFP)(void *);
13 
14 typedef void * (*PFP2)(void *, void *);
15 
16 typedef struct tq_listS {
17 	void * arg;
18 	struct tq_listS * next;
19 } tq_listT, * tq_listTp;
20 
21 typedef struct {
22   tq_listTp first;            /* first element in the queue                */
23 	tq_listTp last;             /* last  element in the queue                */
24 	pthread_mutex_t   lock;     /* queue mutex                               */
25 	pthread_cond_t    cond;     /* queue condition to signal new elements    */
26 	int *     shutdown;         /* variable to test to see shutdown condition*/
27 	int       shutalloc;        /* is the shutdown variable allocated locally*/
28 	int       stopping;         /* queue is currently stopping               */
29 	int       queue_size;       /* current size of the queue                 */
30 	int       nb_thr;           /* current nb of threads pocessing the queue */
31 	int       thr_waiting;      /* current nb of threads waiting             */
32 	int       max_thr;          /* max allowed threads to process the queue  */
33 	int       min_thr;          /* min nb of threads to keep alive           */
34 	PFP       doit;             /* function to call to process the queue     */
35 	PFP2      endit;            /* function called before to end the thread  */
36 	void *    arg;              /* argument to pass to the doit/endit func.  */
37 	pthread_t * tids;           /* array of thread ids for watchdog          */
38 } tqT, * tqTp;
39 
40 extern tqTp   tq_alloc(PFP process_func,   /* function to process the queue */
41                        PFP2 shutdown_func, /* function called before to end */
42                        void * func_arg,    /* arg passed to both functions  */
43                        int * shutdown,     /* shutdown variable to test     */
44                        int max,            /* max allowed threads           */
45                        int min);           /* min allowed threads           */
46 
47 extern int    tq_queue(tqTp queue,         /* pointer to the queue          */
48                        void * arg);        /* element to be queued          */
49 
50 /* tq_dequeue returns the first "arg" passed to tq_queue */
51 extern void * tq_dequeue(tqTp queue,            /* pointer to the queue          */
52                          void * endit_arg);     /* pointer to "shutdown" arguments */
53 /*
54  * tq_end_thread terminates the current
55  * thread and restarts a new one if necessary
56  */
57 extern void tq_end_thread (tqTp queue,          /* pointer to the queue          */
58                           void * endit_arg);    /* pointer to "shutdown" arguments */
59 
60 /*
61  * tq_shutdown, shutdown the queue (alternate way to shutdown if you don't
62  * have a global shutdown integer
63  *
64  * shutdown can be immediate (1) or delayed until there is nothing more
65  * in the queue (immadiate = 0)
66  *
67  * when you call this function, no more argument can be queued using
68  * tq_queue.
69  *
70  * when tq_dequeue returns, the queue pointer is not allocated anymore
71  *
72  */
73 extern void tq_shutdown(tqTp queue,     /* pointer to the queue          */
74                         int immediate); /* 1: don't wait, 0: wait for queue */
75 																				/*                   to be empty    */
76