]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rwlock.h
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / src / lib / rwlock.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 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 #ifndef __RWLOCK_H
30 #define __RWLOCK_H 1
31
32 typedef struct s_rwlock_tag {
33    pthread_mutex_t   mutex;
34    pthread_cond_t    read;            /* wait for read */
35    pthread_cond_t    write;           /* wait for write */
36    pthread_t         writer_id;       /* writer's thread id */
37    int               priority;        /* used in deadlock detection */
38    int               valid;           /* set when valid */
39    int               r_active;        /* readers active */
40    int               w_active;        /* writers active */
41    int               r_wait;          /* readers waiting */
42    int               w_wait;          /* writers waiting */
43 } brwlock_t;
44
45 typedef struct s_rwsteal_tag {
46    pthread_t         writer_id;       /* writer's thread id */
47    int               state;
48 } brwsteal_t;
49
50
51 #define RWLOCK_VALID  0xfacade
52
53 #define RWL_INIIALIZER \
54    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
55     PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
56
57 #define rwl_writelock(x)     rwl_writelock_p((x), __FILE__, __LINE__)
58
59 /*
60  * read/write lock prototypes
61  */
62 extern int rwl_init(brwlock_t *wrlock, int priority=0);
63 extern int rwl_destroy(brwlock_t *rwlock);
64 extern int rwl_readlock(brwlock_t *rwlock);
65 extern int rwl_readtrylock(brwlock_t *rwlock);
66 extern int rwl_readunlock(brwlock_t *rwlock);
67 extern int rwl_writelock_p(brwlock_t *rwlock,
68                            const char *file="*unknown*", int line=0);
69 extern int rwl_writetrylock(brwlock_t *rwlock);
70 extern int rwl_writeunlock(brwlock_t *rwlock);
71 extern bool is_rwl_valid(brwlock_t *rwl);
72
73 #endif /* __RWLOCK_H */