]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rwlock.h
Fix bug #1893
[bacula/bacula] / bacula / src / lib / rwlock.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Bacula Thread Read/Write locking code. It permits
30  *  multiple readers but only one writer.
31  *
32  *  Kern Sibbald, January MMI
33  *
34  *  This code adapted from "Programming with POSIX Threads", by
35  *    David R. Butenhof
36  *
37  *   Version $Id$
38  *
39  */
40
41 #ifndef __RWLOCK_H
42 #define __RWLOCK_H 1
43
44 typedef struct s_rwlock_tag {
45    pthread_mutex_t   mutex;
46    pthread_cond_t    read;            /* wait for read */
47    pthread_cond_t    write;           /* wait for write */
48    pthread_t         writer_id;       /* writer's thread id */
49    int               priority;        /* used in deadlock detection */
50    int               valid;           /* set when valid */
51    int               r_active;        /* readers active */
52    int               w_active;        /* writers active */
53    int               r_wait;          /* readers waiting */
54    int               w_wait;          /* writers waiting */
55 } brwlock_t;
56
57 typedef struct s_rwsteal_tag {
58    pthread_t         writer_id;       /* writer's thread id */
59    int               state;
60 } brwsteal_t;
61
62
63 #define RWLOCK_VALID  0xfacade
64
65 #define RWL_INIIALIZER \
66    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
67     PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
68
69 #define rwl_writelock(x)     rwl_writelock_p((x), __FILE__, __LINE__)
70
71 /*
72  * read/write lock prototypes
73  */
74 extern int rwl_init(brwlock_t *wrlock, int priority=0);
75 extern int rwl_destroy(brwlock_t *rwlock);
76 extern bool rwl_is_init(brwlock_t *rwl);
77 extern int rwl_readlock(brwlock_t *rwlock);
78 extern int rwl_readtrylock(brwlock_t *rwlock);
79 extern int rwl_readunlock(brwlock_t *rwlock);
80 extern int rwl_writelock_p(brwlock_t *rwlock,
81                            const char *file="*unknown*", int line=0);
82 extern int rwl_writetrylock(brwlock_t *rwlock);
83 extern int rwl_writeunlock(brwlock_t *rwlock);
84
85 #endif /* __RWLOCK_H */