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) 2001-2006 Kern Sibbald
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.
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.
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 */
43 typedef struct s_rwsteal_tag {
44 pthread_t writer_id; /* writer's thread id */
49 #define RWLOCK_VALID 0xfacade
51 #define RWL_INIIALIZER \
52 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \
53 PTHREAD_COND_INITIALIZER, RWLOCK_VALID, 0, 0, 0, 0}
56 * read/write lock prototypes
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);
67 #endif /* __RWLOCK_H */