1 /* 2 BAREOS® - Backup Archiving REcovery Open Sourced 3 4 Copyright (C) 2001-2010 Free Software Foundation Europe e.V. 5 Copyright (C) 2016-2018 Bareos GmbH & Co. KG 6 7 This program is Free Software; you can redistribute it and/or 8 modify it under the terms of version three of the GNU Affero General Public 9 License as published by the Free Software Foundation and included 10 in the file LICENSE. 11 12 This program is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Affero General Public License for more details. 16 17 You should have received a copy of the GNU Affero General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 02110-1301, USA. 21 */ 22 /* 23 * Kern Sibbald, January MMI 24 * This code adapted from "Programming with POSIX Threads", by David R. Butenhof 25 */ 26 /** 27 * @file 28 * BAREOS Thread Read/Write locking code. It permits 29 * multiple readers but only one writer. 30 */ 31 32 #ifndef BAREOS_LIB_RWLOCK_H_ 33 #define BAREOS_LIB_RWLOCK_H_ 1 34 35 typedef struct s_rwlock_tag { 36 pthread_mutex_t mutex; 37 pthread_cond_t read; /* wait for read */ 38 pthread_cond_t write; /* wait for write */ 39 pthread_t writer_id; /* writer's thread id */ 40 int priority; /* used in deadlock detection */ 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 */ s_rwlock_tags_rwlock_tag46 s_rwlock_tag() 47 { 48 mutex = PTHREAD_MUTEX_INITIALIZER; 49 read = PTHREAD_COND_INITIALIZER; 50 write = PTHREAD_COND_INITIALIZER; 51 writer_id = 0; 52 priority = 0; 53 valid = 0; 54 r_active = 0; 55 w_active = 0; 56 r_wait = 0; 57 w_wait = 0; 58 }; 59 } brwlock_t; 60 61 #define RWLOCK_VALID 0xfacade 62 63 #define RwlWritelock(x) RwlWritelock_p((x), __FILE__, __LINE__) 64 65 /** 66 * read/write lock prototypes 67 */ 68 extern int RwlInit(brwlock_t* rwl, int priority = 0); 69 extern int RwlDestroy(brwlock_t* rwl); 70 extern bool RwlIsInit(brwlock_t* rwl); 71 extern int RwlReadlock(brwlock_t* rwl); 72 extern int RwlReadtrylock(brwlock_t* rwl); 73 extern int RwlReadunlock(brwlock_t* rwl); 74 extern int RwlWritelock_p(brwlock_t* rwl, 75 const char* file = "*unknown*", 76 int line = 0); 77 extern int RwlWritetrylock(brwlock_t* rwl); 78 extern int RwlWriteunlock(brwlock_t* rwl); 79 80 #endif /* BAREOS_LIB_RWLOCK_H_ */ 81