Skip to content

Commit

Permalink
This fixes a dead-loop that can occur when the same io element is ins…
Browse files Browse the repository at this point in the history
…erted twice into the readers list.

This is only a temporary solution until the underlying issue is solved.
  • Loading branch information
catkira committed Nov 8, 2024
1 parent 0ee5b21 commit 0fd7cc5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 52 deletions.
50 changes: 12 additions & 38 deletions iiod-responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#endif

#define NB_BUFS_MAX 2
#define MAX_DEFAULT_IO_ELEMENTS 100

static void iiod_io_ref_unlocked(struct iiod_io *io);
static void iiod_io_unref_unlocked(struct iiod_io *io);
Expand Down Expand Up @@ -70,10 +69,7 @@ struct iiod_responder {
const struct iiod_responder_ops *ops;
void *d;

struct iiod_io *readers, *writers;
struct iiod_io* default_io_pool[MAX_DEFAULT_IO_ELEMENTS];
uint64_t default_io_pool_thread_ids[MAX_DEFAULT_IO_ELEMENTS];
unsigned int default_io_pool_size;
struct iiod_io *readers, *writers, *default_io;
uint16_t next_client_id;

struct iio_mutex *lock;
Expand Down Expand Up @@ -575,6 +571,7 @@ int iiod_io_get_response_async(struct iiod_io *io,
for (tmp = priv->readers; tmp->r_next; )
tmp = tmp->r_next;
tmp->r_next = io;
io->r_next = NULL;
}

iio_mutex_unlock(priv->lock);
Expand Down Expand Up @@ -641,6 +638,7 @@ void
iiod_responder_set_timeout(struct iiod_responder *priv, unsigned int timeout_ms)
{
priv->timeout_ms = timeout_ms;
priv->default_io->timeout_ms = timeout_ms;
}

void
Expand All @@ -667,13 +665,16 @@ iiod_responder_create(const struct iiod_responder_ops *ops, void *d)
if (err)
goto err_free_priv;

priv->default_io_pool_size = 0;
priv->default_io = iiod_responder_create_io(priv, 0);
err = iio_err(priv->default_io);
if (err)
goto err_free_lock;

priv->write_task = iio_task_create(iiod_responder_write, priv,
"iiod-responder-writer-task");
err = iio_err(priv->write_task);
if (err)
goto err_free_lock;
goto err_free_io;

if (!NO_THREADS) {
priv->read_thrd = iio_thrd_create(iiod_responder_reader_thrd, priv,
Expand All @@ -689,6 +690,8 @@ iiod_responder_create(const struct iiod_responder_ops *ops, void *d)

err_free_write_task:
iio_task_destroy(priv->write_task);
err_free_io:
iiod_io_unref(priv->default_io);
err_free_lock:
iio_mutex_destroy(priv->lock);
err_free_priv:
Expand All @@ -708,11 +711,7 @@ void iiod_responder_destroy(struct iiod_responder *priv)

iio_task_destroy(priv->write_task);

unsigned int i;
for (i = 0; i < priv->default_io_pool_size; i++) {
iiod_io_unref(priv->default_io_pool[i]);
}

iiod_io_unref(priv->default_io);
iio_mutex_destroy(priv->lock);
free(priv);
}
Expand Down Expand Up @@ -798,32 +797,7 @@ void iiod_io_unref(struct iiod_io *io)
struct iiod_io *
iiod_responder_get_default_io(struct iiod_responder *priv)
{
int idx = -1;
const uint64_t thid = iio_curr_thid();
unsigned int i;
for (i = 0; i < priv->default_io_pool_size; i++) {
if (priv->default_io_pool_thread_ids[i] == thid) {
idx = i;
break;
}
}
struct iiod_io *io;
if (idx != -1 && priv->default_io_pool[idx] != NULL && priv->default_io_pool[idx]->refcnt != 0) {
io = priv->default_io_pool[idx];
}
else {
io = iiod_responder_create_io(priv, 0);
io->timeout_ms = priv->timeout_ms;
priv->default_io_pool_thread_ids[priv->default_io_pool_size] = thid;
priv->default_io_pool[priv->default_io_pool_size] = io;
priv->default_io_pool_size++;
if (priv->default_io_pool_size > MAX_DEFAULT_IO_ELEMENTS) {
printf("default_io_pool overflow!!!\n");
return iio_ptr(-ENOMEM);
}

}
return io;
return priv->default_io;
}

struct iiod_io *
Expand Down
2 changes: 0 additions & 2 deletions include/iio/iio-lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ __api _Bool iio_task_is_done(struct iio_task_token *token);
__api int iio_task_sync(struct iio_task_token *token, unsigned int timeout_ms);
__api void iio_task_cancel(struct iio_task_token *token);

__api uint64_t iio_curr_thid(void);

#undef __api

#endif /* _IIO_LOCK_H */
6 changes: 0 additions & 6 deletions lock-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,3 @@ int iio_thrd_join_and_destroy(struct iio_thrd *thrd)

return (int) ret;
}

uint64_t iio_curr_thid()
{
// thread-id should always fit into 64-bit number
return GetCurrentThreadId();
}
6 changes: 0 additions & 6 deletions lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,3 @@ int iio_thrd_join_and_destroy(struct iio_thrd *thrd)

return (int)(intptr_t) retval;
}

uint64_t iio_curr_thid()
{
// thread-id should always fit into 64-bit number
return pthread_self();
}

0 comments on commit 0fd7cc5

Please sign in to comment.