Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: is IpcSharedMemory currently read-only? #241

Open
gterzian opened this issue Aug 7, 2019 · 5 comments
Open

Question: is IpcSharedMemory currently read-only? #241

gterzian opened this issue Aug 7, 2019 · 5 comments

Comments

@gterzian
Copy link
Member

gterzian commented Aug 7, 2019

I couldn't find a way to mutate the contents of shared memory using the existing API. For example DerefMut is not implemented.

Is this intended, or am I missing something on how to actually do the mutation?

I would like to send some shared memory across process, then mutate it and have the other process see the mutation.

I've tried to do this with unsafe code, and I'm actually not sure if that is working or not(I can see the mutation in the process doing it, however I'm still testing if the other process sees it as well).

See https://github.com/servo/servo/pull/23909/files#diff-2967b47a1530b8945948650780afabcaR89

@sagudev
Copy link
Member

sagudev commented Aug 20, 2024

Reading #126 I think not, but I think it should be possible to send same IpcSharedMemory back after inner data was changed.

@mrobinson
Copy link
Member

mrobinson commented Oct 11, 2024

A possibility to make this safe is to create a MutableIpcSharedMemory type that cannot be cloned, copied, or serialized. It shouldn't be possible to have more than one handle to the memory inside. This version should be more safely mutable and can then be converted into an immutable IpcSharedMemory.

@sagudev
Copy link
Member

sagudev commented Oct 13, 2024

A possibility to make this safe is to create a MutableIpcSharedMemory type that cannot be cloned, copied, or serialized. It shouldn't be possible to have more than one handle to the memory inside. This version should be more safely mutable and can then be converted into an immutable IpcSharedMemory.

But we still have the same problem as IpcSharedMemory is clone and serializable which means we can create multiple MutableIpcSharedMemory that points to same data.

@mrobinson
Copy link
Member

The idea would be that you could create an IpcSharedMemory from a MutableIpcSharedMemory, but that conversion could only go one way. Once you've done the conversion into the actually shareable type (that has Clone and Serialize/Deserialize) no more mutation would be possible.

This wouldn't allow making, for example, a lock free shared memory queue, but it would help with the case where you want to produce some data into shared memory without copying.

@sagudev
Copy link
Member

sagudev commented Oct 13, 2024

This wouldn't allow making, for example, a lock free shared memory queue, but it would help with the case where you want to produce some data into shared memory without copying.

Ah, that's different use case then described above:

I would like to send some shared memory across process, then mutate it and have the other process see the mutation.

And your proposed solution is similar to what was described in #126 (comment)

This could be fixed by extending the API to allow decoupling the allocation of the buffer from initialisation. This way we could create a new buffer for each frame, then fill it with data, and finally send it to the consumer process, where it is read and finally freed.

One possible approach for that would be introducing some kind of proto-SHM-object, which is allocated but not initialised, and can be borrowed mutably to fill it with data, before it's converted into a regular immutable SHM object for sending. Another approach would be allowing some kind of callback for supplying the initialisation data -- this would be less flexible in how the data can be supplied (probably precluding DMA?), but keep the API entirely safe, by not exposing uninitialised memory to users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants