Skip to content

Refactor the threading.h C proxying API - #15681

Closed
tlively wants to merge 3 commits into
emscripten-core:mainfrom
tlively:proxying-cleanup
Closed

tlively wants to merge 3 commits into
emscripten-core:mainfrom
tlively:proxying-cleanup

Conversation

@tlively

@tlively tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member

Refactor the C proxying API to separate the proxying logic from the logic for
creation and use of em_queued_call objects. Also provide a richer and more
orthogonal set of emscripten_dispatch_to_thread_* variants:

  • emscripten_dispatch_to_thread - send a task to a thread for asynchronous
    execution.

  • emscripten_dispatch_to_thread_async - same, but schedule for asynchronous
    execution even if already on the target thread.

  • emscripten_dispatch_to_thread_sync - wait for the dispatched function to
    finish before returning.

  • emscripten_dispatch_to_thread_async_as_sync - wait for the dispatched task to
    be marked finished via an explicit call to emscripten_async_as_sync_finish.

Each of these functions comes in a lower-level *_ptr variant that accepts
function pointers and void* arguments as well as higher-level variants that
construct and dispatch em_queued_call objects.

Each variant builds incrementally on some other variant, all bottoming out at
emscripten_dispatch_to_thread_ptr, so it should be easy to incrementally
understand how each layer works. The other miscellaneous proxying functions
exposed in threading.h are now implemented in terms of the most appropriate
emscripten_dispatch_to_thread* variant as well.

This is the first step toward decoupling the logic for proxying work to threads
from the logic for creation and dispatch of `em_queued_call` objects, which
couple a dynamically typed function pointer with its arguments. Decoupling these
pieces will allow for more general proxying that is not required to use the
`em_queued_call` mechanism and will simplify the code.
Refactor the C proxying API to separate the proxying logic from the logic for
creation and use of em_queued_call objects. Also provide a richer and more
orthogonal set of `emscripten_dispatch_to_thread_*` variants:

 - emscripten_dispatch_to_thread - send a task to a thread for asynchronous
   execution.

 - emscripten_dispatch_to_thread_async - same, but schedule for asynchronous
   execution even if already on the target thread.

 - emscripten_dispatch_to_thread_sync - wait for the dispatched function to
   finish before returning.

 - emscripten_dispatch_to_thread_async_as_sync - wait for the dispatched task to
   be marked finished via an explicit call to `emscripten_async_as_sync_finish`.

Each of these functions comes in a lower-level `*_ptr` variant that accepts
function pointers and void* arguments as well as higher-level variants that
construct and dispatch `em_queued_call` objects.

Each variant builds incrementally on some other variant, all bottoming out at
`emscripten_dispatch_to_thread_ptr`, so it should be easy to incrementally
understand how each layer works. The other miscellaneous proxying functions
exposed in threading.h are now implemented in terms of the most appropriate
`emscripten_dispatch_to_thread*` variant as well.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

threading.h changes look nice!

Before I start to read the code, the description mentions both a refactoring and the addition of new things. Could this be split into a NFC refactoring in an initial PR?

@tlively

tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member Author

I can split out a few things, yes. Others would be more difficult to split out because they are now implemented in terms of the new functions. I just reopened #15648 as a first step. I'll make another preliminary PR or two.

 - Rebaseline the failing metadce test
 - Change the expected return value of `emscripten_dispatch_to_thread`. It now
   returns 1 if the call was successfully scheduled, not just if it was
   synchronously executed.
@tlively

tlively commented Dec 1, 2021 •

Copy link
Copy Markdown
Member Author

Just added a commit to fix some tests. First, the code size increased and the exports changed in the metadce test. Second, emscripten_dispatch_to_thread now returns 1 if the call was successfully scheduled while previously it only returned 1 if the call was synchronously executed so a test expectation had to be updated.

I'm still investigating the posixtest.test_pthread_setcanceltype_1_1 failure.

@tlively

tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member Author

Another preliminary NFC change: #15686

@tlively

tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member Author

Another smaller change, this one not NFC: #15690

@tlively

tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member Author

Another NFC change: #15691.

ethanalee added a commit that referenced this pull request Dec 3, 2021
Relevant Issue: #15041

Introduce new proxied backend structure for the new file system.
Proxying will be refactored soon using on this PR: #15681
@tlively

tlively commented Dec 9, 2021

Copy link
Copy Markdown
Member Author

Closing this in favor of the plan in #15705 (comment).

@tlively tlively closed this Dec 9, 2021
mmarczell-graphisoft pushed a commit to GRAPHISOFT/emscripten that referenced this pull request Jan 5, 2022
Relevant Issue: emscripten-core#15041

Introduce new proxied backend structure for the new file system.
Proxying will be refactored soon using on this PR: emscripten-core#15681
@tlively
tlively deleted the proxying-cleanup branch February 5, 2024 18:04
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