mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-16 18:03:36 +00:00
If threads are blocked while there is work in the queue extra threads may be
spawned to keep the processors busy.
Beginning now, tasks created with thread.spawn are use the thread pool.
(thread.spawn_native does not use the thread pool.)
java/runtime/Semaphore.java:
Wrap Java's Semaphore class which call the current thread's blocked()
and running() methods when a thread blocks and then runs after being
blocked.
library/thread.semaphore.m:
Use our own Semaphore class.
java/runtime/MercuryThread.java:
java/runtime/MercuryWorkerThread.java:
Define blocked() and running() on our threads.
java/runtime/NativeThread.java:
This class is used by spawn_native/4 and is required to define blocked()
and running(), however it implements them as no-ops as it isn't included
in the thread pool.
java/runtime/ThreadStatus.java:
Define the BLOCKED status.
java/runtime/MercuryThreadPool.java:
Count blocked threads seperatly and allow the creation of new threads
when existing threads become blocked.
Add some tracing code to help debug the thread management code. This is
disabled by default.
library/thread.m:
Implement spawn for Java using the thread pool. This was not enabled
earlier because without using java/runtime/Semaphore.java it was
possible to deadlock the system.
java/runtime/Task.java:
Add some tracing code to debug thread state changes, this is disabled by
default.
93 lines
2.7 KiB
Java
93 lines
2.7 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;
|
|
|
|
/**
|
|
* Threads for the Mercury code running in Java.
|
|
*/
|
|
public class MercuryWorkerThread extends MercuryThread
|
|
{
|
|
private MercuryThreadPool pool;
|
|
|
|
private ThreadStatus status;
|
|
|
|
/**
|
|
* Construct a new MercuryThread with the given ID and runnable.
|
|
* @param pool The Mercury thread pool.
|
|
* @param id A numeric identifier (should be unique).
|
|
*/
|
|
public MercuryWorkerThread(MercuryThreadPool pool, int id)
|
|
{
|
|
super("Mercury Worker Thread", id);
|
|
this.pool = pool;
|
|
this.status = ThreadStatus.OTHER;
|
|
}
|
|
|
|
/**
|
|
* Run.
|
|
* The worker thread executes tasks that it retrives from the pool.
|
|
*/
|
|
public void run()
|
|
{
|
|
Task task;
|
|
|
|
do {
|
|
task = null;
|
|
try {
|
|
if (status != ThreadStatus.IDLE) {
|
|
setStatus(ThreadStatus.IDLE);
|
|
}
|
|
task = pool.workerGetTask();
|
|
}
|
|
catch (InterruptedException e) {
|
|
/*
|
|
** A worker thread has no semantics for this, so we continue
|
|
** looping.
|
|
*/
|
|
continue;
|
|
}
|
|
if (task != null) {
|
|
try {
|
|
setStatus(ThreadStatus.WORKING);
|
|
task.run();
|
|
pool.taskDone(task);
|
|
} catch (jmercury.runtime.Exception e) {
|
|
// The task threw a Mercury exception.
|
|
pool.taskFailed(task, e);
|
|
JavaInternal.reportUncaughtException(e);
|
|
} catch (Throwable e) {
|
|
// Some other error occured. bail out.
|
|
System.err.println("Uncaught exception: " + e.toString());
|
|
System.err.println(e.getMessage());
|
|
e.printStackTrace();
|
|
System.exit(1);
|
|
} finally {
|
|
setStatus(ThreadStatus.OTHER);
|
|
}
|
|
}
|
|
} while (task != null);
|
|
|
|
pool.threadShutdown(this, status);
|
|
}
|
|
|
|
protected void setStatus(ThreadStatus new_status) {
|
|
pool.updateThreadCounts(status, new_status);
|
|
status = new_status;
|
|
}
|
|
|
|
public void blocked() {
|
|
pool.updateThreadCounts(status, ThreadStatus.BLOCKED);
|
|
status = ThreadStatus.BLOCKED;
|
|
}
|
|
|
|
public void running() {
|
|
pool.updateThreadCounts(status, ThreadStatus.WORKING);
|
|
status = ThreadStatus.WORKING;
|
|
}
|
|
}
|
|
|