1 /* 2 BAREOS® - Backup Archiving REcovery Open Sourced 3 4 Copyright (C) 2001-2010 Free Software Foundation Europe e.V. 5 Copyright (C) 2016-2016 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 25 * David R. Butenhof 26 */ 27 /** 28 * @file 29 * BAREOS Thread Read/Write locking code. It permits 30 * multiple readers but only one writer. 31 */ 32 33 #ifndef BAREOS_LIB_DEVLOCK_H_ 34 #define BAREOS_LIB_DEVLOCK_H_ 1 35 36 struct take_lock_t { 37 pthread_t writer_id; /* id of writer */ 38 int reason; /* save reason */ 39 int prev_reason; /* previous reason */ 40 }; 41 42 43 class DevLock { 44 private: 45 pthread_mutex_t mutex; 46 pthread_cond_t read; /* wait for read */ 47 pthread_cond_t write; /* wait for write */ 48 pthread_t writer_id; /* writer's thread id */ 49 int priority; /* used in deadlock detection */ 50 int valid; /* set when valid */ 51 int r_active; /* readers active */ 52 int w_active; /* writers active */ 53 int r_wait; /* readers waiting */ 54 int w_wait; /* writers waiting */ 55 int reason; /* reason for lock */ 56 int prev_reason; /* previous reason */ 57 bool can_take; /* can the lock be taken? */ 58 59 60 public: 61 DevLock(int reason, bool can_take=false); 62 ~DevLock(); 63 static DevLock *new_devlock(); 64 int init(int initial_priority); 65 int destroy(); 66 int TakeLock(take_lock_t *hold, int reason); 67 int ReturnLock(take_lock_t *hold); NewReason(int nreason)68 void NewReason(int nreason) { prev_reason = reason; reason = nreason; } restore_reason()69 void restore_reason() { reason = prev_reason; prev_reason = 0; } 70 71 int Writelock(int reason, bool can_take=false); 72 int writetrylock(); 73 int writeunlock(); 74 void WriteRelease(); 75 76 int readunlock(); 77 int readlock(); 78 int readtrylock(); 79 void ReadRelease(); 80 81 }; 82 83 84 #define DEVLOCK_VALID 0xfadbec 85 86 #define DEVLOCK_INIIALIZER \ 87 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, \ 88 PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0} 89 90 #endif /* BAREOS_LIB_DEVLOCK_H_ */ 91