Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slist.h related changes #277

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/exec_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
17 changes: 12 additions & 5 deletions lib/slist.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@

#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)
{
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) {
Expand Down
13 changes: 13 additions & 0 deletions lib/slist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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); \
Expand Down
12 changes: 6 additions & 6 deletions lib/usched.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/usched.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <stdbool.h>
#include <time.h>

#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;
};

Expand Down
2 changes: 1 addition & 1 deletion libdyld/dyld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down