Skip to main content
Sign in
Snippets Groups Projects
Commit a36efe68 authored by Tim Übelhör's avatar Tim Übelhör
Browse files

Switched back to std thread since newer wine versions should support it

parent ba156b3b
No related branches found
No related tags found
No related merge requests found
...@@ -22,19 +22,13 @@ namespace Simulation ...@@ -22,19 +22,13 @@ namespace Simulation
_controlerId(id), _sendValuesCallback(valuesCallback), _logCallback(_logCallback) _controlerId(id), _sendValuesCallback(valuesCallback), _logCallback(_logCallback)
{} {}
DWORD WINAPI FmuEngine::staticSimThreadStart(void* instance) void FmuEngine::simulate()
{
FmuEngine* engine = (FmuEngine*)instance;
return engine->simulate();
}
DWORD FmuEngine::simulate(void)
{ {
// Time measuring // Time measuring
LARGE_INTEGER frequency, startCount, currentCount; LARGE_INTEGER frequency, startCount, currentCount;
if (!QueryPerformanceFrequency(&frequency)) if (!QueryPerformanceFrequency(&frequency))
{ {
_logCallback("FmuEngine", "Precision timer nor available."); _logCallback("FmuEngine", "Precision timer nor available.");
return 1;
} }
std::string freqString = "PerformanceFrequency: " + std::to_string(frequency.QuadPart) + "1/sec"; std::string freqString = "PerformanceFrequency: " + std::to_string(frequency.QuadPart) + "1/sec";
std::cout << freqString << std::endl; std::cout << freqString << std::endl;
...@@ -85,7 +79,6 @@ namespace Simulation ...@@ -85,7 +79,6 @@ namespace Simulation
} }
// Lower the accurracy when leaving // Lower the accurracy when leaving
timeEndPeriod(1); timeEndPeriod(1);
return 0;
} }
void FmuEngine::performChannelLinks() void FmuEngine::performChannelLinks()
{ {
...@@ -189,26 +182,27 @@ namespace Simulation ...@@ -189,26 +182,27 @@ namespace Simulation
} }
} }
FResult FmuEngine::startSimulationThread() FResult FmuEngine::startSimulation()
{ {
// Play from initialized or stopped state // Play from initialized or stopped state
if (!_simulating) if (!_simulating)
{ {
// Make the fmus ready
for (auto &pair : _fmus) for (auto &pair : _fmus)
{ {
Fmu &fmu = pair.second; Fmu &fmu = pair.second;
// Exit the mode // Exit the initalizationmode
// TODO check current state
fmi2Status status = fmu.ExitInitializationMode(); fmi2Status status = fmu.ExitInitializationMode();
if (!(status == fmi2OK || status == fmi2Warning)) if (!(status == fmi2OK || status == fmi2Warning))
{ {
return{ status, "Initialization failed, instance: " + fmu.GetInstanceName() }; _logCallback(fmu.GetInstanceName(), "ExitInitializationMode failed");
return{ status, "ExitInitializationMode failed, instance: " + fmu.GetInstanceName() };
} }
} }
// Start simulating // Start simulating
_simulating = true; _simulating = true;
// Create new thread for the simulation // Create new thread for the simulation
_simThreadHandle = CreateThread(NULL, 0, staticSimThreadStart, (void*) this, 0, &_simThreadID); _simThread = std::thread(&FmuEngine::simulate, this);
} }
return{ fmi2OK, "" }; return{ fmi2OK, "" };
} }
...@@ -218,14 +212,14 @@ namespace Simulation ...@@ -218,14 +212,14 @@ namespace Simulation
// Set endTime as large as possible (~5,67e300 annos) // Set endTime as large as possible (~5,67e300 annos)
_endTime_sec = DBL_MAX; _endTime_sec = DBL_MAX;
_playFast = false; _playFast = false;
return startSimulationThread(); return startSimulation();
} }
FResult FmuEngine::PlayFast(double endTime_sec) FResult FmuEngine::PlayFast(double endTime_sec)
{ {
// Update the _endTime // Update the _endTime
_endTime_sec = endTime_sec; _endTime_sec = endTime_sec;
_playFast = true; _playFast = true;
return startSimulationThread(); return startSimulation();
} }
// Stop the simulation thread // Stop the simulation thread
FResult FmuEngine::Pause() FResult FmuEngine::Pause()
...@@ -233,7 +227,7 @@ namespace Simulation ...@@ -233,7 +227,7 @@ namespace Simulation
// Only try to obtain ownership over the thread without blocking the current thread // Only try to obtain ownership over the thread without blocking the current thread
_simulating = false; _simulating = false;
// Wait until the current run has finished // Wait until the current run has finished
WaitForSingleObject(_simThreadHandle, INFINITE); _simThread.join();
return{ fmi2OK,"" }; return{ fmi2OK,"" };
} }
// Stop the thread and reset the simulation // Stop the thread and reset the simulation
...@@ -242,7 +236,7 @@ namespace Simulation ...@@ -242,7 +236,7 @@ namespace Simulation
// Stop simulation // Stop simulation
_simulating = false; _simulating = false;
// Wait until the current run has finished // Wait until the current run has finished
WaitForSingleObject(_simThreadHandle, INFINITE); _simThread.join();
// Reset simulation // Reset simulation
_currentTime_sec = 0; _currentTime_sec = 0;
... ...
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
#include "ChannelLink.h" #include "ChannelLink.h"
#include "templates.hpp" #include "templates.hpp"
#include "fmi2FunctionTypes.h" #include "fmi2FunctionTypes.h"
#include <memory> #include <atomic>
#include <map> #include <map>
#include <memory>
#include <thread>
#include <vector> #include <vector>
#define WIN32_LEAN_AND_MEAN // We don't want to include winsock.h, causes problems with asio
#include <windows.h>
#include "boost/optional.hpp" #include "boost/optional.hpp"
// Forward declaration // Forward declaration
...@@ -73,20 +73,17 @@ namespace Simulation ...@@ -73,20 +73,17 @@ namespace Simulation
LogCallback _logCallback; LogCallback _logCallback;
// Execeutes the simulation: DoStep, pushes the values into storage, performes ChannelLinks // Execeutes the simulation: DoStep, pushes the values into storage, performes ChannelLinks
DWORD _simThreadID; std::thread _simThread;
HANDLE _simThreadHandle; std::atomic_bool _simulating; // Use atomic -> while(!_stopSim) will not be compiled to while(true)
bool _simulating; // Use atomic -> while(!_stopSim) will not be compiled to while(true)
bool _playFast; // Do not sleep when we use the playFast mode bool _playFast; // Do not sleep when we use the playFast mode
double _currentTime_sec = 0; // The current simulation timepoint double _currentTime_sec = 0; // The current simulation timepoint
double _endTime_sec = 0; double _endTime_sec = 0;
const double STEP_SIZE_SEC = 0.01; // Default 10ms const double STEP_SIZE_SEC = 0.01; // Default 10ms
/// Start the simulation Task (if not already running) /// Prepares and runs the simulation loop in a new thread
FResult startSimulationThread(); FResult startSimulation();
/// Static method needed to execute the WINAPI thread /// Simulation loop runs in here
static DWORD WINAPI staticSimThreadStart(void* instance); void simulate();
///
DWORD simulate(void);
void performChannelLinks(); void performChannelLinks();
void sendValues(); void sendValues();
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment