summaryrefslogtreecommitdiff
path: root/python/gras/GRAS_Utils.i
blob: 1b23aca74b30749f64effbde733d407244c4d799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

////////////////////////////////////////////////////////////////////////
// Simple class to deal with smart locking/unlocking of python GIL
////////////////////////////////////////////////////////////////////////
%{

struct PyGILPhondler
{
    PyGILPhondler(void):
        s(PyGILState_Ensure())
    {
        //NOP
    }
    ~PyGILPhondler(void)
    {
        PyGILState_Release(s);
    }
    PyGILState_STATE s;
};

%}

////////////////////////////////////////////////////////////////////////
// Simple class to deal with smart save/restore of python thread state
////////////////////////////////////////////////////////////////////////
%{

struct PyTSPhondler
{
    PyTSPhondler(void):
        s(PyEval_SaveThread())
    {
        //NOP
    }
    ~PyTSPhondler(void)
    {
        PyEval_RestoreThread(s);
    }
    PyThreadState *s;
};

%}

////////////////////////////////////////////////////////////////////////
// Create a reference holder for python objects
////////////////////////////////////////////////////////////////////////
%inline %{

struct PyObjectRefHolder
{
    PyObjectRefHolder(PyObject *o):
        o(o)
    {
        Py_INCREF(o);
    }
    ~PyObjectRefHolder(void)
    {
        PyGILPhondler phil;
        Py_DECREF(o);
    }
    PyObject *o;
};

%}