source: Revenant/geoworld/include/gwpaging.h@ 7ef8ec4

port/mars-tycoon
Last change on this file since 7ef8ec4 was 80a6a52, checked in by Jonathan Neufeld <support@…>, 3 years ago

Get to a compile state for terrain procedural generation

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#ifndef __GEOWORLDPAGING_H__
2#define __GEOWORLDPAGING_H__
3
4#include <string>
5#include <fstream>
6#include <vector>
7
8#include <mars_calc.h>
9#include <mars_streams.h>
10#include <mars_util.h>
11
12#include <boost/thread.hpp>
13
14#include "gwtypes.h"
15#include "gwutil.h"
16#include "geoworld.h"
17#include "stratum.h"
18
19namespace geoworld
20{
21 typedef mars::OctTree_SphereIndexed< MineralPtr, GeoSpherePack::SCALAR > OctTreeMineralDeposits;
22
23 class GeoWorldPage
24 {
25 private:
26 GeoHeightMap _heightmap;
27 Stratum _stratum;
28 OctTreeMineralDeposits _minerals;
29
30 mutable bool _bShouldAbort;
31 mutable size_t _nIFaceRefCount;
32 GW_MUTEX(atomic);
33 GW_MUTEX(work);
34
35 void checkOutIFace () const;
36 void checkInIFace () const;
37
38 public:
39 class IFaceEx : public std::exception
40 {
41 public:
42 const std::string message;
43
44 IFaceEx (const char * szMsg) : message(szMsg) {}
45 };
46
47 class Interface
48 {
49 private:
50 GeoWorldPage * _pParent;
51
52 GW_MUTEX(atomic);
53
54 public:
55 GeoHeightMap & heightmap;
56 Stratum & stratum;
57 OctTreeMineralDeposits & minerals;
58
59 Interface (GeoWorldPage * pParent);
60 Interface (Interface && move);
61 ~Interface();
62
63 Stratum::View * createStratumView (const WorldUnit nLeft, const WorldUnit nTop, const int nShallow, const WorldUnit nRight, const WorldUnit nBottom, const WorldUnit nDeep) const;
64 Stratum::View * createStratumView (const WorldUnit nLeft, const WorldUnit nTop, const WorldUnit nRight, const WorldUnit nBottom) const;
65 void releaseStratumView (const Stratum::View * pView) const;
66
67 GeoHeightMap::View * createHeightMapView (const unsigned short nLeft, const unsigned short nTop, const unsigned short nRight, const unsigned short nBottom) const;
68 void releaseHeightMapView (const GeoHeightMap::View * pView) const;
69
70 inline bool isAborted() const { return _pParent->isAborted(); }
71 };
72
73 const unsigned int x, y;
74
75 GeoWorldPage(const GeoHeightMap::View & hm, const unsigned int nPageX, const unsigned int nPageY, const unsigned short nDepth, const size_t nLayers);
76 ~GeoWorldPage();
77
78 void abort (const bool bWait = false) const;
79 bool isAborted() const;
80
81 const Interface acquireInterface() const { return Interface(const_cast< GeoWorldPage * > (this)); }
82 Interface acquireInterface() { return Interface(this); }
83 };
84
85 class InstanceFile
86 {
87 public:
88 class Ex : public std::exception
89 {
90 public:
91 const std::string message;
92
93 Ex (const char * szMsg) : message(szMsg) {}
94 };
95
96 struct InfoHeader
97 {
98 char IDENT[4];
99 unsigned long endian;
100 unsigned short version;
101 unsigned short stratumcount;
102 unsigned short dim, depth;
103
104 InfoHeader();
105 bool validate() const;
106 };
107
108 InstanceFile();
109 InstanceFile(const unsigned short nDim, const unsigned short nDepth, const unsigned short nLayerCount);
110 ~InstanceFile();
111
112 inline const Stratum & getStratum() const { return *_pStratum; }
113 inline Stratum & getStratum() { return *_pStratum; }
114
115 inline GeoHeightMap::View & top () { return _pStratum->getBase(); }
116 inline const GeoHeightMap::View & top () const { return _pStratum->getBase(); }
117
118 inline GeoStratumField::View & layer (const size_t c) { return _pStratum->field(c); }
119 inline const GeoStratumField::View & layer (const size_t c) const { return _pStratum->field(c); }
120
121 static void write (const std::string & sFileName, const InstanceFile * pInstFile);
122 static InstanceFile * load (const std::string & sFileName);
123
124 private:
125 Stratum * _pStratum;
126 unsigned short _nDim, _nDepth;
127
128 static std::fstream * open (const std::string & sFileName, const std::ios::openmode & nMode);
129
130 std::ostream & operator >> (std::ostream & outs) const;
131 std::istream & operator << (std::istream & ins);
132 };
133
134 class PagingStore
135 {
136 public:
137 PagingStore (const std::string & sFileName, const std::string & sStoreDirectory, const std::string & sFilePrefix = "gwpage-", const std::string & sFileSuffix = ".gwp");
138
139 GeoWorldPage * requestPage (const unsigned int nPageX, const unsigned int nPageY);
140
141 void releasePage (GeoWorldPage * pPage);
142 std::string makeFilename (const unsigned int nPageX, const unsigned int nPageY) const;
143 static unsigned long calculatePageID (const unsigned int nPageX, const unsigned int nPageY);
144
145 inline unsigned int getNumPagesWide () const { return _pFile->getTilesWide(); }
146 inline unsigned int getNumPagesHigh () const { return _pFile->getTilesHigh(); }
147
148 ~PagingStore();
149
150 private:
151 FileSource * _pFile;
152 std::string _sFilePrefix, _sFileSuffix, _sStoreDirectory;
153 FileSource::MainDEM::Block * _pUtilityBlock;
154
155 InstanceFile * loadInstanceFile( const unsigned int nPageX, const unsigned int nPageY ) const;
156 void logBorders( const GeoHeightMap::View &base, const unsigned int nPageX, const unsigned int nPageY );
157
158 GW_MUTEX(mtxMaster);
159
160 class PageMutexProvider
161 {
162 public:
163 class NeighborhoodMutex
164 {
165 public:
166 enum Spot
167 {
168 N = 0,
169 W = 1,
170 E = 2,
171 S = 3,
172 C = 4,
173
174 TotalSpots = 5
175 };
176
177 explicit NeighborhoodMutex (boost::shared_mutex * const * ppmtx);
178 NeighborhoodMutex(NeighborhoodMutex && movable);
179 ~NeighborhoodMutex();
180
181 void genLock();
182
183 private:
184 NeighborhoodMutex (const NeighborhoodMutex & copy);
185 NeighborhoodMutex & operator = (const NeighborhoodMutex & copy);
186
187 typedef boost::upgrade_lock< boost::shared_mutex > preamble_lock;
188 typedef boost::upgrade_to_unique_lock< boost::shared_mutex > upgraded_lock;
189 typedef boost::unique_lock< boost::shared_mutex > gen_lock;
190 typedef boost::shared_lock< boost::shared_mutex > qry_lock;
191
192 typedef DetachableScopedTryLock< boost::shared_mutex, qry_lock > DetachableScopedQueryTryLock;
193 typedef DetachableScopedTryLock< boost::shared_mutex, gen_lock > DetachableScopedGenerationTryLock;
194
195 boost::shared_mutex * _pmtxNeighbors[TotalSpots];
196 qry_lock * _plckNeighbors[TotalSpots];
197 gen_lock * _plckCenter;
198 preamble_lock * _plckPreamble;
199
200 STATIC_GW_MUTEX(ATOMIC);
201 };
202
203 PageMutexProvider (const unsigned int nCellsX, const unsigned int nCellsY);
204 ~PageMutexProvider();
205
206 NeighborhoodMutex acquireNeighborhood (const long nPageX, const long nPageY);
207
208 private:
209 boost::shared_mutex ** _ppShMutexes;
210 unsigned int _nW, _nH;
211 size_t _szLen;
212
213 size_t idx(const long nPageX, const long nPageY) const;
214 } * _pMutexProvider;
215 };
216}
217
218#endif
Note: See TracBrowser for help on using the repository browser.