Skip to content

Commit

Permalink
Merge pull request #437 from ToyotaResearchInstitute/apojomovsky/turn…
Browse files Browse the repository at this point in the history
…_simulation_runner_into_shared_library

Fix the simulation_runner to enable switch form STATIC to SHARED
  • Loading branch information
Alexis Pojomovsky authored Jun 6, 2018
2 parents 896c82f + f0e246c commit bb72ca8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,14 @@ install(TARGETS automotive_simulator
# ----------------------------------------
# Simulator-runner library
#
# Right now this can't be made shared - segfaults. For more
# information, refer to:
#
# https://github.com/ToyotaResearchInstitute/delphyne/issues/341
#
include(${PROJECT_SOURCE_DIR}/cmake/Python.cmake)

add_library(simulation_runner
STATIC
interactive_simulation_stats.cc
simulation_run_stats.cc
simulation_runner.cc
system.h
)
# Position independent code can disappear once we are able to drop STATIC
set_property(TARGET simulation_runner PROPERTY POSITION_INDEPENDENT_CODE ON)
set_target_properties(simulation_runner PROPERTIES OUTPUT_NAME ${PROJECT_NAME}-simulation-runner)
target_include_directories(
simulation_runner
Expand Down
16 changes: 12 additions & 4 deletions src/backend/simulation_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,18 @@ SimulatorRunner::SimulatorRunner(
ignerr << "Error advertising service [" << kSceneRequestServiceName << "]"
<< std::endl;
}
// Initializes the python machinery so we can invoke a python callback
// function on each simulation step.
Py_Initialize();
PyEval_InitThreads();

// The get_internals() function initializes the `internals.tstate` for
// subsequent `gil_scoped_acquire` calls. If the gil_scoped_acquire is
// attempted to be called before having initialized the internals.tstate,
// it would end up into a segmentation fault. This initialization should
// only occur if the SimulatorRunner class is instantiated from within a
// python script, which is checked in the if statement.
// In comment below, a similar approach to the one used here is suggested:
// https://github.com/pybind/pybind11/issues/1360#issuecomment-385988887
if (Py_IsInitialized()) {
pybind11::detail::get_internals();
}

// Tell the simulator to run steps as fast as possible, as are handling the
// sleep (if required) between steps.
Expand Down

0 comments on commit bb72ca8

Please sign in to comment.