elast0ny / shared_memory

A Rust wrapper around native shared memory for Linux and Windows
380 stars 51 forks source link

Emulate POSIX shared memory behavior on Windows #59

Closed defiori closed 3 years ago

defiori commented 3 years ago

Shmem currently behaves differently on Windows than Unix. Specifically, Shmem::set_owner has no effect on the underlying memory object on Windows, as the standard Windows shared file mapping doesn't allow persistence. This means that dropping a unique Shmem will destroy the shared memory even when Shmem::is_owner returns false. This clashes with POSIX behaviour, which persists the memory unless shm_unlink() is called and allows reopening even when all handles have previously been closed.

This PR unifies shared memory behaviour on Windows and Unix and adds a test to check for compliance. It follows the C++ Boost implementation of creating a file to back the shared memory which is renamed and marked for deletion when an owning Shmem drops, but persists otherwise. Shmem::set_owner stops being a no-op on Windows and now behaves as expected.

elast0ny commented 3 years ago

Hi, thanks a lot for the PR.

I will try to review it and fix the build issues in the next couple of days

defiori commented 3 years ago

Sounds good, thanks. The latest commit fixes clippy's complaints on Windows, the remaining build issue isn't specific to this PR.

elast0ny commented 3 years ago

Thanks again for the PR.

I have converted your code to rely on std as much as it could and merged it into master.

I will publish a new version of the crate (0.12.X) in the next couple of days.

Let me know if you spot any issues with my refactoring ;)

defiori commented 3 years ago

This looks much cleaner, thanks. I just added one minor nitpick in a comment, otherwise it looks good. Looking forward to v0.12!