Skip to content

Commit

Permalink
coroutine: some initial bits of this module, incomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniini committed Mar 31, 2012
1 parent f1390da commit 1cfad83
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 2 deletions.
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -3876,7 +3876,7 @@ fi
LIBMOWGLI_MODULES="core base container concurrent dns eventloop ext linebuf module object thread vio"
LIBMOWGLI_MODULES="core base container concurrent coroutine dns eventloop ext linebuf module object thread vio"
LIBMOWGLI_MODULE_BUILD="$(echo && echo x)"
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ AC_CHECK_FUNCS([fcntl kqueue mmap select dispatch_block port_create])
AC_PATH_PROG(AR, ar)
AC_PATH_PROG(RANLIB, ranlib)

LIBMOWGLI_MODULES="core base container concurrent dns eventloop ext linebuf module object thread vio"
LIBMOWGLI_MODULES="core base container concurrent coroutine dns eventloop ext linebuf module object thread vio"
AC_SUBST(LIBMOWGLI_MODULES)

LIBMOWGLI_MODULE_BUILD="$(echo && echo x)"
Expand Down
15 changes: 15 additions & 0 deletions src/libmowgli/coroutine/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include ../../../extra.mk

STATIC_PIC_LIB_NOINST = ${LIBMOWGLI_SHARED_COROUTINE}
STATIC_LIB_NOINST = ${LIBMOWGLI_STATIC_COROUTINE}

SRCS = coroutine.c

INCLUDES := ${SRCS:.c=.h}

include ../../../buildsys.mk

includesubdir = $(PACKAGE_NAME)/coroutine

CPPFLAGS += -I. -I.. -I../../.. -DMOWGLI_CORE

44 changes: 44 additions & 0 deletions src/libmowgli/coroutine/coroutine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* libmowgli: A collection of useful routines for programming.
* coroutine.c: coroutines/fibers implementation
*
* Copyright (c) 2012 William Pitcock <[email protected]>
*
* Permission to use, copy, modify, and/or 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "mowgli.h"
#include "coroutine_private.h"

static mowgli_stack_dir_t mowgli_coroutine_get_stack_dir_leaf_ptr(void *callstack_p)
{
mowgli_stack_dir_t dir;
void *childstack_p = &callstack_p;

dir = (childstack_p > callstack_p ? MOWGLI_STACK_GROWS_UP : MOWGLI_STACK_GROWS_DOWN);

return dir;
}

mowgli_stack_dir_t mowgli_coroutine_get_stack_dir(void)
{
int dummy1 = 0;
void *dummy2 = &dummy1;

return mowgli_coroutine_get_stack_dir_leaf_ptr(dummy2);
}

45 changes: 45 additions & 0 deletions src/libmowgli/coroutine/coroutine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* libmowgli: A collection of useful routines for programming.
* coroutine.h: coroutines/fibers implementation
*
* Copyright (c) 2012 William Pitcock <[email protected]>
*
* Permission to use, copy, modify, and/or 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __MOWGLI_COROUTINE_H__
#define __MOWGLI_COROUTINE_H__

typedef enum {
MOWGLI_STACK_GROWS_DOWN = -1,
MOWGLI_STACK_GROWS_UP = 1,
} mowgli_stack_dir_t;

extern mowgli_stack_dir_t mowgli_coroutine_get_stack_dir(void);

typedef void mowgli_coroutine_t;
typedef void (*mowgli_coroutine_start_fn_t)(void *userdata);

extern int mowgli_coroutine_create(mowgli_coroutine_start_fn_t start_fn, void *userdata);
extern void mowgli_coroutine_destroy(mowgli_coroutine_t *coroutine);
extern void mowgli_coroutine_yield(void);
extern void mowgli_coroutine_yield_to(mowgli_coroutine_t *next);
extern void mowgli_coroutine_exit(void);
extern void mowgli_coroutine_exit_to(mowgli_coroutine_t *next);
extern mowgli_coroutine_t *mowgli_coroutine_self(void);

#endif
2 changes: 2 additions & 0 deletions src/libmowgli/mowgli.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ MOWGLI_DECLS_START
#include "core/allocation_policy.h"
#include "core/alloc.h"

#include "coroutine/coroutine.h"

#include "thread/thread.h"
#include "thread/mutex.h"

Expand Down

0 comments on commit 1cfad83

Please sign in to comment.