Skip to content

Commit

Permalink
Add support for running in fast mode, simply return immediatly from a…
Browse files Browse the repository at this point in the history
…ny call to wait_until.
  • Loading branch information
erlingrj committed Nov 28, 2024
1 parent 9f98fb8 commit ec566b9
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 8 deletions.
1 change: 1 addition & 0 deletions include/reactor-uc/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Environment {
Scheduler *scheduler; // The scheduler in charge of executing the reactions.
Platform *platform; // The platform that provides the physical time and sleep functions.
bool has_async_events;
bool fast_mode;
BuiltinTrigger *startup; // A pointer to a startup trigger, if the program has one.
BuiltinTrigger *shutdown; // A pointer to a chain of shutdown triggers, if the program has one.
FederatedConnectionBundle **net_bundles; // A pointer to an array of NetworkChannel pointers that are used to
Expand Down
3 changes: 2 additions & 1 deletion include/reactor-uc/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ typedef struct FederatedInputConnection FederatedInputConnection;
self->_children[_child_idx++] = &self->instanceName[i].super; \
}

#define ENTRY_POINT(MainReactorName, Timeout, KeepAlive) \
#define ENTRY_POINT(MainReactorName, Timeout, KeepAlive, Fast) \
MainReactorName main_reactor; \
Environment env; \
void lf_exit(void) { Environment_free(&env); } \
Expand All @@ -611,6 +611,7 @@ typedef struct FederatedInputConnection FederatedInputConnection;
MainReactorName##_ctor(&main_reactor, NULL, &env); \
env.scheduler->duration = Timeout; \
env.scheduler->keep_alive = KeepAlive; \
env.fast_mode = Fast; \
env.assemble(&env); \
env.start(&env); \
lf_exit(); \
Expand Down
6 changes: 3 additions & 3 deletions lfc/core/src/main/java/org/lflang/target/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import java.util.Set;
import net.jcip.annotations.Immutable;
import org.lflang.lf.TargetDecl;
import org.lflang.target.property.BuildTypeProperty;
import org.lflang.target.property.PlatformProperty;
import org.lflang.target.property.*;

