Skip to content

Add centralized handling for calling user callbacks #13596

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

Merged
merged 2 commits into from
Mar 12, 2021
Merged

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Mar 4, 2021

This change also changes the way outstanding works keeps the runtime
alive.

We now use a counter rather than a single noExitRuntime variable so
that once outstanding works is complete the thread can be shutdown
if needed.

The specific need for this comes the use case of running fetch thread
and wanting to exit the thread once the fetch completes. Without
this change pthread_exit from a fetch callback generated an
uncaught unwind message on the console.

Also if one wants the fetch thread to become joinable there was no
way other than calling pthread_exit which has no parallel in the C++
threads world and has issues around calling C++ destructors on the
stack. With this solution the thread will exit automatically when returning
from the fetch callback.

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch 2 times, most recently from 61cdcb3 to 6b26bdc Compare March 4, 2021 02:22
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from 6b26bdc to 6b960dc Compare March 4, 2021 02:56
@sbc100 sbc100 requested review from kripken and juj and removed request for kripken March 4, 2021 03:07
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from 6b960dc to c4a4415 Compare March 4, 2021 03:12
@sbc100 sbc100 requested a review from kripken March 4, 2021 03:12
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from c4a4415 to 6f11253 Compare March 4, 2021 04:58
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from 6f11253 to 9732171 Compare March 4, 2021 22:40
@sbc100 sbc100 requested a review from dschuff March 5, 2021 17:46
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from 9732171 to f10722c Compare March 5, 2021 18:04
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

How about adding a specific test for the new keepalive functions, that is, without fetch or another high-level API?

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from f10722c to 8ee81dc Compare March 5, 2021 18:36
Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

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

This LGTM overall, although I'm not as familiar with some of these details. I agree with @kripken that a more targeted test would be good, although it could be done in a followup since we want to start a release cut soon.

@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 5, 2021

Actually I decided to just go for {{{ }}} approach after all. This means absolutely minimal impact on MINIMAL_RUNTIME builds.

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch 2 times, most recently from 88ed81b to fcf052c Compare March 5, 2021 21:31
if (!Browser.mainLoop.timingSet) {
{{{ runtimeKeepalivePush(); }}}
Browser.mainLoop.timingSet = true;
}
Copy link
Member

Choose a reason for hiding this comment

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

this change looks non-obvious. why unset this here, when it wasn't before? and why does that keep the runtime alive? where is the corresponding pop?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This function (emscripten_set_main_loop_timing
) can be used to set the timing of an existing main loop. If the timing already set we don't want to repeated call runtimeKeepalivePush every time the timing is adjusted.

See 'main loop exiting..' below in setMainLoop for where the loops exits ad calls Pop

Copy link
Member

Choose a reason for hiding this comment

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

I still find this confusing. Perhaps some comments could help, if I'm missing something.

It seems like we shouldn't care if the timing was set, but rather whether there was already a main loop running? That is, I'd expect to see a bool that says "I have pushed a runtime keepalive", or "already running" or such?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes that is exactly what this is supposed to mean. I think after I investigated this I determined that a has timing been set was the same things as "is running".. but you are correct I can come up with a better name and some comments.

$_setNetworkCallback: function(event, userData, callback) {
function _callback(data) {
{{{ runtimeKeepalivePop(); }}}
if (!callback) return;
Copy link
Member

Choose a reason for hiding this comment

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

why wasn't this needed before, and why is it now?

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch 2 times, most recently from 1dce557 to 0cc4105 Compare March 5, 2021 23:00
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch 3 times, most recently from 6f3cfb4 to aa0ab7a Compare March 7, 2021 19:59
Base automatically changed from master to main March 8, 2021 23:50
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from aa0ab7a to aadb94d Compare March 9, 2021 19:48
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

lgtm % last comment

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from aadb94d to f06157a Compare March 11, 2021 17:54
This change also changes the way outstanding works keeps the runtime
alive.  We now use a counter rather than a single noExitRuntime variable
so that once outstanding works is complete the thread can be shutdown if
needed.

The specific need for this comes the use case of running fetch thread
and wanting to exit the thread once the fetch completes.  Without this
change `pthread_exit` from a fetch callback generated an `uncaught
unwind` message on the console.

In addition, it doesn't always make sense to call `pthread_exit`, (e.g.
in cases where you want C++ descrutors on the stack to be called, or in
cases where C++ threads are being used and pthread_exit` does not exist,
or at least doesn't makes sense).  With this solution the thread will
exit automatically when returning from the fetch callback, no explict
thread exit call needed.
@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from f06157a to 72f90a6 Compare March 12, 2021 18:04
@sbc100
Copy link
Collaborator Author

sbc100 commented Mar 12, 2021

Added a new specific test for these new functions .. PTAL

@sbc100 sbc100 force-pushed the runtime_keepalive_slim branch from 72f90a6 to 74189c2 Compare March 12, 2021 18:49
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

new test lgtm too

@sbc100 sbc100 enabled auto-merge (squash) March 12, 2021 18:57
@sbc100 sbc100 merged commit a55bece into main Mar 12, 2021
@sbc100 sbc100 deleted the runtime_keepalive_slim branch March 12, 2021 19:31
sbc100 added a commit that referenced this pull request Apr 20, 2021
I believe this was an oversight when this code
was refactored as part of #13596.
sbc100 added a commit that referenced this pull request Apr 20, 2021
I believe this was an oversight when this code
was refactored as part of #13596.
sbc100 added a commit that referenced this pull request Apr 20, 2021
I believe this was an oversight when this code
was refactored as part of #13596.
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

Successfully merging this pull request may close these issues.

3 participants