]> sigrok.org Git - libsigrokdecode.git/blobdiff - instance.c
instance.c: Add a debug message for thread creation.
[libsigrokdecode.git] / instance.c
index b56a3e662400938839a89d99d8ce0bd8ebd0dfd1..1753912147a5fc6d2942a1015b11cee3c1856e86 100644 (file)
@@ -297,6 +297,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
        struct srd_decoder_inst *di;
        char *inst_id;
 
+       i = 1;
        srd_dbg("Creating new %s instance.", decoder_id);
 
        if (session_is_valid(sess) != SRD_OK) {
@@ -313,12 +314,22 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
 
        di->decoder = dec;
        di->sess = sess;
+
        if (options) {
                inst_id = g_hash_table_lookup(options, "id");
-               di->inst_id = g_strdup(inst_id ? inst_id : decoder_id);
+               if (inst_id)
+                       di->inst_id = g_strdup(inst_id);
                g_hash_table_remove(options, "id");
-       } else
-               di->inst_id = g_strdup(decoder_id);
+       }
+
+       /* Create a unique instance ID (as none was provided). */
+       if (!di->inst_id) {
+               di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++);
+               while (srd_inst_find_by_id(sess, di->inst_id)) {
+                       g_free(di->inst_id);
+                       di->inst_id = g_strdup_printf("%s-%d", decoder_id, i++);
+               }
+       }
 
        /*
         * Prepare a default channel map, where samples come in the
@@ -356,11 +367,11 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
 
        di->condition_list = NULL;
        di->match_array = NULL;
-       di->start_samplenum = 0;
-       di->end_samplenum = 0;
+       di->abs_start_samplenum = 0;
+       di->abs_end_samplenum = 0;
        di->inbuf = NULL;
        di->inbuflen = 0;
-       di->cur_samplenum = 0;
+       di->abs_cur_samplenum = 0;
        di->old_pins_array = NULL;
        di->thread_handle = NULL;
        di->got_new_samples = FALSE;
@@ -368,6 +379,7 @@ SRD_API struct srd_decoder_inst *srd_inst_new(struct srd_session *sess,
 
        /* Instance takes input from a frontend by default. */
        sess->di_list = g_slist_append(sess->di_list, di);
+       srd_dbg("Created new %s instance with ID %s.", decoder_id, di->inst_id);
 
        return di;
 }
@@ -411,11 +423,43 @@ SRD_API int srd_inst_stack(struct srd_session *sess,
        return SRD_OK;
 }
 
+/**
+ * Search a decoder instance and its stack for instance ID.
+ *
+ * @param[in] inst_id ID to search for.
+ * @param[in] stack A decoder instance, potentially with stacked instances.
+ *
+ * @return The matching instance, or NULL.
+ */
+static struct srd_decoder_inst *srd_inst_find_by_id_stack(const char *inst_id,
+               struct srd_decoder_inst *stack)
+{
+       const GSList *l;
+       struct srd_decoder_inst *tmp, *di;
+
+       if (!strcmp(stack->inst_id, inst_id))
+               return stack;
+
+       /* Otherwise, look recursively in our stack. */
+       di = NULL;
+       if (stack->next_di) {
+               for (l = stack->next_di; l; l = l->next) {
+                       tmp = l->data;
+                       if (!strcmp(tmp->inst_id, inst_id)) {
+                               di = tmp;
+                               break;
+                       }
+               }
+       }
+
+       return di;
+}
+
 /**
  * Find a decoder instance by its instance ID.
  *
- * Only the bottom level of instances are searched -- instances already stacked
- * on top of another one will not be found.
+ * This will recurse to find the instance anywhere in the stack tree of the
+ * given session.
  *
  * @param sess The session holding the protocol decoder instance.
  * @param inst_id The instance ID to be found.
@@ -438,10 +482,8 @@ SRD_API struct srd_decoder_inst *srd_inst_find_by_id(struct srd_session *sess,
        di = NULL;
        for (l = sess->di_list; l; l = l->next) {
                tmp = l->data;
-               if (!strcmp(tmp->inst_id, inst_id)) {
-                       di = tmp;
+               if ((di = srd_inst_find_by_id_stack(inst_id, tmp)) != NULL)
                        break;
-               }
        }
 
        return di;
@@ -580,8 +622,8 @@ SRD_PRIV int srd_inst_start(struct srd_decoder_inst *di)
        /* Set self.samplenum to 0. */
        PyObject_SetAttrString(di->py_inst, "samplenum", PyLong_FromLong(0));
 
