1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 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 worker thread class
21  *
22  *  Kern Sibbald, August 2014
23  *
24  */
25 
26 #ifndef __WORKER_H
27 #define __WORKER_H 1
28 
29 enum WORKER_STATE {
30    WORKER_WAIT,
31    WORKER_RUN,
32    WORKER_QUIT
33 };
34 
35 class worker : public SMARTALLOC {
36 private:
37    pthread_mutex_t   mutex;           /* main fifo mutex */
38    pthread_mutex_t   fmutex;          /* free pool mutex */
39    pthread_cond_t    full_wait;       /* wait because full */
40    pthread_cond_t    empty_wait;      /* wait because empty */
41    pthread_cond_t    m_wait;          /* wait state */
42    pthread_t         worker_id;       /* worker's thread id */
43    void        *(*user_sub)(void *);  /* user subroutine */
44    void             *user_ctx;        /* user context */
45    flist            *fifo;            /* work fifo */
46    alist            *fpool;           /* free pool */
47    int               valid;           /* set when valid */
48    WORKER_STATE      m_state;         /* worker state */
49    bool              worker_running;  /* set when worker running */
50    bool              worker_waiting;  /* set when worker is waiting */
51    bool              done;            /* work done */
52    bool              waiting_on_empty; /* hung waiting on empty queue */
53 
54 
55 public:
56    worker(int fifo_size = 10);
57    ~worker();
58    int init(int fifo_size = 10);
59    int destroy();
60 
61    bool queue(void *item);
62    void *dequeue();
63    int start(void *(*sub)(void *), void *wctx);  /* Start worker */
64    void wait_queue_empty();           /* Main thread wait for fifo to be emptied */
65    void discard_queue();              /* Discard the fifo queue */
66    int stop();                        /* Stop worker */
67    void wait();                       /* Wait for main thread to release us */
68    void set_wait_state();
69    void set_run_state();
70    void set_quit_state();
71    void finish_work();
72 
73    void *pop_free_buffer();
74    void push_free_buffer(void *buf);
75 
set_running()76    inline void set_running() { worker_running = true; };
is_running()77    inline bool is_running() const { return worker_running; };
get_ctx()78    inline void *get_ctx() const { return user_ctx; };
79 
80    void release_lock();               /* Cleanup release lock */
81 
empty()82    inline bool empty() const { return fifo->empty(); };
full()83    inline bool full() const { return fifo->full(); };
size()84    inline int size() const { return fifo->size(); };
is_quit_state()85    inline bool is_quit_state() const { return m_state == WORKER_QUIT; };
is_wait_state()86    inline bool is_wait_state() const { return m_state == WORKER_WAIT; };
87 };
88 
89 
worker(int fifo_size)90 inline worker::worker(int fifo_size)
91 {
92    init(fifo_size);
93 }
94 
~worker()95 inline worker::~worker()
96 {
97    destroy();
98 }
99 
100 
101 #define WORKER_VALID  0xfadbec
102 
103 #endif /* __WORKER_H */
104