Skip to content

Commit

Permalink
Change slist to qlist and qlist to slist
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Dec 20, 2016
1 parent fc83f64 commit 20e449d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 130 deletions.
52 changes: 26 additions & 26 deletions cr.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/* When doing stack size census we will keep maximum stack size in a list
indexed by go() call, i.e. by file name and line number. */
struct dill_census_item {
struct dill_qlist crs;
struct dill_slist crs;
const char *file;
int line;
size_t max_stack;
Expand All @@ -60,7 +60,7 @@ static void dill_resume(struct dill_cr *cr, int id, int err) {
struct dill_ctx_cr *ctx = &dill_getctx->cr;
cr->id = id;
cr->err = err;
dill_slist_push_back(&ctx->ready, &cr->ready);
dill_qlist_push_back(&ctx->ready, &cr->ready);
}

int dill_canblock(void) {
Expand Down Expand Up @@ -88,24 +88,24 @@ int dill_ctx_cr_init(struct dill_ctx_cr *ctx) {
it's called once only and you can't even create a different coroutine
without calling it. */
ctx->r = &ctx->main;
dill_slist_init(&ctx->ready);
dill_qlist_init(&ctx->ready);
dill_list_init(&ctx->timers);
ctx->wait_counter = 0;
/* Initialize main coroutine. */
memset(&ctx->main, 0, sizeof(ctx->main));
dill_slist_item_init(&ctx->main.ready);
dill_qlist_init(&ctx->main.clauses);
dill_qlist_item_init(&ctx->main.ready);
dill_slist_init(&ctx->main.clauses);
#if defined DILL_CENSUS
dill_qlist_init(&ctx->census);
dill_slist_init(&ctx->census);
#endif
return 0;
}

void dill_ctx_cr_term(struct dill_ctx_cr *ctx) {
#if defined DILL_CENSUS
struct dill_qlist *it;
for(it = dill_qlist_next(&ctx->census); it != &ctx->census;
it = dill_qlist_next(it)) {
struct dill_slist *it;
for(it = dill_slist_next(&ctx->census); it != &ctx->census;
it = dill_slist_next(it)) {
struct dill_census_item *ci =
dill_cont(it, struct dill_census_item, crs);
fprintf(stderr, "%s:%d - maximum stack size %zu B\n",
Expand Down Expand Up @@ -247,8 +247,8 @@ int dill_prologue(sigjmp_buf **jb, void **ptr, size_t len,
int hndl = hmake(&cr->vfs);
if(dill_slow(hndl < 0)) {
int err = errno; dill_freestack(cr + 1); errno = err; return -1;}
dill_slist_item_init(&cr->ready);
dill_qlist_init(&cr->clauses);
dill_qlist_item_init(&cr->ready);
dill_slist_init(&cr->clauses);
cr->closer = NULL;
cr->no_blocking1 = 0;
cr->no_blocking2 = 0;
Expand All @@ -260,9 +260,9 @@ int dill_prologue(sigjmp_buf **jb, void **ptr, size_t len,
#if defined DILL_CENSUS
/* Find the appropriate census item if it exists. It's O(n) but meh. */
cr->census = NULL;
struct dill_qlist *it;
for(it = dill_qlist_next(&ctx->census); it != &ctx->census;
it = dill_qlist_next(it)) {
struct dill_slist *it;
for(it = dill_slist_next(&ctx->census); it != &ctx->census;
it = dill_slist_next(it)) {
cr->census = dill_cont(it, struct dill_census_item, crs);
if(cr->census->line == line && strcmp(cr->census->file, file) == 0)
break;
Expand All @@ -271,7 +271,7 @@ int dill_prologue(sigjmp_buf **jb, void **ptr, size_t len,
if(it == &ctx->census) {
cr->census = malloc(sizeof(struct dill_census_item));
dill_assert(cr->census);
dill_qlist_push(&ctx->census, &cr->census->crs);
dill_slist_push(&ctx->census, &cr->census->crs);
cr->census->file = file;
cr->census->line = line;
cr->census->max_stack = 0;
Expand All @@ -298,7 +298,7 @@ void dill_epilogue(void) {
if(ctx->r->closer)
dill_cancel(ctx->r->closer, 0);
/* With no clauses added, this call will never return. */
dill_assert(dill_qlist_empty(&ctx->r->clauses));
dill_assert(dill_slist_empty(&ctx->r->clauses));
dill_wait();
}

Expand All @@ -317,7 +317,7 @@ static void dill_cr_close(struct hvfs *vfs) {
/* No blocking calls from this point on. */
cr->no_blocking1 = 1;
/* Resume the coroutine if it was blocked. */
if(!dill_slist_item_inlist(&cr->ready))
if(!dill_qlist_item_inlist(&cr->ready))
dill_cancel(cr, ECANCELED);
/* Wait till the coroutine finishes execution. With no clauses added
the only mechanism to resume is dill_cancel(). This is not really
Expand Down Expand Up @@ -365,7 +365,7 @@ void dill_waitfor(struct dill_clause *cl, int id,
/* Add clause to the coroutine list of active clauses. */
cl->cr = ctx->r;
dill_assert(ctx->r->clauses.next);
dill_qlist_push(&ctx->r->clauses, &cl->item);
dill_slist_push(&ctx->r->clauses, &cl->item);
cl->id = id;
}

Expand All @@ -384,16 +384,16 @@ int dill_wait(void) {
if(ctx->r) {
if(dill_setjmp(ctx->r->ctx)) {
/* We get here once the coroutine is resumed. */
dill_qlist_init(&ctx->r->clauses);
dill_slist_init(&ctx->r->clauses);
errno = ctx->r->err;
return ctx->r->id;
}
}
while(1) {
/* If there's a coroutine ready to be executed jump to it. */
if(!dill_slist_empty(&ctx->ready)) {
if(!dill_qlist_empty(&ctx->ready)) {
++ctx->wait_counter;
struct dill_slist_item *it = dill_slist_pop(&ctx->ready);
struct dill_qlist_item *it = dill_qlist_pop(&ctx->ready);
ctx->r = dill_cont(it, struct dill_cr, ready);
dill_longjmp(ctx->r->ctx);
}
Expand All @@ -402,18 +402,18 @@ int dill_wait(void) {
dill_poller_wait(1);
/* Sanity check: External events must have unblocked at least
one coroutine. */
dill_assert(!dill_slist_empty(&ctx->ready));
dill_assert(!dill_qlist_empty(&ctx->ready));
ctx->wait_counter = 0;
}
}

static void dill_docancel(struct dill_cr *cr, int id, int err) {
/* Sanity check: Make sure that the coroutine was really suspended. */
dill_assert(!dill_slist_item_inlist(&cr->ready));
dill_assert(!dill_qlist_item_inlist(&cr->ready));
/* Remove the clauses from endpoints' lists of waiting coroutines. */
struct dill_qlist *it;
for(it = dill_qlist_next(&cr->clauses); it != &cr->clauses;
it = dill_qlist_next(it)) {
struct dill_slist *it;
for(it = dill_slist_next(&cr->clauses); it != &cr->clauses;
it = dill_slist_next(it)) {
struct dill_clause *cl = dill_cont(it, struct dill_clause, item);
if(cl->eplist)
dill_list_erase(&cl->epitem);
Expand Down
12 changes: 6 additions & 6 deletions cr.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#include "libdill.h"
#include "list.h"
#include "qlist.h"
#include "slist.h"
#include "qlist.h"

/* The coroutine. The memory layout looks like this:
+-------------------------------------------------------------+---------+
Expand All @@ -43,7 +43,7 @@ struct dill_cr {
it lives in this list (dill_ready). 'id' is a result value to return
from dill_wait() once the coroutine is resumed. Additionally, errno
will be set to value of 'err'. */
struct dill_slist_item ready;
struct dill_qlist_item ready;
/* Virtual function table. */
struct hvfs vfs;
int id;
Expand All @@ -52,7 +52,7 @@ struct dill_cr {
(registers and such). */
sigjmp_buf ctx;
/* If coroutine is blocked, here's the list of clauses it waits for. */
struct dill_qlist clauses;
struct dill_slist clauses;
/* There are two possible reasons to disable blocking calls.
1. The coroutine is being closed by its owner.
2. The execution is happening within a context of hclose() function. */
Expand Down Expand Up @@ -86,7 +86,7 @@ struct dill_ctx_cr {
/* Currently running coroutine. */
struct dill_cr *r;
/* List of coroutines ready for execution. */
struct dill_slist ready;
struct dill_qlist ready;
/* Global linked list of all timers. The list is ordered.
First timer to be resumed comes first and so on. */
struct dill_list timers;
Expand All @@ -95,15 +95,15 @@ struct dill_ctx_cr {
so we have to store this info here instead on the top of the stack. */
struct dill_cr main;
#if defined DILL_CENSUS
struct dill_qlist census;
struct dill_slist census;
#endif
};

struct dill_clause {
/* The coroutine that owns this clause. */
struct dill_cr *cr;
/* List of clauses coroutine is waiting for. See dill_cr::clauses. */
struct dill_qlist item;
struct dill_slist item;
/* These two fields are completely opaque to the coroutine. They are meant
to be used by endpoints. The only thing coroutine does with it is, just
before dill_wait() exits, it removes 'epitem' from 'eplist' for each
Expand Down
86 changes: 62 additions & 24 deletions qlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,85 @@
*/

#ifndef DILL_QLIST_INCLUDED
#define DILL_QLIST_INCLUDED
#ifndef DILL_SLIST_INCLUDED
#define DILL_SLIST_INCLUDED

#include <stddef.h>
#include "utils.h"

/* Singly-linked list. Works in LIFO manner, so it's actually a stack.
However, not to confuse it with C language stack, let's call it a slist. */
/* Singly-linked list. Having both push and push_back functions means that
it can be used both as a queue and as a stack. */

struct dill_qlist_item {
struct dill_qlist_item *next;
};

struct dill_qlist {
struct dill_qlist *next;
struct dill_qlist_item *first;
struct dill_qlist_item *last;
};

/* Initialise the list. */
static inline void dill_qlist_init(struct dill_qlist *self) {
self->next = self;
/* After removing item from a list, next points here. */
extern struct dill_qlist_item dill_qlist_item_none;

/* Initialise a list item. */
static inline void dill_qlist_item_init(struct dill_qlist_item *self) {
self->next = &dill_qlist_item_none;
}

/* True is the list has no items. */
static inline int dill_qlist_empty(struct dill_qlist *self) {
return self->next == self;
/* Returns 1 if the item is part of a list. 0 otherwise. */
static inline int dill_qlist_item_inlist(struct dill_qlist_item *self) {
return self->next == &dill_qlist_item_none ? 0 : 1;
}

/* Returns next item in the list. If 'it' is the list itself, it returns the
first element in the list. If there are no more elements in the list
returns pointer to the list itself. */
static inline struct dill_qlist *dill_qlist_next(struct dill_qlist *it) {
return it->next;
/* Initialise the list. To statically initialise the list use = {0}. */
static inline void dill_qlist_init(struct dill_qlist *self) {
self->first = NULL;
self->last = NULL;
}

/* True is the list has no items. */
#define dill_qlist_empty(self) (!((self)->first))

/* Returns iterator to the first item in the list or NULL if
the list is empty. */
#define dill_qlist_begin(self) ((self)->first)

/* Returns iterator to one past the item pointed to by 'it'.
If there are no more items returns NULL. */
#define dill_qlist_next(it) ((it)->next)

/* Push the item to the beginning of the list. */
static inline void dill_qlist_push(struct dill_qlist *self,
struct dill_qlist *item) {
item->next = self->next;
self->next = item;
static inline void dill_qlist_push(struct dill_qlist *self, struct dill_qlist_item *item) {
dill_assert(!dill_qlist_item_inlist(item));
item->next = self->first;
self->first = item;
if(!self->last)
self->last = item;
}

/* Push the item to the end of the list. */
static inline void dill_qlist_push_back(struct dill_qlist *self,
struct dill_qlist_item *item) {
dill_assert(!dill_qlist_item_inlist(item));
item->next = NULL;
if(!self->last)
self->first = item;
else
self->last->next = item;
self->last = item;
}

/* Pop an item from the beginning of the list. */
static inline struct dill_qlist *dill_qlist_pop(struct dill_qlist *self) {
struct dill_qlist *item = self->next;
self->next = item->next;
return item;
static inline struct dill_qlist_item *dill_qlist_pop(struct dill_qlist *self) {
if(!self->first)
return NULL;
struct dill_qlist_item *it = self->first;
self->first = self->first->next;
if(!self->first)
self->last = NULL;
it->next = &dill_qlist_item_none;
return it;
}

#endif
Expand Down
4 changes: 2 additions & 2 deletions slist.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <stddef.h>

#include "slist.h"
#include "qlist.h"

/* After removing item from a list, next points here. */
struct dill_slist_item dill_slist_item_none = {NULL};
struct dill_qlist_item dill_qlist_item_none = {NULL};
Loading

0 comments on commit 20e449d

Please sign in to comment.