Skip to content

Commit

Permalink
chan.c and cr.c use dill_ prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Apr 16, 2018
1 parent a13b1d1 commit a11c572
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
48 changes: 26 additions & 22 deletions chan.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@

#include "cr.h"
#include "ctx.h"
#include "libdillimpl.h"
#include "list.h"
#include "utils.h"

#define DILL_DISABLE_RAW_NAMES
#include "libdillimpl.h"

struct dill_halfchan {
/* Table of virtual functions. */
struct hvfs vfs;
struct dill_hvfs vfs;
/* List of clauses wanting to receive from the inbound halfchannel. */
struct dill_list in;
/* List of clauses wanting to send to the inbound halfchannel. */
Expand All @@ -60,16 +62,17 @@ struct dill_chclause {
size_t len;
};

DILL_CT_ASSERT(sizeof(struct chstorage) >= sizeof(struct dill_halfchan) * 2);
DILL_CT_ASSERT(sizeof(struct dill_chstorage) >=
sizeof(struct dill_halfchan) * 2);

/******************************************************************************/
/* Handle implementation. */
/******************************************************************************/

static const int dill_halfchan_type_placeholder = 0;
const void *dill_halfchan_type = &dill_halfchan_type_placeholder;
static void *dill_halfchan_query(struct hvfs *vfs, const void *type);
static void dill_halfchan_close(struct hvfs *vfs);
static void *dill_halfchan_query(struct dill_hvfs *vfs, const void *type);
static void dill_halfchan_close(struct dill_hvfs *vfs);

/******************************************************************************/
/* Helpers. */
Expand All @@ -93,7 +96,7 @@ static void dill_halfchan_init(struct dill_halfchan *ch, int index) {
ch->closed = 0;
}

int dill_chmake_mem(struct chstorage *mem, int chv[2]) {
int dill_chmake_mem(struct dill_chstorage *mem, int chv[2]) {
int err;
if(dill_slow(!mem)) {err = EINVAL; goto error1;}
/* Returns ECANCELED if the coroutine is shutting down. */
Expand All @@ -102,25 +105,25 @@ int dill_chmake_mem(struct chstorage *mem, int chv[2]) {
struct dill_halfchan *ch = (struct dill_halfchan*)mem;
dill_halfchan_init(&ch[0], 0);
dill_halfchan_init(&ch[1], 1);
chv[0] = hmake(&ch[0].vfs);
chv[0] = dill_hmake(&ch[0].vfs);
if(dill_slow(chv[0] < 0)) {err = errno; goto error1;}
chv[1] = hmake(&ch[1].vfs);
chv[1] = dill_hmake(&ch[1].vfs);
if(dill_slow(chv[1] < 0)) {err = errno; goto error2;}
return 0;
error2:
/* This closes the handle but leaves everything else alone given
that the second handle wasn't event created. */
hclose(chv[0]);
dill_hclose(chv[0]);
error1:
errno = err;
return -1;
}

int dill_chmake(int chv[2]) {
int err;
struct chstorage *ch = malloc(sizeof(struct chstorage));
struct dill_chstorage *ch = malloc(sizeof(struct dill_chstorage));
if(dill_slow(!ch)) {err = ENOMEM; goto error1;}
int h = chmake_mem(ch, chv);
int h = dill_chmake_mem(ch, chv);
if(dill_slow(h < 0)) {err = errno; goto error2;}
((struct dill_halfchan*)ch)[0].mem = 0;
((struct dill_halfchan*)ch)[1].mem = 0;
Expand All @@ -132,7 +135,7 @@ int dill_chmake(int chv[2]) {
return -1;
}

static void *dill_halfchan_query(struct hvfs *vfs, const void *type) {
static void *dill_halfchan_query(struct dill_hvfs *vfs, const void *type) {
if(dill_fast(type == dill_halfchan_type)) return vfs;
errno = ENOTSUP;
return NULL;
Expand All @@ -153,7 +156,7 @@ static void dill_halfchan_term(struct dill_halfchan *ch) {
}
}

static void dill_halfchan_close(struct hvfs *vfs) {
static void dill_halfchan_close(struct dill_hvfs *vfs) {
struct dill_halfchan *ch = (struct dill_halfchan*)vfs;
dill_assert(ch && !ch->closed);
/* If the other half of the channel is still open do nothing. */
Expand All @@ -180,7 +183,7 @@ int dill_chsend(int h, const void *val, size_t len, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* Get the channel interface. */
struct dill_halfchan *ch = hquery(h, dill_halfchan_type);
struct dill_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) return -1;
/* Sending is always done to the opposite side of the channel. */
ch = dill_halfchan_other(ch);
Expand Down Expand Up @@ -220,7 +223,7 @@ int dill_chrecv(int h, void *val, size_t len, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
/* Get the channel interface. */
struct dill_halfchan *ch = hquery(h, dill_halfchan_type);
struct dill_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) return -1;
/* Check whether the channel is done. */
if(dill_slow(ch->done)) {errno = EPIPE; return -1;}
Expand Down Expand Up @@ -256,7 +259,7 @@ int dill_chrecv(int h, void *val, size_t len, int64_t deadline) {
}

int dill_chdone(int h) {
struct dill_halfchan *ch = hquery(h, dill_halfchan_type);
struct dill_halfchan *ch = dill_hquery(h, dill_halfchan_type);
if(dill_slow(!ch)) return -1;
/* Done is always done to the opposite side of the channel. */
ch = dill_halfchan_other(ch);
Expand Down Expand Up @@ -285,12 +288,12 @@ int dill_choose(struct chclause *clauses, int nclauses, int64_t deadline) {
int i;
for(i = 0; i != nclauses; ++i) {
struct chclause *cl = &clauses[i];
struct dill_halfchan *ch = hquery(cl->ch, dill_halfchan_type);
struct dill_halfchan *ch = dill_hquery(cl->ch, dill_halfchan_type);
if(dill_slow(!ch)) return i;
if(dill_slow(cl->len > 0 && !cl->val)) {errno = EINVAL; return i;}
struct dill_chclause *chcl;
switch(cl->op) {
case CHSEND:
case DILL_CHSEND:
ch = dill_halfchan_other(ch);
if(dill_slow(ch->done)) {errno = EPIPE; return i;}
if(dill_list_empty(&ch->in)) break;
Expand All @@ -305,7 +308,7 @@ int dill_choose(struct chclause *clauses, int nclauses, int64_t deadline) {
dill_trigger(&chcl->cl, 0);
errno = 0;
return i;
case CHRECV:
case DILL_CHRECV:
if(dill_slow(ch->done)) {errno = EPIPE; return i;}
if(dill_list_empty(&ch->out)) break;
chcl = dill_cont(dill_list_next(&ch->out),
Expand All @@ -329,10 +332,11 @@ int dill_choose(struct chclause *clauses, int nclauses, int64_t deadline) {
/* Let's wait. */
struct dill_chclause chcls[nclauses];
for(i = 0; i != nclauses; ++i) {
struct dill_halfchan *ch = hquery(clauses[i].ch, dill_halfchan_type);
struct dill_halfchan *ch = dill_hquery(clauses[i].ch,
dill_halfchan_type);
dill_assert(ch);
dill_list_insert(&chcls[i].item,
clauses[i].op == CHRECV ? &ch->in : &dill_halfchan_other(ch)->out);
dill_list_insert(&chcls[i].item, clauses[i].op == DILL_CHRECV ?
&ch->in : &dill_halfchan_other(ch)->out);
chcls[i].val = clauses[i].val;
chcls[i].len = clauses[i].len;
dill_waitfor(&chcls[i].cl, i, dill_chcancel);
Expand Down
45 changes: 23 additions & 22 deletions cr.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ volatile void *dill_unoptimisable = NULL;

static const int dill_cr_type_placeholder = 0;
static const void *dill_cr_type = &dill_cr_type_placeholder;
static void *dill_cr_query(struct hvfs *vfs, const void *type);
static void dill_cr_close(struct hvfs *vfs);
static void *dill_cr_query(struct dill_hvfs *vfs, const void *type);
static void dill_cr_close(struct dill_hvfs *vfs);

/******************************************************************************/
/* Bundle. */
/******************************************************************************/

static const int dill_bundle_type_placeholder = 0;
const void *dill_bundle_type = &dill_bundle_type_placeholder;
static void *dill_bundle_query(struct hvfs *vfs, const void *type);
static void dill_bundle_close(struct hvfs *vfs);
static void *dill_bundle_query(struct dill_hvfs *vfs, const void *type);
static void dill_bundle_close(struct dill_hvfs *vfs);

struct dill_bundle {
/* Table of virtual functions. */
struct hvfs vfs;
struct dill_hvfs vfs;
/* List of coroutines in this bundle. */
struct dill_list crs;
/* If somebody is doing hdone() on this bundle, here's the clause
Expand All @@ -81,9 +81,10 @@ struct dill_bundle {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct bundle_storage) >= sizeof(struct dill_bundle));
DILL_CT_ASSERT(sizeof(struct dill_bundle_storage) >=
sizeof(struct dill_bundle));

int dill_bundle_mem(struct bundle_storage *mem) {
int dill_bundle_mem(struct dill_bundle_storage *mem) {
int err;
if(dill_slow(!mem)) {err = EINVAL; return -1;}
/* Returns ECANCELED if the coroutine is shutting down. */
Expand All @@ -95,14 +96,14 @@ int dill_bundle_mem(struct bundle_storage *mem) {
dill_list_init(&b->crs);
b->waiter = NULL;
b->mem = 1;
return hmake(&b->vfs);
return dill_hmake(&b->vfs);
}

int dill_bundle(void) {
int err;
struct dill_bundle *b = malloc(sizeof(struct dill_bundle));
if(dill_slow(!b)) {err = ENOMEM; goto error1;}
int h = bundle_mem((struct bundle_storage*)b);
int h = dill_bundle_mem((struct dill_bundle_storage*)b);
if(dill_slow(h < 0)) {err = errno; goto error2;}
b->mem = 0;
return h;
Expand All @@ -113,13 +114,13 @@ int dill_bundle(void) {
return -1;
}

static void *dill_bundle_query(struct hvfs *vfs, const void *type) {
static void *dill_bundle_query(struct dill_hvfs *vfs, const void *type) {
if(dill_fast(type == dill_bundle_type)) return vfs;
errno = ENOTSUP;
return NULL;
}

static void dill_bundle_close(struct hvfs *vfs) {
static void dill_bundle_close(struct dill_hvfs *vfs) {
struct dill_bundle *self = (struct dill_bundle*)vfs;
struct dill_list *it = &self->crs;
for(it = self->crs.next; it != &self->crs; it = dill_list_next(it)) {
Expand All @@ -132,7 +133,7 @@ static void dill_bundle_close(struct hvfs *vfs) {
int dill_bundle_wait(int h, int64_t deadline) {
int rc = dill_canblock();
if(dill_slow(rc < 0)) return -1;
struct dill_bundle *self = hquery(h, dill_bundle_type);
struct dill_bundle *self = dill_hquery(h, dill_bundle_type);
if(dill_slow(!self)) return -1;
/* If there are no coroutines in the bundle succeed immediately. */
if(dill_list_empty(&self->crs)) return 0;
Expand Down Expand Up @@ -252,16 +253,16 @@ int dill_prologue(sigjmp_buf **jb, void **ptr, size_t len, int bndl,
int new_bundle = bndl < 0;
if(new_bundle) {
if(*ptr) {
bndl = bundle_mem(*ptr);
*ptr = ((uint8_t*)*ptr) + sizeof(struct bundle_storage);
len -= sizeof(struct bundle_storage);
bndl = dill_bundle_mem(*ptr);
*ptr = ((uint8_t*)*ptr) + sizeof(struct dill_bundle_storage);
len -= sizeof(struct dill_bundle_storage);
}
else {
bndl = bundle();
bndl = dill_bundle();
}
if(dill_slow(bndl < 0)) {err = errno; goto error1;}
}
struct dill_bundle *bundle = hquery(bndl, dill_bundle_type);
struct dill_bundle *bundle = dill_hquery(bndl, dill_bundle_type);
if(dill_slow(!bundle)) {err = errno; goto error2;}
/* Allocate a stack. */
struct dill_cr *cr;
Expand Down Expand Up @@ -335,7 +336,7 @@ int dill_prologue(sigjmp_buf **jb, void **ptr, size_t len, int bndl,
return new_bundle ? bndl : 0;
error2:
if(new_bundle) {
rc = hclose(bndl);
rc = dill_hclose(bndl);
dill_assert(rc == 0);
}
error1:
Expand Down Expand Up @@ -369,15 +370,15 @@ void dill_epilogue(void) {
dill_wait();
}

static void *dill_cr_query(struct hvfs *vfs, const void *type) {
static void *dill_cr_query(struct dill_hvfs *vfs, const void *type) {
struct dill_cr *cr = dill_cont(vfs, struct dill_cr, vfs);
if(dill_fast(type == dill_cr_type)) return cr;
errno = ENOTSUP;
return NULL;
}

/* Gets called when coroutine handle is closed. */
static void dill_cr_close(struct hvfs *vfs) {
static void dill_cr_close(struct dill_hvfs *vfs) {
struct dill_ctx_cr *ctx = &dill_getctx->cr;
struct dill_cr *cr = dill_cont(vfs, struct dill_cr, vfs);
/* If the coroutine has already finished, we are done. */
Expand Down Expand Up @@ -446,7 +447,7 @@ int dill_wait(void) {
/* For performance reasons, we want to avoid excessive checking of current
time, so we cache the value here. It will be recomputed only after
a blocking call. */
int64_t nw = now();
int64_t nw = dill_now();
/* Wait for timeouts and external events. However, if there are ready
coroutines there's no need to poll for external events every time.
Still, we'll do it at least once a second. The external signal may
Expand All @@ -469,7 +470,7 @@ int dill_wait(void) {
}
/* Wait for events. */
int fired = dill_pollset_poll(timeout);
if(timeout != 0) nw = now();
if(timeout != 0) nw = dill_now();
if(dill_slow(fired < 0)) continue;
/* Fire all expired timers. */
if(!dill_rbtree_empty(&ctx->timers)) {
Expand Down
6 changes: 4 additions & 2 deletions cr.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

#include <stdint.h>

#include "libdillimpl.h"
#include "list.h"
#include "qlist.h"
#include "rbtree.h"
#include "slist.h"

#define DILL_DISABLE_RAW_NAMES
#include "libdillimpl.h"

/* The coroutine. The memory layout looks like this:
+-------------------------------------------------------------+---------+
| stack | dill_cr |
Expand All @@ -46,7 +48,7 @@ struct dill_cr {
will be set to 'err'. */
struct dill_slist ready;
/* Virtual function table. */
struct hvfs vfs;
struct dill_hvfs vfs;
int id;
int err;
/* When the coroutine is suspended 'ctx' holds the context
Expand Down
6 changes: 3 additions & 3 deletions handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

struct dill_handle {
/* Table of virtual functions. */
struct hvfs *vfs;
struct dill_hvfs *vfs;
/* Index of the next handle in the linked list of unused handles. -1 means
'the end of the list'. -2 means 'handle is in use'. */
int next;
Expand Down Expand Up @@ -67,7 +67,7 @@ void dill_ctx_handle_term(struct dill_ctx_handle *ctx) {
free(ctx->handles);
}

int dill_hmake(struct hvfs *vfs) {
int dill_hmake(struct dill_hvfs *vfs) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
if(dill_slow(!vfs || !vfs->query || !vfs->close)) {
errno = EINVAL; return -1;}
Expand Down Expand Up @@ -110,7 +110,7 @@ int dill_hown(int h) {
struct dill_ctx_handle *ctx = &dill_getctx->handle;
DILL_CHECKHANDLE(h, -1);
/* Create a new handle for the same object. */
int res = hmake(hndl->vfs);
int res = dill_hmake(hndl->vfs);
if(dill_slow(res < 0)) return -1;
/* Return a handle to the shared pool. */
hndl->ptr = NULL;
Expand Down

0 comments on commit a11c572

Please sign in to comment.