Move Non Send Resources into thread locals #5135
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Fix problem with world being send and dropping non send resources on the wrong thread.
Alternative to #3519,
Solution
thread_local_objectcrate to move non send resources into thread local memory. This means that trying to access the non send resource off the thread that spawned it will end up with the resource not being found.thread_local_objectdrops the objects it keeps when the thread is dropped instead of when it's instance is dropped.Advantages
Disadvantages
world.remove_non_send_resourcevs removing the resource from the ThreadLocalResource,ThreadLocalResouce::remove.thread_local_objectare not dropped when theThreadLocalResourceis dropped. They are only dropped when the thread is dropped orThreadLocalResource;:removeis called on the corrent thread. I added an explicit drop for the thread thatThreadLocalResourceis dropped on, but this doesn't help if the world has been sent. So this is a potential memory leak.Migration Guide
// TODO
ToDo