]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rwlock.h
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[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    Copyright (C) 2000-2003 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #ifndef __RWLOCK_H
34 #define __RWLOCK_H 1
35
36 typedef struct s_rwlock_tag {
37    pthread_mutex_t   mutex;
38    pthread_cond_t    read;            /* wait for read */
39    pthread_cond_t    write;           /* wait for write */
40    pthread_t         writer_id;       /* writer's thread id */
41    int               valid;           /* set when valid */
42    int               r_active;        /* readers active */
43    int               w_active;        /* writers active */
44    int               r_wait;          /* readers waiting */
45    int               w_wait;          /* writers waiting */
46 } brwlock_t;
47
48 typedef struct s_rwsteal_tag {
49    pthread_t         writer_id;       /* writer's thread id */
50    int               state;
51 } brwsteal_t;
52
53
54 #define RWLOCK_VALID  0xfacade
55
56 #define RWL_INIIALIZER \
57    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
58     PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
59
60 /*
61  * read/write lock prototypes
62  */
63 extern int rwl_init(brwlock_t *wrlock);
64 extern int rwl_destroy(brwlock_t *rwlock);
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(brwlock_t *rwlock);
69 extern int rwl_writetrylock(brwlock_t *rwlock);
70 extern int rwl_writeunlock(brwlock_t *rwlock);
71
72 #endif /* __RWLOCK_H */