Anatoscope / sofa

Real-time multi-physics simulation with an emphasis on medical simulation.
https://www.sofa-framework.org
2 stars 1 forks source link

GIL management #16

Closed maxime-tournier closed 7 years ago

maxime-tournier commented 7 years ago

(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:

void my_function(...) {
   // ...
   PySomething_Something(...);
}

Should now be GIL-protected as follows:

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


This PR:

Reviewers will merge only if all these checks are true.