From 1ef14bee7aa063a939a3bffb11e2931c23fdf8f5 Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Wed, 12 Jun 2019 22:27:29 -0400 Subject: [PATCH] lib: Add check for non-preexisting thread When adding a read/write poll event and we are using a developmental build add a bit of code to ensure that we do not already have an read or write event scheduled. Signed-off-by: Donald Sharp --- lib/thread.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/thread.c b/lib/thread.c index 2e7b0ca0ef..9d00d7c52d 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -756,6 +756,7 @@ struct thread *funcname_thread_add_read_write(int dir, struct thread_master *m, debugargdef) { struct thread *thread = NULL; + struct thread **thread_array; assert(fd >= 0 && fd < m->fd_limit); pthread_mutex_lock(&m->mtx); @@ -770,11 +771,25 @@ struct thread *funcname_thread_add_read_write(int dir, struct thread_master *m, /* default to a new pollfd */ nfds_t queuepos = m->handler.pfdcount; + if (dir == THREAD_READ) + thread_array = m->read; + else + thread_array = m->write; + /* if we already have a pollfd for our file descriptor, find and * use it */ for (nfds_t i = 0; i < m->handler.pfdcount; i++) if (m->handler.pfds[i].fd == fd) { queuepos = i; + +#ifdef DEV_BUILD + /* + * What happens if we have a thread already + * created for this event? + */ + if (thread_array[fd]) + assert(!"Thread already scheduled for file descriptor"); +#endif break; } @@ -794,10 +809,7 @@ struct thread *funcname_thread_add_read_write(int dir, struct thread_master *m, pthread_mutex_lock(&thread->mtx); { thread->u.fd = fd; - if (dir == THREAD_READ) - m->read[thread->u.fd] = thread; - else - m->write[thread->u.fd] = thread; + thread_array[thread->u.fd] = thread; } pthread_mutex_unlock(&thread->mtx); -- 2.39.5