void my_function(...) {
// ...
// the lock runs until the end of scope
sofa::simulation::PythonEnvironment::gil lock;
PySomething_Something(...);
}
Or even better yet: don't use naked python calls and use the PythonEnvironment API instead. You may also temporarily release the GIL while doing a costly/blocking c++ operation as follows:
void my_function_that_already_owns_the_gil(...) {
// ...
// unlocks the GIL until the end of scope
sofa::simulation::PythonEnvironment::no_gil unlock;
// ...
}
I tried looking for all python calls in the codebase, but I may have missed some.
Changelog
added PythonEnvironment::gil RAII for scoped acquisition of the GIL
added PythonEnvironment::no_gil RAII for scoped release of the GIL
GIL-protected most (all?) python calls with the above (except in extension code)
This PR:
[ ] builds with SUCCESS for all platforms on the CI.
[ ] does not generate new warnings.
[ ] does not generate new unit test failures.
[ ] does not generate new scene test failures.
[ ] does not break API compatibility.
[ ] is more than 1 week old (or has fast-merge label).
Reviewers will merge only if all these checks are true.
(this is a mirror for #326, which @ocarre need for multithreading)
Please don't merge it too soon, I'd like it to stage here for a while to make sure we don't break anything.
EDIT: apparently the above was not quite visible enough, lemme restate that:
Please don't merge it too soon, I'd like it to stage here for a while to make sure we don't break anything.
TL;DR: There should be no unprotected python code in sofa except in extension code.
Any code like this:
Should now be GIL-protected as follows:
Or even better yet: don't use naked python calls and use the
PythonEnvironment
API instead. You may also temporarily release the GIL while doing a costly/blocking c++ operation as follows:I tried looking for all python calls in the codebase, but I may have missed some.
Changelog
PythonEnvironment::gil
RAII for scoped acquisition of the GILPythonEnvironment::no_gil
RAII for scoped release of the GILThis PR:
Reviewers will merge only if all these checks are true.