]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/workq.h
Fix bug reported by jesper@schmitz.computer where bat hangs on FreeBSD
[bacula/bacula] / bacula / src / lib / workq.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Bacula work queue routines. Permits passing work to
21  *  multiple threads.
22  *
23  *  Kern Sibbald, January MMI
24  *
25  *  This code adapted from "Programming with POSIX Threads", by
26  *    David R. Butenhof
27  */
28
29 #ifndef __WORKQ_H
30 #define __WORKQ_H 1
31
32 /*
33  * Structure to keep track of work queue request
34  */
35 typedef struct workq_ele_tag {
36    struct workq_ele_tag *next;
37    void                 *data;
38 } workq_ele_t;
39
40 /*
41  * Structure describing a work queue
42  */
43 typedef struct workq_tag {
44    pthread_mutex_t   mutex;           /* queue access control */
45    pthread_cond_t    work;            /* wait for work */
46    pthread_cond_t    idle;            /* wait for idle */
47    pthread_attr_t    attr;            /* create detached threads */
48    workq_ele_t       *first, *last;   /* work queue */
49    int               valid;           /* queue initialized */
50    int               quit;            /* workq should quit */
51    int               max_workers;     /* max threads */
52    int               num_workers;     /* current threads */
53    int               idle_workers;    /* idle threads */
54    int               num_running;     /* Number of jobs running */
55    void             *(*engine)(void *arg); /* user engine */
56 } workq_t;
57
58 #define WORKQ_VALID  0xdec1992
59
60 extern int workq_init(
61               workq_t *wq,
62               int     threads,        /* maximum threads */
63               void   *(*engine)(void *)   /* engine routine */
64                     );
65 extern int workq_destroy(workq_t *wq);
66 extern int workq_add(workq_t *wq, void *element, workq_ele_t **work_item, int priority);
67 extern int workq_remove(workq_t *wq, workq_ele_t *work_item);
68 extern int workq_wait_idle(workq_t *wq);
69
70
71 #endif /* __WORKQ_H */