1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /******************************************************************************
19  * $Id: copylocks.h 1936 2013-07-09 22:10:29Z dhall $
20  *
21  *****************************************************************************/
22 
23 /** @file
24  * class XXX interface
25  */
26 
27 #ifndef COPYLOCKS_H_
28 #define COPYLOCKS_H_
29 
30 #include <set>
31 #include <sys/types.h>
32 //#define NDEBUG
33 #include <cassert>
34 
35 #include <boost/thread.hpp>
36 #include <boost/interprocess/shared_memory_object.hpp>
37 #include <boost/interprocess/mapped_region.hpp>
38 
39 #include "brmtypes.h"
40 #include "mastersegmenttable.h"
41 
42 #include "shmkeys.h"
43 #include "undoable.h"
44 
45 #include "brmshmimpl.h"
46 
47 /* Should load these from a config file */
48 #define CL_INITIAL_SIZE (50*sizeof(CopyLockEntry))
49 #define CL_INCREMENT (50*sizeof(CopyLockEntry))
50 
51 #if defined(_MSC_VER) && defined(xxxCOPYLOCKS_DLLEXPORT)
52 #define EXPORT __declspec(dllexport)
53 #else
54 #define EXPORT
55 #endif
56 
57 
58 namespace idbdatafile
59 {
60 class IDBDataFile;
61 }
62 
63 namespace BRM
64 {
65 
66 struct CopyLockEntry
67 {
68     LBID_t start;
69     int size;
70     VER_t txnID;
71     EXPORT CopyLockEntry();
72 };
73 
74 class CopyLocksImpl
75 {
76 public:
77     static CopyLocksImpl* makeCopyLocksImpl(unsigned key, off_t size, bool readOnly = false);
78 
grow(unsigned key,off_t size)79     inline void grow(unsigned key, off_t size)
80 #ifdef NDEBUG
81     {
82         fCopyLocks.grow(key, size);
83     }
84 #else
85     {
86         int rc = fCopyLocks.grow(key, size);
87         idbassert(rc == 0);
88     }
89 #endif
makeReadOnly()90     inline void makeReadOnly()
91     {
92         fCopyLocks.setReadOnly();
93     }
clear(unsigned key,off_t size)94     inline void clear(unsigned key, off_t size)
95     {
96         fCopyLocks.clear(key, size);
97     }
swapout(BRMShmImpl & rhs)98     inline void swapout(BRMShmImpl& rhs)
99     {
100         fCopyLocks.swap(rhs);
101         rhs.destroy();
102     }
key()103     inline unsigned key() const
104     {
105         return fCopyLocks.key();
106     }
107 
get()108     inline CopyLockEntry* get() const
109     {
110         return reinterpret_cast<CopyLockEntry*>(fCopyLocks.fMapreg.get_address());
111     }
112 
113 private:
114     CopyLocksImpl(unsigned key, off_t size, bool readOnly = false);
115     ~CopyLocksImpl();
116     CopyLocksImpl(const CopyLocksImpl& rhs);
117     CopyLocksImpl& operator=(const CopyLocksImpl& rhs);
118 
119     BRMShmImpl fCopyLocks;
120 
121     static boost::mutex fInstanceMutex;
122     static CopyLocksImpl* fInstance;
123 };
124 
125 class CopyLocks : public Undoable
126 {
127 public:
128 
129     enum OPS
130     {
131         NONE,
132         READ,
133         WRITE
134     };
135 
136     EXPORT CopyLocks();
137     EXPORT ~CopyLocks();
138 
139     EXPORT void lockRange(const LBIDRange& range, VER_t txnID);
140     EXPORT void releaseRange(const LBIDRange& range);
141     EXPORT bool isLocked(const LBIDRange& range) const;
142     EXPORT void rollback(VER_t txnID);
143 
144     EXPORT void lock(OPS op);
145     EXPORT void release(OPS op);
146     EXPORT void setReadOnly();
147     EXPORT void getCurrentTxnIDs(std::set<VER_t>& txnList) const;
148 
149     EXPORT void forceRelease(const LBIDRange& range);
150 
151 private:
152     CopyLocks(const CopyLocks&);
153     CopyLocks& operator=(const CopyLocks&);
154 
155     key_t chooseShmkey();
156     void growCL();
157 
158     CopyLockEntry* entries;
159     key_t currentShmkey;
160     int shmid;    //shmid's necessary?
161     MSTEntry* shminfo;
162     MasterSegmentTable mst;
163     bool r_only;
164     static boost::mutex mutex;
165     static const int MAX_IO_RETRIES = 10;
166     ShmKeys fShmKeys;
167     CopyLocksImpl* fCopyLocksImpl;
168 };
169 
170 }
171 
172 #undef EXPORT
173 
174 #endif
175