Hello,
I’m implementing a native method that shall start a DMA transfer and then wait for its completion without blocking the Core Engine.
My plan is to suspend the Java thread with SNI_suspendCurrentJavaThreadWithCallback(), and to wake it up from the DMA interrupt with SNI_resumeJavaThread().
I’m not sure about the right order, though: should I suspend the thread before or after starting the transfer?
And if the transfer fails to start once the thread is already suspended, how do I cancel the suspend and throw an exception to the Java side?
Thanks for your help!
Hi @lfleuve,
SNI_suspendCurrentJavaThreadWithCallback() does not block your native function: the native returns first, and only then does the Java thread go to sleep (see the Synchronization section of the SNI Specification).
If someone already called SNI_resumeJavaThread() in the meantime (the pending resume flag is set), the thread does not sleep at all, and your callback is called right away.
The callback always runs in the Core Engine task when the thread wakes up, so both orders work.
One thing to keep in mind: SNI_throwNativeException() must be called from the Core Engine task (your native or your callback), never from the interrupt handler.
Thanks, that’s clearer!
So I could start the transfer first, and suspend the thread only if the start succeeds?
Exactly.
With this order, if start_transfer() fails, you throw the exception directly from the native, and there is no suspend to cancel with a resume().
