From fd93e8f7e5076e45006707dee89779d8bd7fa035 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Wed, 24 Jan 2024 12:41:32 +0100 Subject: [PATCH] iiod: Use iio_mutex locks instead of using pthread directly This permits to compile a thread-less IIOD by using the dummy iio-lock implementation. Signed-off-by: Paul Cercueil --- iiod/iiod.c | 20 +++++++++++++++- iiod/ops.h | 6 ++++- iiod/responder.c | 61 ++++++++++++++++++++++++++---------------------- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/iiod/iiod.c b/iiod/iiod.c index 0971ed36e..312c3ce78 100644 --- a/iiod/iiod.c +++ b/iiod/iiod.c @@ -11,6 +11,8 @@ #include "ops.h" #include "thread-pool.h" +#include + #include #include #include @@ -316,6 +318,18 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, xml_zstd = get_xml_zstd_data(ctx, &xml_zstd_len); + buflist_lock = iio_mutex_create(); + if (iio_err(buflist_lock)) { + ret = EXIT_FAILURE; + goto out_free_xml_data; + } + + evlist_lock = iio_mutex_create(); + if (iio_err(evlist_lock)) { + ret = EXIT_FAILURE; + goto out_free_buflist_lock; + } + if (WITH_IIOD_USBD && ffs_mountpoint) { ret = start_usb_daemon(ctx, ffs_mountpoint, (unsigned int) nb_pipes, ep0_fd, @@ -323,7 +337,7 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, if (ret) { IIO_PERROR(ret, "Unable to start USB daemon"); ret = EXIT_FAILURE; - goto out_free_xml_data; + goto out_free_evlist_lock; } } @@ -356,6 +370,10 @@ static int start_iiod(const char *uri, const char *ffs_mountpoint, * the worker threads are signaled to shutdown. */ thread_pool_stop_and_wait(main_thread_pool); +out_free_evlist_lock: + iio_mutex_destroy(evlist_lock); +out_free_buflist_lock: + iio_mutex_destroy(buflist_lock); out_free_xml_data: free(xml_zstd); out_destroy_context: diff --git a/iiod/ops.h b/iiod/ops.h index 11011e6e2..f116f0ff1 100644 --- a/iiod/ops.h +++ b/iiod/ops.h @@ -45,12 +45,16 @@ #define TEST_BIT(addr, bit) (!!(*(((uint32_t *) addr) + BIT_WORD(bit)) \ & BIT_MASK(bit))) +struct iio_mutex; struct iio_task; struct iiod_io; struct thread_pool; extern struct thread_pool *main_thread_pool; struct DevEntry; +extern struct iio_mutex *buflist_lock; +extern struct iio_mutex *evlist_lock; + enum iio_attr_type { IIO_ATTR_TYPE_DEVICE, IIO_ATTR_TYPE_DEBUG, @@ -76,7 +80,7 @@ struct buffer_entry { uint16_t idx; SLIST_HEAD(BlockList, block_entry) blocklist; - pthread_mutex_t lock; + struct iio_mutex *lock; }; struct evstream_entry { diff --git a/iiod/responder.c b/iiod/responder.c index 176597d64..4fa83e1c7 100644 --- a/iiod/responder.c +++ b/iiod/responder.c @@ -14,7 +14,6 @@ #include #include -#include #include #include #include @@ -29,12 +28,12 @@ static struct iio_buffer * get_iio_buffer(struct parser_pdata *pdata, static SLIST_HEAD(BufferList, buffer_entry) bufferlist; /* Protect bufferlist from parallel access */ -static pthread_mutex_t buflist_lock = PTHREAD_MUTEX_INITIALIZER; +struct iio_mutex *buflist_lock; static SLIST_HEAD(EventStreamList, evstream_entry) evlist; /* Protect evlist from parallel access */ -static pthread_mutex_t evlist_lock = PTHREAD_MUTEX_INITIALIZER; +struct iio_mutex *evlist_lock; static void free_block_entry(struct block_entry *entry) { @@ -56,7 +55,7 @@ static void free_buffer_entry(struct buffer_entry *entry) iio_task_destroy(entry->enqueue_task); iio_task_destroy(entry->dequeue_task); - pthread_mutex_lock(&entry->lock); + iio_mutex_lock(entry->lock); for (block_entry = SLIST_FIRST(&entry->blocklist); block_entry; block_entry = block_next) { @@ -64,10 +63,10 @@ static void free_buffer_entry(struct buffer_entry *entry) free_block_entry(block_entry); } - pthread_mutex_unlock(&entry->lock); + iio_mutex_unlock(entry->lock); iio_buffer_destroy(entry->buf); - pthread_mutex_destroy(&entry->lock); + iio_mutex_destroy(entry->lock); free(entry->words); free(entry); } @@ -412,10 +411,15 @@ static void handle_create_buffer(struct parser_pdata *pdata, entry->dev = dev; entry->idx = (uint16_t) cmd->code; + entry->lock = iio_mutex_create(); + ret = iio_err(entry->lock); + if (ret) + goto err_destroy_dequeue_task; + buf = iio_device_create_buffer(dev, entry->idx, mask); ret = iio_err(buf); if (ret) - goto err_destroy_dequeue_task; + goto err_destroy_lock; /* Rewrite the "words" bitmask according to the mask object, * which may have been modified when creating the buffer. */ @@ -433,11 +437,10 @@ static void handle_create_buffer(struct parser_pdata *pdata, entry->buf = buf; entry->pdata = pdata; - pthread_mutex_init(&entry->lock, NULL); - pthread_mutex_lock(&buflist_lock); + iio_mutex_lock(buflist_lock); SLIST_INSERT_HEAD(&bufferlist, entry, entry); - pthread_mutex_unlock(&buflist_lock); + iio_mutex_unlock(buflist_lock); IIO_DEBUG("Buffer %u created.\n", entry->idx); @@ -450,6 +453,8 @@ static void handle_create_buffer(struct parser_pdata *pdata, iiod_io_send_response(io, data.size, &data, 1); return; +err_destroy_lock: + iio_mutex_destroy(entry->lock); err_destroy_dequeue_task: iio_task_destroy(entry->dequeue_task); err_destroy_enqueue_task: @@ -476,7 +481,7 @@ static struct iio_buffer * get_iio_buffer(struct parser_pdata *pdata, if (!dev) return iio_ptr(-EINVAL); - pthread_mutex_lock(&buflist_lock); + iio_mutex_lock(buflist_lock); SLIST_FOREACH(entry, &bufferlist, entry) { if (entry->dev == dev && entry->idx == (cmd->code & 0xffff)) { @@ -485,7 +490,7 @@ static struct iio_buffer * get_iio_buffer(struct parser_pdata *pdata, } } - pthread_mutex_unlock(&buflist_lock); + iio_mutex_unlock(buflist_lock); if (buf && entry_ptr) *entry_ptr = entry; @@ -502,7 +507,7 @@ static struct iio_block * get_iio_block(struct parser_pdata *pdata, struct iio_block *block = NULL; int err; - pthread_mutex_lock(&entry_buf->lock); + iio_mutex_lock(entry_buf->lock); SLIST_FOREACH(entry, &entry_buf->blocklist, entry) { if (entry->client_id == cmd->client_id) { @@ -511,7 +516,7 @@ static struct iio_block * get_iio_block(struct parser_pdata *pdata, } } - pthread_mutex_unlock(&entry_buf->lock); + iio_mutex_unlock(entry_buf->lock); if (block && entry_ptr) *entry_ptr = entry; @@ -541,7 +546,7 @@ static void handle_free_buffer(struct parser_pdata *pdata, ret = -EBADF; - pthread_mutex_lock(&buflist_lock); + iio_mutex_lock(buflist_lock); SLIST_FOREACH(entry, &bufferlist, entry) { if (entry != buf_entry) @@ -553,7 +558,7 @@ static void handle_free_buffer(struct parser_pdata *pdata, break; } - pthread_mutex_unlock(&buflist_lock); + iio_mutex_unlock(buflist_lock); IIO_DEBUG("Buffer %u freed.\n", cmd->code); @@ -670,9 +675,9 @@ static void handle_create_block(struct parser_pdata *pdata, /* Keep a reference to the iiod_io until the block is freed. */ iiod_io_ref(io); - pthread_mutex_lock(&buf_entry->lock); + iio_mutex_lock(buf_entry->lock); SLIST_INSERT_HEAD(&buf_entry->blocklist, entry, entry); - pthread_mutex_unlock(&buf_entry->lock); + iio_mutex_unlock(buf_entry->lock); out_send_response: iiod_io_send_response_code(io, ret); @@ -702,7 +707,7 @@ static void handle_free_block(struct parser_pdata *pdata, ret = -EBADF; - pthread_mutex_lock(&buf_entry->lock); + iio_mutex_lock(buf_entry->lock); SLIST_FOREACH(entry, &buf_entry->blocklist, entry) { if (entry->block != block) @@ -715,7 +720,7 @@ static void handle_free_block(struct parser_pdata *pdata, break; } - pthread_mutex_unlock(&buf_entry->lock); + iio_mutex_unlock(buf_entry->lock); IIO_DEBUG("Block %u freed.\n", cmd->code); @@ -900,9 +905,9 @@ static void handle_create_evstream(struct parser_pdata *pdata, /* Keep a reference to the iiod_io until the evstream is freed. */ iiod_io_ref(io); - pthread_mutex_lock(&evlist_lock); + iio_mutex_lock(evlist_lock); SLIST_INSERT_HEAD(&evlist, entry, entry); - pthread_mutex_unlock(&evlist_lock); + iio_mutex_unlock(evlist_lock); out_send_response: iiod_io_send_response_code(io, ret); @@ -921,7 +926,7 @@ static struct evstream_entry * get_evstream(struct parser_pdata *pdata, if (!dev) return NULL; - pthread_mutex_lock(&evlist_lock); + iio_mutex_lock(evlist_lock); SLIST_FOREACH(entry, &evlist, entry) { if (entry->client_id == idx && entry->dev == dev @@ -933,7 +938,7 @@ static struct evstream_entry * get_evstream(struct parser_pdata *pdata, if (entry && remove) SLIST_REMOVE(&evlist, entry, evstream_entry, entry); - pthread_mutex_unlock(&evlist_lock); + iio_mutex_unlock(evlist_lock); return entry; } @@ -1076,7 +1081,7 @@ static void iiod_responder_free_resources(struct parser_pdata *pdata) struct buffer_entry *buf_entry, *buf_next; struct evstream_entry *ev_entry, *ev_next; - pthread_mutex_lock(&buflist_lock); + iio_mutex_lock(buflist_lock); for (buf_entry = SLIST_FIRST(&bufferlist); buf_entry; buf_entry = buf_next) { @@ -1089,9 +1094,9 @@ static void iiod_responder_free_resources(struct parser_pdata *pdata) } } - pthread_mutex_unlock(&buflist_lock); + iio_mutex_unlock(buflist_lock); - pthread_mutex_lock(&evlist_lock); + iio_mutex_lock(evlist_lock); for (ev_entry = SLIST_FIRST(&evlist); ev_entry; ev_entry = ev_next) { ev_next = SLIST_NEXT(ev_entry, entry); @@ -1103,7 +1108,7 @@ static void iiod_responder_free_resources(struct parser_pdata *pdata) } } - pthread_mutex_unlock(&evlist_lock); + iio_mutex_unlock(evlist_lock); } int binary_parse(struct parser_pdata *pdata)