+static void srd_inst_join_decode_thread(struct srd_decoder_inst *di)
+{
+ if (!di)
+ return;
+ if (!di->thread_handle)
+ return;
+
+ srd_dbg("%s: Joining decoder thread.", di->inst_id);
+
+ /*
+ * Terminate potentially running threads which still
+ * execute the decoder instance's decode() method.
+ */
+ srd_dbg("%s: Raising want_term, sending got_new.", di->inst_id);
+ g_mutex_lock(&di->data_mutex);
+ di->want_wait_terminate = TRUE;
+ g_cond_signal(&di->got_new_samples_cond);
+ g_mutex_unlock(&di->data_mutex);
+
+ srd_dbg("%s: Running join().", di->inst_id);
+ (void)g_thread_join(di->thread_handle);
+ srd_dbg("%s: Call to join() done.", di->inst_id);
+ di->thread_handle = NULL;
+
+ /*
+ * Reset condition and mutex variables, such that next
+ * operations on them will find them in a clean state.
+ */
+ g_cond_clear(&di->got_new_samples_cond);
+ g_cond_init(&di->got_new_samples_cond);
+ g_cond_clear(&di->handled_all_samples_cond);
+ g_cond_init(&di->handled_all_samples_cond);
+ g_mutex_clear(&di->data_mutex);
+ g_mutex_init(&di->data_mutex);
+}
+
+static void srd_inst_reset_state(struct srd_decoder_inst *di)
+{
+ if (!di)
+ return;
+
+ srd_dbg("%s: Resetting decoder state.", di->inst_id);
+
+ /*
+ * Reset internal state of the decoder.
+ */
+ condition_list_free(di);
+ match_array_free(di);
+ di->abs_start_samplenum = 0;
+ di->abs_end_samplenum = 0;
+ di->inbuf = NULL;
+ di->inbuflen = 0;
+ di->abs_cur_samplenum = 0;
+ oldpins_array_free(di);
+ di->got_new_samples = FALSE;
+ di->handled_all_samples = FALSE;
+ di->want_wait_terminate = FALSE;
+ /* Conditions and mutex got reset after joining the thread. */
+}
+