Skip to content

[Proxying] Add proxying functions that return promises - #18825

Merged
tlively merged 2 commits into
mainfrom
proxying-promise
Mar 6, 2023
Merged

[Proxying] Add proxying functions that return promises#18825
tlively merged 2 commits into
mainfrom
proxying-promise

Conversation

@tlively

@tlively tlively commented Feb 23, 2023

Copy link
Copy Markdown
Member

Add emscripten_proxy_promise and emscripten_proxy_promise_with_ctx that
return em_promise_t handles to promises that are fulfilled when the proxied
work is completed or rejected when the target thread dies before it can complete
the work.

Do not document these new APIs yet since they depend on the promise API in
emscripten/promise.h, which is not yet documented. Similarly, do not add a C++
wrapper yet because emscripten/promise.h does not yet have a C++ API.

@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 1fe81f5 to 4baabe9 Compare February 25, 2023 05:27
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 4baabe9 to 6a5e4e7 Compare February 25, 2023 06:41
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 6a5e4e7 to 5cb7113 Compare February 25, 2023 06:44
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 5cb7113 to da1f95e Compare February 27, 2023 15:35
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from da1f95e to 289203e Compare February 28, 2023 15:39
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 289203e to 2ef3195 Compare March 1, 2023 01:10
@tlively
tlively force-pushed the proxying-promise branch from 5573a31 to a2d0ef3 Compare March 1, 2023 01:10
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 2ef3195 to 4ccf2ee Compare March 1, 2023 16:38
@tlively
tlively force-pushed the proxying-promise branch from a2d0ef3 to 0fa4fc7 Compare March 1, 2023 16:38
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 4ccf2ee to 1f0ecb4 Compare March 1, 2023 19:37
@tlively
tlively force-pushed the proxying-promise branch from 0fa4fc7 to a8184bf Compare March 1, 2023 19:37
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from 1f0ecb4 to af490a7 Compare March 1, 2023 21:07
@tlively
tlively force-pushed the proxying-promise branch from a8184bf to c3b5047 Compare March 1, 2023 21:07
@tlively
tlively force-pushed the proxying-callback-with-ctx branch from af490a7 to 5415a72 Compare March 1, 2023 22:33
@tlively
tlively force-pushed the proxying-promise branch from c3b5047 to c30ca68 Compare March 1, 2023 22:33
Base automatically changed from proxying-callback-with-ctx to main March 2, 2023 01:36

@sbc100 sbc100 left a comment

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.

Looks like this needs a rebase?

Comment thread system/include/emscripten/proxying.h
@tlively
tlively force-pushed the proxying-promise branch from c30ca68 to 2f1ddd5 Compare March 2, 2023 02:21
@tlively

tlively commented Mar 2, 2023

Copy link
Copy Markdown
Member Author

@sbc100 this is ready for review.

Comment thread system/lib/pthread/proxying.c Outdated
q, target_thread, func, arg, promise, &block->ctx, &block->promise_ctx);
}

__attribute__((warn_unused_result)) em_promise_t

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 don't think you need to (or should) duplicate these attributes in the .c file.

promise_ctx promise_ctx;
task task;
};
struct block* block = malloc(sizeof(*block));

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.

Where are these "blocks" freed? I don't see the block pointer escaping this function. Does it rely on ctx being free'd?

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.

Yes, it relies on ctx being freed, which happens in all the places free_ctx is called.

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.

Perhaps add a comment on the first eleemnt of the struct? e.g. // This struct is free'd via the ctx pointer so this must be the first element

Comment thread test/pthread/test_pthread_proxying.c
Comment thread test/pthread/test_pthread_proxying.c

em_promise_result_t exit_thread(void** result, void* data, void* value) {
// TODO: exit the thread directly rather than on the next turn of the event
// loop.

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.

Why is this a TODO?

Could we make this automatic (one day) and have the outstanding promises keep the thread alive and have the resolution of the final outstanding promise take the thread down?

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.

Trying to exit directly with pthread_exit does not work because that ends up throwing an exception to try to unwind, which in turn ends up rejecting the promise rather than gracefully exiting the thread.

Outstanding promises (at least those created with emscripten_promise_then) do keep the thread alive until their callbacks are executed, so this almost works. The only thing missing is that we don't use callUserCallback in the promise callbacks, so nothing actually checks the runtimeKeepAlive and exits. I'll fix that in a separate PR.

Comment thread test/pthread/test_pthread_proxying_canceled_work.c
@tlively
tlively enabled auto-merge (squash) March 4, 2023 00:27
tlively added 2 commits March 6, 2023 08:37
Add `emscripten_proxy_promise` and `emscripten_proxy_promise_with_ctx` that
return `em_promise_t` handles to promises that are fulfilled when the proxied
work is completed or rejected when the target thread dies before it can complete
the work.

Do not document these new APIs yet since they depend on the promise API in
emscripten/promise.h, which is not yet documented. Similarly, do not add a C++
wrapper yet because emscripten/promise.h does not yet have a C++ API.
@tlively
tlively force-pushed the proxying-promise branch from 0bc125b to c3c38e7 Compare March 6, 2023 16:46
@tlively
tlively merged commit 5a8a8dd into main Mar 6, 2023
@tlively
tlively deleted the proxying-promise branch March 6, 2023 18:09
impact-maker pushed a commit to impact-maker/emscripten that referenced this pull request Mar 17, 2023
…re#18825)

Add `emscripten_proxy_promise` and `emscripten_proxy_promise_with_ctx` that
return `em_promise_t` handles to promises that are fulfilled when the proxied
work is completed or rejected when the target thread dies before it can complete
the work.

Do not document these new APIs yet since they depend on the promise API in
emscripten/promise.h, which is not yet documented. Similarly, do not add a C++
wrapper yet because emscripten/promise.h does not yet have a C++ API.
impact-maker pushed a commit to impact-maker/emscripten that referenced this pull request Mar 17, 2023
…re#18825)

Add `emscripten_proxy_promise` and `emscripten_proxy_promise_with_ctx` that
return `em_promise_t` handles to promises that are fulfilled when the proxied
work is completed or rejected when the target thread dies before it can complete
the work.

Do not document these new APIs yet since they depend on the promise API in
emscripten/promise.h, which is not yet documented. Similarly, do not add a C++
wrapper yet because emscripten/promise.h does not yet have a C++ API.
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.

2 participants