Best practice for calling Julia functions via API

Chrysoberyl
Thu, 15 Jan 2026 20:04:30 +0000
General Usage

In my setup, a Julia function calls a C/Rust function play(agent, environment), which spawns some threads, and call functions on agent. agent and environment are Julia objects.

Each of these foreign threads gets registered using jl_adopt_thread, jl_gc_safe_enter, and jl_enter_threaded_region.

During a call to play, the following functions get called in order:

1. jl_gc_safe_enter

2. (some computation)

3. jl_gc_unsafe_enter

4. (calls a function in agent)

5. jl_gc_unsafe_leave

6. (some computation)

7. jl_gc_safe_leave

Steps 2-6 execute on multiple threads.

According to earlier threads e.g. GC problems with `jl_gc_unsafe_enter` with multithreaded embedding, it seems like calling jl_gc_unsafe_enter while the GC is running leads to segmentation faults. For this reason, what is the best practice for entering a GC-unsafe region? Do I have to call jl_safepoint_wait_gc before jl_gc_unsafe_enter? This function is not exposed in julia.h.

Moreover, does ReentrantLock work for Julia code called in this context?

1 post - 1 participant