From ddf9919274745e46fc8005f8a86d9b58500b628e Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Thu, 27 Nov 2008 04:38:43 +0100 Subject: [PATCH] Added muthread and logging library components. --- lib/Makefile.am | 2 +- lib/log.c | 47 +++++++++++++++++++++++ lib/log.h | 9 +++++ lib/mt.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/mt.h | 26 +++++++++++++ lib/utils.c | 3 ++ 6 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 lib/log.c create mode 100644 lib/log.h create mode 100644 lib/mt.c create mode 100644 lib/mt.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 986586e..fb07224 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,4 +1,4 @@ noinst_LIBRARIES = libht.a -libht_a_SOURCES = utils.c +libht_a_SOURCES = utils.c mt.c log.c libht_a_CFLAGS = -fPIC diff --git a/lib/log.c b/lib/log.c new file mode 100644 index 0000000..b87d4f6 --- /dev/null +++ b/lib/log.c @@ -0,0 +1,47 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +#include + +static int tostderr = 1, tosyslog = 0; + +void flog(int level, char *format, ...) +{ + va_list args; + + va_start(args, format); + if(tostderr) { + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + } else if(tosyslog) { + vsyslog(level, format, args); + } + va_end(args); +} + +void opensyslog(void) +{ + openlog("ashd", 0, LOG_DAEMON); + tostderr = 0; + tosyslog = 1; +} diff --git a/lib/log.h b/lib/log.h new file mode 100644 index 0000000..fdcc756 --- /dev/null +++ b/lib/log.h @@ -0,0 +1,9 @@ +#ifndef _LOG_H +#define _LOG_H + +#include + +void flog(int level, char *format, ...); +void opensyslog(void); + +#endif diff --git a/lib/mt.c b/lib/mt.c new file mode 100644 index 0000000..db37936 --- /dev/null +++ b/lib/mt.c @@ -0,0 +1,115 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif +#include +#include +#include + +struct muth *current = NULL; +static ucontext_t mainctxt; + +static void freemt(struct muth *muth) +{ + if(muth->running) + abort(); +#ifdef VALGRIND_STACK_DEREGISTER + VALGRIND_STACK_DEREGISTER(muth->vgid); +#endif + free(muth->stack); + free(muth); +} + +static void muboot(void) +{ + struct muth *muth; + + muth = current; + muth->running = 1; + muth->entry(muth, *muth->arglist); + muth->running = 0; + swapcontext(&muth->ctxt, muth->last); +} + +struct muth *mustart(void (*fn)(struct muth *muth, va_list args), ...) +{ + struct muth *muth, *last; + va_list args; + + omalloc(muth); + getcontext(&muth->ctxt); + muth->ctxt.uc_link = &mainctxt; + muth->ctxt.uc_stack.ss_size = 65536; + muth->ctxt.uc_stack.ss_sp = muth->stack = smalloc(muth->ctxt.uc_stack.ss_size); +#ifdef VALGRIND_STACK_REGISTER + muth->vgid = VALGRIND_STACK_REGISTER(muth->stack, muth->stack + 65536); +#endif + va_start(args, fn); + muth->entry = fn; + muth->arglist = &args; + makecontext(&muth->ctxt, muboot, 0); + if(current == NULL) + muth->last = &mainctxt; + else + muth->last = ¤t->ctxt; + last = current; + current = muth; + swapcontext(muth->last, &muth->ctxt); + current = last; + va_end(args); + if(!muth->running) + freemt(muth); + return(muth); +} + +int yield(void) +{ + ucontext_t *ret; + + if((current == NULL) || (current->last == NULL)) + abort(); + ret = current->last; + current->last = NULL; + swapcontext(¤t->ctxt, ret); + return(current->yr); +} + +void resume(struct muth *muth, int ret) +{ + struct muth *last; + + if(muth->last != NULL) + abort(); + if(current == NULL) + muth->last = &mainctxt; + else + muth->last = ¤t->ctxt; + last = current; + current = muth; + muth->yr = ret; + swapcontext(muth->last, ¤t->ctxt); + current = last; + if(!muth->running) + freemt(muth); +} diff --git a/lib/mt.h b/lib/mt.h new file mode 100644 index 0000000..ad049e7 --- /dev/null +++ b/lib/mt.h @@ -0,0 +1,26 @@ +#ifndef _MUTHREAD_H +#define _MUTHREAD_H + +#include +#include + +#define vavar(type, name) type name = va_arg(args, type) + +struct muth { + ucontext_t ctxt, *last; + void *stack; + void (*entry)(struct muth *muth, va_list args); + va_list *arglist; + int running; + int yr; + int freeme; + int vgid; +}; + +struct muth *mustart(void (*fn)(struct muth *muth, va_list args), ...); +void resume(struct muth *muth, int ret); +int yield(void); + +extern struct muth *current; + +#endif diff --git a/lib/utils.c b/lib/utils.c index 0045235..8b64b76 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -18,6 +18,9 @@ #include +#ifdef HAVE_CONFIG_H +#include +#endif #include void _sizebuf(struct buffer *buf, size_t wanted, size_t el) -- 2.11.0