AlexIIL / LibMultiPart

Mozilla Public License 2.0
29 stars 3 forks source link

Improve thread-safety of PartContainer shape caches #39

Closed Kneelawk closed 2 years ago

Kneelawk commented 2 years ago

This PR

This pull request fixes the specific threading issue encountered in #37, where a PartContainer's shape cache was cleared while it was building a new one. In #37, this caused a NullPointerException when the PartContainer attempted to union the, now null, incomplete culling shape cache with a part's culling shape.

This PR fixes the threading issue by doing two things: first, this changes the cache building functions to deal with local VoxelShapes instead of the PartContainer's global ones; second, this changes PartContainer to keep its cached shapes in AtomicReferences, allowing them to be safely read and modified by multiple threads, even on architectures where 32-bit simple operations are not necessarily atomic, like ARM.

Testing

This PR was quickly run in an environment with VanillaParts and WiredRedstone. Due to the nature of this issue, it is not really possible to easily reproduce and therefore not easy to test if this PR actually fixes it.

Related Issues

This PR fixes #37

AlexIIL commented 2 years ago

I don't want AbstractPart methods to be called on different threads (including getShape methods) so I'll need to fix this a different way.