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

Fix preemption locks in Clocks.fmu #478

Merged
merged 2 commits into from
Mar 26, 2024
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
56 changes: 26 additions & 30 deletions Clocks/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
/*

time 0 1 2 3 4 5 6 7 8 9
inClock1 + + + + + + + + + + t % 4 == 0
inClock2 + + + + t % 8 == 0 || (t - 1) % 8 == 0
inClock3 + countdown depends on inClock1
outClock ? ? ? ? ? ? ? ? ? ? totalInTicks % 5 == 0 (triggered by all inClocks)
inClock1 + + + + + + + + + + input, triggered by the simulation algorithm every second
inClock2 + + + + input, triggered by the simulation algorithm at 0, 1, 8 and 9
inClock3 + input, triggered by inClock1
outClock ? ? ? ? ? ? ? ? ? ? output, triggered by all inClocks (if totalInTicks % 5 == 0)
time 0 1 2 3 4 5 6 7 8 9

*/
Expand All @@ -22,9 +22,10 @@ ModelPartition 1 does the following:
**************************************/
static void activateModelPartition1(ModelInstance* comp, double time) {

if (comp->lockPreemtion) {
comp->lockPreemtion();
}
/* Note: model partition of highest priority
-> no interruption by other model partitions possible
-> no lock preemption needed
*/

// increment the counters
M(inClock1Ticks)++;
Expand All @@ -38,10 +39,6 @@ static void activateModelPartition1(ModelInstance* comp, double time) {

M(outClock) = ((M(outClock) == false) && (M(totalInClockTicks) % 5 == 0));

if (comp->unlockPreemtion) {
comp->unlockPreemtion();
}

if (M(inClock3_qualifier) == 2 || M(outClock)) {
comp->clockUpdate(comp->componentEnvironment);
}
Expand All @@ -50,29 +47,29 @@ static void activateModelPartition1(ModelInstance* comp, double time) {
/**************************************
ModelPartition 2 does the following:
- increments the clock tick counters
- gets an input value (from ModelPartition3)
- gets an input value
- triggers outClock, if the number of totalInTicks is a multiple of 5
**************************************/
static void activateModelPartition2(ModelInstance* comp, double time) {

UNUSED(time);

if (comp->lockPreemtion) {
comp->lockPreemtion();
if (comp->lockPreemption) {
comp->lockPreemption();
}

// increment the counters
M(inClock2Ticks)++;
M(totalInClockTicks)++;

M(result2) += M(input2); // add the output from mp3
M(input2) = 0; // then reset the value
M(result2) += M(input2);
M(input2) = 0;

// set output clocks
M(outClock) = ((M(outClock) == false) && (M(totalInClockTicks) % 5 == 0));

if (comp->unlockPreemtion) {
comp->unlockPreemtion();
if (comp->unlockPreemption) {
comp->unlockPreemption();
}

if (M(outClock)) {
Expand All @@ -91,30 +88,29 @@ static void activateModelPartition3(ModelInstance *comp, double time) {

UNUSED(time);

if (comp->lockPreemtion) {
comp->lockPreemtion();
}

// increment the counters
M(inClock3Ticks)++;

if (comp->unlockPreemtion) {
comp->unlockPreemtion();
}

// This partition is supposed to consume a bit of time on a low prio ...
unsigned long sum = 0;
for (int loop = 1; loop < 1000000000; loop++) {
sum += loop;
}
(void)sum; // use variable to avoid compiler warnings

// ... end of burning CPU cycles
M(output3) = 1000; // this is suposed to find its way into mp2
M(totalInClockTicks)++;
M(output3) = 1000;

// set output clocks
if (comp->lockPreemption) {
comp->lockPreemption();
}

M(totalInClockTicks)++;
M(outClock) = ((M(outClock) == false) && (M(totalInClockTicks) % 5 == 0));

if (comp->unlockPreemption) {
comp->unlockPreemption();
}

if (M(outClock)) {
comp->clockUpdate(comp->componentEnvironment);
}
Expand Down
4 changes: 2 additions & 2 deletions include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ typedef struct {
intermediateUpdateType intermediateUpdate;
clockUpdateType clockUpdate;

lockPreemptionType lockPreemtion;
unlockPreemptionType unlockPreemtion;
lockPreemptionType lockPreemption;
unlockPreemptionType unlockPreemption;

bool logEvents;
bool logErrors;
Expand Down
8 changes: 4 additions & 4 deletions src/cosimulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ ModelInstance *createModelInstance(
comp->componentEnvironment = componentEnvironment;
comp->logger = cbLogger;
comp->intermediateUpdate = intermediateUpdate;
comp->lockPreemtion = NULL;
comp->unlockPreemtion = NULL;
comp->lockPreemption = NULL;
comp->unlockPreemption = NULL;
comp->instanceName = strdup(instanceName);
comp->resourceLocation = resourceLocation ? strdup(resourceLocation) : NULL;
comp->status = OK;
Expand Down Expand Up @@ -562,8 +562,8 @@ Status setFMUState(ModelInstance* comp, void* FMUState) {
// intermediateUpdate
// clockUpdate

// lockPreemtion
// unlockPreemtion
// lockPreemption
// unlockPreemption

// logEvents
// logErrors
Expand Down
4 changes: 2 additions & 2 deletions src/fmi3Functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ fmi3Instance fmi3InstantiateScheduledExecution(
if (instance) {
instance->state = Instantiated;
instance->clockUpdate = clockUpdate;
instance->lockPreemtion = lockPreemption;
instance->unlockPreemtion = unlockPreemption;
instance->lockPreemption = lockPreemption;
instance->unlockPreemption = unlockPreemption;
}

return instance;
Expand Down
Loading