Communicating with external C threads

Just to clear up some confusion. Right now you can obtain a C-callback pointer to a Julia function with @cfunction. You can use that callback in two ways:

  1. On a Julia thread that enters a C-library. Most callbacks are fine in that situation.
  2. On a non-Julia thread. Callbacks are severely limited in what they can do.

The prime restriction in the second case is: You are not allowed to interact with the Julia runtime, among those interactions are:

  1. Allocating objects
  2. Entering the runtime
  • IO
  • yields/task/etc…

Jeff’s comment was that one could setup the necessary thread-local state to allow some of these interactions, like allocating memory. as @cdsousa points out right now we assume that there is a fixed number of threads and the GC needs to have some visibility into the threads that use or allocate Julia objects.

The challenge is that even when you allow for some interactions, there are others that are detrimental to callbacks. You wouldn’t want the Julia scheduler to steal your thread, just because you entered a yield point.

Right now the only safe method to communicate with the Julia runtime from a non-Julia thread is AsyncCondition and the associated uv_async_send.

As an example of the usage:

5 Likes