From e632c5fdf04e44e9f1414ad7391b92079b15a969 Mon Sep 17 00:00:00 2001 From: K Pamnany Date: Wed, 4 Oct 2023 16:02:57 -0400 Subject: [PATCH] Add GC-safe region around `Highs_run` Prevent a long-running `Highs_run` call from blocking GC (and freezing the other Julia threads). --- src/gen/libhighs.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/gen/libhighs.jl b/src/gen/libhighs.jl index 536579f..d8757b8 100644 --- a/src/gen/libhighs.jl +++ b/src/gen/libhighs.jl @@ -282,7 +282,19 @@ Optimize a model. The algorithm used by HiGHS depends on the options that have b A `kHighsStatus` constant indicating whether the call succeeded. """ function Highs_run(highs) - ccall((:Highs_run, libhighs), HighsInt, (Ptr{Cvoid},), highs) + r = 0 + # if `Highs_run` implicitly uses memory or other resources owned by `highs`, preserve it + GC.@preserve highs begin + # allow Julia to GC while this thread is in `Highs_run` + gc_state = ccall(:jl_gc_safe_enter, Int8, ()) + + r = ccall((:Highs_run, libhighs), HighsInt, (Ptr{Cvoid},), highs) + + # leave GC-safe region, waiting for GC to complete if it's running + ccall(:jl_gc_safe_leave, Cvoid, (Int8,), gc_state) + end + + return r end """