]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/lock.h
Backport from BEE
[bacula/bacula] / bacula / src / stored / lock.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * Definitions for locking and blocking functions in the SD
18  *
19  * Kern Sibbald, pulled out of dev.h June 2007
20  *
21  */
22
23
24 #ifndef __LOCK_H
25 #define __LOCK_H 1
26
27 void    _lock_reservations(const char *file="**Unknown**", int line=0);
28 void    _unlock_reservations();
29 void    _lock_volumes(const char *file="**Unknown**", int line=0);
30 void    _unlock_volumes();
31
32 #ifdef  SD_DEBUG_LOCK
33
34 #define lock_reservations() \
35          do { Dmsg3(sd_dbglvl, "lock_reservations at %s:%d precnt=%d\n", \
36               __FILE__, __LINE__, \
37               reservations_lock_count); \
38               _lock_reservations(__FILE__, __LINE__); \
39               Dmsg0(sd_dbglvl, "lock_reservations: got lock\n"); \
40          } while (0)
41 #define unlock_reservations() \
42          do { Dmsg3(sd_dbglvl, "unlock_reservations at %s:%d precnt=%d\n", \
43               __FILE__, __LINE__, \
44               reservations_lock_count); \
45                    _unlock_reservations(); } while (0)
46
47 #define lock_volumes() \
48          do { Dmsg3(sd_dbglvl, "lock_volumes at %s:%d precnt=%d\n", \
49               __FILE__, __LINE__, \
50               vol_list_lock_count); \
51               _lock_volumes(__FILE__, __LINE__); \
52               Dmsg0(sd_dbglvl, "lock_volumes: got lock\n"); \
53          } while (0)
54
55 #define unlock_volumes() \
56          do { Dmsg3(sd_dbglvl, "unlock_volumes at %s:%d precnt=%d\n", \
57               __FILE__, __LINE__, \
58               vol_list_lock_count); \
59                    _unlock_volumes(); } while (0)
60
61 #else
62
63 #define lock_reservations() _lock_reservations(__FILE__, __LINE__)
64 #define unlock_reservations() _unlock_reservations()
65 #define lock_volumes() _lock_volumes(__FILE__, __LINE__)
66 #define unlock_volumes() _unlock_volumes()
67
68 #endif
69
70 #ifdef DEV_DEBUG_LOCK
71 #define Lock() dbg_Lock(__FILE__, __LINE__)
72 #define Unlock() dbg_Unlock(__FILE__, __LINE__)
73 #define rLock(locked) dbg_rLock(__FILE__, __LINE__, locked)
74 #define rUnlock() dbg_rUnlock(__FILE__, __LINE__)
75 #endif
76
77 #ifdef SD_DEBUG_LOCK
78 #define Lock_acquire() dbg_Lock_acquire(__FILE__, __LINE__)
79 #define Unlock_acquire() dbg_Unlock_acquire(__FILE__, __LINE__)
80 #define Lock_read_acquire() dbg_Lock_read_acquire(__FILE__, __LINE__)
81 #define Unlock_read_acquire() dbg_Unlock_read_acquire(__FILE__, __LINE__)
82 #define Lock_VolCatInfo() dbg_Lock_VolCatInfo(__FILE__, __LINE__)
83 #define Unlock_VolCatInfo() dbg_Unlock_VolCatInfo(__FILE__, __LINE__)
84 #endif
85
86 #define block_device(d, s)          _block_device(__FILE__, __LINE__, (d), s)
87 #define unblock_device(d)           _unblock_device(__FILE__, __LINE__, (d))
88 #define steal_device_lock(d, p, s)  _steal_device_lock(__FILE__, __LINE__, (d), (p), s)
89 #define give_back_device_lock(d, p) _give_back_device_lock(__FILE__, __LINE__, (d), (p))
90
91 /* m_blocked states (mutually exclusive) */
92 enum {
93    BST_NOT_BLOCKED = 0,               /* not blocked */
94    BST_UNMOUNTED,                     /* User unmounted device */
95    BST_WAITING_FOR_SYSOP,             /* Waiting for operator to mount tape */
96    BST_DOING_ACQUIRE,                 /* Opening/validating/moving tape */
97    BST_WRITING_LABEL,                 /* Labeling a tape */
98    BST_UNMOUNTED_WAITING_FOR_SYSOP,   /* User unmounted during wait for op */
99    BST_MOUNT,                         /* Mount request */
100    BST_DESPOOLING,                    /* Despooling -- i.e. multiple writes */
101    BST_RELEASING                      /* Releasing the device */
102 };
103
104 typedef struct s_steal_lock {
105    pthread_t  no_wait_id;             /* id of no wait thread */
106    int        dev_blocked;            /* state */
107    int        dev_prev_blocked;       /* previous blocked state */
108 } bsteal_lock_t;
109
110 /*
111  * Used in unblock() call
112  */
113 enum {
114    DEV_LOCKED = true,
115    DEV_UNLOCKED = false
116 };
117
118 #endif