[Minetest-dev] Do we need envlock?

Ryan Kwolek kwolekr at minetest.net
Mon Apr 6 00:46:53 UTC 2015


I think envlock may ultimately be unnecessary.

Envlock locks the *whole* environment, meaning:
  - Map
  - Players
  - Time of day
  - Active objects
  - Active block list
  - Active block modifiers

CURRENTLY, there only two threads accessing these: ServerThread and 
EmergeThread.  The only significant contention here is map; all the 
others resources, if at all accessed by the EmergeThread, would be 
inside an on_generate callback.  These non-map environment resource 
accesses I believe are rare enough to be locked inside of each SAPI call 
without any significant performance degradation - remember that locks 
are not slow unless they're in contention.

That being said, it is trivial to split envlock into locks corresponding 
to each of the 6 (or perhaps, 5) categories.
The map, however, may possibly not need locking at all (aside from SAPI 
accesses; we'll get to that in a minute) if mapgen design were cleaned up.

EmergeThread creates *new* blocks.  So if we were to develop efficient, 
deterministic methods of placing discrete structures such as trees or 
caves onto new chunks of map without having to access neighboring blocks 
that already exist, the envlock could be removed.
The only issue here would be synchronizing access to data structures 
which is trivial.  ServerThread could pre-allocate the space inside of 
these data structures for each of the blocks, initialized to NULL, the 
pointers to which passed along in block-making data to the Emerge queue.
Attempted map access to one of these "dummy" blocks would simply act as 
if it did not exist at all.  When the EmergeThread is finished preparing 
the generated or loaded blocks, it would then atomically swap in the 
addresses for the new blocks into the pointer spaces passed along in the 
block make data.

As for SAPI access:  This could be solved by only permitting access to 
the map data that had just been generated, or adding an abstraction 
layer that would create a Maplock if an access outside the generated 
region is performed.

Of course, this is all useless if we have ambitions for multithreaded 
scripting =]

  - kwolekr


More information about the Minetest-dev mailing list