Skip to content

Commit b1cdb92

Browse files
works
1 parent 019af35 commit b1cdb92

File tree

11 files changed

+326
-53
lines changed

11 files changed

+326
-53
lines changed

include/physics/physics.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "util/logging.h"
4545
#include "util/object_counter.h"
4646
#include "util/ordered_weak_ptr.h"
47+
#include "util/lock.h"
4748

4849
namespace programmerjake
4950
{
@@ -292,7 +293,8 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
292293
{
293294
friend class PhysicsObject;
294295
private:
295-
mutable std::recursive_mutex theLock; // lock for all state except for this->chunks
296+
mutable checked_recursive_lock<200> theLockImp;
297+
mutable generic_lock_wrapper theLock; // lock for all state except for this->chunks
296298
double currentTime = 0;
297299
int variableSetIndex = 0;
298300
private:
@@ -303,6 +305,10 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
303305
return b.descriptor->blockShape;
304306
}
305307
public:
308+
PhysicsWorld()
309+
: theLock(theLockImp)
310+
{
311+
}
306312
BlockChunkMap chunks; // not locked by theLock
307313
BlockIterator getBlockIterator(PositionI pos)
308314
{
@@ -335,29 +341,29 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
335341
static constexpr float timeEPS = eps;
336342
inline double getCurrentTime() const
337343
{
338-
std::unique_lock<std::recursive_mutex> lockIt(theLock);
344+
std::unique_lock<generic_lock_wrapper> lockIt(theLock);
339345
return currentTime;
340346
}
341347
inline int getOldVariableSetIndex() const
342348
{
343-
std::unique_lock<std::recursive_mutex> lockIt(theLock);
349+
std::unique_lock<generic_lock_wrapper> lockIt(theLock);
344350
return variableSetIndex;
345351
}
346352
inline int getNewVariableSetIndex() const
347353
{
348-
std::unique_lock<std::recursive_mutex> lockIt(theLock);
354+
std::unique_lock<generic_lock_wrapper> lockIt(theLock);
349355
return 1 - variableSetIndex;
350356
}
351357
private:
352358
std::unordered_set<ordered_weak_ptr<PhysicsObject>> objects;
353359
void addObject(std::shared_ptr<PhysicsObject> o)
354360
{
355-
std::unique_lock<std::recursive_mutex> lockIt(theLock);
361+
std::unique_lock<generic_lock_wrapper> lockIt(theLock);
356362
objects.insert(o);
357363
}
358364
void removeObject(std::shared_ptr<PhysicsObject> o)
359365
{
360-
std::unique_lock<std::recursive_mutex> lockIt(theLock);
366+
std::unique_lock<generic_lock_wrapper> lockIt(theLock);
361367
objects.erase(o);
362368
}
363369
struct CollisionEvent final
@@ -419,8 +425,8 @@ inline void PhysicsObject::transferToNewWorld(std::shared_ptr<PhysicsWorld> newW
419425
std::shared_ptr<PhysicsWorld> world = getWorld();
420426
if(world == newWorld)
421427
return;
422-
std::unique_lock<std::recursive_mutex> worldLock(world->theLock, std::defer_lock);
423-
std::unique_lock<std::recursive_mutex> newWorldLock(newWorld->theLock, std::defer_lock);
428+
std::unique_lock<generic_lock_wrapper> worldLock(world->theLock, std::defer_lock);
429+
std::unique_lock<generic_lock_wrapper> newWorldLock(newWorld->theLock, std::defer_lock);
424430
std::lock(worldLock, newWorldLock);
425431
double deltaTime = newWorld->getCurrentTime() - world->getCurrentTime();
426432
for(double &v : objectTime)
@@ -655,7 +661,7 @@ inline PhysicsObject::~PhysicsObject()
655661
inline PositionF PhysicsObject::getPosition() const
656662
{
657663
auto world = getWorld();
658-
std::unique_lock<std::recursive_mutex> lockIt(world->theLock);
664+
std::unique_lock<generic_lock_wrapper> lockIt(world->theLock);
659665
int variableSetIndex = world->getOldVariableSetIndex();
660666
float deltaTime = world->getCurrentTime() - objectTime[variableSetIndex];
661667
VectorF gravityVector = getProperties().gravity;
@@ -667,7 +673,7 @@ inline PositionF PhysicsObject::getPosition() const
667673
inline VectorF PhysicsObject::getVelocity() const
668674
{
669675
auto world = getWorld();
670-
std::unique_lock<std::recursive_mutex> lockIt(world->theLock);
676+
std::unique_lock<generic_lock_wrapper> lockIt(world->theLock);
671677
int variableSetIndex = world->getOldVariableSetIndex();
672678
VectorF gravityVector = getProperties().gravity;
673679
if(!affectedByGravity || isSupported())
@@ -679,7 +685,7 @@ inline VectorF PhysicsObject::getVelocity() const
679685
inline void PhysicsObject::setNewState(PositionF newPosition, VectorF newVelocity)
680686
{
681687
auto world = getWorld();
682-
std::unique_lock<std::recursive_mutex> lockIt(world->theLock);
688+
std::unique_lock<generic_lock_wrapper> lockIt(world->theLock);
683689
int variableSetIndex = world->getNewVariableSetIndex();
684690
objectTime[variableSetIndex] = world->getCurrentTime();
685691
newPosition += position[variableSetIndex] * newStateCount;
@@ -696,7 +702,7 @@ inline void PhysicsObject::setNewState(PositionF newPosition, VectorF newVelocit
696702
inline void PhysicsObject::setupNewState()
697703
{
698704
auto world = getWorld();
699-
std::unique_lock<std::recursive_mutex> lockIt(world->theLock);
705+
std::unique_lock<generic_lock_wrapper> lockIt(world->theLock);
700706
int oldVariableSetIndex = world->getOldVariableSetIndex();
701707
int newVariableSetIndex = world->getNewVariableSetIndex();
702708
objectTime[newVariableSetIndex] = objectTime[oldVariableSetIndex];

include/util/block_chunk.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ typedef std::uint64_t BlockChunkInvalidateCountType;
173173

174174
struct BlockChunkSubchunk final
175175
{
176-
std::mutex lock;
177-
std::mutex biome_lock;
176+
checked_lock<150> lockImp;
177+
generic_lock_wrapper lock;
178178
std::shared_ptr<enum_array<Mesh, RenderLayer>> cachedMeshes;
179179
std::atomic_bool cachedMeshesUpToDate;
180180
WrappedEntity::SubchunkListType entityList;
@@ -203,7 +203,7 @@ struct BlockChunkSubchunk final
203203
{
204204
}
205205
BlockChunkSubchunk()
206-
: cachedMeshes(nullptr), cachedMeshesUpToDate(false), entityList([](WrappedEntity *)
206+
: lock(lockImp), cachedMeshes(nullptr), cachedMeshesUpToDate(false), entityList([](WrappedEntity *)
207207
{
208208
})
209209
{
@@ -263,11 +263,11 @@ class BlockChunkFullLock final
263263
{
264264
VectorI pos;
265265
BlockChunk *chunk;
266-
std::mutex &operator *() const
266+
generic_lock_wrapper &operator *() const
267267
{
268268
return chunk->subchunks[pos.x][pos.y][pos.z].lock;
269269
}
270-
std::mutex *operator ->() const
270+
generic_lock_wrapper *operator ->() const
271271
{
272272
return &operator *();
273273
}
@@ -331,11 +331,11 @@ class BlockChunkColumnLock final
331331
{
332332
VectorI pos;
333333
BlockChunk *chunk;
334-
std::mutex &operator *() const
334+
generic_lock_wrapper &operator *() const
335335
{
336336
return chunk->subchunks[pos.x][0][pos.z].lock;
337337
}
338-
std::mutex *operator ->() const
338+
generic_lock_wrapper *operator ->() const
339339
{
340340
return &operator *();
341341
}

include/util/block_iterator.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ class BlockIterator final
6363
{
6464
lock_manager.block_biome_lock.set(getSubchunk().lock);
6565
}
66-
void updateBiomeLock(WorldLockManager &lock_manager) const
66+
template <typename T>
67+
void updateLock(WorldLockManager &lock_manager, T beginV, T endV) const
6768
{
68-
lock_manager.block_biome_lock.set(getBiomeSubchunk().biome_lock);
69+
lock_manager.block_biome_lock.set(getSubchunk().lock, beginV, endV);
70+
}
71+
bool tryUpdateLock(WorldLockManager &lock_manager) const
72+
{
73+
return lock_manager.block_biome_lock.try_set(getSubchunk().lock);
6974
}
7075
BlockIterator(BlockChunk *chunk, BlockChunkMap *chunks, PositionI currentBasePosition, VectorI currentRelativePosition)
7176
: chunk(chunk), chunks(chunks), currentBasePosition(currentBasePosition), currentRelativePosition(currentRelativePosition)
@@ -245,7 +250,7 @@ class BlockIterator final
245250
}
246251
const BiomeProperties &getBiomeProperties(WorldLockManager &lock_manager) const
247252
{
248-
updateBiomeLock(lock_manager);
253+
updateLock(lock_manager);
249254
return getBiome().biomeProperties;
250255
}
251256
BlockUpdateIterator updatesBegin(WorldLockManager &lock_manager) const

include/util/iterator.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ namespace programmerjake
3131
namespace voxels
3232
{
3333

34-
template <typename IT1, typename IT2, typename = typename std::enable_if<std::is_same<typename std::remove_const<typename IT1::value_type>::type, typename std::remove_const<typename IT2::value_type>::type>::value>::type>
34+
template <typename IT1, typename IT2>
3535
struct joined_iterator_value_type_helper final
3636
{
37-
typedef typename std::common_type<typename IT1::value_type, typename IT2::value_type>::type value_type;
37+
static_assert(std::is_same<typename std::remove_const<typename std::iterator_traits<IT1>::value_type>::type, typename std::remove_const<typename std::iterator_traits<IT1>::value_type>::type>::value, "can't join incompatible iterators");
38+
typedef typename std::conditional<std::is_const<typename std::iterator_traits<IT1>::value_type>::value || std::is_const<typename std::iterator_traits<IT1>::value_type>::value, typename std::add_const<typename std::iterator_traits<IT1>::value_type>::type, typename std::iterator_traits<IT1>::value_type>::type type;
3839
};
3940

4041
template <typename TT1, typename TT2>
@@ -224,11 +225,11 @@ template <typename T, typename = void>
224225
struct iterator_type final
225226
{
226227
typedef typename iterator_type_helper<T>::type type;
227-
static type begin(T &c)
228+
static type begin(T &&c)
228229
{
229230
return iterator_type_helper<T>::beginImp(c);
230231
}
231-
static type end(T &c)
232+
static type end(T &&c)
232233
{
233234
return iterator_type_helper<T>::endImp(c);
234235
}
@@ -237,7 +238,7 @@ struct iterator_type final
237238
template <typename T, std::size_t N>
238239
struct iterator_type<T[N], void> final
239240
{
240-
typedef T *type;
241+
typedef typename std::decay<T[N]>::type type;
241242
static type begin(T (&c)[N])
242243
{
243244
return c;
@@ -252,24 +253,24 @@ template <typename CT>
252253
struct iterator_type<CT, typename std::enable_if<std::is_same<decltype(std::declval<CT>().begin()), decltype(std::declval<CT>().begin())>::value>::type> final
253254
{
254255
typedef typename std::decay<decltype(std::declval<CT>().begin())>::type type;
255-
static type begin(CT &c)
256+
static type begin(CT &&c)
256257
{
257258
return c.begin();
258259
}
259-
static type end(CT &c)
260+
static type end(CT &&c)
260261
{
261262
return c.end();
262263
}
263264
};
264265

265266
template <typename CT>
266-
range<typename iterator_type<CT>::type> to_range(CT &container)
267+
range<typename iterator_type<CT>::type> to_range(CT &&container)
267268
{
268269
return range<typename iterator_type<CT>::type>(iterator_type<CT>::begin(container), iterator_type<CT>::end(container));
269270
}
270271

271272
template <typename CT1, typename CT2>
272-
range<joined_iterator<typename iterator_type<CT1>::type, typename iterator_type<CT2>::type>> join_ranges(CT1 &c1, CT2 &c2)
273+
range<joined_iterator<typename iterator_type<CT1>::type, typename iterator_type<CT2>::type>> join_ranges(CT1 &&c1, CT2 &&c2)
273274
{
274275
typedef joined_iterator<typename iterator_type<CT1>::type, typename iterator_type<CT2>::type> joined_iterator_type;
275276
range<typename iterator_type<CT1>::type> r1 = to_range(c1);
@@ -283,6 +284,7 @@ template <typename T>
283284
struct empty_range_iterator final : public std::iterator<std::random_access_iterator_tag, T>
284285
{
285286
public:
287+
typedef T value_type;
286288
bool operator ==(const empty_range_iterator &rt) const
287289
{
288290
return true;
@@ -373,6 +375,7 @@ struct unit_range_iterator final : public std::iterator<std::forward_iterator_ta
373375
private:
374376
T *value;
375377
public:
378+
typedef T value_type;
376379
explicit unit_range_iterator(T &value)
377380
: value(std::addressof(value))
378381
{

0 commit comments

Comments
 (0)