02-10-2012, 01:44 AM
Okay, I dug really deep into this and I think it's working out nicely. I've decided to make a shared pointer to cChunk to be the entity that is passed to and from routines. I'm thread-locking everything up to protect against race conditions. This also requires that many of the interface functions be rewritten. The changes are really massive, I think I've re-written about 30 % of the entire codebase by now, and still not finished. I hope no-one commits anything big to the repository right now, or else I'll choke on the conflicts
The main idea that I'm trying to implement is that the code doesn't get a direct cChunk pointer anywhere. Anyone asking for a chunk will get a shared pointer, and a valid one. This way there's no problem with a thread accessing a chunk when another thread has deleted it. However, it requires that all code checks the returned pointer whether the chunk is valid (loaded / generated) and act accordingly if it is not.
I've run into a few cases that'll need rewriting for thread safety as well - clients in a server, entities in a world, block entities in a chunk - these all will need a similar set of changes in the near future. I'm leaving them as-is for now - it worked till now, it'll probably still work, though with the occasional danger of a crash.
The main idea that I'm trying to implement is that the code doesn't get a direct cChunk pointer anywhere. Anyone asking for a chunk will get a shared pointer, and a valid one. This way there's no problem with a thread accessing a chunk when another thread has deleted it. However, it requires that all code checks the returned pointer whether the chunk is valid (loaded / generated) and act accordingly if it is not.
I've run into a few cases that'll need rewriting for thread safety as well - clients in a server, entities in a world, block entities in a chunk - these all will need a similar set of changes in the near future. I'm leaving them as-is for now - it worked till now, it'll probably still work, though with the occasional danger of a crash.