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-2003 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 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 */
48 typedef struct s_rwsteal_tag {
49 pthread_t writer_id; /* writer's thread id */
54 #define RWLOCK_VALID 0xfacade
56 #define RWL_INIIALIZER \
57 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
58 PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
61 * read/write lock prototypes
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);
72 #endif /* __RWLOCK_H */