Skip to content

Commit

Permalink
#43 Added mutex for locking SimulationStatus due to race condition wh…
Browse files Browse the repository at this point in the history
…ere TrendLoop and CommandLoop write to SimulationStatus simultaneously
  • Loading branch information
eidekrist committed Dec 21, 2018
1 parent ad9139a commit 66d57e6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cse/cse.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func findVariableIndex(fmu structs.FMU, signalName string, causality string, val

func TrendLoop(sim *Simulation, status *structs.SimulationStatus) {
for {
status.Mutex.Lock()
if len(status.TrendSignals) > 0 {
for i, _ := range status.TrendSignals {
var trend = &status.TrendSignals[i]
Expand All @@ -267,6 +268,7 @@ func TrendLoop(sim *Simulation, status *structs.SimulationStatus) {
}
}
}
status.Mutex.Unlock()
time.Sleep(500 * time.Millisecond)
}
}
Expand Down Expand Up @@ -327,6 +329,7 @@ func CommandLoop(sim *Simulation, command chan []string, status *structs.Simulat
for {
select {
case cmd := <-command:
status.Mutex.Lock()
switch cmd[0] {
case "load":
initializeSimulation(sim, cmd[1], cmd[2])
Expand Down Expand Up @@ -367,8 +370,9 @@ func CommandLoop(sim *Simulation, command chan []string, status *structs.Simulat
case "set-value":
setVariableValue(sim, cmd[1], cmd[2], cmd[3], cmd[4], cmd[5])
default:
fmt.Println("Empty command, mildt sagt not good: ", cmd)
fmt.Println("Unknown command, this is not good: ", cmd)
}
status.Mutex.Unlock()
}
}
}
Expand Down Expand Up @@ -418,6 +422,8 @@ func GetSignalValue(module string, cardinality string, signal string) int {
}

func SimulationStatus(simulationStatus *structs.SimulationStatus, sim *Simulation) structs.JsonResponse {
simulationStatus.Mutex.Lock()
defer simulationStatus.Mutex.Unlock()
virtualMemoryStats, _ := mem.VirtualMemory()
if simulationStatus.Loaded {
execStatus := getExecutionStatus(sim.Execution)
Expand Down
2 changes: 2 additions & 0 deletions structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package structs

import (
"github.com/shirou/gopsutil/mem"
"sync"
)

type Signal struct {
Expand Down Expand Up @@ -46,6 +47,7 @@ type TrendSpec struct {
}

type SimulationStatus struct {
Mutex sync.Mutex
Loaded bool
ConfigDir string
Module Module
Expand Down

0 comments on commit 66d57e6

Please sign in to comment.