Skip to content

[Proxying] Update proxying with callback to support cancellation - #18776

Merged
tlively merged 3 commits into
mainfrom
proxying-cancel-callback
Mar 1, 2023
Merged

[Proxying] Update proxying with callback to support cancellation#18776
tlively merged 3 commits into
mainfrom
proxying-cancel-callback

Conversation

@tlively

@tlively tlively commented Feb 16, 2023

Copy link
Copy Markdown
Member

Completely overhaul the interface for proxying with callbacks:

  • Rename emscripten_proxy_async_with_callback to the simpler emscripten_proxy_callback.

  • Add an optional cancel callback argument that will be scheduled on the proxying thread if the worker thread dies before the work is started. A future PR will change this so that the cancel callback will be called if a worker thread dies before the work is finished to be more consistent with the proxy_sync APIs.

  • Remove the ability to receive the result of the proxied function as an argument to the callback function since the extra expressiveness was not general enough to warrant the added complexity.

  • Use only a single context argument rather than a separate context for the proxied function and the callback. This is just as expressive, but it is simpler avoids the question of what context object should be passed to the cancellation callback.

Also update the only user of the callback API, dynlink.c, to use the new API. A future PR will implement a promise-based proxying API and update dynlink.c to use that instead, so this PR just tries to make the minimal necessary changes to dynlink.c.

