]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rwlock.h
This commit was manufactured by cvs2svn to create tag
[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) 2001-2006 Kern Sibbald
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
18    version 2 as amended with additional clauses defined in the
19    file LICENSE in the main source directory.
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 
24    the file LICENSE for additional details.
25
26  */
27
28 #ifndef __RWLOCK_H
29 #define __RWLOCK_H 1
30
31 typedef struct s_rwlock_tag {
32    pthread_mutex_t   mutex;
33    pthread_cond_t    read;            /* wait for read */
34    pthread_cond_t    write;           /* wait for write */
35    pthread_t         writer_id;       /* writer's thread id */
36    int               valid;           /* set when valid */
37    int               r_active;        /* readers active */
38    int               w_active;        /* writers active */
39    int               r_wait;          /* readers waiting */
40    int               w_wait;          /* writers waiting */
41 } brwlock_t;
42
43 typedef struct s_rwsteal_tag {
44    pthread_t         writer_id;       /* writer's thread id */
45    int               state;
46 } brwsteal_t;
47
48
49 #define RWLOCK_VALID  0xfacade
50
51 #define RWL_INIIALIZER \
52    {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
53     PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
54
55 /*
56  * read/write lock prototypes
57  */
58 extern int rwl_init(brwlock_t *wrlock);
59 extern int rwl_destroy(brwlock_t *rwlock);
60 extern int rwl_readlock(brwlock_t *rwlock);
61 extern int rwl_readtrylock(brwlock_t *rwlock);
62 extern int rwl_readunlock(brwlock_t *rwlock);
63 extern int rwl_writelock(brwlock_t *rwlock);
64 extern int rwl_writetrylock(brwlock_t *rwlock);
65 extern int rwl_writeunlock(brwlock_t *rwlock);
66
67 #endif /* __RWLOCK_H */