]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/workq.h
add jobq+serial.h+priorities+recycling
[bacula/bacula] / bacula / src / lib / workq.h
1 /*
2  * Bacula work queue routines. Permits passing work to
3  *  multiple threads.
4  *
5  *  Kern Sibbald, January MMI
6  *
7  *  This code adapted from "Programming with POSIX Threads", by
8  *    David R. Butenhof
9  *
10  *   Version $Id$
11  */
12 /*
13    Copyright (C) 2000-2003 Kern Sibbald and John Walker
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #ifndef __WORKQ_H 
33 #define __WORKQ_H 1
34
35 /* 
36  * Structure to keep track of work queue request
37  */
38 typedef struct workq_ele_tag {
39    struct workq_ele_tag *next;
40    void                 *data;
41 } workq_ele_t;
42
43 /* 
44  * Structure describing a work queue
45  */
46 typedef struct workq_tag {
47    pthread_mutex_t   mutex;           /* queue access control */
48    pthread_cond_t    work;            /* wait for work */
49    pthread_attr_t    attr;            /* create detached threads */
50    workq_ele_t       *first, *last;   /* work queue */
51    int               valid;           /* queue initialized */
52    int               quit;            /* workq should quit */
53    int               max_workers;     /* max threads */
54    int               num_workers;     /* current threads */
55    int               idle_workers;    /* idle threads */
56    void             *(*engine)(void *arg); /* user engine */
57 } workq_t;
58
59 #define WORKQ_VALID  0xdec1992
60
61 extern int workq_init(
62               workq_t *wq,
63               int     threads,        /* maximum threads */
64               void   *(*engine)(void *)   /* engine routine */
65                     );
66 extern int workq_destroy(workq_t *wq);
67 extern int workq_add(workq_t *wq, void *element, workq_ele_t **work_item, int priority);
68 extern int workq_remove(workq_t *wq, workq_ele_t *work_item);
69
70 #endif /* __WORKQ_H */