odyssey/lib/ft_fiber.h

96 lines
1.6 KiB
C
Raw Normal View History

2016-11-08 12:03:58 +00:00
#ifndef FT_FIBER_H_
#define FT_FIBER_H_
/*
2016-11-08 14:53:52 +00:00
* flint.
2016-11-08 12:03:58 +00:00
*
* Cooperative multitasking engine.
*/
2016-11-17 10:33:19 +00:00
typedef struct ftfiberop ftfiberop;
2016-11-08 12:03:58 +00:00
typedef struct ftfiber ftfiber;
typedef enum {
FT_FNEW,
FT_FREADY,
FT_FACTIVE,
FT_FFREE
} ftfiberstate;
typedef void (*ftfiberf)(void *arg);
2016-11-17 10:33:19 +00:00
typedef void (*ftfibercancelf)(ftfiber*, void *arg);
struct ftfiberop {
int in_progress;
ftfibercancelf cancel;
void *arg;
};
2016-11-08 12:03:58 +00:00
struct ftfiber {
uint64_t id;
ftfiberstate state;
ftfiberf function;
2016-11-17 10:33:19 +00:00
ftfiberop op;
int cancel;
2016-11-08 12:03:58 +00:00
void *arg;
ftcontext context;
ftfiber *resume;
uv_timer_t timer;
void *scheduler;
void *data;
2016-11-17 10:03:42 +00:00
ftlist waiters;
ftlist link_wait;
2016-11-08 12:03:58 +00:00
ftlist link;
};
void ft_fiber_init(ftfiber*);
2016-11-17 10:33:19 +00:00
static inline int
ft_fiber_is_cancel(ftfiber *fiber) {
return fiber->cancel;
}
2016-11-08 12:03:58 +00:00
static inline char*
ft_fiber_stackof(ftfiber *fiber) {
return (char*)fiber + sizeof(ftfiber);
}
ftfiber*
ft_fiber_alloc(int);
void
ft_fiber_free(ftfiber*);
2016-11-17 10:33:19 +00:00
static inline void
ft_fiber_opbegin(ftfiber *fiber, ftfibercancelf cancel, void *arg)
{
ftfiberop *op = &fiber->op;
op->in_progress = 1;
op->cancel = cancel;
op->arg = arg;
}
static inline void
ft_fiber_opfinish(ftfiber *fiber)
{
ftfiberop *op = &fiber->op;
op->in_progress = 0;
op->cancel = NULL;
op->arg = NULL;
}
static inline void
ft_fiber_opcancel(ftfiber *fiber)
{
ftfiberop *op = &fiber->op;
if (fiber->cancel)
return;
fiber->cancel++;
if (! op->in_progress)
return;
assert(op->cancel);
op->cancel(fiber, op->arg);
}
2016-11-08 12:03:58 +00:00
#endif