[squeak-dev] Asynchronous FFI callback safety?
eliot.miranda at gmail.com
Sun Jul 10 16:59:31 UTC 2022
> On Jul 10, 2022, at 6:39 AM, Michał Olszewski <m.olszewski at nexat.pl> wrote:
> How safe are asynchronous FFI callbacks that could possibly be called from another thread?
Not at all safe. They would almost certainly result in a vm level crash.
> For example, a library that may perform such call to notify about an AIO that has just been completed.
> If not, how to ensure it?
There are two ways. One is to contribute to the development of the multi-threaded vm/threaded FFI; see CoInterpreterMT, CogThreadManager et al in VMMaker.oscog.
The other is to use the signalSemaphoreWithIndex api (paired with the image-level newExternalSemaphore method), which allows safe asynchronous signaling of a Smalltalk semaphore. One then arranges that a high-enough priority Smalltalk process looping waiting on the semaphore invokes some C (or plugin code) synchronously once past the wait that fetches the callback data and evaluates the callback synchronously in the main vm thread.
More information about the Squeak-dev