Skip to content

Decouple dispatching from em_queued_call management - #15690

Closed
tlively wants to merge 7 commits into
mainfrom
dispatch_ptr
Closed

tlively wants to merge 7 commits into
mainfrom
dispatch_ptr

Conversation

@tlively

@tlively tlively commented Dec 1, 2021

Copy link
Copy Markdown
Member

Introduce new emscripten_dispatch_to_thread_{async_}ptr functions that take
void (*)(void*) function pointers and void* arguments and do not use the
em_queued_call functionality. These functions are both useful to users, who
may want to dispatch work that does not fit cleanly into the em_queued_call
system, and also simplifies the implementation by separating the proxying logic
from the em_queued_call creation and dispatch logic.

Now that these two pieces have been decoupled, the dispatching logic needs to
report whether or not dispatching succeeded so that the em_queued_call logic
can clean up in the event of failure. Previously emscripten_dispatch_to_thread
returned 1 if the dispatched function was synchronously called and 0 otherwise,
so there was no way to differentiate an error from a successful dispatch to
another thread. To fix this wart and enable proper resource management, change
the return value to 1 when the dispatch is successful.

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.
Instead of implementing both emscripten_dispatch_to_thread and
emscripten_dispatch_to_thread_async in terms of the internal function
_emscripten_call_on_thread, implement emscripten_dispatch_to_thread_async in
terms of emscripten_dispatch_to_thread. This simplifies the logic overall by
separating the dispatching logic from the decision about whether to dispatch.

A version of emscripten_dispatch_to_thread that takes a va_list rather than
varargs was necessary to implement this change. To make the public API more
robust, expose that new version as well as a corresponding version of
emscripten_dispatch_to_thread_async in threading.h. These new functions are
similar enough to existing functionality that this still arguably qualifies as
NFC.
Introduce new `emscripten_dispatch_to_thread_{async_}ptr` functions that take
`void (*)(void*)` function pointers and `void*` arguments and do not use the
`em_queued_call` functionality. These functions are both useful to users, who
may want to dispatch work that does not fit cleanly into the `em_queued_call`
system, and also simplifies the implementation by separating the proxying logic
from the `em_queued_call` creation and dispatch logic.

Now that these two pieces have been decoupled, the dispatching logic needs to
report whether or not dispatching succeeded so that the `em_queued_call` logic
can clean up in the event of failure. Previously `emscripten_dispatch_to_thread`
returned 1 if the dispatched function was synchronously called and 0 otherwise,
so there was no way to differentiate an error from a successful dispatch to
another thread. To fix this wart and enable proper resource management, change
the return value to 1 when the dispatch is successful.
// Schedule the call to run later on this thread.
emscripten_set_timeout(func, 0, arg);
return 1;
} else {

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.

Avoid else after return

// Setup is the same as in emscripten_dispatch_to_thread_args.
em_queued_call* q = em_queued_call_create(sig, func, satellite, args);
if (!q)
return 0;

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 out style requires curlies doesn't it? (I can't remember actually).

// Otherwise, dispatch as usual.
return emscripten_dispatch_to_thread_args(
target_thread, sig, func_ptr, satellite, args);
return 1;

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.

How about returning EMSCRIPTEN_RESULT_SUCCESS?

int emscripten_dispatch_to_thread_ptr(pthread_t target_thread,
void (*func)(void*),
void* arg) {
// Can't be a null pointer here, and can't be

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.

If you implement this function above where the old _emscripten_do_dispatch_to_thread does it allow github to show this diff more reasonably?

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.

Would be good to retain the location so that this can be diffed easier, not completely sure what happens here

@kripken

kripken commented Dec 1, 2021

Copy link
Copy Markdown
Member

Previously emscripten_dispatch_to_thread returned 1 if the dispatched function was synchronously called and 0 otherwise, so there was no way to differentiate an error from a successful dispatch to another thread. To fix this wart and enable proper resource management, change the return value to 1 when the dispatch is successful.

This breaking change worries me. Code that checked if the function already executed will now get 1 and think it ran when it hasn't. I do agree a way to see if dispatch succeeded is important, but perhaps a new functions without a breaking change is better? Internally the old version can call the new one and convert the return values.

Base automatically changed from no-call_on_thread to main December 2, 2021 22:47
@tlively

tlively commented Dec 3, 2021

Copy link
Copy Markdown
Member Author

Thanks for the comments, @sbc100! I'll push fixes for those issues.

Previously emscripten_dispatch_to_thread returned 1 if the dispatched function was synchronously called and 0 otherwise, so there was no way to differentiate an error from a successful dispatch to another thread. To fix this wart and enable proper resource management, change the return value to 1 when the dispatch is successful.

This breaking change worries me. Code that checked if the function already executed will now get 1 and think it ran when it hasn't. I do agree a way to see if dispatch succeeded is important, but perhaps a new functions without a breaking change is better? Internally the old version can call the new one and convert the return values.

yeah, creating new functions might make sense here. This could tie in with the new naming @sbc100 wants in #15705.

// was successfully run or dispatched and 0 otherwise.
int emscripten_dispatch_to_thread_ptr(pthread_t target_thread,
void (*func)(void*),
void* arg);

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.

Sorry, I am probably missing something.

If I follow properly, this new _ptr variant would be used in the form

void myProxiedFunction(void *arg) {
  // do something
}

...

void *someOpaqueArg = ...;
emscripten_dispatch_to_thread_ptr(target_thread, myProxiedFunction, someOpaqueArg);

What prevented one from using the existing emscripten_dispatch_to_thread() method instead? That would look

emscripten_dispatch_to_thread(target_thread, EM_FUNC_SIG_VI, (void*)myProxiedFunction, someOpaqueArg);

I am not sure I see yet why this new x_ptr() function family would be needed?

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, those two calls would be equivalent. Even without adding any new API, I would want to refactor the implementation to have something like emscripten_dispatch_to_thread_ptr internally and at that point it seems reasonable to expose it as a public API so that users don't need to deal with the sig or satellite arguments.

But latest plan described here doesn't require us to change this API at all, so I'll close this PR.

@tlively

tlively commented Dec 9, 2021

Copy link
Copy Markdown
Member Author

If we implement the API in #15705 (comment), we won't need to add the new public functions in this PR.

@tlively tlively closed this Dec 9, 2021
@tlively
tlively deleted the dispatch_ptr branch February 14, 2022 20:40
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.

4 participants