Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify thread utilities #1025

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SpiNNaker-nmpiexec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>SpiNNaker-nmpimodel</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>SpiNNaker-utils</artifactId>
</dependency>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import uk.ac.manchester.spinnaker.nmpi.model.job.Status;
import uk.ac.manchester.spinnaker.nmpi.model.job.pynn.PyNNJobParameters;
import uk.ac.manchester.spinnaker.nmpi.model.machine.SpinnakerMachine;
import uk.ac.manchester.spinnaker.nmpiexec.utils.ThreadUtils;
import uk.ac.manchester.spinnaker.utils.ThreadUtils;

/**
* A process for running PyNN jobs.
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.nmpi.model.job.JobManagerInterface.JOB_PROCESS_MANAGER;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static org.apache.commons.io.FileUtils.forceMkdirParent;
import static org.apache.commons.io.FileUtils.listFiles;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;

import java.io.File;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.apache.commons.io.IOUtils.copy;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.nmpi.model.job.JobManagerInterface.JOB_PROCESS_MANAGER;

import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;

import java.io.File;
import java.io.FileInputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.nmpi.model.job.JobManagerInterface.JOB_PROCESS_MANAGER;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package uk.ac.manchester.spinnaker.nmpi.machinemanager;

import static java.util.Objects.nonNull;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import static org.apache.commons.io.IOUtils.buffer;
import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.waitfor;
import static uk.ac.manchester.spinnaker.nmpi.machinemanager.responses.JobState.DESTROYED;
import static uk.ac.manchester.spinnaker.nmpi.machinemanager.responses.JobState.READY;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static java.util.Objects.nonNull;
import static org.joda.time.DateTimeZone.UTC;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.sleep;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static java.util.Objects.nonNull;
import static org.joda.time.DateTimeZone.UTC;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.spinnaker.nmpi.ThreadUtils.sleep;
import static uk.ac.manchester.spinnaker.utils.ThreadUtils.sleep;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,28 @@
*/
/**
* The NMPI service classes.
* <p>
* The actual key service APIs are:
* <ul>
* <li>{@link NMPIQueueManager} &mdash; Handles the queue of {@linkplain Job
* NMPI jobs}.
* <li>{@link JobExecuter} &mdash; Handles the running of a single NMPI job.
* Note that job executer implementations ask the queue manager for the job that
* they should run.
* <li>{@link JobExecuterFactory} &mdash; Creates job executers.
* <li>{@link MachineManager} &mdash; Handles the resources on which NMPI jobs
* run.
* </ul>
*/
@UsedInJavadocOnly({
Job.class, JobExecuter.class, JobExecuterFactory.class,
MachineManager.class, NMPIQueueManager.class
})
package uk.ac.manchester.spinnaker.nmpi;

import uk.ac.manchester.spinnaker.nmpi.jobmanager.JobExecuter;
import uk.ac.manchester.spinnaker.nmpi.jobmanager.JobExecuterFactory;
import uk.ac.manchester.spinnaker.nmpi.machinemanager.MachineManager;
import uk.ac.manchester.spinnaker.nmpi.model.job.nmpi.Job;
import uk.ac.manchester.spinnaker.nmpi.nmpi.NMPIQueueManager;
import uk.ac.manchester.spinnaker.utils.UsedInJavadocOnly;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
* Utilities for working with threads.
*/
public abstract class ThreadUtils {
/** Avoid creation. */
private ThreadUtils() {
}

/**
* Produce a dump of what all threads are doing. Useful for debugging as it
* means you can get a dump of threads programmatically at the time when the
Expand All @@ -36,4 +40,55 @@ public static String threadDump() {
return stream(getThreadMXBean().dumpAllThreads(true, true))
.map(ThreadInfo::toString).collect(joining());
}

/**
* Recommended way of doing "quiet" sleeps.
*
* @param delay
* How long to sleep for, in milliseconds.
* @see <a href="https://stackoverflow.com/q/1087475/301832">Stack Overflow
* Question: When does Java's Thread.sleep throw
* InterruptedException?</a>
*/
public static void sleep(final long delay) {
try {
Thread.sleep(delay);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
}

/**
* Wait for the given object.
*
* @param obj
* The object to wait for
* @return True if the wait was interrupted, false otherwise
*/
public static boolean waitfor(final Object obj) {
try {
obj.wait();
return false;
} catch (final InterruptedException e) {
return true;
}
}

/**
* Wait for the given object.
*
* @param obj
* The object to wait for
* @param timeout
* The maximum time to wait, in milliseconds
* @return True if the wait was interrupted, false otherwise
*/
public static boolean waitfor(final Object obj, final long timeout) {
try {
obj.wait(timeout);
return false;
} catch (final InterruptedException e) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.SOURCE;

Expand All @@ -32,7 +33,7 @@
* @author Donal Fellows
*/
@Retention(SOURCE)
@Target({ TYPE, METHOD, CONSTRUCTOR, FIELD })
@Target({ TYPE, METHOD, CONSTRUCTOR, FIELD, PACKAGE })
public @interface UsedInJavadocOnly {
/**
* Class or classes just used by the annotated thing in Javadoc generation.
Expand Down
Loading