@tlively
tlively requested a review from sbc100 February 16, 2023 21:34
Comment thread system/lib/libc/dynlink.c
struct promise_result* info = malloc(sizeof(struct promise_result));
if (!info) {
return false;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems a little unfortunate to have to add an extra allocation here... but I guess it keeps the interface simpler?

Comment thread system/lib/libc/dynlink.c Outdated
if (!info) {
return false;
}
*info = (struct promise_result){.promise = promise, .result = false};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think find it little more readable to just do:

info->promise = promise;
info->result = false;

Is the cast here actually needed? I guess so?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, it's not actually a cast, but rather part of the compound literal syntax.

In general I prefer this syntax, but I agree that it's nice to give each field its own line. Maybe I'll try to tweak the clang format settings to make this nicer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It turns out that there's no clang-format setting for this, but putting a comma after the last item achieves the same thing.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It just seems like more, and less obvious, syntax than the simple pair of assignments.. but maybe I'm just not familiar with reading code with this syntax.

@tlively

tlively commented Feb 16, 2023

Copy link
Copy Markdown
Member Author

This depends on #18741, by the way.

@tlively
tlively force-pushed the proxying-cancel-callback branch 2 times, most recently from f04027b to 60fd680 Compare February 17, 2023 05:47
@tlively
tlively force-pushed the proxying-cancel-callback branch from 60fd680 to 2d74803 Compare February 17, 2023 06:02
@tlively
tlively force-pushed the proxying-cancel-callback branch from 848ee7d to 2213f1a Compare February 25, 2023 05:27
@tlively
tlively force-pushed the proxying-cancel-callback branch from 2213f1a to 8b7eadf Compare February 25, 2023 06:41
@tlively
tlively force-pushed the proxying-cancel-callback branch from 8b7eadf to d67be45 Compare February 25, 2023 06:43
@tlively
tlively force-pushed the proxying-cancel-callback branch from d67be45 to aaa19c3 Compare February 27, 2023 15:35
@tlively
tlively force-pushed the proxying-shutdown branch from e0abb19 to 09a3df8 Compare March 1, 2023 01:09
@tlively
tlively force-pushed the proxying-cancel-callback branch from a1353b4 to a5b591a Compare March 1, 2023 01:09
@tlively
tlively force-pushed the proxying-shutdown branch from 09a3df8 to 8b4eb90 Compare March 1, 2023 16:38
@tlively
tlively force-pushed the proxying-cancel-callback branch from a5b591a to 32309a3 Compare March 1, 2023 16:38
@tlively
tlively force-pushed the proxying-shutdown branch from 8b4eb90 to ea71ab3 Compare March 1, 2023 19:37
@tlively
tlively force-pushed the proxying-cancel-callback branch from 32309a3 to 26bcd74 Compare March 1, 2023 19:37
Base automatically changed from proxying-shutdown to main March 1, 2023 21:04
tlively added 3 commits March 1, 2023 13:06
Completely overhaul the interface for proxying with callbacks:

 - Rename `emscripten_proxy_async_with_callback` to the simpler
   `emscripten_proxy_callback`.

 - Add an optional `cancel` callback argument that will be scheduled on the
   proxying thread if the worker thread dies before the work is started. A
   future PR will change this so that the `cancel` callback will be called if a
   worker thread dies before the work is finished to be more consistent with the
   `proxy_sync` APIs.

 - Remove the ability to receive the result of the proxied function as an
   argument to the callback function since the extra expressiveness was not
   general enough to warrant the added complexity.

 - Use only a single context argument rather than a separate context for the
   proxied function and the callback. This is just as expressive, but it is
   simpler avoids the question of what context object should be passed to the
   cancellation callback.

Also update the only user of the callback API, dynlink.c, to use the new API. A
future PR will implement a promise-based proxying API and update dynlink.c to
use that instead, so this PR just tries to make the minimal necessary changes to
dynlink.c.
@tlively
tlively force-pushed the proxying-cancel-callback branch from 26bcd74 to 413728b Compare March 1, 2023 21:07
@tlively
tlively merged commit 4589faa into main Mar 1, 2023
@tlively
tlively deleted the proxying-cancel-callback branch March 1, 2023 22:30
impact-maker pushed a commit to impact-maker/emscripten that referenced this pull request Mar 17, 2023
…cripten-core#18776)

Completely overhaul the interface for proxying with callbacks:

 - Rename `emscripten_proxy_async_with_callback` to the simpler
   `emscripten_proxy_callback`.

 - Add an optional `cancel` callback argument that will be scheduled on the
   proxying thread if the worker thread dies before the work is started. A
   future PR will change this so that the `cancel` callback will be called if a
   worker thread dies before the work is finished to be more consistent with the
   `proxy_sync` APIs.

 - Remove the ability to receive the result of the proxied function as an
   argument to the callback function since the extra expressiveness was not
   general enough to warrant the added complexity.

 - Use only a single context argument rather than a separate context for the
   proxied function and the callback. This is just as expressive, but it is
   simpler avoids the question of what context object should be passed to the
   cancellation callback.

Also update the only user of the callback API, dynlink.c, to use the new API. A
future PR will implement a promise-based proxying API and update dynlink.c to
use that instead, so this PR just tries to make the minimal necessary changes to
dynlink.c.
impact-maker pushed a commit to impact-maker/emscripten that referenced this pull request Mar 17, 2023
…cripten-core#18776)

Completely overhaul the interface for proxying with callbacks:

 - Rename `emscripten_proxy_async_with_callback` to the simpler
   `emscripten_proxy_callback`.

 - Add an optional `cancel` callback argument that will be scheduled on the
   proxying thread if the worker thread dies before the work is started. A
   future PR will change this so that the `cancel` callback will be called if a
   worker thread dies before the work is finished to be more consistent with the
   `proxy_sync` APIs.

 - Remove the ability to receive the result of the proxied function as an
   argument to the callback function since the extra expressiveness was not
   general enough to warrant the added complexity.

 - Use only a single context argument rather than a separate context for the
   proxied function and the callback. This is just as expressive, but it is
   simpler avoids the question of what context object should be passed to the
   cancellation callback.

Also update the only user of the callback API, dynlink.c, to use the new API. A
future PR will implement a promise-based proxying API and update dynlink.c to
use that instead, so this PR just tries to make the minimal necessary changes to
dynlink.c.
@DreamOfIce

Copy link
Copy Markdown

@tlively @sbc100 I do need a callback to get the result returned by the function, is there any way to do this?

@DreamOfIce

DreamOfIce commented Apr 4, 2023

Copy link
Copy Markdown

Also, changelog of this seems to be written to 3.1.32 instead of correct 3.1.33

@tlively

tlively commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

@DreamOfIce, if you reserve space for the result in the memory block you pass as the arg parameter, then you can store the result there in func and retrieve it in callback, since the same arg parameter will be passed to both of them.

@tlively

tlively commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

Also, changelog of this seems to be written to 3.1.32 instead of correct 3.1.33

Thanks for catching that. I'll fix it.

tlively added a commit that referenced this pull request Apr 4, 2023
PR #18776 updated the changelog for 3.1.32, but it should have updated the changelog for 3.1.33, which was in development at the time.
@tlively

tlively commented Apr 4, 2023

Copy link
Copy Markdown
Member Author

#19132

tlively added a commit that referenced this pull request Apr 4, 2023
PR #18776 updated the changelog for 3.1.32, but it should have updated the changelog for 3.1.33, which was in development at the time.
@DreamOfIce

Copy link
Copy Markdown

Also, any progress on the promise based proxy API yet? Is the promise here in js or cpp?

@tlively

tlively commented Apr 5, 2023

Copy link
Copy Markdown
Member Author

@DreamOfIce, yes, you can see it here: https://github.com/emscripten-core/emscripten/blob/main/system/include/emscripten/proxying.h#L106-L116.

Note that there is no documentation yet, because it relies on the promise API in promise.h, which isn't finished yet. There's also no C++ wrapper for emscripten_proxy_promise for the same reason.

@DreamOfIce

Copy link
Copy Markdown

In that case, I'll go ahead and use the version I implemented before

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