mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-15 17:33:38 +00:00
The thread pool code used in the Java backend was tied the execution of
main/2. However if Mercury is used as a library the thread pool won't have
been started and threads created with thread.spawn would not be executed.
This patch makes it possible to start and stop the thread pool independently of
main/2 by calling startup() and shutdown(). These calls are called
implicitly by calling runMain(). The thread pool can also be started on
demand.
This patch also adds the MercuryRuntime class, which now contains methods
that may be called by users' Java code to interact with the Mercury runtime
system, including a new finalise() method.
java/runtime/MercuryThreadPool.java:
Add startup() method.
shutdown() method is now public and it's meaning has changed, it now
requests the shutdown rather than performing it.
Renamed some variables to make their meanings clearer.
java/runtime/JavaInternal.java:
Initialise the ThreadPool and MercuryOptions objects on demand.
Make all members of this class static to avoid confusion.
Add a private constructor.
java/runtime/MercuryRuntime.java:
Add methods that can be called by Mercury users to interact with the
runtime system. Including a convenient finalise() method that does all
the finalisation.
samples/java_interface/standalone_java/mercury_lib.m:
samples/java_interface/standalone_java/JavaMain.java:
Extend the standalone Java example so that it makes use of threads: Add
a fibs function in Mercury that uses concurrency and therefore starts
the thread pool; call it from the Java code.
Use the new finalise() method from the MercuryRuntime class inside of a
finally block.
samples/java_interface/standalone_java/Makefile:
Fix a minor error.
60 lines
1.6 KiB
Java
60 lines
1.6 KiB
Java
//
|
|
// Copyright (C) 2014 The Mercury Team
|
|
// This file may only be copied under the terms of the GNU Library General
|
|
// Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
//
|
|
|
|
package jmercury.runtime;
|
|
|
|
/**
|
|
* Interface to the Mercury Runtime System for Java code.
|
|
*
|
|
* No instance of this class is ever created, all it's members and methods
|
|
* are static.
|
|
*/
|
|
public class MercuryRuntime
|
|
{
|
|
/**
|
|
* Private constructor.
|
|
* This private constructor doesn't do anything and isn't called by
|
|
* anyone. It exists only to prevent people from creating an instance.
|
|
*/
|
|
private MercuryRuntime() {
|
|
}
|
|
|
|
private static MercuryThreadPool thread_pool = null;
|
|
|
|
/**
|
|
* Return the thread pool, initalising it if required.
|
|
* This does not start the thread pool. It is started either when
|
|
* startup() is called or automatically when the first task is
|
|
* submitted.
|
|
*/
|
|
public static synchronized MercuryThreadPool getThreadPool()
|
|
{
|
|
if (thread_pool == null) {
|
|
thread_pool = new MercuryThreadPool(
|
|
JavaInternal.getOptions().getNumProcessors());
|
|
}
|
|
return thread_pool;
|
|
}
|
|
|
|
/**
|
|
* Retrive the exit status stored in the IO state.
|
|
*/
|
|
public static int getExitStatus() {
|
|
return JavaInternal.exit_status;
|
|
}
|
|
|
|
/**
|
|
* Finalise the runtime system.
|
|
* This _must_ be cAlled at the normal end of any program. Currently
|
|
* it runs finalisers and stops the thread pool.
|
|
*/
|
|
public static void finalise() {
|
|
JavaInternal.run_finalisers();
|
|
getThreadPool().shutdown();
|
|
}
|
|
}
|
|
|