Skip to content
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
121 changes: 31 additions & 90 deletions wrapper/Base/releasegil_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "SireBase/console.h"
#include "SireBase/releasegil.h"

#include "Helpers/scoped_gil_acquire.hpp"

#include <QDebug>

static bool _is_ipython = false;
Expand Down Expand Up @@ -282,140 +284,80 @@ class ConsoleImpl : public SireBase::ConsoleBase
{
if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();
SireHelpers::ScopedGILAcquire gil;
Py_DECREF(pyconsole);
delete pyconsole;
pyconsole = 0;
PyGILState_Release(gilstate);
}
}

void debug(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "debug", "s", message.toUtf8().constData());

if (result == 0)
{
qDebug() << "UNABLE TO CALL DEBUG";
qDebug() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("debug", message))
{
qDebug() << "NO PYCONSOLE";
qDebug() << message;
}
}

void warning(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "warning", "s", message.toUtf8().constData());

if (result == 0)
{
qWarning() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("warning", message))
{
qWarning() << message;
}
}

void error(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
{
PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *result = PyObject_CallMethod(pyconsole, "error", "s", message.toUtf8().constData());

if (result == 0)
{
qCritical() << message;
}
else
{
Py_DECREF(result);
}

PyGILState_Release(gilstate);
}
else
if (not this->call("error", message))
{
qCritical() << message;
}
}

void info(const QString &message) const
{
const_cast<ConsoleImpl *>(this)->get_pyconsole();

if (pyconsole)
if (not this->call("info", message))
{
PyGILState_STATE gilstate = PyGILState_Ensure();
qInfo() << message;
}
}

PyObject *result = PyObject_CallMethod(pyconsole, "info", "s", message.toUtf8().constData());
private:
/** Call the named method of the Python console with the message,
* returning whether it succeeded. */
bool call(const char *method, const QString &message) const
{
SireHelpers::ScopedGILAcquire gil;

if (result == 0)
{
qInfo() << "UNABLE TO CALL INFO";
qInfo() << message;
}
else
{
Py_DECREF(result);
}
const_cast<ConsoleImpl *>(this)->get_pyconsole();

PyGILState_Release(gilstate);
}
else
if (not pyconsole)
return false;

PyObject *result = PyObject_CallMethod(pyconsole, method, "s", message.toUtf8().constData());

if (result == 0)
{
qInfo() << "NO PYCONSOLE";
qInfo() << message;
PyErr_Clear();
return false;
}

Py_DECREF(result);
return true;
}

private:
/** Import the Python console. This must be called with the GIL held. */
void get_pyconsole()
{
if (pyconsole)
return;

PyGILState_STATE gilstate = PyGILState_Ensure();

PyObject *sire_utils = PyImport_ImportModule("sire.utils");

if (sire_utils == 0)
{
PyErr_Clear();
qWarning() << "COULD NOT IMPORT SIRE.UTILS";
PyGILState_Release(gilstate);
return;
}

Expand All @@ -425,9 +367,8 @@ class ConsoleImpl : public SireBase::ConsoleBase

if (pyconsole == 0)
{
PyErr_Clear();
qWarning() << "Could not import Console from sire.utils";
PyGILState_Release(gilstate);
return;
}
}

Expand Down
16 changes: 4 additions & 12 deletions wrapper/Convert/SireOpenMM/pyqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#include "openmm/serialization/SerializationNode.h"
#include "openmm/serialization/SerializationProxy.h"

#include "Helpers/scoped_gil_acquire.hpp"
#include "SireError/errors.h"

#include "SireMaths/vector.h"
#include "SireStream/datastream.h"
#include "SireStream/shareddatastream.h"
Expand All @@ -52,16 +54,6 @@ using namespace SireVol;
// atoms, in Angstrom.
static const double VIRTUAL_PC_DELTA = 0.01;

class GILLock
{
public:
GILLock() { state_ = PyGILState_Ensure(); }
~GILLock() { PyGILState_Release(state_); }

private:
PyGILState_STATE state_;
};

/////////
///////// Implementation of PyQMCallback
/////////
Expand Down Expand Up @@ -160,7 +152,7 @@ PyQMCallback::call(
{

// Acquire GIL before calling Python code.
GILLock lock;
SireHelpers::ScopedGILAcquire gil;

if (this->is_method)
{
Expand Down Expand Up @@ -217,7 +209,7 @@ PyQMCallback::call4(
QVector<int> idx_mm) const
{
// Acquire GIL before calling Python code.
GILLock lock;
SireHelpers::ScopedGILAcquire gil;

bp::object result;
try
Expand Down
1 change: 1 addition & 0 deletions wrapper/Helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set ( WRAPHELPERS_SOURCES

release_gil_policy.hpp
release_gil_policy.cpp
scoped_gil_acquire.hpp
scoped_gil_release.hpp

)
Expand Down
34 changes: 34 additions & 0 deletions wrapper/Helpers/scoped_gil_acquire.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _HELPERS_SCOPED_GIL_ACQUIRE_HPP_
#define _HELPERS_SCOPED_GIL_ACQUIRE_HPP_

#include "boost/python.hpp"

namespace SireHelpers
{
/** Acquire the GIL for the lifetime of this object.
*
* Use this before calling into Python from C++ that may run without the
* GIL, e.g. within a ScopedGILRelease or on a worker thread. The GIL is
* released on every return path, including during stack unwinding.
*/
class ScopedGILAcquire
{
public:
ScopedGILAcquire() : state(PyGILState_Ensure())
{
}

~ScopedGILAcquire()
{
PyGILState_Release(state);
}

private:
ScopedGILAcquire(const ScopedGILAcquire &);
ScopedGILAcquire &operator=(const ScopedGILAcquire &);

PyGILState_STATE state;
};
}

#endif