darfink / region-rs

A cross-platform virtual memory API written in Rust
https://darfink.github.io/region-rs/
MIT License
119 stars 23 forks source link

Question: How to use this in multi-threaded environment? #18

Closed Robbepop closed 2 years ago

Robbepop commented 2 years ago

First of all: Nice crate and API!

I am trying to use your region crate in order to efficiently model virtual memory for my virtual machine: https://github.com/Robbepop/runwell

The crate's docs are telling about issues with multiple threads but gave no suggestion in how to operate in that environment properly: https://docs.rs/region/3.0.0/region/#parallelism

For my project I have this thin-wrapper for region::Allocation: https://github.com/Robbepop/runwell/blob/master/crates/vmem/src/lib.rs ... and some unit tests on top: https://github.com/Robbepop/runwell/blob/master/crates/vmem/src/tests.rs

Using cargo test --package runwell_vmem results in 3 different scenarios:

Using cargo test --package runwell_vmem -- --test-threads 1 solves the problem. So it is pretty clear that the concurrently running test threads are kinda having conflicts with each other.

From the Rust point of view those tests do not have shared data and yet they compromise each other. I am sure I have some misconception or misunderstanding about the underlying APIs.

I was trying to remove as many of the unit tests to see which minimum subset still triggers the above bugs: At minimum the grow_to_capacity_works, grow_works, read_write_works unit tests need to be run.

Please help to resolve my misconceptions about this crate and its invariants and ideally help me on track so that I can actually use this nice API of yours. :)

Robbepop commented 2 years ago

Closed as my misunderstanding was with the alloc_at function since I thought it allows for resizing the virtually allocated memory which it does not. For this we'd need support for mremap on unix, however, to my knowledge there is no dedicated Windows API for resizing virtual memories which is probably why this crate does not support it.