]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rwlock.h
Backport from BEE
[bacula/bacula] / bacula / src / lib / rwlock.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  *   Version $Id$
26  *
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 bool rwl_is_init(brwlock_t *rwl);
65 extern int rwl_readlock(brwlock_t *rwlock);
66 extern int rwl_readtrylock(brwlock_t *rwlock);
67 extern int rwl_readunlock(brwlock_t *rwlock);
68 extern int rwl_writelock_p(brwlock_t *rwlock,
69                            const char *file="*unknown*", int line=0);
70 extern int rwl_writetrylock(brwlock_t *rwlock);
71 extern int rwl_writeunlock(brwlock_t *rwlock);
72
73 #endif /* __RWLOCK_H */