From jan@swi.psy.uva.nl  Fri Apr 21 12:57:46 2000
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id MAA06593;
	Fri, 21 Apr 2000 12:57:46 +0200 (MET DST)
Received: (from jan@localhost)
	by gollem.swi.psy.uva.nl (8.9.3/8.9.3/SuSE Linux 8.9.3-0.1) id MAA03498;
	Fri, 21 Apr 2000 12:58:20 +0200
Date: Fri, 21 Apr 2000 12:58:20 +0200
Message-Id: <200004211058.MAA03498@gollem.swi.psy.uva.nl>
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Subject: Re: synchronising foreign & top-level queries
To: Paul Singleton <p.singleton@keele.ac.uk>,
        SWI Prolog <prolog@swi.psy.uva.nl>
In-Reply-To: Paul Singleton's message of Thu, 20 Apr 2000 00:10:23 +0100
Phone: +31 - 20 - 525 6121

> Can I, in the non-threaded (Win32) Prolog VM, synchronise queries
> received at the FLI with queries posed at the top-level interpreter,
> so that they complete independently?

In theory this is possible using an interrupt.  This can be programmed
using signal handling (on_signal/3).  I'm not really convinced it is
100% safe though (I know it can be made 100% safe, but it is not very well
tested).  The Windows version might be even worse as asynchronous signals
are practically absent in Windows anyway.

> I'm using SWI-Prolog (Version 3.3.4) under NT4, and running an
> experimental Web service; the Prolog VM receives requests via JPL
> and the FLI, while I hack away at the top-level interpreter.
> 
> If an HTTP request comes in while the TLI is idle, or awaiting user
> input, it is handled OK, but if one comes in while the PVM is busy
> proving a top-level goal (or vice versa), there's a crash.

What really happens here?  Is the request entering on another Java thread
and it has to be processed using the SWI-Prolog running in the main
thread?  This can only be done if you manage to tell the main thread
running Prolog to sleep.  I don't know about the Windows thread model,
but using the Posix thread model this cannot be achieved reliable and
portable.

I think there are only two options.  One is to port the multi-threaded
version to Windows, in which case you can make a Prolog thread for the
Java web-server thread.  The other is to store the request, signal
Prolog to process it (using PL_raise()), wait for it to be processed
and pick the result.  Sounds pretty hairy!

> NB I am not running a public web server ;-) and am only doing things
> this way for debugging convenience, and I have no trouble interleaving
> HTTP requests and TLI activities safely, but if there's some protocol
> I could observe to make this safe, then I'd like to.
> 
> Alternatively, does this indicate some shortcoming in the foreign code?

Using the infra-structure created for multi-threading you could
achieve multiple Prolog engines (i.e. VM instances with their stacks)
and run queries interleaved on the various machines.  This would also
solve your problem, but basically you want multi-threading so the
clean route is to port this to Win32.  If there is a reliable and fast
port of the POSIX thread API for Win32 this should be rather trivial.
As far as I know the situation however this is not the case.  Note
that speed of mutexes is important: they are a major bottleneck in the
current SWI-Prolog MT version.

	Regards --- Jan

