Skip to content

Commit

Permalink
[sre] New forced shutdown.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed May 14, 2020
1 parent 8fa6ab4 commit d2827f9
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 314 deletions.
Expand Up @@ -234,8 +234,8 @@ public <T> T getService(Class<T> serviceType) {
}

@Override
public void shutdown(boolean blocking, int timeout) throws InterruptedException {
throw new UnsupportedOperationException();
public void shutdown(int timeout) throws InterruptedException {
//
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.util.UUID;
import java.util.logging.Logger;

import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pure;

import io.sarl.lang.core.Agent;
Expand Down Expand Up @@ -257,15 +258,20 @@ default Logger getKernelLogger() {
* @see #shutdown(int)
* @see #shutdown(boolean, int)
*/
@Inline("shutdown(-1)")
default void shutdown() throws InterruptedException {
shutdown(true, -1);
shutdown(-1);
}

/**
* Stop the SRE without an agent.
* This function may cause the agents to stop during the run of a behavior.
* This function has no timeout.
*
* <p>If the argument's value is evaluated to {@code true}, the {@link #shutdown(int)}
* functions is invoked with negative timeout; Otherwise the {@link #shutdown(int)}
* functions is invoked with {@code 0} value.
*
* @param blocking indicates if the functions is blocked until the shutdown task is finished.
* If it is {@code true}, the function returns when the kernel is down. If it is
* {@code false}, the function could return before the kernel is down.
Expand All @@ -275,41 +281,30 @@ default void shutdown() throws InterruptedException {
* @see #shutdown(int)
* @see #shutdown(boolean, int)
*/
@Inline("shutdown(($1) ? -1 : 0)")
default void shutdown(boolean blocking) throws InterruptedException {
shutdown(blocking, -1);
shutdown(blocking ? -1 : 0);
}

/**
* Stop the SRE without an agent.
* This function may cause the agents to stop during the run of a behavior.
* This function returns when the kernel and all its services are stopped.
*
* @param timeout the maximal duration to wait for the shutdown, in milliseconds.
* @throws InterruptedException the shutdown process is interrupted.
* @since 0.11
* @see #shutdown()
* @see #shutdown(boolean)
* @see #shutdown(int)
*/
default void shutdown(int timeout) throws InterruptedException {
shutdown(true, timeout);
}

/**
* Stop the SRE without an agent.
* This function may cause the agents to stop during the run of a behavior.
*
* @param blocking indicates if the functions is blocked until the shutdown task is finished.
* If it is {@code true}, the function returns when the kernel is down. If it is
* {@code false}, the function could return before the kernel is down.
* @param timeout the maximal duration to wait for the shutdown, in milliseconds.
* @param timeout the maximum amount of milliseconds to wait for the shutdown.
* If the provided value is strictly positive, it is the number of milliseconds
* to wait for the termination of the shutdown. If the provided value is equal
* to {@code 0}, the function returns as soon as the shutdown process is launch
* (no waiting for the termination of the shutdown). If the value is strictly
* negative, the function waits forever for the termination of the shutdown
* process. The default value is {@code -1}.
* @throws InterruptedException the shutdown process is interrupted.
* @since 0.11
* @see #shutdown()
* @see #shutdown(boolean)
* @see #shutdown(int)
*/
void shutdown(boolean blocking, int timeout) throws InterruptedException;
void shutdown(int timeout) throws InterruptedException;

/** Replies the service of the given type that is implemented into the SRE.
*
Expand Down
Expand Up @@ -238,6 +238,12 @@ class Kernel {
* Shutdown the kernel.
*
* @param timeout the maximum amount of milliseconds to wait for the shutdown.
* If the provided value is strictly positive, it is the number of milliseconds
* to wait for the termination of the shutdown. If the provided value is equal
* to {@code 0}, the function returns as soon as the shutdown process is launch
* (no waiting for the termination of the shutdown). If the value is strictly
* negative, the function waits forever for the termination of the shutdown
* process. The default value is {@code -1}.
* @since 0.10
*/
@SuppressWarnings("use_reserved_sarl_annotation")
Expand Down Expand Up @@ -286,7 +292,7 @@ class Kernel {
while (!agentIds.empty && System::currentTimeMillis <= endTime) {
Thread::yield
}
} else {
} else if (timeout != 0) {
while (!agentIds.empty) {
Thread::yield
}
Expand Down
Expand Up @@ -45,16 +45,16 @@ import io.sarl.sre.services.lifecycle.LifecycleService
import io.sarl.sre.services.logging.LoggingService
import java.util.List
import java.util.UUID
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicReference
import java.util.logging.Level
import org.apache.log4j.Logger
import org.arakhne.afc.bootique.log4j.configs.Level
import org.arakhne.afc.bootique.log4j.configs.Log4jIntegrationConfig
import org.arakhne.afc.bootique.variables.VariableNames
import org.arakhne.afc.util.ListenerCollection
import org.eclipse.xtend.lib.annotations.Accessors

import static io.sarl.bootstrap.SRE.*
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.Executors

/** Class that implements the standard main function for running a SARL run-time environment
* with bootique modules.
Expand All @@ -67,6 +67,8 @@ import java.util.concurrent.Executors
*/
class SreMain implements SREBootstrap {

static val BACKGROUND_SHUTDOWN_TIMEOUT = 20000

val kernel = new AtomicReference<Kernel>

@Accessors(PUBLIC_GETTER)
Expand Down Expand Up @@ -139,7 +141,7 @@ class SreMain implements SREBootstrap {

override setVerboseLevel(level : int) {
var config = ensureBootiqueRuntimeInstance(null, null).getInstance(typeof(Log4jIntegrationConfig))
var levels = Level::values
var levels = org.arakhne.afc.bootique.log4j.configs.Level::values
var lvl = if (level < 0) 0 else (if (level >= levels.length) levels.length - 1 else level)
var levelObject = levels.get(lvl)
config.level = levelObject
Expand Down Expand Up @@ -330,14 +332,14 @@ class SreMain implements SREBootstrap {
}

@SuppressWarnings("discouraged_reference")
def shutdown(blocking : boolean, timeout : int) {
def shutdown(timeout : int) {
val kern = this.kernel.getAndSet(null)
if (kern !== null) {
if (blocking) {
if (timeout !== 0) {
kern.shutdown(timeout)
} else {
val th = Executors::defaultThreadFactory.newThread [
kern.shutdown(timeout)
kern.shutdown(BACKGROUND_SHUTDOWN_TIMEOUT)
]
th => [
daemon = true
Expand Down Expand Up @@ -382,7 +384,7 @@ class SreMain implements SREBootstrap {
try {
observer.sreStarted(this)
} catch (exception : Throwable) {
getKernelLogger.log(java.util.logging.Level::SEVERE, exception.localizedMessage, exception)
getKernelLogger.log(Level::SEVERE, exception.localizedMessage, exception)
}
}
}
Expand All @@ -397,7 +399,7 @@ class SreMain implements SREBootstrap {
try {
observer.sreStopped(this)
} catch (exception : Throwable) {
getKernelLogger.log(java.util.logging.Level::SEVERE, exception.localizedMessage, exception)
getKernelLogger.log(Level::SEVERE, exception.localizedMessage, exception)
}
}
}
Expand Down

0 comments on commit d2827f9

Please sign in to comment.