mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-16 09:53:36 +00:00
[java] The thread pool now works when Mercury is used as a library
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.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//
|
||||
// Copyright (C) 2001-2003, 2009 The University of Melbourne.
|
||||
// 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.
|
||||
//
|
||||
@@ -9,33 +10,46 @@
|
||||
package jmercury.runtime;
|
||||
|
||||
/**
|
||||
* Internals for Mercury's runtime system on the Java backend.
|
||||
* At the moment this class is used to store the main module's name (progname),
|
||||
* command line arguments and the exit status. We can't put them in one of the
|
||||
* Internals and static objects for Mercury's runtime system on the Java
|
||||
* backend.
|
||||
* This class is used to store the main module's name (progname), command
|
||||
* line arguments and the exit status. We can't put them in one of the
|
||||
* library modules because we need to hold them in a class variable in a top
|
||||
* level class.
|
||||
*
|
||||
* The class also contains utility methods.
|
||||
* The class also contains utility methods and other objects such as a
|
||||
* reference to the thread pool.
|
||||
*
|
||||
* No instance of this class is ever created, all it's members and methods
|
||||
* are static.
|
||||
*/
|
||||
public class JavaInternal {
|
||||
|
||||
private static JavaInternal instance;
|
||||
|
||||
/**
|
||||
* 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 JavaInternal() {
|
||||
options = new MercuryOptions();
|
||||
options.process();
|
||||
thread_pool = new MercuryThreadPool(options.getNumProcessors());
|
||||
}
|
||||
|
||||
private MercuryThreadPool thread_pool;
|
||||
private MercuryOptions options;
|
||||
private static MercuryThreadPool thread_pool = null;
|
||||
private static MercuryOptions options = null;
|
||||
|
||||
public static MercuryThreadPool getThreadPool() {
|
||||
return instance.thread_pool;
|
||||
public static synchronized MercuryThreadPool getThreadPool() {
|
||||
if (thread_pool == null) {
|
||||
thread_pool = new MercuryThreadPool(
|
||||
getOptions().getNumProcessors());
|
||||
}
|
||||
return thread_pool;
|
||||
}
|
||||
|
||||
public static MercuryOptions getOptions() {
|
||||
return instance.options;
|
||||
public static synchronized MercuryOptions getOptions() {
|
||||
if (options == null) {
|
||||
options = new MercuryOptions();
|
||||
options.process();
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
public static java.lang.String progname;
|
||||
@@ -57,11 +71,12 @@ public class JavaInternal {
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the main task using the thread pool.
|
||||
* Run the main task.
|
||||
* The maun task is executed by the thread pool so that when it blocks
|
||||
* the thread pool is notified correctly.
|
||||
*/
|
||||
public static void runMain(Runnable main)
|
||||
{
|
||||
instance = new JavaInternal();
|
||||
getThreadPool().runMain(main);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user