static int element_callback(snd_hctl_elem_t *elem, unsigned int mask)
{
- PyThreadState *tstate, *origstate;
struct pyalsahcontrolelement *pyhelem;
PyObject *o, *t, *r;
int res = 0, inside = 1;
+ CALLBACK_VARIABLES;
if (elem == NULL)
return -EINVAL;
if (pyhelem == NULL || pyhelem->callback == NULL)
return -EINVAL;
- tstate = PyThreadState_New(main_interpreter);
- origstate = PyThreadState_Swap(tstate);
+ CALLBACK_INIT;
o = PyObject_GetAttr(pyhelem->callback, InternFromString("callback"));
if (!o) {
Py_DECREF(o);
}
- PyThreadState_Swap(origstate);
- PyThreadState_Delete(tstate);
+ CALLBACK_DONE;
return res;
}
static int element_callback(snd_mixer_elem_t *elem, unsigned int mask)
{
- PyThreadState *tstate, *origstate;
struct pyalsamixerelement *pyelem;
PyObject *o, *t, *r;
int res = 0, inside = 1;
+ CALLBACK_VARIABLES;
if (elem == NULL)
return -EINVAL;
if (pyelem == NULL || pyelem->callback == NULL)
return -EINVAL;
- tstate = PyThreadState_New(main_interpreter);
- origstate = PyThreadState_Swap(tstate);
+ CALLBACK_INIT;
o = PyObject_GetAttr(pyelem->callback, InternFromString("callback"));
if (!o) {
Py_DECREF(o);
}
- PyThreadState_Swap(origstate);
- PyThreadState_Delete(tstate);
+ CALLBACK_DONE;
return res;
}
ob = Py_InitModule3(name, methods, doc);
#endif
+/*
+ *
+ */
+#if PY_MAJOR_VERSION >= 3
+ #define CALLBACK_VARIABLES \
+ PyGILState_STATE __gstate
+ #define CALLBACK_INIT \
+ __gstate = PyGILState_Ensure()
+ #define CALLBACK_DONE \
+ PyGILState_Release(__gstate)
+#else
+ #define CALLBACK_VARIABLES \
+ PyThreadState *__tstate, *__origstate
+ #define CALLBACK_INIT \
+ do { \
+ __tstate = PyThreadState_New(main_interpreter); \
+ __origstate = PyThreadState_Swap(__tstate); \
+ } while (0)
+ #define CALLBACK_DONE \
+ do { \
+ PyThreadState_Swap(origstate); \
+ PyThreadState_Delete(tstate); \
+ } while (0)
+#endif
+
/*
*
*/