]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/devlock.h
Allow plugins to add drives to vss snapshot
[bacula/bacula] / bacula / src / lib / devlock.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Bacula Thread Read/Write locking code. It permits
30  *  multiple readers but only one writer.
31  *
32  *  Kern Sibbald, January MMI
33  *
34  *  This code adapted from "Programming with POSIX Threads", by
35  *    David R. Butenhof
36  *
37  */
38
39 #ifndef __DEVLOCK_H
40 #define __DEVLOCK_H 1
41
42 struct take_lock_t {
43    pthread_t  writer_id;              /* id of writer */
44    int        reason;                 /* save reason */
45    int        prev_reason;            /* previous reason */
46 };
47
48
49 class devlock {
50 private:
51    pthread_mutex_t   mutex;
52    pthread_cond_t    read;            /* wait for read */
53    pthread_cond_t    write;           /* wait for write */
54    pthread_t         writer_id;       /* writer's thread id */
55    int               priority;        /* used in deadlock detection */
56    int               valid;           /* set when valid */
57    int               r_active;        /* readers active */
58    int               w_active;        /* writers active */
59    int               r_wait;          /* readers waiting */
60    int               w_wait;          /* writers waiting */
61    int               reason;          /* reason for lock */
62    int               prev_reason;     /* previous reason */
63    bool              can_take;        /* can the lock be taken? */
64
65
66 public:
67    devlock(int reason, bool can_take=false);
68    ~devlock();
69    int init(int priority);
70    int destroy();
71    int take_lock(take_lock_t *hold, int reason);
72    int return_lock(take_lock_t *hold);
73    void new_reason(int nreason) { prev_reason = reason; reason = nreason; };
74    void restore_reason() { reason = prev_reason; prev_reason = 0; };
75
76    int writelock(int reason, bool can_take=false); 
77    int writetrylock();
78    int writeunlock();
79    void write_release();
80
81    int readunlock();
82    int readlock();
83    int readtrylock();
84    void read_release();
85
86 };
87
88
89 #define DEVLOCK_VALID  0xfadbec
90
91 #define DEVLOCK_INIIALIZER \
92    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
93     PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0}
94
95 #endif /* __DEVLOCK_H */