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

Make Ref<> assignments atomic #2330

Closed
ellenhp opened this issue Feb 21, 2021 · 1 comment
Closed

Make Ref<> assignments atomic #2330

ellenhp opened this issue Feb 21, 2021 · 1 comment

Comments

@ellenhp
Copy link

ellenhp commented Feb 21, 2021

@RandomShaper

Describe the project you are working on

Godot Engine.

Describe the problem or limitation you are having in your project

I'm trying to implement #2299 and I'm running into issues. Godot seems to have a hard rule of no allocations or mutexes on the audio thread, but I'd still like to be able to modify data structures read by the audio thread in non-trivial ways from other threads. To do that without mutexes, I tentatively arrived on using a lockless singly linked list. It would be very convenient to use Ref<> for this.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Ref<> assignment will use an atomic exchange operation to swap its internal reference with the new reference. The semantics of the swap operation ensure that the Reference object that gets its count decremented is always the one that was replaced by the assignment.

Not doing this is dangerous and can cause leaks or use-after-frees. To understand the following claims you'll need to flip back and forth between this and the code. https://github.com/godotengine/godot/blob/master/core/object/reference.h#L60

Ref<X> r = obj1;
r = obj2; // Thread 1
r = obj2; // Thread 2

If the two threads run code in lock-step, the reference count of obj2 will be double-incremented. That's not too bad, it just just causes a leak. What's really bad is that it also double-decrements the reference count of obj1. Meaning there could be another Ref<X> somewhere pointing to obj1, and anyone accessing it will experience a use-after-free.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Hopefully it's fine to just link to the PR here. godotengine/godot#46302

If this enhancement will not be used often, can it be worked around with a few lines of script?

No, thread safety can not be simulated in a script :)

Is there a reason why this should be core and not an add-on in the asset library?

This one answers itself I think. :) Ref<> is in core.

@Calinou Calinou changed the title Make Ref<> assignments atomic. Make Ref<> assignments atomic Feb 22, 2021
@ellenhp
Copy link
Author

ellenhp commented Aug 27, 2021

This was worked around by the addition of a lock-free linked list class called SafeList<>

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

Successfully merging a pull request may close this issue.

3 participants