From 5975b3707bcae469d54066a9883964fa345c5ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20S=C3=A1?= Date: Wed, 5 Feb 2025 17:17:15 +0000 Subject: [PATCH] test_7 --- iiod/ops.h | 2 ++ iiod/responder.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/iiod/ops.h b/iiod/ops.h index e4d9681e5..82371d982 100644 --- a/iiod/ops.h +++ b/iiod/ops.h @@ -64,6 +64,8 @@ struct block_entry { bool cyclic; int dmabuf_fd; int ep_fd; + uint16_t refcnt; + bool dead; }; struct buffer_entry { diff --git a/iiod/responder.c b/iiod/responder.c index 91aceb55b..4169e3230 100644 --- a/iiod/responder.c +++ b/iiod/responder.c @@ -36,12 +36,25 @@ struct iio_mutex *evlist_lock; static void free_block_entry(struct block_entry *entry) { + printf("Block Entry (%p): %u freed.\n", entry, entry->idx); iiod_io_cancel(entry->io); iiod_io_unref(entry->io); iio_block_destroy(entry->block); free(entry); } +static void block_entry_put(struct block_entry *entry) +{ + entry->refcnt--; + if (entry->refcnt == 0) + free_block_entry(entry); +} + +static void block_entry_get(struct block_entry *entry) +{ + entry->refcnt++; +} + static void free_buffer_entry(struct buffer_entry *entry) { struct block_entry *block_entry, *block_next; @@ -61,7 +74,8 @@ static void free_buffer_entry(struct buffer_entry *entry) for (block_entry = SLIST_FIRST(&entry->blocklist); block_entry; block_entry = block_next) { block_next = SLIST_NEXT(block_entry, entry); - free_block_entry(block_entry); + block_entry_put(block_entry); + //free_block_entry(block_entry); } iio_mutex_unlock(entry->lock); @@ -348,6 +362,9 @@ static int buffer_dequeue_block(void *priv, void *d) out_send_response: printf("Dequeue block(%u) send response\n", entry->idx); iiod_io_send_response(entry->io, ret, &data, nb_data); + iio_mutex_lock(buffer->lock); + block_entry_put(entry); + iio_mutex_unlock(buffer->lock); return 0; } @@ -531,6 +548,7 @@ static struct iio_block * get_iio_block(struct parser_pdata *pdata, SLIST_FOREACH(entry, &entry_buf->blocklist, entry) { if (entry->idx == cmd->code >> 16) { block = entry->block; + block_entry_get(entry); break; } } @@ -549,7 +567,6 @@ static void handle_free_buffer(struct parser_pdata *pdata, { struct iiod_io *io = iiod_command_get_default_io(cmd_data); const struct iio_device *dev; - struct block_entry *block_entry, *block_next; struct buffer_entry *entry, *buf_entry; struct iio_buffer *buf; int ret; @@ -673,6 +690,9 @@ static void handle_create_block(struct parser_pdata *pdata, /* No error? This block already exists, so return * -EINVAL. */ ret = -EINVAL; + iio_mutex_lock(buf_entry->lock); + block_entry_put(entry); + iio_mutex_unlock(buf_entry->lock); goto out_send_response; } @@ -691,6 +711,7 @@ static void handle_create_block(struct parser_pdata *pdata, entry->block = block; entry->io = io; entry->idx = cmd->code >> 16; + entry->refcnt = 1; if (WITH_IIOD_USB_DMABUF && pdata->is_usb) { entry->dmabuf_fd = iio_block_get_dmabuf_fd(block); @@ -757,12 +778,13 @@ static void handle_free_block(struct parser_pdata *pdata, SLIST_REMOVE(&buf_entry->blocklist, entry, block_entry, entry); - printf("Block (%p): %u freed.\n", entry, entry->idx); - free_block_entry(entry); + printf("Block (%p): %u put.\n", entry, entry->idx); + block_entry_put(entry); ret = 0; break; } + //block_entry_put(entry); iio_mutex_unlock(buf_entry->lock); IIO_DEBUG("Block %u freed.\n", cmd->code);