2 Bacula® - The Network Backup Solution
4 Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
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.
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.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Bacula Thread Read/Write locking code. It permits
18 * multiple readers but only one writer.
20 * Kern Sibbald, January MMI
22 * This code adapted from "Programming with POSIX Threads", by
31 pthread_t writer_id; /* id of writer */
32 int reason; /* save reason */
33 int prev_reason; /* previous reason */
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? */
55 devlock(int reason, bool can_take=false);
57 int init(int init_priority);
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; };
64 int writelock(int reason, bool can_take=false);
77 #define DEVLOCK_VALID 0xfadbec
79 #define DEVLOCK_INIIALIZER \
80 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
81 PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0}
83 #endif /* __DEVLOCK_H */