Subject: Re: synchronising foreign & top-level queries

From: Jan Wielemaker <[email-masked]>
Date: Fri, 21 Apr 2000 12:58:20 +0200

> 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