@@ -16,7 +16,7 @@ namespace zylann {
1616
1717// Locking on a large voxel data structure can be done with this, instead of putting RWLocks on every chunk or
1818// every octree node. This also reduces the amount of required mutexes considerably (that matters on some platforms with
19- // low limits).
19+ // low limits; it also reduces memory usage, as mutex implementations can occupy a lot of bytes ).
2020//
2121// Some methods allow to `try` locking areas. They should be used in contextes where blocking is worse than
2222// delaying operations. Such contextes may then postpone their work, or cancel it. If that's not possible, then it
@@ -48,11 +48,14 @@ class SpatialLock3D {
4848 bool try_lock_read (const BoxBounds3i &box) {
4949 _boxes_mutex.lock ();
5050 if (can_lock_for_read (box)) {
51- _boxes.push_back (Box{ box, MODE_READ,
51+ _boxes.push_back (
52+ Box{ box,
53+ MODE_READ,
5254#ifdef ZN_SPATIAL_LOCK_3D_CHECKS
53- Thread::get_caller_id ()
55+ Thread::get_caller_id ()
5456#endif
55- });
57+ }
58+ );
5659 _boxes_mutex.unlock ();
5760 return true ;
5861 } else {
@@ -74,11 +77,14 @@ class SpatialLock3D {
7477 bool try_lock_write (const BoxBounds3i &box) {
7578 _boxes_mutex.lock ();
7679 if (can_lock_for_write (box)) {
77- _boxes.push_back (Box{ box, MODE_WRITE,
80+ _boxes.push_back (
81+ Box{ box,
82+ MODE_WRITE,
7883#ifdef ZN_SPATIAL_LOCK_3D_CHECKS
79- Thread::get_caller_id ()
84+ Thread::get_caller_id ()
8085#endif
81- });
86+ }
87+ );
8288 _boxes_mutex.unlock ();
8389 return true ;
8490 } else {
@@ -144,8 +150,9 @@ class SpatialLock3D {
144150 // This is a deadlock.
145151 // Note: this is not true if threads only lock for reading, but if we didn't ever write we'd not use locks.
146152 // Note: this is also not true if threads use `try_lock` instead!
147- ZN_ASSERT_RETURN_V_MSG (existing_box.thread_id != thread_id, false ,
148- " Locking two areas from the same threads is not allowed" );
153+ ZN_ASSERT_RETURN_V_MSG (
154+ existing_box.thread_id != thread_id, false , " Locking two areas from the same threads is not allowed"
155+ );
149156#endif
150157 if (existing_box.bounds .intersects (box) && existing_box.mode == MODE_WRITE) {
151158 return false ;
@@ -164,8 +171,9 @@ class SpatialLock3D {
164171 const Box &existing_box = _boxes[i];
165172
166173#ifdef ZN_SPATIAL_LOCK_3D_CHECKS
167- ZN_ASSERT_RETURN_V_MSG (existing_box.thread_id != thread_id, false ,
168- " Locking two areas from the same threads is not allowed" );
174+ ZN_ASSERT_RETURN_V_MSG (
175+ existing_box.thread_id != thread_id, false , " Locking two areas from the same threads is not allowed"
176+ );
169177#endif
170178 if (existing_box.bounds .intersects (box)) {
171179 return false ;
0 commit comments