From ef4e93fd678fa8b913343573984f33d6417acb24 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 26 Dec 2024 11:37:15 +0900 Subject: [PATCH 1/4] slist.h: add SLIST_REMOVE_HEAD --- lib/slist.c | 17 ++++++++++++----- lib/slist.h | 13 +++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/slist.c b/lib/slist.c index f5dbe1b8..bfb8b64b 100644 --- a/lib/slist.c +++ b/lib/slist.c @@ -5,6 +5,17 @@ #include "slist.h" +void +slist_remove_head(struct slist_head *h, struct slist_entry *e) +{ + assert(h->sh_first != NULL); + h->sh_first = e->se_next; + if (e->se_next == NULL) { + /* removing the only entry */ + h->sh_tailnextp = &h->sh_first; + } +} + void slist_remove(struct slist_head *h, struct slist_entry *prev, struct slist_entry *e) @@ -12,11 +23,7 @@ slist_remove(struct slist_head *h, struct slist_entry *prev, assert(h->sh_first != NULL); if (prev == NULL) { /* removing the first entry */ - h->sh_first = e->se_next; - if (e->se_next == NULL) { - /* removing the only entry */ - h->sh_tailnextp = &h->sh_first; - } + slist_remove_head(h, e); } else { prev->se_next = e->se_next; if (h->sh_tailnextp == &e->se_next) { diff --git a/lib/slist.h b/lib/slist.h index d88d528c..d303b108 100644 --- a/lib/slist.h +++ b/lib/slist.h @@ -28,6 +28,7 @@ __BEGIN_EXTERN_C void slist_head_init(struct slist_head *h); void slist_remove(struct slist_head *h, struct slist_entry *prev, struct slist_entry *e); +void slist_remove_head(struct slist_head *h, struct slist_entry *e); void slist_insert_tail(struct slist_head *h, void *elem, struct slist_entry *e); void slist_insert_head(struct slist_head *h, void *elem, @@ -91,6 +92,18 @@ __END_EXTERN_C : &(PREV)->NAME), \ (struct slist_entry *)&(ELEM)->NAME) +/* + * SLIST_REMOVE_HEAD(h, e, name) is an equivalent of + * SLIST_REMOVE(h, (TYPE *)NULL, e, name). + */ +#define SLIST_REMOVE_HEAD(HEAD, ELEM, NAME) \ + CHECK_TYPE(&(HEAD)->sh_first, (HEAD)->sh_tailnextp); \ + CHECK_TYPE((HEAD)->sh_first, (ELEM)->NAME.se_next); \ + ctassert(sizeof(*(HEAD)) == sizeof(struct slist_head)); \ + ctassert(sizeof((ELEM)->NAME) == sizeof(struct slist_entry)); \ + slist_remove_head((struct slist_head *)(HEAD), \ + (struct slist_entry *)&(ELEM)->NAME) + #define SLIST_INSERT_TAIL(HEAD, ELEM, NAME) \ CHECK_TYPE(&(HEAD)->sh_first, (HEAD)->sh_tailnextp); \ CHECK_TYPE((HEAD)->sh_first, (ELEM)->NAME.se_next); \ From cff5663a430c72b7dbe62ec9e6d168f3f8bfc643 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 26 Dec 2024 11:37:39 +0900 Subject: [PATCH 2/4] test/test.c: use SLIST_REMOVE_HEAD --- test/test.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.c b/test/test.c index b2e72b73..b2b7581e 100644 --- a/test/test.c +++ b/test/test.c @@ -890,7 +890,7 @@ test_slist(void **state) #if 0 assert_null(SLIST_PREV(&item, &h, struct item, entry)); #endif - SLIST_REMOVE(&h, (struct item *)NULL, &item, entry); + SLIST_REMOVE_HEAD(&h, &item, entry); assert_true(SLIST_EMPTY(&h)); assert_null(SLIST_FIRST(&h)); assert_null(SLIST_LAST(&h, struct item, entry)); @@ -904,7 +904,7 @@ test_slist(void **state) #if 0 assert_null(SLIST_PREV(&item, &h, struct item, entry)); #endif - SLIST_REMOVE(&h, (struct item *)NULL, &item, entry); + SLIST_REMOVE_HEAD(&h, &item, entry); assert_true(SLIST_EMPTY(&h)); assert_null(SLIST_FIRST(&h)); assert_null(SLIST_LAST(&h, struct item, entry)); @@ -948,7 +948,7 @@ test_slist(void **state) } #endif - SLIST_REMOVE(&h, (struct item *)NULL, &items[0], entry); + SLIST_REMOVE_HEAD(&h, &items[0], entry); SLIST_REMOVE(&h, &items[1], &items[2], entry); SLIST_REMOVE(&h, &items[3], &items[4], entry); SLIST_REMOVE(&h, &items[7], &items[8], entry); @@ -973,7 +973,7 @@ test_slist(void **state) while ((it = SLIST_FIRST(&h)) != NULL) { assert_int_equal(it - items, i * 2 + 1); i++; - SLIST_REMOVE(&h, (struct item *)NULL, it, entry); + SLIST_REMOVE_HEAD(&h, it, entry); } assert_int_equal(i, 5); assert_true(SLIST_EMPTY(&h)); From fefeab5622557a8239abcec92df9370643371941 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 26 Dec 2024 11:38:17 +0900 Subject: [PATCH 3/4] libdyld: use SLIST_REMOVE_HEAD --- libdyld/dyld.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libdyld/dyld.c b/libdyld/dyld.c index b6e19753..59695791 100644 --- a/libdyld/dyld.c +++ b/libdyld/dyld.c @@ -1398,7 +1398,7 @@ dyld_clear(struct dyld *d) struct mem_context *mctx = d->mctx; struct dyld_object *obj; while ((obj = SLIST_FIRST(&d->objs)) != NULL) { - SLIST_REMOVE(&d->objs, (struct dyld_object *)NULL, obj, q); + SLIST_REMOVE_HEAD(&d->objs, obj, q); dyld_object_destroy(obj); } if (d->pie) { From 1376d3b1180845e9acd1a7cb01b0243389f5bebf Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Thu, 26 Dec 2024 12:41:44 +0900 Subject: [PATCH 4/4] usched: switch to slist.h --- lib/exec_context.h | 4 ++-- lib/usched.c | 12 ++++++------ lib/usched.h | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/exec_context.h b/lib/exec_context.h index 47523a3b..08c34947 100644 --- a/lib/exec_context.h +++ b/lib/exec_context.h @@ -7,10 +7,10 @@ #include "toywasm_config.h" -#include "list.h" #include "options.h" #include "platform.h" #include "report.h" +#include "slist.h" #include "vec.h" struct val; @@ -279,7 +279,7 @@ struct exec_context { #if defined(TOYWASM_USE_USER_SCHED) /* scheduler */ struct sched *sched; - LIST_ENTRY(struct exec_context) rq; + SLIST_ENTRY(struct exec_context) rq; #endif /* Trap */ diff --git a/lib/usched.c b/lib/usched.c index 4202b66c..75c4679a 100644 --- a/lib/usched.c +++ b/lib/usched.c @@ -39,7 +39,7 @@ sched_enqueue(struct sched *sched, struct exec_context *ctx) { xlog_trace("%s: enqueueing ctx %p", __func__, (void *)ctx); assert(sched == ctx->sched); - LIST_INSERT_TAIL(&sched->runq, ctx, rq); + SLIST_INSERT_TAIL(&sched->runq, ctx, rq); } #define RR_INTERVAL_MS 100 @@ -50,9 +50,9 @@ sched_run(struct sched *sched, struct exec_context *caller) struct runq *q = &sched->runq; struct exec_context *ctx; - while ((ctx = LIST_FIRST(q)) != NULL) { + while ((ctx = SLIST_FIRST(q)) != NULL) { int ret; - LIST_REMOVE(q, ctx, rq); + SLIST_REMOVE_HEAD(q, ctx, rq); xlog_trace("%s: running ctx %p", __func__, (void *)ctx); ret = abstime_from_reltime_ms( CLOCK_MONOTONIC, &sched->next_resched, RR_INTERVAL_MS); @@ -71,7 +71,7 @@ sched_run(struct sched *sched, struct exec_context *caller) if (IS_RESTARTABLE(ret) && ret != ETOYWASMUSERINTERRUPT) { xlog_trace("%s: re-enqueueing ctx %p", __func__, (void *)ctx); - LIST_INSERT_TAIL(q, ctx, rq); + SLIST_INSERT_TAIL(q, ctx, rq); continue; } xlog_trace("%s: finishing ctx %p", __func__, (void *)ctx); @@ -87,7 +87,7 @@ sched_run(struct sched *sched, struct exec_context *caller) void sched_init(struct sched *sched) { - LIST_HEAD_INIT(&sched->runq); + SLIST_HEAD_INIT(&sched->runq); } void @@ -102,7 +102,7 @@ sched_need_resched(struct sched *sched) int ret; /* if we are the only thread, no point to resched. */ - if (LIST_FIRST(&sched->runq) == NULL) { + if (SLIST_FIRST(&sched->runq) == NULL) { return false; } diff --git a/lib/usched.h b/lib/usched.h index 992b704e..cde2bed4 100644 --- a/lib/usched.h +++ b/lib/usched.h @@ -1,13 +1,13 @@ #include #include -#include "list.h" #include "platform.h" +#include "slist.h" struct exec_context; struct sched { - LIST_HEAD_NAMED(struct exec_context, runq) runq; + SLIST_HEAD_NAMED(struct exec_context, runq) runq; struct timespec next_resched; };