+ srd_dbg("%s: Starting thread routine for decoder.", di->inst_id);
+
+ gstate = PyGILState_Ensure();
+
+ /*
+ * Call self.decode(). Only returns if the PD throws an exception.
+ * "Regular" termination of the decode() method is not expected.
+ */
+ Py_INCREF(di->py_inst);
+ srd_dbg("%s: Calling decode().", di->inst_id);
+ py_res = PyObject_CallMethod(di->py_inst, "decode", NULL);
+ srd_dbg("%s: decode() terminated.", di->inst_id);
+
+ /*
+ * Termination with an EOFError exception is accepted to simplify
+ * the implementation of decoders and for backwards compatibility.
+ */
+ if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_EOFError)) {
+ srd_dbg("%s: ignoring EOFError during decode() execution.",
+ di->inst_id);
+ PyErr_Clear();
+ if (!py_res)
+ py_res = Py_None;
+ }
+ if (!py_res)
+ di->decoder_state = SRD_ERR;
+
+ /*
+ * Make sure to unblock potentially pending srd_inst_decode()
+ * calls in application threads after the decode() method might
+ * have terminated, while it neither has processed sample data
+ * nor has terminated upon request. This happens e.g. when "need
+ * a samplerate to decode" exception is thrown.
+ */
+ g_mutex_lock(&di->data_mutex);
+ wanted_term = di->want_wait_terminate;
+ di->want_wait_terminate = TRUE;
+ di->handled_all_samples = TRUE;
+ g_cond_signal(&di->handled_all_samples_cond);
+ g_mutex_unlock(&di->data_mutex);
+
+ /*
+ * Check for the termination cause of the decode() method.
+ * Though this is mostly for information.
+ */
+ if (!py_res && wanted_term) {
+ /*
+ * Silently ignore errors upon return from decode() calls
+ * when termination was requested. Terminate the thread
+ * which executed this instance's decode() logic.
+ */
+ srd_dbg("%s: Thread done (!res, want_term).", di->inst_id);
+ PyErr_Clear();
+ PyGILState_Release(gstate);
+ return NULL;
+ }
+ if (!py_res) {
+ /*
+ * The decode() invocation terminated unexpectedly. Have
+ * the back trace printed, and terminate the thread which
+ * executed the decode() method.
+ */
+ srd_dbg("%s: decode() terminated unrequested.", di->inst_id);