Files
mercury/java/runtime/MercuryThread.java
Paul Bone a2879c6837 Make MercuryThreadPool (java) notice when a thread blocks on a semaphore.
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.
2014-10-03 19:22:32 +10:00

55 lines
1.2 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;
/**
* A thread with some Mercury specific support.
*/
public abstract class MercuryThread extends Thread
{
private int id;
/**
* Construct a new MercuryThread with the given ID.
* @param name A string that identifies the type of thread.
* @param id A numeric identifier (should be unique).
*/
public MercuryThread(String name, int id)
{
super(name + " " + id);
this.id = id;
}
/**
* The thread has become blocked.
*/
public abstract void blocked();
/**
* The thread is unblocked and is now running again.
*/
public abstract void running();
/**
* If the current thread is a MercuryThread then return a reference to
* it.
*/
public static MercuryThread currentThread()
{
Thread thread;
thread = Thread.currentThread();
if (thread instanceof MercuryThread) {
return (MercuryThread)thread;
} else {
return null;
}
}
}