Description
Currently I'm having some issues with efficiently calling physfs from multiple threads. For some context here's what I've got going on:
- A task that does a massive enumeration through the entire virtual filesystem (to find archives to mount in the background)
- A task to enumerate and find the items to display in my tool's file browser
- A task that just changes what dirs are mounted when the user changes some path in my tool's settings
You can assume that all of this is running on different threads. The problem I ran into is that my massive enumeration will block the other two. Do you have any ideas for this issue? Here are the three solutions I've come up with, but they all have caveats:
- Implement shared mutexes, where a unique lock is only used in functions that modify state. Cons: This only alleviates the issue for concurrent function calls that do not modify state, and would require changes to the native implementations.
- Make a copy of needed state at the beginning of expensive functions, so the mutex only needs to be locked during cloning. Cons: cloning the opaque pointers, and cloning state would have to be carefully considered per each function it's implemented on so it doesn't cause unexpected outcomes.
- Instantiating physfs contexts, as it appears you have planned for V4. Cons: the user has to manage the state of contexts across threads.
I didn't rule out option 1 because although it doesn't fully solve the problem on its own it could be a minor optimization to employ alongside another solution. Honestly it might make sense to do all three, as option two would help with performance for those who don't choose to maintain multiple contexts. All in all, 3 seems the most promising, so I'm glad that you have it planned.