-       /* Set self.matches to None. */
-       PyObject_SetAttrString(di->py_inst, "matches", Py_None);
+       /* Set self.matched to None. */
+       PyObject_SetAttrString(di->py_inst, "matched", Py_None);
 
        /* Start all the PDs stacked on top of this one. */
        for (l = di->next_di; l; l = l->next) {
@@ -787,16 +829,16 @@ static gboolean find_match(struct srd_decoder_inst *di)
                return TRUE;
        }
 
-       num_samples_to_process = di->end_samplenum - di->cur_samplenum;
+       num_samples_to_process = di->abs_end_samplenum - di->abs_cur_samplenum;
        num_conditions = g_slist_length(di->condition_list);
 
        /* di->match_array is NULL here. Create a new GArray. */
        di->match_array = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), num_conditions);
        g_array_set_size(di->match_array, num_conditions);
 
-       for (i = 0, s = 0; i < num_samples_to_process; i++, s++, (di->cur_samplenum)++) {
+       for (i = 0, s = 0; i < num_samples_to_process; i++, s++, (di->abs_cur_samplenum)++) {
 
-               sample_pos = di->inbuf + ((di->cur_samplenum - di->start_samplenum) * di->data_unitsize);
+               sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
 
                /* Check whether the current sample matches at least one of the conditions (logical OR). */
                /* IMPORTANT: We need to check all conditions, even if there was a match already! */
@@ -844,9 +886,10 @@ SRD_PRIV int process_samples_until_condition_match(struct srd_decoder_inst *di,
                *found_match = find_match(di);
 
                /* Did we handle all samples yet? */
-               if (di->cur_samplenum >= di->end_samplenum) {
-                       srd_dbg("Done, handled all samples (%" PRIu64 "/%" PRIu64 ").",
-                               di->cur_samplenum, di->end_samplenum);
+               if (di->abs_cur_samplenum >= di->abs_end_samplenum) {
+                       srd_dbg("Done, handled all samples (abs cur %" PRIu64
+                               " / abs end %" PRIu64 ").",
+                               di->abs_cur_samplenum, di->abs_end_samplenum);
                        return SRD_OK;
                }
 
@@ -894,11 +937,45 @@ static gpointer di_thread(gpointer data)
 /**
  * Decode a chunk of samples.
  *
+ * The calls to this function must provide the samples that shall be
+ * used by the protocol decoder
+ *  - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug),
+ *  - starting from sample zero (2, 3, 4, 5, 6[...] is a bug),
+ *  - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug).
+ *
+ * The start- and end-sample numbers are absolute sample numbers (relative
+ * to the start of the whole capture/file/stream), i.e. they are not relative
+ * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'.
+ *
+ * Correct example (4096 samples total, 4 chunks @ 1024 samples each):
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 1024, 2048, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1);
+ *
+ * The chunk size ('inbuflen') can be arbitrary and can differ between calls.
+ *
+ * Correct example (4096 samples total, 7 chunks @ various samples each):
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 1024, 1124, inbuf,  100, 1);
+ *   srd_inst_decode(di, 1124, 1424, inbuf,  300, 1);
+ *   srd_inst_decode(di, 1424, 1643, inbuf,  219, 1);
+ *   srd_inst_decode(di, 1643, 2048, inbuf,  405, 1);
+ *   srd_inst_decode(di, 2048, 3072, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 3072, 4096, inbuf, 1024, 1);
+ *
+ * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but
+ * the start- and end-samplenumbers are not absolute):
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *   srd_inst_decode(di, 0,    1024, inbuf, 1024, 1);
+ *
  * @param di The decoder instance to call. Must not be NULL.
- * @param start_samplenum The starting sample number for the buffer's sample
- *                       set, relative to the start of capture.
- * @param end_samplenum The ending sample number for the buffer's sample
- *                       set, relative to the start of capture.
+ * @param abs_start_samplenum The absolute starting sample number for the
+ *             buffer's sample set, relative to the start of capture.
+ * @param abs_end_samplenum The absolute ending sample number for the
+ *             buffer's sample set, relative to the start of capture.
  * @param inbuf The buffer to decode. Must not be NULL.
  * @param inbuflen Length of the buffer. Must be > 0.
  * @param unitsize The number of bytes per sample. Must be > 0.
@@ -908,7 +985,7 @@ static gpointer di_thread(gpointer data)
  * @private
  */
 SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
-               uint64_t start_samplenum, uint64_t end_samplenum,
+               uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
                const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
 {
        PyObject *py_res;
@@ -933,12 +1010,20 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
                return SRD_ERR_ARG;
        }
 
+       if (abs_start_samplenum != di->abs_cur_samplenum ||
+           abs_end_samplenum < abs_start_samplenum) {
+               srd_dbg("Incorrect sample numbers: start=%" PRIu64 ", cur=%"
+                       PRIu64 ", end=%" PRIu64 ".", abs_start_samplenum,
+                       di->abs_cur_samplenum, abs_end_samplenum);
+               return SRD_ERR_ARG;
+       }
+
        di->data_unitsize = unitsize;
 
-       srd_dbg("Decoding: start sample %" PRIu64 ", end sample %"
+       srd_dbg("Decoding: abs start sample %" PRIu64 ", abs end sample %"
                PRIu64 " (%" PRIu64 " samples, %" PRIu64 " bytes, unitsize = "
-               "%d), instance %s.", start_samplenum, end_samplenum,
-               end_samplenum - start_samplenum, inbuflen, di->data_unitsize,
+               "%d), instance %s.", abs_start_samplenum, abs_end_samplenum,
+               abs_end_samplenum - abs_start_samplenum, inbuflen, di->data_unitsize,
                di->inst_id);
 
        apiver = srd_decoder_apiver(di->decoder);
@@ -951,7 +1036,7 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
                logic = PyObject_New(srd_logic, (PyTypeObject *)srd_logic_type);
                Py_INCREF(logic);
                logic->di = (struct srd_decoder_inst *)di;
-               logic->start_samplenum = start_samplenum;
+               logic->abs_start_samplenum = abs_start_samplenum;
                logic->itercnt = 0;
                logic->inbuf = (uint8_t *)inbuf;
                logic->inbuflen = inbuflen;
@@ -960,22 +1045,26 @@ SRD_PRIV int srd_inst_decode(struct srd_decoder_inst *di,
 
                Py_IncRef(di->py_inst);
                if (!(py_res = PyObject_CallMethod(di->py_inst, "decode",
-                       "KKO", start_samplenum, end_samplenum, logic))) {
+                       "KKO", abs_start_samplenum, abs_end_samplenum, logic))) {
                        srd_exception_catch("Protocol decoder instance %s",
                                        di->inst_id);
                        return SRD_ERR_PYTHON;
                }
+               di->abs_cur_samplenum = abs_end_samplenum;
                Py_DecRef(py_res);
        } else {
                /* If this is the first call, start the worker thread. */
-               if (!di->thread_handle)
-                       di->thread_handle = g_thread_new("di_thread",
+               if (!di->thread_handle) {
+                       srd_dbg("No worker thread for this decoder stack "
+                               "exists yet, creating one: %s.", di->inst_id);
+                       di->thread_handle = g_thread_new(di->inst_id,
                                                         di_thread, di);
+               }
 
                /* Push the new sample chunk to the worker thread. */
                g_mutex_lock(&di->data_mutex);
-               di->start_samplenum = start_samplenum;
-               di->end_samplenum = end_samplenum;
+               di->abs_start_samplenum = abs_start_samplenum;
+               di->abs_end_samplenum = abs_end_samplenum;
                di->inbuf = inbuf;
                di->inbuflen = inbuflen;
                di->got_new_samples = TRUE;
@@ -1018,27 +1107,14 @@ SRD_PRIV void srd_inst_free(struct srd_decoder_inst *di)
 }
 
 /** @private */
-SRD_PRIV void srd_inst_free_all(struct srd_session *sess, GSList *stack)
+SRD_PRIV void srd_inst_free_all(struct srd_session *sess)
 {
-       GSList *l;
-       struct srd_decoder_inst *di;
-
        if (session_is_valid(sess) != SRD_OK) {
                srd_err("Invalid session.");
                return;
        }
 
-       di = NULL;
-       for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
-               di = l->data;
-               if (di->next_di)
-                       srd_inst_free_all(sess, di->next_di);
-               srd_inst_free(di);
-       }
-       if (!stack) {
-               g_slist_free(sess->di_list);
-               sess->di_list = NULL;
-       }
+       g_slist_free_full(sess->di_list, (GDestroyNotify)srd_inst_free);
 }
 
 /** @} */