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 Thread Read/Write locking code. It permits
21  *  multiple readers but only one writer.
22  *
23  *  Kern Sibbald, January MMI
24  *
25  *  This code adapted from "Programming with POSIX Threads", by
26  *    David R. Butenhof
27  *
28  */
29 
30 #ifndef __DEVLOCK_H
31 #define __DEVLOCK_H 1
32 
33 struct take_lock_t {
34    pthread_t  writer_id;              /* id of writer */
35    int        reason;                 /* save reason */
36    int        prev_reason;            /* previous reason */
37 };
38 
39 
40 class devlock {
41 private:
42    pthread_mutex_t   mutex;
43    pthread_cond_t    read;            /* wait for read */
44    pthread_cond_t    write;           /* wait for write */
45    pthread_t         writer_id;       /* writer's thread id */
46    int               priority;        /* used in deadlock detection */
47    int               valid;           /* set when valid */
48    int               r_active;        /* readers active */
49    int               w_active;        /* writers active */
50    int               r_wait;          /* readers waiting */
51    int               w_wait;          /* writers waiting */
52    int               reason;          /* reason for lock */
53    int               prev_reason;     /* previous reason */
54    bool              can_take;        /* can the lock be taken? */
55 
56 
57 public:
58    devlock(int reason, bool can_take=false);
59    ~devlock();
60    int init(int init_priority);
61    int destroy();
62    int take_lock(take_lock_t *hold, int reason);
63    int return_lock(take_lock_t *hold);
new_reason(int nreason)64    void new_reason(int nreason) { prev_reason = reason; reason = nreason; };
restore_reason()65    void restore_reason() { reason = prev_reason; prev_reason = 0; };
66 
67    int writelock(int reason, bool can_take=false);
68    int writetrylock();
69    int writeunlock();
70    void write_release();
71 
72    int readunlock();
73    int readlock();
74    int readtrylock();
75    void read_release();
76 
77 };
78 
79 
80 #define DEVLOCK_VALID  0xfadbec
81 
82 #define DEVLOCK_INIIALIZER \
83    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
84     PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0}
85 
86 #endif /* __DEVLOCK_H */
87