-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
GdCell::borrow_mut
should block on main thread if shared ref exists
#787
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-787 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative would be to also introduce a mutable reference count per thread. This would be clearer but introduce some additional complexity.
The current semantics are a bit difficult to understand indeed. Maybe it's because a separate shared/mut count would be clearer, or maybe it just lacks some comments 🤔 I made a corresponding remark in the code, but maybe wait with addressing changes just yet.
Any opinion on this, @lilizoey?
Would this only happen for multithreaded use case though? Can we force that it is only for those use cases? |
This is a bug fix for an existing feature in the "experimental-threads" crate feature. Regardless if "experimental-threads" is enabled or not, there should never be an infinite hang. |
@TitanNano Did you have a chance to look into the suggestions yet? 🙂 Maybe also cc @lilizoey for review 🔭 |
Not yet.
I held off on changes due to this remark and went into other topics while waiting for feedback from @lilizoey. But I will look into addressing the feedback. |
True, forgot about that -- it's fine for me either way 🙂 |
So is the goal here for the logic to be that when attempting to borrow: if there are no conflicting borrows (i.e mut conflicts with all, shared only conflicts with mut) then perform the borrow. otherwise, if there is a conflicting borrow on the same thread then panic finally, if all conflicting borrows are on different threads, then block |
Yes, that is accurate. |
One thing i dont quite get is why Would changing it to an |
@lilizoey I tired to describe this here:
"No longer has to borrow" has to be tracked in The current implementation avoids this additional overhead by always assigning a |
oh right it's the same logic as in gdcell for tracking the previous pointer, so you'd need to maintain the stacked nature of the mutable thread ids in the same way. |
4bdbd34
to
95f4700
Compare
Thanks a lot! |
The main thread sometimes panics if a shared borrow to a
GdCell
on another thread exists while trying to mutably borrow the cell.This is caused by the
ThreadTracker::mut_thread
ThreadId
being set to the current thread when creating theGdCell
. The thread then assumes to already hold a mutable borrow and does not block.A solution to this problem is to move the claim to the mutable borrow to which ever thread is creating a shared borrow first.
Alternatives
An alternative would be to also introduce a mutable reference count per thread. This would be clearer but introduce some additional complexity.
Just making
ThreadTracker::mut_thread
optional does not work due to the reentrant nature of the cell. Without a clear reference count, it's not possible to tell when themut_thread
should be cleared. (There could be inaccessible mutable borrows which bind the mutable reference to their thread.)