From 6397735bc92707e2e9ddf56e57fa42d108649bb9 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Sat, 4 Apr 2020 12:16:27 +0900 Subject: [PATCH] lib/thread: Introduce thread state to distinguish exiting and exited Add enum spdk_thread_state made of RUNNING, EXITING, and EXITED, and the current state to struct spdk_thread. The state EXITING is not actually used in this patch yet. Replace the flag exit simply by the state EXITED. Signed-off-by: Shuhei Matsumoto Change-Id: I6e5dc7184d50ae6d00e6ba00f5e2cf6045e5d48d Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/1630 Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Jim Harris --- include/spdk_internal/thread.h | 15 ++++++++++++++- lib/thread/thread.c | 18 ++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/include/spdk_internal/thread.h b/include/spdk_internal/thread.h index 15c445ae1..a047a750e 100644 --- a/include/spdk_internal/thread.h +++ b/include/spdk_internal/thread.h @@ -77,12 +77,25 @@ struct spdk_poller { char name[SPDK_MAX_POLLER_NAME_LEN + 1]; }; +enum spdk_thread_state { + /* The thread is pocessing poller and message by spdk_thread_poll(). */ + SPDK_THREAD_STATE_RUNNING, + + /* The thread is in the process of termination. It reaps unregistering + * poller are releasing I/O channel. + */ + SPDK_THREAD_STATE_EXITING, + + /* The thread is exited. It is ready to call spdk_thread_destroy(). */ + SPDK_THREAD_STATE_EXITED, +}; + struct spdk_thread { TAILQ_HEAD(, spdk_io_channel) io_channels; TAILQ_ENTRY(spdk_thread) tailq; char name[SPDK_MAX_THREAD_NAME_LEN + 1]; uint64_t id; - bool exit; + enum spdk_thread_state state; struct spdk_cpuset cpumask; uint64_t tsc_last; struct spdk_thread_stats stats; diff --git a/lib/thread/thread.c b/lib/thread/thread.c index 52a725c15..030893199 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -305,6 +305,8 @@ spdk_thread_create(const char *name, struct spdk_cpuset *cpumask) return NULL; } + thread->state = SPDK_THREAD_STATE_RUNNING; + return thread; } @@ -350,7 +352,7 @@ _spdk_thread_exit(struct spdk_thread *thread) } } - thread->exit = true; + thread->state = SPDK_THREAD_STATE_EXITED; return 0; } @@ -361,9 +363,9 @@ spdk_thread_exit(struct spdk_thread *thread) assert(tls_thread == thread); - if (thread->exit) { + if (thread->state >= SPDK_THREAD_STATE_EXITING) { SPDK_INFOLOG(SPDK_LOG_THREAD, - "thread %s is already marked as exited\n", + "thread %s is already exiting\n", thread->name); return 0; } @@ -374,7 +376,7 @@ spdk_thread_exit(struct spdk_thread *thread) bool spdk_thread_is_exited(struct spdk_thread *thread) { - return thread->exit; + return thread->state == SPDK_THREAD_STATE_EXITED; } void @@ -382,7 +384,7 @@ spdk_thread_destroy(struct spdk_thread *thread) { SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Destroy thread %s\n", thread->name); - assert(thread->exit == true); + assert(thread->state == SPDK_THREAD_STATE_EXITED); if (tls_thread == thread) { tls_thread = NULL; @@ -801,7 +803,7 @@ spdk_thread_send_msg(const struct spdk_thread *thread, spdk_msg_fn fn, void *ctx assert(thread != NULL); - if (spdk_unlikely(thread->exit)) { + if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { SPDK_ERRLOG("Thread %s is marked as exited.\n", thread->name); return -EIO; } @@ -868,7 +870,7 @@ _spdk_poller_register(spdk_poller_fn fn, return NULL; } - if (spdk_unlikely(thread->exit)) { + if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { SPDK_ERRLOG("thread %s is marked as exited\n", thread->name); return NULL; } @@ -1290,7 +1292,7 @@ spdk_get_io_channel(void *io_device) return NULL; } - if (spdk_unlikely(thread->exit)) { + if (spdk_unlikely(thread->state == SPDK_THREAD_STATE_EXITED)) { SPDK_ERRLOG("Thread %s is marked as exited\n", thread->name); pthread_mutex_unlock(&g_devlist_mutex); return NULL;