]> sigrok.org Git - libsigrokdecode.git/blobdiff - type_decoder.c
decoder: Accept more forms of "unconditional wait()" (None, no args)
[libsigrokdecode.git] / type_decoder.c
index 7836640c45d4702765596b3f9fe88331d5248dbb..231a8939b461ce6c1c09a26761f6b3e41e3aea28 100644 (file)
@@ -427,8 +427,6 @@ static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
                }
        }
 
-       Py_IncRef(py_pinvalues);
-
        return py_pinvalues;
 }
 
@@ -522,14 +520,31 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
                return SRD_ERR;
        }
 
-       /* Parse the argument of self.wait() into 'py_conds'. */
-       if (!PyArg_ParseTuple(args, "O", &py_conds)) {
-               /* Let Python raise this exception. */
+       /*
+        * Return an error condition from .wait() when termination is
+        * requested, such that decode() will terminate.
+        */
+       if (di->want_wait_terminate) {
+               srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
                return SRD_ERR;
        }
 
-       /* Check whether 'py_conds' is a dict or a list. */
-       if (PyList_Check(py_conds)) {
+       /*
+        * Parse the argument of self.wait() into 'py_conds', and check
+        * the data type. The argument is optional, None is assumed in
+        * its absence. None or an empty dict or an empty list mean that
+        * there is no condition, and the next available sample shall
+        * get returned to the caller.
+        */
+       py_conds = Py_None;
+       if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
+               /* Let Python raise this exception. */
+               return SRD_ERR;
+       }
+       if (py_conds == Py_None) {
+               /* 'py_conds' is None. */
+               return 9999;
+       } else if (PyList_Check(py_conds)) {
                /* 'py_conds' is a list. */
                py_conditionlist = py_conds;
                num_conditions = PyList_Size(py_conditionlist);
@@ -578,9 +593,45 @@ static int set_new_condition_list(PyObject *self, PyObject *args)
        return ret;
 }
 
+/**
+ * Create a SKIP condition list for condition-less .wait() calls.
+ *
+ * @param di Decoder instance.
+ * @param count Number of samples to skip.
+ *
+ * @retval SRD_OK The new condition list was set successfully.
+ * @retval SRD_ERR There was an error setting the new condition list.
+ *                 The contents of di->condition_list are undefined.
+ *
+ * This routine is a reduced and specialized version of the @ref
+ * set_new_condition_list() and @ref create_term_list() routines which
+ * gets invoked when .wait() was called without specifications for
+ * conditions. This minor duplication of the SKIP term list creation
+ * simplifies the logic and avoids the creation of expensive Python
+ * objects with "constant" values which the caller did not pass in the
+ * first place. It results in maximum sharing of match handling code
+ * paths.
+ */
+static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
+{
+       struct srd_term *term;
+       GSList *term_list;
+
+       condition_list_free(di);
+       term = g_malloc0(sizeof(*term));
+       term->type = SRD_TERM_SKIP;
+       term->num_samples_to_skip = count;
+       term->num_samples_already_skipped = 0;
+       term_list = g_slist_append(NULL, term);
+       di->condition_list = g_slist_append(di->condition_list, term_list);
+
+       return SRD_OK;
+}
+
 static PyObject *Decoder_wait(PyObject *self, PyObject *args)
 {
        int ret;
+       uint64_t skip_count;
        unsigned int i;
        gboolean found_match;
        struct srd_decoder_inst *di;
@@ -595,21 +646,46 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
        }
 
        ret = set_new_condition_list(self, args);
-
+       if (ret < 0) {
+               srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
+               return NULL;
+       }
        if (ret == 9999) {
-               /* Empty condition list, automatic match. */
-               PyObject_SetAttrString(di->py_inst, "matched", Py_None);
-               /* Leave self.samplenum unchanged (== di->abs_cur_samplenum). */
-               return get_current_pinvalues(di);
+               /*
+                * Empty condition list, automatic match. Arrange for the
+                * execution of regular match handling code paths such that
+                * the next available sample is returned to the caller.
+                * Make sure to skip one sample when "anywhere within the
+                * stream", yet make sure to not skip sample number 0.
+                */
+               if (di->abs_cur_samplenum)
+                       skip_count = 1;
+               else if (!di->condition_list)
+                       skip_count = 0;
+               else
+                       skip_count = 1;
+               ret = set_skip_condition(di, skip_count);
+               if (ret < 0) {
+                       srd_dbg("%s: %s: Cannot setup condition-less wait().",
+                               di->inst_id, __func__);
+                       return NULL;
+               }
        }
 
        while (1) {
-               /* Wait for new samples to process. */
+               /* Wait for new samples to process, or termination request. */
                g_mutex_lock(&di->data_mutex);
-               while (!di->got_new_samples)
+               while (!di->got_new_samples && !di->want_wait_terminate)
                        g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
 
-               /* Check whether any of the current condition(s) match. */
+               /*
+                * Check whether any of the current condition(s) match.
+                * Arrange for termination requests to take a code path which
+                * won't find new samples to process, pretends to have processed
+                * previously stored samples, and returns to the main thread,
+                * while the termination request still gets signalled.
+                */
+               found_match = FALSE;
                ret = process_samples_until_condition_match(di, &found_match);
 
                /* If there's a match, set self.samplenum etc. and return. */
@@ -646,6 +722,17 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args)
                /* Signal the main thread that we handled all samples. */
                g_cond_signal(&di->handled_all_samples_cond);
 
+               /*
+                * When termination of wait() and decode() was requested,
+                * then exit the loop after releasing the mutex.
+                */
+               if (di->want_wait_terminate) {
+                       srd_dbg("%s: %s: Will return from wait().",
+                               di->inst_id, __func__);
+                       g_mutex_unlock(&di->data_mutex);
+                       return NULL;
+               }
+
                g_mutex_unlock(&di->data_mutex);
        }