Re: SOLVED: Was Garbage collector - ccl:terminate (memory stuff)

"R. Matthew Emerson" <[email protected]> Fri, 31 May 2024 11:33:15 -0700
Newsgroups gmane.lisp.openmcl.devel
Message-ID <[email protected]>

> On May 22, 2024, at 2:22=E2=80=AFPM, Gr=C3=A9gory Vanuxem =
<[email protected]> wrote:
>=20
> Hi Robert, hi all,
>=20
> Many thanks for your reply. It allowed me to go further into this and
> I have found a solution.
>=20
> In the FriCAS context, the email, I just sent to the FriCAS mailing =
list:
> https://groups.google.com/g/fricas-devel/c/kHMffXyMIB0/m/hMh1mMasBQAJ
>=20
> Basically FriCAS create a simple fricas-class application:
>=20
> (defclass fricas-application (ccl::application) ())

It looks a bit odd that you are inheriting from ccl::application here, =
yet in your toplevel-function method below, you create an instance of =
ccl::lisp-development-system.  I=E2=80=99m somewhat surprised that works =
for you.

>=20
> and use it at startup:
>=20
> (defmethod ccl::toplevel-function ((app fricas-application) init-file)
> [snippet]
>        (funcall *my-toplevel-function*)
>        (let ((ap (make-instance 'ccl::lisp-development-system)))
>            (ccl::toplevel-function ap init-file)))
>=20
> And, basically, the fricas-application (FiCAS) when
>=20
>       (CCL::save-application core-image
>                                      :PREPEND-KERNEL t
>                                      :application-class =
'fricas-application)
>=20
> Does not execute ccl:terminate, you have to return (start) to the
> "main" thead/process to allow CCL to execute the terminate method.

What does *my-toplevel-function* do?  If it=E2=80=99s just some =
additional initialization, maybe you could put a :before method on =
toplevel-function (application t) instead?

In the method toplevel-function (ccl::lisp-development-system t)

=
https://github.com/Clozure/ccl/blob/19906f6afdff4dbf232f560a4d13a81da65daf=
5f/level-1/l1-application.lisp#L284

we start a loop that calls the function called ccl::housekeeping a few =
times a second.  One of the housekeeping tasks is to run gc hook =
functions.  (The hook functions are managed via ccl::add-gc-hook and =
ccl::remove-gc-hook.)

=
https://github.com/Clozure/ccl/blob/19906f6afdff4dbf232f560a4d13a81da65daf=
5f/level-1/l1-events.lisp#L166

As you may suspect by now, the way termination functions get called is =
via a gc hook. So, if the housekeeping loop doesn=E2=80=99t run for some =
reason, then automatic termination won=E2=80=99t work.

If you can=E2=80=99t run the housekeeping loop, I think it would work to =
call ccl:drain-termination-queue manually as needed.



>=20
> Removing the need of a specific class for Clozure CL, FriCAS built
> with other CL implementations does not use a specific class, allowed
> me to solve my problem. But a question remains: why, after a
> save-application, and running directly at startup the above method,
> fricas can not use ccl:::terminate, apparently only the "main" thread
> is allowed to do that?
>=20
> Thanks all!
>=20
> - Greg
>=20
> PS: the diff when saving FriCAS application
>=20
>          (top-fun #'(lambda ()
>                        (set-initial-parameters)
> -                       (funcall restart-fun))))
> +                       (funcall restart-fun)
> +                       (ccl::toplevel-loop))))
>         (setf *ccl-default-directory* ccl-dir)
> -        (setf *my-toplevel-function* top-fun)
> -        (CCL::save-application core-image
> -                                       :PREPEND-KERNEL t
> -                                       :application-class =
'fricas-application)
> +        (CCL::save-application core-image :toplevel-function top-fun
> +                                       :PREPEND-KERNEL t)
>         (QUIT))
>=20