diff --git a/coio.c b/coio.c index b50bad3..ad42fb3 100644 --- a/coio.c +++ b/coio.c @@ -6,8 +6,8 @@ static coro_context _sched_ctx; static unsigned long _taskcount = 0; -CoioTaskList coio_ready = {0, 0}; -CoioTask *coio_current; +CoioTaskList coio_ready = {0, 0}; +CoioTask *coio_current; int coio_main() @@ -24,28 +24,24 @@ coio_main() coio_del(&coio_ready, coio_current); coro_transfer(&_sched_ctx, &coio_current->ctx); - if (coio_current->done) - { + if (coio_current->done) { _taskcount--; coro_stack_free(&coio_current->stk); free(coio_current); } - coio_current = NULL; } - if (_taskcount) - { + if (_taskcount) { return -1; } - return 0; } static void _coio_entry(void *arg) { - CoioTask *task = (CoioTask *)arg; + CoioTask *task = (CoioTask *) arg; task->func(task->arg); @@ -54,20 +50,18 @@ _coio_entry(void *arg) } int -coio_create(void (*f)(void *arg), void *arg, unsigned int stacksize) +coio_create(coio_func f, void *arg, unsigned int stacksize) { - CoioTask *task; + CoioTask *task; task = calloc(1, sizeof(*task)); if (!task) return -1; - if(!coro_stack_alloc(&task->stk, stacksize / sizeof(void *))) - { + if (!coro_stack_alloc(&task->stk, stacksize / sizeof(void *))) { free(task); return -1; } - task->func = f; task->arg = arg; @@ -87,40 +81,31 @@ coio_yield() } void -coio_add(CoioTaskList *lst, CoioTask *task) +coio_add(CoioTaskList * lst, CoioTask * task) { - if (lst->tail) - { + if (lst->tail) { lst->tail->next = task; task->prev = lst->tail; - } - else - { + } else { lst->head = task; task->prev = NULL; } lst->tail = task; - task->next = NULL; + task->next = NULL; } void -coio_del(CoioTaskList *lst, CoioTask *task) +coio_del(CoioTaskList * lst, CoioTask * task) { - if (task->prev) - { + if (task->prev) { task->prev->next = task->next; - } - else - { + } else { lst->head = task->next; } - if (task->next) - { + if (task->next) { task->next->prev = task->prev; - } - else - { + } else { lst->tail = task->prev; } } diff --git a/coio.h b/coio.h index c90cf95..02f6b62 100644 --- a/coio.h +++ b/coio.h @@ -2,14 +2,15 @@ #define COIO_H #ifdef __cplusplus -extern "C" { +extern "C" { #endif -typedef struct CoioTask CoioTask; + typedef struct CoioTask CoioTask; + typedef void (*coio_func) (void *arg); -int coio_main(); -int coio_create(void (*f)(void *arg), void *arg, unsigned int stacksize); -void coio_yield(); + int coio_main (); + int coio_create(void (*f) (void *arg), void *arg, unsigned int stacksize); + void coio_yield(); #ifdef __cplusplus } diff --git a/coioimpl.h b/coioimpl.h index 757a631..1c0d88b 100644 --- a/coioimpl.h +++ b/coioimpl.h @@ -7,25 +7,25 @@ typedef struct CoioTaskList CoioTaskList; struct CoioTask { - coro_context ctx; + coro_context ctx; struct coro_stack stk; - coro_func func; - void *arg; + coio_func func; + void *arg; - int done; + int done; /* linked list support */ - CoioTask *next; - CoioTask *prev; + CoioTask *next; + CoioTask *prev; }; struct CoioTaskList { - CoioTask *head; - CoioTask *tail; + CoioTask *head; + CoioTask *tail; }; -void coio_add(CoioTaskList *lst, CoioTask *task); -void coio_del(CoioTaskList *lst, CoioTask *task); +void coio_add (CoioTaskList * lst, CoioTask * task); +void coio_del (CoioTaskList * lst, CoioTask * task); #endif diff --git a/testyield.c b/testyield.c index 86c89d2..3a64f5b 100644 --- a/testyield.c +++ b/testyield.c @@ -1,33 +1,34 @@ #include #include "coio.h" -void _t1(void *arg) +void +_t1(void *arg) { printf("Hello 1 from _t1\n"); coio_yield(); printf("Hello 2 from _t1\n"); } -void _t2(void *arg) +void +_t2(void *arg) { printf("Hello 1 from _t2\n"); coio_yield(); printf("Hello 2 from _t2\n"); } -int main(int argc, char **argv) +int +main(int argc, char **argv) { - (void)argc; - (void)argv; + (void) argc; + (void) argv; coio_create(_t1, NULL, 0x8000); coio_create(_t2, NULL, 0x8000); - if (coio_main() < 0) - { + if (coio_main() < 0) { printf("Deadlocked\n"); return 1; } - return 0; }