Skip to content

Commit

Permalink
Merge pull request #343 from gama-platform/Addresses-Issues-339-341-a…
Browse files Browse the repository at this point in the history
…nd-monitor-order-issue
  • Loading branch information
lesquoyb authored Sep 30, 2024
2 parents 6a3e85e + 5dac08e commit 974ec0f
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 139 deletions.
66 changes: 66 additions & 0 deletions gama.core/src/gama/core/common/interfaces/GeneralSynchronizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gama.core.common.interfaces;

import java.util.concurrent.Semaphore;

/**
* Encapsulates a general purpose {@link Semaphore}, with optional control over the maximum amount of permits allowed
*/
public class GeneralSynchronizer {

/**
* Returns a synchronizer with {@code init} initial permits and no maximum amount
*
* @param init
* @return
*/
public static GeneralSynchronizer withInitialPermits(final int init) {
return withInitialAndMaxPermits(init, Integer.MAX_VALUE);
}

/**
* Returns a synchronizer with {@code init} initial permits and a maximum amount of permits of {@code max}
*
* @param init
* @param max
* @return
*/
public static GeneralSynchronizer withInitialAndMaxPermits(final int init, final int max) {
return new GeneralSynchronizer(init, max);
}

private GeneralSynchronizer(final int n, final int max) {
semaphore = new Semaphore(n);
this.max = max;
}

final Semaphore semaphore;
int max;

public void release() {
if (semaphore.availablePermits() >= max) return;
semaphore.release();
}

public void acquire() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void release(final int nb) {
int already = semaphore.availablePermits();
if (already >= max) return;
semaphore.release(Math.min(max - already, nb));
}

public void acquire(final int n) {
try {
semaphore.acquire(n);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.concurrent.Semaphore;

import org.locationtech.jts.geom.Envelope;

Expand Down Expand Up @@ -112,12 +111,13 @@ public interface OpenGL extends IDisplaySurface {
* Asks the surface to update its display, optionnaly forcing it to do so (if it is paused, for instance). A
* synchronizer (possibly null) is passed, that needs to be released when the physical display is done
**/
void updateDisplay(boolean force, Semaphore synchronizer);
void updateDisplay(boolean force, GeneralSynchronizer synchronizer);

/**
* Update display.
*
* @param force the force
* @param force
* the force
*/
default void updateDisplay(final boolean force) {
updateDisplay(force, null);
Expand Down Expand Up @@ -419,7 +419,6 @@ default boolean canTriggerContextualMenu() {
return !getManager().hasMouseMenuEventLayer();
}


/**
* Gets the scope.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
package gama.core.kernel.experiment;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Semaphore;

import gama.core.common.interfaces.GeneralSynchronizer;
import gama.core.runtime.GAMA;
import gama.core.runtime.IScope;
import gama.core.runtime.server.GamaServerExperimentConfiguration;
Expand Down Expand Up @@ -48,7 +48,7 @@ public abstract class AbstractExperimentController implements IExperimentControl
protected volatile boolean acceptingCommands = true;

/** The lock. Used to pause the experiment */
protected final Semaphore lock = new Semaphore(1);
protected final GeneralSynchronizer lock = GeneralSynchronizer.withInitialAndMaxPermits(1, 1);

/** The experiment. */
protected IExperimentPlan experiment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public DefaultExperimentController(final IExperimentPlan experiment) {
executionThread = new Thread(() -> { while (experimentAlive) { step(); } }, "Front end scheduler");
executionThread.setUncaughtExceptionHandler(GamaExecutorService.EXCEPTION_HANDLER);
commandThread.setUncaughtExceptionHandler(GamaExecutorService.EXCEPTION_HANDLER);
try {
lock.acquire();
} catch (final InterruptedException e) {}
lock.acquire();
commandThread.start();
executionThread.start();
}
Expand Down Expand Up @@ -212,11 +210,8 @@ public void schedule(final ExperimentAgent agent) {
*/
protected void step() {
if (paused) {
try {
lock.acquire();
} catch (InterruptedException e) {
experimentAlive = false;
}
lock.acquire();
// experimentAlive = false;
}
try {
if (scope == null) return;
Expand Down
38 changes: 21 additions & 17 deletions gama.core/src/gama/core/kernel/simulation/SimulationAgent.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*******************************************************************************************************
*
* SimulationAgent.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform
* .
* SimulationAgent.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform .
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
Expand Down Expand Up @@ -455,7 +454,7 @@ public void dispose() {
}
if (externalInitsAndParameters != null) { externalInitsAndParameters.clear(); }

//we make sure that all pending write operations are flushed
// we make sure that all pending write operations are flushed
GAMA.getBufferingController().flushSaveFilesOfAgent(this);
GAMA.getBufferingController().flushWriteOfAgent(this);
GAMA.releaseScope(getScope());
Expand Down Expand Up @@ -789,7 +788,7 @@ public String buildPostfix() {
* @param iOutputManager
* the new outputs
*/
@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
public void setOutputs(final IOutputManager iOutputManager) {
if (iOutputManager == null) return;
// AD : condition removed for Issue #3748
Expand All @@ -800,23 +799,23 @@ public void setOutputs(final IOutputManager iOutputManager) {
if (des == null) return;
outputs = (SimulationOutputManager) des.compile();
final Map<String, IOutput> mm = GamaMapFactory.create();
for (final Map.Entry<String, ? extends IOutput> entry : outputs.getOutputs().entrySet()) {
final IOutput output = entry.getValue();
outputs.forEach((oName, output) -> {
String keyName, newOutputName;
if (!scheduled) {
keyName = output.getName() + "#" + this.getSpecies().getDescription().getModelDescription().getAlias()
+ "#" + this.getExperiment().getSpecies().getName() + "#" + this.getExperiment().getIndex();
newOutputName = keyName;
} else {
final String postfix = buildPostfix();
keyName = entry.getKey() + postfix;
keyName = oName + postfix;
newOutputName = output.getName() + postfix;
}
mm.put(keyName, output);
output.setName(newOutputName);
}
});
outputs.clear();
outputs.putAll(mm);

// AD : reverted for Issue #3748
// } else {
// outputs = (SimulationOutputManager) iOutputManager;
Expand Down Expand Up @@ -969,7 +968,7 @@ public void executeAction(final IExecutable executable) {

}

@SuppressWarnings("unchecked")
@SuppressWarnings ("unchecked")
@Override
public void updateWith(final IScope scope, final ISerialisedAgent sa) {

Expand Down Expand Up @@ -998,14 +997,19 @@ public void updateWith(final IScope scope, final ISerialisedAgent sa) {
}

// If attributes are related to the RNG, we keep them to initialise the RNG later, in the proper order.
if (IKeyword.SEED.equals(varName)) {
seedValue = (Double) attrValue;
} else if (IKeyword.RNG.equals(varName)) {
rngValue = (String) attrValue;
} else if (SimulationAgent.USAGE.equals(varName)) {
usageValue = (Integer) attrValue;
} else {
this.setDirectVarValue(scope, varName, attrValue);
switch (varName) {
case IKeyword.SEED:
seedValue = (Double) attrValue;
break;
case IKeyword.RNG:
rngValue = (String) attrValue;
break;
case SimulationAgent.USAGE:
usageValue = (Integer) attrValue;
break;
default:
this.setDirectVarValue(scope, varName, attrValue);
break;
}

}
Expand Down
Loading

0 comments on commit 974ec0f

Please sign in to comment.