randomPoison / cs-bindgen

Experiment in using Rust to build a library that can be loaded by Unity across multiple platforms
4 stars 0 forks source link

Improve thread-safety for handle types #17

Open randomPoison opened 4 years ago

randomPoison commented 4 years ago

The current code generation for handle types works in a single-threaded context, but is not fully robust in a multi-threaded context. On the Rust side we wrap the data in a Mutex so it's safe to call methods concurrently from multiple threads. However, on the C# side the Dispose implementation doesn't have any synchronization so I believe we could attempt to drop the Rust data at the sime time from two or more threads, resulting in a double-free.

I'm not super familiar with thread-safety practices in C#, so it'll take some research to determine how to best address this issue. Ideally we should do it in a way that doesn't require use to perform locking/synchronization on both the C# and Rust side, as that would add a lot of unnecessary overhead.