Repository navigation
Conversation
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 { |
| // 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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
If you implement this function above where the old _emscripten_do_dispatch_to_thread does it allow github to show this diff more reasonably?
There was a problem hiding this comment.
Would be good to retain the location so that this can be diffed easier, not completely sure what happens here
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. |
|
Thanks for the comments, @sbc100! I'll push fixes for those issues.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
If we implement the API in #15705 (comment), we won't need to add the new public functions in this PR. |
Introduce new
emscripten_dispatch_to_thread_{async_}ptrfunctions that takevoid (*)(void*)function pointers andvoid*arguments and do not use theem_queued_callfunctionality. These functions are both useful to users, whomay want to dispatch work that does not fit cleanly into the
em_queued_callsystem, and also simplifies the implementation by separating the proxying logic
from the
em_queued_callcreation 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_calllogiccan clean up in the event of failure. Previously
emscripten_dispatch_to_threadreturned 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.