From 1a14956151de25e4586864264bbc5dfe41e3da40 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 16 Sep 2001 01:34:36 +0000 Subject: *** empty log message *** --- conftest.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ coro.c | 25 ++++++---- coro.h | 3 +- 3 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 conftest.c diff --git a/conftest.c b/conftest.c new file mode 100644 index 0000000..17cc1ec --- /dev/null +++ b/conftest.c @@ -0,0 +1,154 @@ +/* + * This file was taken from pth-1.40/aclocal.m4 + * The original copyright is below. + * + * GNU Pth - The GNU Portable Threads + * Copyright (c) 1999-2001 Ralf S. Engelschall + * + * This file is part of GNU Pth, a non-preemptive thread scheduling + * library which can be found at http://www.gnu.org/software/pth/. + * + * This file is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This file 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this file; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA, or contact Marc Lehmann . + */ + +#include +#include +#include +#if defined(TEST_sigstack) || defined(TEST_sigaltstack) +#include +#include +#include +#endif +#if defined(TEST_makecontext) +#include +#endif +union alltypes { + long l; + double d; + void *vp; + void (*fp)(void); + char *cp; +}; +static volatile char *handler_addr = (char *)0xDEAD; +#if defined(TEST_sigstack) || defined(TEST_sigaltstack) +static volatile int handler_done = 0; +void handler(int sig) +{ + char garbage[1024]; + int i; + auto int dummy; + for (i = 0; i < 1024; i++) + garbage[i] = 'X'; + handler_addr = (char *)&dummy; + handler_done = 1; + return; +} +#endif +#if defined(TEST_makecontext) +static ucontext_t uc_handler; +static ucontext_t uc_main; +void handler(void) +{ + char garbage[1024]; + int i; + auto int dummy; + for (i = 0; i < 1024; i++) + garbage[i] = 'X'; + handler_addr = (char *)&dummy; + swapcontext(&uc_handler, &uc_main); + return; +} +#endif +int main(int argc, char *argv[]) +{ + FILE *f; + char *skaddr; + char *skbuf; + int sksize; + char result[1024]; + int i; + sksize = 32768; + skbuf = (char *)malloc(sksize*2+2*sizeof(union alltypes)); + if (skbuf == NULL) + exit(1); + for (i = 0; i < sksize*2+2*sizeof(union alltypes); i++) + skbuf[i] = 'A'; + skaddr = skbuf+sizeof(union alltypes); +#if defined(TEST_sigstack) || defined(TEST_sigaltstack) + { + struct sigaction sa; +#if defined(TEST_sigstack) + struct sigstack ss; +#elif defined(TEST_sigaltstack) && defined(HAVE_STACK_T) + stack_t ss; +#else + struct sigaltstack ss; +#endif +#if defined(TEST_sigstack) + ss.ss_sp = (void *)(skaddr + sksize); + ss.ss_onstack = 0; + if (sigstack(&ss, NULL) < 0) + exit(1); +#elif defined(TEST_sigaltstack) + ss.ss_sp = (void *)(skaddr + sksize); + ss.ss_size = sksize; + ss.ss_flags = 0; + if (sigaltstack(&ss, NULL) < 0) + exit(1); +#endif + memset((void *)&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = handler; + sa.sa_flags = SA_ONSTACK; + sigemptyset(&sa.sa_mask); + sigaction(SIGUSR1, &sa, NULL); + kill(getpid(), SIGUSR1); + while (!handler_done) + /*nop*/; + } +#endif +#if defined(TEST_makecontext) + { + if (getcontext(&uc_handler) != 0) + exit(1); + uc_handler.uc_link = NULL; + uc_handler.uc_stack.ss_sp = (void *)(skaddr + sksize); + uc_handler.uc_stack.ss_size = sksize; + uc_handler.uc_stack.ss_flags = 0; + makecontext(&uc_handler, handler, 1); + swapcontext(&uc_main, &uc_handler); + } +#endif + if (handler_addr == (char *)0xDEAD) + exit(1); + if (handler_addr < skaddr+sksize) { + /* stack was placed into lower area */ + if (*(skaddr+sksize) != 'A') + sprintf(result, "(skaddr)+(sksize)-%d,(sksize)-%d", + sizeof(union alltypes), sizeof(union alltypes)); + else + strcpy(result, "(skaddr)+(sksize),(sksize)"); + } + else { + /* stack was placed into higher area */ + if (*(skaddr+sksize*2) != 'A') + sprintf(result, "(skaddr),(sksize)-%d", sizeof(union alltypes)); + else + strcpy(result, "(skaddr),(sksize)"); + } + printf("%s\n", result); + exit(0); +} + diff --git a/coro.c b/coro.c index 7c36538..7d4dfe3 100644 --- a/coro.c +++ b/coro.c @@ -32,11 +32,18 @@ #include "coro.h" +#if !defined(STACK_ADJUST_PTR) /* IRIX is decidedly NON-unix */ -#if __sgi -# define STACK_ADJUST(sp,ss) ((char *)(sp) + (ss) - 8) -#else -# define STACK_ADJUST(sp,ss) (sp) +# if __sgi +# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) +# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) +# elif __i386__ && CORO_LINUX +# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss)) +# define STACK_ADJUST_SIZE(sp,ss) (ss) +# else +# define STACK_ADJUST_PTR(sp,ss) (sp) +# define STACK_ADJUST_SIZE(sp,ss) (ss) +# endif #endif #if CORO_SJLJ || CORO_LOOSE || CORO_LINUX || CORO_IRIX @@ -89,8 +96,8 @@ void coro_create(coro_context *ctx, getcontext (&(ctx->uc)); ctx->uc.uc_link = 0; - ctx->uc.uc_stack.ss_sp = STACK_ADJUST(sptr,ssize); - ctx->uc.uc_stack.ss_size = (size_t) ssize; + ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR(sptr,ssize); + ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); ctx->uc.uc_stack.ss_flags = 0; makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); @@ -126,7 +133,7 @@ void coro_create(coro_context *ctx, /* set the new stack */ nstk.ss_sp = STACK_ADJUST(sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ - nstk.ss_size = ssize; + nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); nstk.ss_flags = 0; if (sigaltstack (&nstk, &ostk) < 0) @@ -167,7 +174,7 @@ void coro_create(coro_context *ctx, #if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(JB_PC) && defined(JB_SP) ctx->env[0].__jmpbuf[JB_PC] = (int)coro_init; - ctx->env[0].__jmpbuf[JB_SP] = (int)((char *)sptr + ssize); + ctx->env[0].__jmpbuf[JB_SP] = (int)STACK_ADJUST_PTR(sptr,ssize); #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) \ && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined(__mc68000__) ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; @@ -183,7 +190,7 @@ void coro_create(coro_context *ctx, setjmp (ctx->env); ctx->env[JB_PC] = (__uint64_t)coro_init; - ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST(sptr,ssize); + ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR(sptr,ssize); # endif diff --git a/coro.h b/coro.h index 42bb76c..87de6e9 100644 --- a/coro.h +++ b/coro.h @@ -84,11 +84,10 @@ */ /* - * This is the type for the top function of a new coroutine. + * This is the type for the initialization function of a new coroutine. */ typedef void (*coro_func)(void *); - /* * A coroutine state is saved in the following structure. Treat it as a * opaque type. errno and sigmask might be saved, but don't rely on it, -- cgit v1.2.3