]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/devlock.h
Backport from BEE
[bacula/bacula] / bacula / src / lib / devlock.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    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    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * Bacula Thread Read/Write locking code. It permits
18  *  multiple readers but only one writer.
19  *
20  *  Kern Sibbald, January MMI
21  *
22  *  This code adapted from "Programming with POSIX Threads", by
23  *    David R. Butenhof
24  *
25  */
26
27 #ifndef __DEVLOCK_H
28 #define __DEVLOCK_H 1
29
30 struct take_lock_t {
31    pthread_t  writer_id;              /* id of writer */
32    int        reason;                 /* save reason */
33    int        prev_reason;            /* previous reason */
34 };
35
36
37 class devlock {
38 private:
39    pthread_mutex_t   mutex;
40    pthread_cond_t    read;            /* wait for read */
41    pthread_cond_t    write;           /* wait for write */
42    pthread_t         writer_id;       /* writer's thread id */
43    int               priority;        /* used in deadlock detection */
44    int               valid;           /* set when valid */
45    int               r_active;        /* readers active */
46    int               w_active;        /* writers active */
47    int               r_wait;          /* readers waiting */
48    int               w_wait;          /* writers waiting */
49    int               reason;          /* reason for lock */
50    int               prev_reason;     /* previous reason */
51    bool              can_take;        /* can the lock be taken? */
52
53
54 public:
55    devlock(int reason, bool can_take=false);
56    ~devlock();
57    int init(int init_priority);
58    int destroy();
59    int take_lock(take_lock_t *hold, int reason);
60    int return_lock(take_lock_t *hold);
61    void new_reason(int nreason) { prev_reason = reason; reason = nreason; };
62    void restore_reason() { reason = prev_reason; prev_reason = 0; };
63
64    int writelock(int reason, bool can_take=false);
65    int writetrylock();
66    int writeunlock();
67    void write_release();
68
69    int readunlock();
70    int readlock();
71    int readtrylock();
72    void read_release();
73
74 };
75
76
77 #define DEVLOCK_VALID  0xfadbec
78
79 #define DEVLOCK_INIIALIZER \
80    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
81     PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0}
82
83 #endif /* __DEVLOCK_H */