/**
* Enumeration of targets and their associated properties.
Expand Down Expand Up @@ -567,7 +566,8 @@ public void initialize(TargetConfig config) {
// TracePluginProperty.INSTANCE,
// VerifyProperty.INSTANCE,
// WorkersProperty.INSTANCE);
case UC -> config.register(BuildTypeProperty.INSTANCE, PlatformProperty.INSTANCE);
case UC -> config.register(BuildTypeProperty.INSTANCE, PlatformProperty.INSTANCE, TimeOutProperty.INSTANCE, FastProperty.INSTANCE, KeepaliveProperty.INSTANCE);

// case CPP ->
// config.register(
// BuildTypeProperty.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.lflang.target.property;

import org.lflang.MessageReporter;
import org.lflang.ast.ASTUtils;
import org.lflang.lf.Element;
import org.lflang.target.property.type.PrimitiveType;

public abstract class BooleanProperty extends TargetProperty<Boolean, PrimitiveType> {

protected BooleanProperty() {
super(PrimitiveType.BOOLEAN);
}

@Override
public Boolean initialValue() {
return false;
}

@Override
public Boolean fromAst(Element node, MessageReporter reporter) {
return ASTUtils.toBoolean(node);
}

@Override
protected Boolean fromString(String string, MessageReporter reporter) {
return Boolean.parseBoolean(string);
}

@Override
public Element toAstElement(Boolean value) {
return ASTUtils.toElement(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.lflang.target.property;

import org.lflang.MessageReporter;
import org.lflang.ast.ASTUtils;
import org.lflang.lf.Action;
import org.lflang.lf.ActionOrigin;
import org.lflang.lf.LfPackage.Literals;
import org.lflang.lf.Reactor;
import org.lflang.target.Target;
import org.lflang.target.TargetConfig;

/**
* If true, configure the execution environment such that it does not wait for physical time to
* match logical time. The default is false.
*/
public final class FastProperty extends BooleanProperty {

/** Singleton target property instance. */
public static final FastProperty INSTANCE = new FastProperty();

private FastProperty() {
super();
}

@Override
public String name() {
return "fast";
}

@Override
public void validate(TargetConfig config, MessageReporter reporter) {
var pair = config.lookup(this);
if (config.isSet(this) && config.isFederated()) {
reporter
.at(pair, Literals.KEY_VALUE_PAIR__NAME)
.error("The fast target property is incompatible with federated programs.");
}

// if (config.target != Target.CPP) {
// Check for physical actions
for (Reactor reactor : ASTUtils.getAllReactors(config.getMainResource())) {
// Check to see if the program has a physical action in a reactor
for (Action action : reactor.getActions()) {
if (action.getOrigin().equals(ActionOrigin.PHYSICAL)) {
reporter
.at(pair, Literals.KEY_VALUE_PAIR__NAME)
.error(
String.format(
"In the %s target, the fast target property is incompatible with physical"
+ " actions.",
config.target.toString()));
break;
}
}
}
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.lflang.target.property;

/**
* If true, configure the execution environment to keep executing if there are no more events on the
* event queue. The default is false.
*/
public final class KeepaliveProperty extends BooleanProperty {

/** Singleton target property instance. */
public static final KeepaliveProperty INSTANCE = new KeepaliveProperty();

private KeepaliveProperty() {
super();
}

@Override
public String name() {
return "keepalive";
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.lflang.generator.uc

import com.ibm.icu.util.CodePointTrie.Fast
import org.lflang.target.TargetConfig
import org.lflang.generator.PrependOperator
import org.lflang.generator.uc.UcReactorGenerator.Companion.codeType
import org.lflang.inferredType
import org.lflang.lf.Parameter
import org.lflang.lf.Reactor
import org.lflang.target.property.FastProperty
import org.lflang.target.property.KeepaliveProperty
import org.lflang.target.property.PlatformProperty
import org.lflang.target.property.TimeOutProperty
import org.lflang.target.property.type.PlatformType
Expand Down Expand Up @@ -37,13 +40,15 @@ class UcMainGenerator(

fun getDuration() = if (targetConfig.isSet(TimeOutProperty.INSTANCE)) targetConfig.get(TimeOutProperty.INSTANCE).toCCode() else "FOREVER"

fun keepAlive() = "false"
fun keepAlive() = if(targetConfig.isSet(KeepaliveProperty.INSTANCE)) "true" else "false"

fun fast() = if(targetConfig.isSet(FastProperty.INSTANCE)) "true" else "false"

fun generateMainSource() = with(PrependOperator) {
"""
|#include "reactor-uc/reactor-uc.h"
|#include "${fileConfig.getReactorHeaderPath(main).toUnixString()}"
|ENTRY_POINT(${main.codeType}, ${getDuration()}, ${keepAlive()});
|ENTRY_POINT(${main.codeType}, ${getDuration()}, ${keepAlive()}, ${fast()});
${" |"..generateMainFunction()}
""".trimMargin()
}
Expand Down
3 changes: 2 additions & 1 deletion src/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void Environment_start(Environment *self) {
}

lf_ret_t Environment_wait_until(Environment *self, instant_t wakeup_time) {
if (wakeup_time <= self->get_physical_time(self)) {
if (wakeup_time <= self->get_physical_time(self) || self->fast_mode) {
return LF_OK;
}

Expand Down Expand Up @@ -82,6 +82,7 @@ void Environment_ctor(Environment *self, Reactor *main) {
self->enter_critical_section = Environment_enter_critical_section;
self->request_shutdown = Environment_request_shutdown;
self->has_async_events = false;
self->fast_mode = false;
self->startup = NULL;
self->shutdown = NULL;
}
Expand Down
13 changes: 13 additions & 0 deletions test/lf/src/Fast.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
target uC {
fast: true,
timeout: 1 hour
}

main reactor {
timer t(0, 1 sec)
reaction(t) {= =}

reaction(shutdown) {=
printf("Finally shutdown executed for %" PRId64 " logical sec\n", env->get_elapsed_logical_time(env)/SEC(1));
=}
}
2 changes: 1 addition & 1 deletion test/unit/startup_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ REACTOR_CTOR_SIGNATURE(StartupTest) {
STARTUP_REGISTER_EFFECT(self->r_startup);
}

ENTRY_POINT(StartupTest, FOREVER, false);
ENTRY_POINT(StartupTest, FOREVER, false, false);

int main() {
UNITY_BEGIN();
Expand Down

0 comments on commit ec566b9

Please sign in to comment.