<div dir="ltr"><div style>Hello,</div><div style><br></div><div style>to ensure that finalizers are run from the main thread, I'm doing the following:</div><div style><br></div><div style><div>(setq ccl:*enable-automatic-termination* nil)</div>
<div>(ccl::add-gc-hook</div><div> (let ((process ccl:*current-process*))</div><div>   (lambda ()</div><div>     (ccl:process-interrupt process #'ccl:drain-termination-queue)))</div><div> :post-gc))</div><div><br></div>
</div><div style><div style>Is this a good idea? The fact that add-gc-hook is not exported makes me doubt...</div><div style><br></div><div style>The context is the following: RCL embeds R in Common Lisp (using CFFI, the rf-protect and rf-unprotect-ptr functions below are foreign functions). When I need to hold a foreign pointer in the lisp side I wrap it in an object: I "protect" it to prevent it from being garbage-collected in the R side and I set up a finalizer to "unprotect" it when it's no longer in use. </div>
<div style><br></div></div><div>(defun make-r-pointer (ptr)</div><div>  (let ((r-pointer (make-instance 'r-pointer :pointer ptr)))</div><div>    (rf-protect ptr)</div><div>    (trivial-garbage:finalize r-pointer (lambda () (rf-unprotect-ptr ptr)))</div>
<div>    r-pointer))</div><div><br></div><div style>However, a function triggering garbage collection like the following one crashes the program (because the protect and unprotect functions will run concurrently, I think).</div>
<div style><br></div><div style><div>(ql:quickload :rcl)</div><div>(rcl:r-init)</div><div>(reduce #'+ (loop repeat 10000 append (rcl:r "runif" 1)))</div><div><br></div><div style>The workaround mentioned above seems to solve the problem, but I wonder if there might be some undesirable effects. </div>
<div style><br></div><div style>I also tried to let the automatic termination enabled, forcing the finalizer to run each time from the main thread</div><div style><br></div><div style>    (trivial-garbage:finalize r-pointer (let ((process  ccl:*current-process*))<span style="white-space:pre"> </span>(lambda () (process-interrupt process #'rf-unprotect-ptr ptr))))<br>
</div><div style><div><br></div><div>but I couldn't make it work (the program hangs).<br></div><div><br></div><div style>Cheers,</div><div style><br></div><div style>Carlos</div></div><div><br></div></div></div>