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

use_counting_ptr, a threadsave callback solution #1713

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

daschuer
Copy link
Member

@daschuer daschuer commented Jun 16, 2018

This is a wrapper class to prevent to delete an object that is still in use
in a multithread environment. It allows to use the object from mutible
threads without locking. The final clear is however locking if the object is still
in use.
This can be used to implement callbacks to an object with shorter livetime. It is
a replacement for Qt's direct connections, which are not save across threads
in this case.

Usage:
call set(po) and clear() to store and remove a pinter from the object.
call the callback from a scope like this

{
    auto obj = m_obj.get();
    if (obj) {
        obj->callback();
    }
}

@daschuer daschuer changed the title Added use_counting_ptr a threadsave calback solution use_counting_ptr, a threadsave calback solution Jun 16, 2018
@Be-ing
Copy link
Contributor

Be-ing commented Jun 16, 2018

Could you explain what specific problem motivated you to start working on this? Does it solve a problem that came up in #1675?

@daschuer
Copy link
Member Author

I am experience rar crashes when shutting down Mixxx.I was never able to reproduce it under gdb, so I have never reported a bug. One idea is that this is caused by explicit direct connections across threads. Qt does not prevent to call a callback from an already deleted object, if there is a race between the callback call from one thread and the delete call from an other thread.
If we use this new use_counting_ptr, the deleting thread is locked until the callback has returned.

@daschuer
Copy link
Member Author

We use these direct connection for the engine thread COs

@daschuer
Copy link
Member Author

I cannot use this currently, because of pending #1700

@Be-ing Be-ing added the engine label Jun 19, 2018
@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2018

Can we come back to this in a few weeks after the 2.2 feature freeze?

@daschuer
Copy link
Member Author

I like to have this included.

@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2018

Why?

@daschuer
Copy link
Member Author

I actually waiting for a review of #1700 more urgently. Our control objects where becoming a bit messy, and I think it is a good idea to clean them up in small steps instead of one giant PR.
This one so should hopefully solve the pending shut down crashes, finally.

@Be-ing
Copy link
Contributor

Be-ing commented Jun 19, 2018

I'm not questioning that we should clean the code, but I don't think < 2 weeks until a feature freeze is a good time to be focusing on it unless it fixes an important bug.

@Be-ing Be-ing added this to the 2.3.0 milestone Jun 23, 2018
@daschuer
Copy link
Member Author

@rryan Do you want to have a look here?
I like to have this new pointer to wrap the m_pCreatorCO here:
https://github.com/mixxxdj/mixxx/blob/master/src/control/control.h#L164

@daschuer
Copy link
Member Author

@uklotzde do you have interest to wrap your head around this? I would like it to fix the a probably cause of the crash on close fix I experience some times.

@uklotzde
Copy link
Contributor

Honestly I still don't get the point by reading the code??

Couldn't this be implemented in a simpler and more comprehensible way? I'm not able to decide if this works correctly in all possible cases. My feeling is that we might try to cover a design flaw by mitigating it with just another half-baked and fairly complex solution.

I noticed that the implementation has some technical flaws, e.g. the constructors are inconsistent and behave unexpectedly when invoked with a nullptr.

I came to the conclusion that we should use asynchronous signal/slot messaging whenever possible. Why plain callbacks? Trying to work around Qt's foundation and patterns is one of the main problems in our code base. I'm just criticizing myself, because this is what I would've done when rewriting the multi-threaded analysis code now. But I won't rewrite it a second time in C++ ;)

The global track cache is a slightly different story. It's also far too complex, but since track objects appear as shared mutable state everywhere we didn't have a choice without rewriting large parts of the application.

@daschuer
Copy link
Member Author

Thank you for looking into it.

This pointer was my idea for solving a crash on shut down when this https://github.com/mixxxdj/mixxx/blob/master/src/control/control.h#L164 object is deleted while an other thread is just about to call callback.

I would love to use a Qt solution for it, but it has not. Direct connections have no safety net in QT and suffer basically the same issue.

My idea was to use this pointer type to workaround this limitation, in an almost look free way.
The idea was to use this new pointer, for a maybe unusual but working solution of minimal scope.

Do you have alternative ideas? Is there a chance to tun this into a simpler and more comprehensible solution? That would be great.

@uklotzde
Copy link
Contributor

Would you please include an example were this technique is applied? I need to see it in action. We need this anyway. Adding unused and untested code to the repository is discouraged ;)

@daschuer
Copy link
Member Author

daschuer commented Dec 23, 2018

I have started to use the new pointer here: https://github.com/mixxxdj/mixxx/blob/master/src/control/control.h#L164
But it is becoming a great mess, because it is copied and stored all over the Mixxx code base.
We need to fix that first.

Many instances can be replaced by ControlProxyLt #1717

@Be-ing Be-ing modified the milestones: 2.3.0, 2.4.0 Feb 7, 2019
@Holzhaus Holzhaus added this to In progress in 2.4 release Apr 9, 2020
@github-actions
Copy link

This PR is marked as stale because it has been open 90 days with no activity.

@github-actions github-actions bot added the stale Stale issues that haven't been updated for a long time. label Oct 20, 2020
@Be-ing Be-ing changed the base branch from master to main October 23, 2020 23:50
@ronso0 ronso0 marked this pull request as draft November 20, 2020 10:21
@Be-ing Be-ing removed this from In progress in 2.4 release Apr 5, 2021
@ronso0 ronso0 changed the title use_counting_ptr, a threadsave calback solution use_counting_ptr, a threadsave callback solution Dec 12, 2021
@daschuer daschuer removed this from the 2.4.0 milestone Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine stale Stale issues that haven't been updated for a long time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants