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

Fix for 14005. GIL wasn't managed correctly in python spawned threads #2115

Merged
merged 1 commit into from
Jan 25, 2013
Merged
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
61 changes: 38 additions & 23 deletions xbmc/interfaces/python/PyContext.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ namespace XBMCAddon
{ {
struct PyContextState struct PyContextState
{ {
inline PyContextState() : value(0), state(NULL), gilReleasedDepth(0) {} inline PyContextState(bool pcreatedByGilRelease = false) :
value(0), state(NULL), gilReleasedDepth(0), createdByGilRelease(pcreatedByGilRelease) {}


int value; int value;
PyThreadState* state; PyThreadState* state;
int gilReleasedDepth; int gilReleasedDepth;
bool createdByGilRelease;
}; };


static XbmcThreads::ThreadLocal<PyContextState> tlsPyContextState; static XbmcThreads::ThreadLocal<PyContextState> tlsPyContextState;


PyContext::PyContext() void* PyContext::enterContext()
{ {
PyContextState* cur = tlsPyContextState.get(); PyContextState* cur = tlsPyContextState.get();
if (cur == NULL) if (cur == NULL)
Expand All @@ -51,9 +53,11 @@ namespace XBMCAddon


// increment the count // increment the count
cur->value++; cur->value++;

return cur;
} }


PyContext::~PyContext() void PyContext::leaveContext()
{ {
// here we ASSUME that the constructor was called. // here we ASSUME that the constructor was called.
PyContextState* cur = tlsPyContextState.get(); PyContextState* cur = tlsPyContextState.get();
Expand All @@ -78,37 +82,48 @@ namespace XBMCAddon
void PyGILLock::releaseGil() void PyGILLock::releaseGil()
{ {
PyContextState* cur = tlsPyContextState.get(); PyContextState* cur = tlsPyContextState.get();
if (cur) // this means we're within the python context
// This means we're not within the python context, but
// because we may be in a thread spawned by python itself,
// we need to handle this.
if (!cur)
{
cur = (PyContextState*)PyContext::enterContext();
cur->createdByGilRelease = true;
}

if (cur->gilReleasedDepth == 0) // true if we are at the outermost
{ {
if (cur->gilReleasedDepth == 0) // true if we are at the outermost PyThreadState* _save;
// this macro sets _save
{ {
PyThreadState* _save; Py_UNBLOCK_THREADS
// this macro sets _save
{
Py_UNBLOCK_THREADS
}
cur->state = _save;
} }
cur->gilReleasedDepth++; // the first time this goes to 1 cur->state = _save;
} }
cur->gilReleasedDepth++; // the first time this goes to 1
} }


void PyGILLock::acquireGil() void PyGILLock::acquireGil()
{ {
PyContextState* cur = tlsPyContextState.get(); // this must be non-null given we released the Gil in the constructor PyContextState* cur = tlsPyContextState.get();
if (cur) // we're within a PyContext
// it's not possible for cur to be NULL (and if it is, we want to fail anyway).

// decrement the depth and make sure we're in the right place.
cur->gilReleasedDepth--;
if (cur->gilReleasedDepth == 0) // are we back to zero?
{ {
// decrement the depth and make sure we're in the right place. PyThreadState* _save = cur->state;
cur->gilReleasedDepth--; // This macros uses _save
if (cur->gilReleasedDepth == 0) // are we back to zero?
{ {
PyThreadState* _save = cur->state; Py_BLOCK_THREADS
// This macros uses _save
{
Py_BLOCK_THREADS
}
cur->state = NULL; // clear the state to indicate we've reacquired the gil
} }
cur->state = NULL; // clear the state to indicate we've reacquired the gil

// we clear it only if we created it on this level.
if (cur->createdByGilRelease)
PyContext::leaveContext();
} }
} }
} }
Expand Down
10 changes: 8 additions & 2 deletions xbmc/interfaces/python/PyContext.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace XBMCAddon
{ {
namespace Python namespace Python
{ {
class PyGILLock;

/** /**
* These classes should NOT be used with 'new'. They are expected to reside * These classes should NOT be used with 'new'. They are expected to reside
* as stack instances and they act as "Guard" classes that track the * as stack instances and they act as "Guard" classes that track the
Expand All @@ -30,9 +32,13 @@ namespace XBMCAddon
class PyContext class PyContext
{ {
protected: protected:
friend class PyGILLock;
static void* enterContext();
static void leaveContext();
public: public:
PyContext();
~PyContext(); inline PyContext() { enterContext(); }
inline ~PyContext() { leaveContext(); }
}; };


/** /**
Expand Down