Merge branch 'glib-integration'

Add glib integration
This commit is contained in:
Moritz Bitsch 2018-07-27 10:48:17 +02:00
commit be9a95fcf2
9 changed files with 296 additions and 65 deletions

20
CMakeLists.txt Normal file
View file

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.7)
project(coio)
add_library(${PROJECT_NAME}
coro.c
coio.c
coio_glib.c)
add_custom_target(${PROJECT_NAME}_files SOURCES coioimpl.h)
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
target_compile_options(${PROJECT_NAME} PRIVATE -W -Wall -Wextra -Werror)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB2 REQUIRED glib-2.0)
target_include_directories(${PROJECT_NAME} PUBLIC ${GLIB2_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLIB2_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC .)

View file

@ -2,14 +2,15 @@ LIB = libcoio.a
OBJS = \ OBJS = \
coro.o \ coro.o \
coio.o coio.o \
coio_glib.o
all: $(LIB) all: $(LIB)
$(OBJS): coio.h coro.h $(OBJS): coio.h coro.h
.c.o: .c.o:
$(CC) $(CFLAGS) -W -Wall -Wextra -Werror -c $*.c $(CC) $(shell pkg-config --cflags glib-2.0) $(CFLAGS) -W -Wall -Wextra -Werror -c $*.c
$(LIB): $(OBJS) $(LIB): $(OBJS)
$(AR) rvc $(LIB) $? $(AR) rvc $(LIB) $?

125
coio.c
View file

@ -13,8 +13,12 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include <stdio.h>
#include "coioimpl.h" #include "coioimpl.h"
#include "coro.h" #include "coro.h"
@ -24,19 +28,33 @@
#include <mach/mach.h> #include <mach/mach.h>
#endif #endif
static coro_context _sched_ctx; CoioTaskList coio_ready_list = {0, 0};
static unsigned long _taskcount = 0;
CoioTaskList coio_ready = {0, 0};
CoioTaskList coio_sleeping = {0, 0}; CoioTaskList coio_sleeping = {0, 0};
CoioTask *coio_current; coro_context coio_sched_ctx;
CoioTask* coio_current;
unsigned long coio_taskcount = 0;
static int msleep(uvlong ms)
{
struct timespec req, rem;
if (ms > 999) {
req.tv_sec = (int)(ms / 1000);
req.tv_nsec = (ms - ((long)req.tv_sec * 1000)) * 1000000;
} else {
req.tv_sec = 0;
req.tv_nsec = ms * 1000000;
}
return nanosleep(&req, &rem);
}
static void static void
_process_events() _process_events()
{ {
uvlong now; uvlong now;
int ms = 5; int ms = 5;
CoioTask *t; CoioTask* t;
if ((t = coio_sleeping.head) != NULL && t->timeout != 0) { if ((t = coio_sleeping.head) != NULL && t->timeout != 0) {
now = coio_now(); now = coio_now();
@ -47,14 +65,14 @@ _process_events()
} }
} }
/* TODO:do I/O polling instead of usleep */ /* TODO:do I/O polling instead of usleep */
usleep(ms * 1000); msleep(ms);
/* handle CLOCK_MONOTONIC bugs (VirtualBox anyone?) */ /* handle CLOCK_MONOTONIC bugs (VirtualBox anyone?) */
while (!coio_ready.head) { while (!coio_ready_list.head) {
/* wake up timed out tasks */ /* wake up timed out tasks */
now = coio_now(); now = coio_now();
while ((t = coio_sleeping.head) && now >= t->timeout) { while ((t = coio_sleeping.head) && t->timeout && now >= t->timeout) {
coio_rdy(t); coio_ready(t);
} }
} }
} }
@ -63,78 +81,83 @@ int
coio_main() coio_main()
{ {
/* initialize empty ctx for scheduler */ /* initialize empty ctx for scheduler */
coro_create(&_sched_ctx, NULL, NULL, NULL, 0); coro_create(&coio_sched_ctx, NULL, NULL, NULL, 0);
/* scheduler mainloop */ /* scheduler mainloop */
for (;;) { for (;;) {
if (!coio_ready.head && coio_sleeping.head) if (!coio_ready_list.head && coio_sleeping.head)
_process_events(); _process_events();
if (!coio_ready.head) if (!coio_ready_list.head)
break; break;
coio_current = coio_ready.head; coio_current = coio_ready_list.head;
coio_del(&coio_ready, coio_current); coio_current->ready = 0;
coro_transfer(&_sched_ctx, &coio_current->ctx); coio_del(&coio_ready_list, coio_current);
coro_transfer(&coio_sched_ctx, &coio_current->ctx);
if (coio_current->done) { if (coio_current->done) {
_taskcount--; coio_taskcount--;
coro_stack_free(&coio_current->stk); coro_stack_free(&coio_current->stk);
free(coio_current); free(coio_current);
} }
coio_current = NULL; coio_current = NULL;
} }
if (_taskcount) { if (coio_taskcount) {
return -1; return -1;
} }
return 0; return 0;
} }
static void static void
_coio_entry(void *arg) _coio_entry(void* arg)
{ {
CoioTask *task = (CoioTask *) arg; CoioTask* task = (CoioTask*) arg;
task->func(task->arg); task->func(task->arg);
task->done = 1; task->done = 1;
coro_transfer(&coio_current->ctx, &_sched_ctx); coro_transfer(&coio_current->ctx, &coio_sched_ctx);
} }
int int
coio_create(coio_func f, void *arg, unsigned int stacksize) coio_create(const char* name, coio_func f, void* arg, unsigned int stacksize)
{ {
CoioTask *task; CoioTask* task;
task = calloc(1, sizeof(*task)); task = calloc(1, sizeof(*task));
if (!task) if (!task)
return -1; return -1;
if (!coro_stack_alloc(&task->stk, stacksize / sizeof(void *))) { if (!coro_stack_alloc(&task->stk, stacksize / sizeof(void*))) {
free(task); free(task);
return -1; return -1;
} }
task->name = name;
task->func = f; task->func = f;
task->arg = arg; task->arg = arg;
coro_create(&task->ctx, _coio_entry, task, task->stk.sptr, task->stk.ssze); coro_create(&task->ctx, _coio_entry, task, task->stk.sptr, task->stk.ssze);
coio_add(&coio_ready, task); coio_add(&coio_ready_list, task);
_taskcount++; coio_taskcount++;
return 0; return 0;
} }
uvlong uvlong
coio_timeout(CoioTask * task, int ms) coio_timeout(CoioTask* task, int ms)
{ {
CoioTask *t; CoioTask* t;
if (ms > 0) if (ms >= 0) {
task->timeout = coio_now() + (ms * 1000000); task->timeout = coio_now() + (ms * 1000000);
for (t = coio_sleeping.head; t != NULL && t->timeout && t->timeout < task->timeout; t = t->next);
for (t = coio_sleeping.head; t != NULL && t->timeout && t->timeout < task->timeout; t = t->next); } else {
task->timeout = 0;
t = NULL;
}
if (t) { if (t) {
task->prev = t->prev; task->prev = t->prev;
@ -173,12 +196,12 @@ coio_delay(int ms)
void void
coio_yield() coio_yield()
{ {
coio_rdy(coio_current); coio_ready(coio_current);
coio_transfer(); coio_transfer();
} }
void void
coio_add(CoioTaskList * lst, CoioTask * task) coio_add(CoioTaskList* lst, CoioTask* task)
{ {
if (lst->tail) { if (lst->tail) {
lst->tail->next = task; lst->tail->next = task;
@ -192,33 +215,36 @@ coio_add(CoioTaskList * lst, CoioTask * task)
} }
void void
coio_del(CoioTaskList * lst, CoioTask * task) coio_del(CoioTaskList* lst, CoioTask* task)
{ {
if (task->prev) { if (task->prev) {
task->prev->next = task->next; task->prev->next = task->next;
} else { } else if (lst->head == task) {
lst->head = task->next; lst->head = task->next;
} }
if (task->next) { if (task->next) {
task->next->prev = task->prev; task->next->prev = task->prev;
} else { } else if (lst->tail == task) {
lst->tail = task->prev; lst->tail = task->prev;
} }
} }
void void
coio_rdy(CoioTask * task) coio_ready(CoioTask* task)
{ {
task->timeout = 0; task->timeout = 0;
coio_del(&coio_sleeping, task); if (!task->ready) {
coio_add(&coio_ready, task); task->ready = 1;
coio_del(&coio_sleeping, task);
coio_add(&coio_ready_list, task);
}
} }
void void
coio_transfer() coio_transfer()
{ {
coro_transfer(&coio_current->ctx, &_sched_ctx); coro_transfer(&coio_current->ctx, &coio_sched_ctx);
} }
uvlong uvlong
@ -242,3 +268,22 @@ coio_now()
return (uvlong) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; return (uvlong) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
#endif #endif
} }
void
coio_debug()
{
CoioTask* t;
fprintf(stderr, ">>>\nCurrent tasks: %s\n", coio_current->name);
fprintf(stderr, "Sleeping tasks: ");
for (t = coio_sleeping.head; t != NULL; t = t->next) {
fprintf(stderr, "%s ", t->name);
}
fprintf(stderr, "\nReady tasks: ");
for (t = coio_ready_list.head; t != NULL; t = t->next) {
fprintf(stderr, "%s ", t->name);
}
fprintf(stderr, "\n");
}

19
coio.h
View file

@ -20,15 +20,18 @@
extern "C" { extern "C" {
#endif #endif
typedef struct CoioTask CoioTask; typedef struct CoioTask CoioTask;
typedef void (*coio_func) (void *arg); typedef void (*coio_func)(void* arg);
typedef unsigned long long uvlong; typedef unsigned long long uvlong;
int coio_main (); extern CoioTask* coio_current;
int coio_create(coio_func f, void *arg, unsigned int stacksize);
void coio_yield(); int coio_main();
uvlong coio_now(); int coio_create(const char* name, coio_func f, void* arg, unsigned int stacksize);
int coio_delay(int ms); void coio_yield();
uvlong coio_now();
int coio_delay(int ms);
void coio_ready(CoioTask* task);
#ifdef __cplusplus #ifdef __cplusplus
} }

134
coio_glib.c Normal file
View file

@ -0,0 +1,134 @@
/*
* Copyright (c) 2018 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.
*/
#include "coio_glib.h"
#include "coio.h"
#include "coioimpl.h"
#include <glib.h>
struct coio_source {
GSource base;
};
static gboolean coio_source_prepare(GSource* source, gint* timeout_)
{
(void)source;
uvlong now;
int ms = 5;
CoioTask* t;
if (coio_ready_list.head) {
*timeout_ = 0;
/* if we return true here our check functions do not get called */
if (coio_sleeping.head) {
return FALSE;
}
return TRUE;
}
if ((t = coio_sleeping.head) != NULL && t->timeout != 0) {
now = coio_now();
if (now >= t->timeout) {
ms = 0;
} else {
ms = (t->timeout - now) / 1000000;
}
}
*timeout_ = ms;
return FALSE;
}
static gboolean coio_source_check(GSource* source)
{
(void)source;
CoioTask* t;
if (coio_sleeping.head) {
/* wake up timed out tasks */
uvlong now = coio_now();
while ((t = coio_sleeping.head) && t->timeout && now >= t->timeout) {
coio_ready(t);
}
}
return TRUE;
}
static gboolean coio_source_dispatch(GSource* source, GSourceFunc callback, gpointer user_data)
{
(void)source;
CoioTask* last;
gboolean result = G_SOURCE_CONTINUE;
/* error condition */
if (!coio_ready_list.head && !coio_sleeping.head)
return G_SOURCE_REMOVE;
if (!coio_ready_list.head)
return G_SOURCE_CONTINUE;
last = coio_ready_list.tail;
do {
coio_current = coio_ready_list.head;
coio_current->ready = 0;
coio_del(&coio_ready_list, coio_current);
coro_transfer(&coio_sched_ctx, &coio_current->ctx);
if (coio_current->done) {
coio_taskcount--;
coro_stack_free(&coio_current->stk);
free(coio_current);
}
} while (coio_current != last);
if (callback) {
result = callback(user_data);
}
return result;
}
static void coio_source_finalize(GSource* source)
{
(void)source;
}
GSource* coio_gsource_create()
{
coro_create(&coio_sched_ctx, NULL, NULL, NULL, 0);
static GSourceFuncs funcs = {
coio_source_prepare,
coio_source_check,
coio_source_dispatch,
coio_source_finalize,
NULL,
NULL
};
return g_source_new(&funcs, sizeof(struct coio_source));
}
gboolean coio_task_wakeup_helper(gpointer task)
{
coio_ready((CoioTask*)task);
return G_SOURCE_REMOVE;
}

25
coio_glib.h Normal file
View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2018 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.
*/
#ifndef COIO_GLIB_H
#define COIO_GLIB_H
#include <glib.h>
GSource* coio_gsource_create();
gboolean coio_task_wakeup_helper(gpointer task);
#endif

View file

@ -25,29 +25,32 @@ struct CoioTask {
coro_context ctx; coro_context ctx;
struct coro_stack stk; struct coro_stack stk;
const char* name;
coio_func func; coio_func func;
void *arg; void* arg;
uvlong timeout; uvlong timeout;
int ready;
int done; int done;
/* linked list support */ /* linked list support */
CoioTask *next; CoioTask* next;
CoioTask *prev; CoioTask* prev;
}; };
struct CoioTaskList { struct CoioTaskList {
CoioTask *head; CoioTask* head;
CoioTask *tail; CoioTask* tail;
}; };
extern CoioTaskList coio_ready; extern CoioTaskList coio_ready_list;
extern CoioTaskList coio_sleeping; extern CoioTaskList coio_sleeping;
extern CoioTask *coio_current; extern coro_context coio_sched_ctx;
extern unsigned long coio_taskcount;
void coio_add (CoioTaskList * lst, CoioTask * task); void coio_add(CoioTaskList* lst, CoioTask* task);
void coio_del (CoioTaskList * lst, CoioTask * task); void coio_del(CoioTaskList* lst, CoioTask* task);
void coio_rdy (CoioTask * task);
void coio_transfer(); void coio_transfer();
void coio_debug();
#endif #endif

View file

@ -17,7 +17,7 @@
#include "coio.h" #include "coio.h"
void void
_t1(void *arg) _t1(void* arg)
{ {
printf("going to sleep 1000ms (1s)\n"); printf("going to sleep 1000ms (1s)\n");
coio_delay(1000); coio_delay(1000);
@ -25,7 +25,7 @@ _t1(void *arg)
} }
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
(void) argc; (void) argc;
(void) argv; (void) argv;

View file

@ -17,7 +17,7 @@
#include "coio.h" #include "coio.h"
void void
_t1(void *arg) _t1(void* arg)
{ {
printf("Hello 1 from _t1\n"); printf("Hello 1 from _t1\n");
coio_yield(); coio_yield();
@ -25,7 +25,7 @@ _t1(void *arg)
} }
void void
_t2(void *arg) _t2(void* arg)
{ {
printf("Hello 1 from _t2\n"); printf("Hello 1 from _t2\n");
coio_yield(); coio_yield();
@ -33,7 +33,7 @@ _t2(void *arg)
} }
int int
main(int argc, char **argv) main(int argc, char** argv)
{ {
(void) argc; (void) argc;
(void) argv; (void) argv;