2015-03-25 21:02:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Moritz Bitsch <moritzbitsch@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2015-03-21 11:47:51 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "coioimpl.h"
|
|
|
|
#include "coro.h"
|
|
|
|
|
|
|
|
static coro_context _sched_ctx;
|
|
|
|
static unsigned long _taskcount = 0;
|
|
|
|
|
2015-03-25 20:16:49 +00:00
|
|
|
CoioTaskList coio_ready = {0, 0};
|
|
|
|
CoioTask *coio_current;
|
2015-03-21 11:47:51 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
coio_main()
|
|
|
|
{
|
|
|
|
/* initialize empty ctx for scheduler */
|
|
|
|
coro_create(&_sched_ctx, NULL, NULL, NULL, 0);
|
|
|
|
|
|
|
|
/* scheduler mainloop */
|
|
|
|
for (;;) {
|
|
|
|
if (!coio_ready.head)
|
|
|
|
break;
|
|
|
|
|
|
|
|
coio_current = coio_ready.head;
|
|
|
|
coio_del(&coio_ready, coio_current);
|
|
|
|
coro_transfer(&_sched_ctx, &coio_current->ctx);
|
|
|
|
|
2015-03-25 20:16:49 +00:00
|
|
|
if (coio_current->done) {
|
2015-03-21 11:47:51 +00:00
|
|
|
_taskcount--;
|
|
|
|
coro_stack_free(&coio_current->stk);
|
|
|
|
free(coio_current);
|
|
|
|
}
|
|
|
|
coio_current = NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:16:49 +00:00
|
|
|
if (_taskcount) {
|
2015-03-21 11:47:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_coio_entry(void *arg)
|
|
|
|
{
|
2015-03-25 20:16:49 +00:00
|
|
|
CoioTask *task = (CoioTask *) arg;
|
2015-03-21 11:47:51 +00:00
|
|
|
|
|
|
|
task->func(task->arg);
|
|
|
|
|
|
|
|
task->done = 1;
|
|
|
|
coro_transfer(&coio_current->ctx, &_sched_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-03-25 20:16:49 +00:00
|
|
|
coio_create(coio_func f, void *arg, unsigned int stacksize)
|
2015-03-21 11:47:51 +00:00
|
|
|
{
|
2015-03-25 20:16:49 +00:00
|
|
|
CoioTask *task;
|
2015-03-21 11:47:51 +00:00
|
|
|
|
|
|
|
task = calloc(1, sizeof(*task));
|
|
|
|
if (!task)
|
|
|
|
return -1;
|
|
|
|
|
2015-03-25 20:16:49 +00:00
|
|
|
if (!coro_stack_alloc(&task->stk, stacksize / sizeof(void *))) {
|
2015-03-21 11:47:51 +00:00
|
|
|
free(task);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
task->func = f;
|
|
|
|
task->arg = arg;
|
|
|
|
|
|
|
|
coro_create(&task->ctx, _coio_entry, task, task->stk.sptr, task->stk.ssze);
|
|
|
|
|
|
|
|
coio_add(&coio_ready, task);
|
|
|
|
_taskcount++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
coio_yield()
|
|
|
|
{
|
|
|
|
coio_add(&coio_ready, coio_current);
|
|
|
|
coro_transfer(&coio_current->ctx, &_sched_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-25 20:16:49 +00:00
|
|
|
coio_add(CoioTaskList * lst, CoioTask * task)
|
2015-03-21 11:47:51 +00:00
|
|
|
{
|
2015-03-25 20:16:49 +00:00
|
|
|
if (lst->tail) {
|
2015-03-21 11:47:51 +00:00
|
|
|
lst->tail->next = task;
|
|
|
|
task->prev = lst->tail;
|
2015-03-25 20:16:49 +00:00
|
|
|
} else {
|
2015-03-21 11:47:51 +00:00
|
|
|
lst->head = task;
|
|
|
|
task->prev = NULL;
|
|
|
|
}
|
|
|
|
lst->tail = task;
|
2015-03-25 20:16:49 +00:00
|
|
|
task->next = NULL;
|
2015-03-21 11:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-25 20:16:49 +00:00
|
|
|
coio_del(CoioTaskList * lst, CoioTask * task)
|
2015-03-21 11:47:51 +00:00
|
|
|
{
|
2015-03-25 20:16:49 +00:00
|
|
|
if (task->prev) {
|
2015-03-21 11:47:51 +00:00
|
|
|
task->prev->next = task->next;
|
2015-03-25 20:16:49 +00:00
|
|
|
} else {
|
2015-03-21 11:47:51 +00:00
|
|
|
lst->head = task->next;
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:16:49 +00:00
|
|
|
if (task->next) {
|
2015-03-21 11:47:51 +00:00
|
|
|
task->next->prev = task->prev;
|
2015-03-25 20:16:49 +00:00
|
|
|
} else {
|
2015-03-21 11:47:51 +00:00
|
|
|
lst->tail = task->prev;
|
|
|
|
}
|
|
|
|
}
|