Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: GIL release via %threadallow #3238

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Components/Bites/include/OgreBites.i
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%module(package="Ogre", directors="1") Bites
%module(package="Ogre", directors="1", threads="1") Bites
%{
/* Includes the header in the wrapper code */
#include "Ogre.h"
Expand All @@ -15,6 +15,7 @@
#include "OgreImGuiInputListener.h"
%}

%feature("nothreadallow");
%include std_vector.i
%include std_string.i
%include exception.i
Expand Down Expand Up @@ -73,4 +74,4 @@ JNIEnv* OgreJNIGetEnv();
#ifndef SWIGCSHARP
%include "OgreTrays.h"
%include "OgreAdvancedRenderControls.h"
#endif
#endif
3 changes: 0 additions & 3 deletions Docs/src/tutorials/numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,3 @@ def printer():
threading.Thread(target=printer).start()
root.startRendering()
```

The "printer" Thread will be blocked until the rendering is finished.
To allow background threads to run, you can use `root.allowPyThread()`, which will release the GIL during swapping, while rendering is waiting for vsync.
36 changes: 8 additions & 28 deletions OgreMain/include/Ogre.i
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%module(package="Ogre", directors="1") Ogre
%module(package="Ogre", directors="1", threads="1") Ogre
%{
/* Includes the header in the wrapper code */
#include "Ogre.h"
Expand All @@ -19,6 +19,7 @@
#include "OgreDefaultDebugDrawer.h"
%}

%feature("nothreadallow");
%include stdint.i
%include std_shared_ptr.i
%include std_string.i
Expand Down Expand Up @@ -515,6 +516,7 @@ SHARED_PTR(FileHandleDataStream);
%include "OgreCodec.h"
%include "OgreSerializer.h"
%include "OgreScriptLoader.h"

// Listeners
%feature("director") Ogre::FrameListener;
%include "OgreFrameListener.h"
Expand All @@ -528,6 +530,7 @@ SHARED_PTR(FileHandleDataStream);
%include "OgreRenderTargetListener.h"
%feature("director") Ogre::MeshSerializerListener;
%feature("director") Ogre::ResourceLoadingListener;

// More Data Types
%ignore Ogre::ColourValue::getHSB; // deprecated
%include "OgreColourValue.h"
Expand Down Expand Up @@ -925,33 +928,10 @@ SHARED_PTR(Mesh);
%ignore Ogre::Root::createSceneManager(uint16, const String&);
%ignore Ogre::Root::getMovableObjectFactoryIterator;
#ifdef SWIGPYTHON
%{
class ThreadAllowFrameListener : public Ogre::FrameListener {
PyThreadState* _save = 0;
public:
bool frameRenderingQueued(const Ogre::FrameEvent& evt)
{
if(!_save)
_save = PyEval_SaveThread();
return true;
}
bool frameEnded(const Ogre::FrameEvent& evt)
{
if(_save) {
PyEval_RestoreThread(_save);
_save = 0;
}
return true;
}
};
%}
%extend Ogre::Root {
void allowPyThread()
{
static ThreadAllowFrameListener listener;
$self->addFrameListener(&listener);
}
}
/* Unlocks SIG when called from python */
%threadallow Ogre::Root::startRendering;
%threadallow Ogre::Root::renderOneFrame;
%threadallow Ogre::Root::_updateAllRenderTargets;
#endif
%include "OgreRoot.h"
// dont wrap: not useful in high level languages
Expand Down
Loading