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