From p.singleton@keele.ac.uk  Thu Feb 10 18:04:48 2000
Received: from cmailg4.svr.pol.co.uk (cmailg4.svr.pol.co.uk [195.92.195.174])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id SAA13268
	for <prolog@swi.psy.uva.nl>; Thu, 10 Feb 2000 18:04:47 +0100 (MET)
Received: from modem4294967188.people.dialup.pol.co.uk ([195.92.3.108] helo=keele.ac.uk)
	by cmailg4.svr.pol.co.uk with esmtp (Exim 3.13 #0)
	id 12Ix1f-0002kU-00
	for prolog@swi.psy.uva.nl; Thu, 10 Feb 2000 17:05:08 +0000
Message-ID: <38A2EE6C.E2EE116A@keele.ac.uk>
Date: Thu, 10 Feb 2000 16:59:24 +0000
From: Paul Singleton <p.singleton@keele.ac.uk>
Organization: SmartArts Computing Consultancy
X-Mailer: Mozilla 4.7 [en] (WinNT; I)
X-Accept-Language: en
MIME-Version: 1.0
To: SWI Prolog <prolog@swi.psy.uva.nl>
Subject: Re: initialising SWI-Prolog from foreign code
References: <38A1F16C.FB6BAECD@keele.ac.uk> <00021010154601.01684@gollem>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Jan Wielemaker wrote:

> On Wed, 09 Feb 2000, Paul Singleton wrote:

> >Is there a safe, portable way to discover, from foreign code,
> >whether Prolog has already been successfully initialised?

> No.  Of course this would be easy to add.

I think hybrid Prolog+Java applications will need this in general,
so I'd like to put in a request for this.

Just as Prolog apps can (whether they realise it or not) dynamically
load, start and use a JVM (e.g. via my "JSP" Prolog-calls-Java module
of Prolog and foreign predicates), so can Java apps dynamically load,
start and use a SWI-Prolog VM (if I may call it that :-) (e.g. via
Fred Dushin's JPL package of Java and "native methods").

Furthermore, Prolog may start Java via JSP to run a Java app which
calls Prolog via JPL, and vice versa!

Another possibility is that some third party decides to start both
Java and Prolog together.

Whichever, the apps shouldn't have to know or care about the order
in which things were started.

What JSP does currently is to check, on entry to every relevant
foreign predicate, whether a JVM is present, and start it with
default initialisation parameters if necessary.

This is done with a macro which checks whether a crucial pointer
(JVM *jvm :-) is NULL, so I don't begrudge the cost for the nice
properties it brings.

I't would be good to be able to do the corresponding thing from
Java.

> >Is there a way to "clean up the Prolog environment" without
> >exiting from the host process?

> Neither.   In the current implementation this would not be trivial.
> For quite a few things, Prolog has little clue what memory it
> holds.  On the other hand, if you accept some leaking it shouldn't
> be too hard.  Deallocating the stacks is implemented for the
> multi-threaded version and almost all memory is allocated through
> pl-alloc.c.  Only very few places use direct malloc().

This I *don't* need (nor have I figured out how to clean up
and restart a JVM if it aborts or exits).  This just came to mind
while I was thinking about initialisation.  But it could be of value
in the future.

> >If PL_initialise() is called when Prolog is already initialised,
> >does it put Prolog into its normal freshly initialised state?

> I think it might `appear to work', but I wouldn't give any
> guarantee on the consistency of the new environment.

This was another idle question, and given an already-initialised
test, there seems no need for this to ever be defined?

Paul Singleton

