// Utility class to release and acquire the GIL when variable is initialised and
// destructed, respectively. To call functions that acquire the global pthread
// mutex, just make an instance of MakeThreadsafe to release the GIL, and it 
// will be re-acquired by the interpreter when the function returns.

#include <ceval.h>

class MakeThreadsafe
{
public:
    inline MakeThreadsafe()
    {
        // From [1]
        //gil_state = PyEval_SaveThread();

        // From [2]
        gil_state = PyGILState_Ensure();

    }

    inline ~MakeThreadsafe()
    {
        // From 1
        //PyEval_RestoreThread(gil_state);
        //gil_state = NULL;

        // From [2] 
        PyGILState_Release(gil_state);
    }

private:
    // from [1]
//    PyThreadState *gil_state;

    // from [2]
    PyGILState_STATE gil_state;
};


/// [1] - http://wiki.python.org/moin/boost.python/HowTo#Multithreading_Support_for_my_function
/// [2] - http://docs.python.org/2.7/c-api/init.html