2 * Bacula Thread Read/Write locking code. It permits
3 * multiple readers but only one writer.
5 * Kern Sibbald, January MMI
7 * This code adapted from "Programming with POSIX Threads", by
14 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
36 typedef struct 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 */
48 #define RWLOCK_VALID 0xfacade
50 #define RWL_INIIALIZER \
51 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
52 PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
55 * read/write lock prototypes
57 extern int rwl_init(rwlock_t *wrlock);
58 extern int rwl_destroy(rwlock_t *rwlock);
59 extern int rwl_readlock(rwlock_t *rwlock);
60 extern int rwl_readtrylock(rwlock_t *rwlock);
61 extern int rwl_readunlock(rwlock_t *rwlock);
62 extern int rwl_writelock(rwlock_t *rwlock);
63 extern int rwl_writetrylock(rwlock_t *rwlock);
64 extern int rwl_writeunlock(rwlock_t *rwlock);
66 #endif /* __RWLOCK_H */