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

Enable PendingJob use on Python #3817

Merged
merged 4 commits into from
Jan 3, 2024
Merged

Enable PendingJob use on Python #3817

merged 4 commits into from
Jan 3, 2024

Conversation

sauwming
Copy link
Member

@sauwming sauwming commented Jan 2, 2024

Continuing #3774.

First of all, using PendingJob is only useful on Python.

Endpoint::utilAddPendingJob() will always execute the pending job directly unless mainThreadOnly is set to true (the default is false) (and thread count 0).

     * Utility to register a pending job to be executed by main thread.
     * If EpConfig::UaConfig::mainThreadOnly is false, the job will be
     * executed immediately.
    void utilAddPendingJob(PendingJob *job);
    /**
     * When this flag is non-zero, all callbacks that come from thread
     * other than main thread will be posted to the main thread and
     * to be executed by Endpoint::libHandleEvents() function. This
     * includes the logging callback. Note that this will only work if
     * threadCnt is set to zero and Endpoint::libHandleEvents() is
     * performed by main thread. By default, the main thread is set
     * from the thread that invoke Endpoint::libCreate()
     *
     * Default: false
     */
    bool                mainThreadOnly;
    /* See if we can execute immediately */
    if (!mainThreadOnly || pj_thread_this()==mainThread) {
        job->execute(false);
        delete job;
        return;
    }

This makes the problem much less complicated since mainThreadOnly typically won't be enabled on Java nor C# (and enabling this on Android is definitely a no no due to the main thread being the GUI thread).
Therefore, the simplest solution has been provided by @nanangizz in (#3774 (comment)) using __disown__().

This turns out to be a classical "elements within a container" problem, where we want the container to own the elements. And just for future reference, in case that we also encounter similar issues for Java and C#, the solutions are readily available in the docs:

For Python, the solution can be found here:

@sauwming
Copy link
Member Author

sauwming commented Jan 2, 2024

Test output:

Creating job 1
Job created id: 4312656208 value: 17136
Adding job 1
Executing job value: 17136 False
Job deleted id: 4312656208 value: 17136
Creating job 2
Job created id: 4311885776 value: 84049
Adding job 2
Collecting gc 1
Handling events
Executing job value: 84049 True
Collecting gc 2
Job deleted id: 4311885776 value: 84049

@@ -121,6 +121,7 @@ using namespace pj;
%feature("director") FindBuddyMatch;
%feature("director") AudioMediaPlayer;
%feature("director") AudioMediaPort;
%feature("director") PendingJob;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only for Python, shouldn't we enable the SWIG director for Python only?
Even perhaps disable/remove the PendingJob class & utilAddPendingJob() method on other languages?

@sauwming sauwming merged commit 86e22f4 into master Jan 3, 2024
35 checks passed
@sauwming sauwming deleted the pending-job branch January 3, 2024 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants