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
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)
69   {
70     prev_reason = reason;
71     reason = nreason;
72   }
restore_reason()73   void restore_reason()
74   {
75     reason = prev_reason;
76     prev_reason = 0;
77   }
78 
79   int Writelock(int reason, bool can_take = false);
80   int writetrylock();
81   int writeunlock();
82   void WriteRelease();
83 
84   int readunlock();
85   int readlock();
86   int readtrylock();
87   void ReadRelease();
88 };
89 
90 
91 #define DEVLOCK_VALID 0xfadbec
92 
93 #define DEVLOCK_INIIALIZER                                 \
94   {                                                        \
95     PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,   \
96         PTHREAD_COND_INITIALIZER, DEVOCK_VALID, 0, 0, 0, 0 \
97   }
98 
99 #endif /* BAREOS_LIB_DEVLOCK_H_ */
100