2002-01-28 01:29:10 +00:00
|
|
|
/* Pthread-friendly coroutines with pth
|
|
|
|
* Copyright (C) 2002 Andy Wingo <wingo@pobox.com>
|
|
|
|
*
|
2002-01-29 05:49:57 +00:00
|
|
|
* cothreads.c: public API implementation
|
2002-01-28 01:29:10 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cothreads-private.h"
|
|
|
|
|
2002-01-28 01:56:17 +00:00
|
|
|
#define HAVE_LINUXTHREADS
|
2002-01-28 01:29:10 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_LINUXTHREADS
|
2002-02-02 19:07:10 +00:00
|
|
|
static cothreads_config cothreads_config_default = COTHREADS_CONFIG_LINUXTHREADS_INITIALIZER;
|
2002-01-28 01:29:10 +00:00
|
|
|
#else
|
2002-02-02 19:07:10 +00:00
|
|
|
static cothreads_config cothreads_config_default = COTHREADS_CONFIG_GTHREAD_INITIALIZER;
|
2002-01-28 01:29:10 +00:00
|
|
|
#endif
|
|
|
|
|
2002-02-02 19:07:10 +00:00
|
|
|
cothreads_config *_cothreads_config_global = NULL;
|
2002-01-28 01:29:10 +00:00
|
|
|
|
|
|
|
static gboolean (*stack_alloc_func) (char**, char**);
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
static void cothread_private_set (char *sp, void *priv, size_t size);
|
|
|
|
static void cothread_private_get (char *sp, void *priv, size_t size);
|
|
|
|
static void cothread_stub (void);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cothreads_initialized:
|
|
|
|
*
|
|
|
|
* Query the state of the cothreads system.
|
|
|
|
*
|
|
|
|
* Returns: TRUE if cothreads_init() has already been called, FALSE otherwise
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
cothreads_initialized (void)
|
|
|
|
{
|
2002-02-02 19:07:10 +00:00
|
|
|
return (_cothreads_config_global != NULL);
|
2002-01-29 05:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cothreads_init:
|
2002-02-02 19:07:10 +00:00
|
|
|
* @config: attributes for creation of cothread stacks
|
2002-01-29 05:49:57 +00:00
|
|
|
*
|
2002-02-02 19:07:10 +00:00
|
|
|
* Initialize the cothreads system. If @config is NULL, use the default parameters
|
2002-01-29 05:49:57 +00:00
|
|
|
* detected at compile-time.
|
|
|
|
*/
|
|
|
|
void
|
2002-02-02 19:07:10 +00:00
|
|
|
cothreads_init (cothreads_config *config)
|
2002-01-28 01:29:10 +00:00
|
|
|
{
|
2002-02-02 19:07:10 +00:00
|
|
|
static cothreads_config _config;
|
2002-01-28 01:29:10 +00:00
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
if (cothreads_initialized()) {
|
2002-01-28 01:29:10 +00:00
|
|
|
g_warning ("cothread system has already been initialized");
|
|
|
|
return;
|
|
|
|
}
|
2002-02-02 19:07:10 +00:00
|
|
|
|
|
|
|
/* we don't hold on to *config, we copy it (if it's supplied) */
|
|
|
|
if (!config)
|
|
|
|
_config = cothreads_config_default;
|
2002-01-28 01:29:10 +00:00
|
|
|
else
|
2002-02-02 19:07:10 +00:00
|
|
|
_config = *config;
|
2002-01-28 01:29:10 +00:00
|
|
|
|
2002-02-02 19:07:10 +00:00
|
|
|
_cothreads_config_global = &_config;
|
2002-01-28 01:29:10 +00:00
|
|
|
|
2002-02-02 19:07:10 +00:00
|
|
|
switch (_cothreads_config_global->method) {
|
|
|
|
case COTHREADS_ALLOC_METHOD_MALLOC:
|
2002-01-28 01:29:10 +00:00
|
|
|
stack_alloc_func = cothread_stack_alloc_on_heap;
|
|
|
|
break;
|
2002-02-02 19:07:10 +00:00
|
|
|
case COTHREADS_ALLOC_METHOD_GTHREAD_STACK:
|
2002-01-28 01:29:10 +00:00
|
|
|
stack_alloc_func = cothread_stack_alloc_on_gthread_stack;
|
|
|
|
break;
|
2002-02-02 19:07:10 +00:00
|
|
|
case COTHREADS_ALLOC_METHOD_LINUXTHREADS:
|
2002-01-28 01:29:10 +00:00
|
|
|
stack_alloc_func = cothread_stack_alloc_linuxthreads;
|
|
|
|
break;
|
|
|
|
default:
|
2002-02-02 19:07:10 +00:00
|
|
|
g_error ("unexpected value for config method %d", _cothreads_config_global->method);
|
2002-01-28 01:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
/**
|
|
|
|
* cothread_create:
|
|
|
|
* @func: function to start with this cothread
|
|
|
|
* @argc: argument count
|
|
|
|
* @argv: argument vector
|
|
|
|
*
|
|
|
|
* Create a new cothread running a given function. You must explictly switch
|
|
|
|
* into this cothread to give it control. If @func is NULL, a cothread is
|
|
|
|
* created on the current stack with the current stack pointer.
|
|
|
|
*
|
|
|
|
* Returns: A pointer to the new cothread
|
|
|
|
*/
|
2002-01-28 01:29:10 +00:00
|
|
|
cothread*
|
2002-01-29 05:49:57 +00:00
|
|
|
cothread_create (void (*func)(int, void **), int argc, void **argv)
|
2002-01-28 01:29:10 +00:00
|
|
|
{
|
|
|
|
char *low, *high;
|
2002-01-29 05:49:57 +00:00
|
|
|
cothread_private priv;
|
2002-01-28 01:29:10 +00:00
|
|
|
cothread *ret = g_new0 (cothread, 1);
|
|
|
|
|
|
|
|
if (!func) {
|
|
|
|
/* we are being asked to save the current thread into a new cothread. this
|
|
|
|
* only happens for the first cothread. */
|
2002-02-02 19:07:10 +00:00
|
|
|
if (_cothreads_config_global->alloc_cothread_0)
|
2002-01-28 01:29:10 +00:00
|
|
|
if (!stack_alloc_func (&low, &high))
|
|
|
|
g_error ("couldn't create cothread 0");
|
2002-01-29 05:49:57 +00:00
|
|
|
else
|
|
|
|
g_message ("created cothread 0 with low=%p, high=%p", low, high);
|
|
|
|
else
|
|
|
|
g_message ("created cothread 0");
|
2002-01-28 01:29:10 +00:00
|
|
|
|
|
|
|
pth_mctx_save (ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stack_alloc_func (&low, &high))
|
|
|
|
g_error ("could not allocate a new cothread stack");
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
g_message ("created a cothread with low=%p, high=%p", low, high);
|
|
|
|
|
|
|
|
pth_mctx_set (ret, cothread_stub, low, high);
|
|
|
|
|
|
|
|
priv.argc = argc;
|
|
|
|
priv.argv = argv;
|
|
|
|
priv.func = func;
|
|
|
|
cothread_private_set (low, &priv, sizeof(priv));
|
2002-01-28 01:29:10 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-01-29 05:49:57 +00:00
|
|
|
/**
|
|
|
|
* cothread_destroy:
|
|
|
|
* @thread: cothread to destroy
|
|
|
|
*
|
|
|
|
* Deallocate any memory used by the cothread data structures.
|
|
|
|
*/
|
2002-01-28 01:29:10 +00:00
|
|
|
void
|
|
|
|
cothread_destroy (cothread *thread)
|
|
|
|
{
|
|
|
|
/* FIXME: have method-specific destroy functions. */
|
|
|
|
|
|
|
|
g_free (thread);
|
|
|
|
}
|
2002-01-29 05:49:57 +00:00
|
|
|
|
|
|
|
/* the whole 'page size' thing is to allow for the last page of a stack or chunk
|
|
|
|
* to be mmap'd as a boundary page */
|
|
|
|
|
|
|
|
static void
|
|
|
|
cothread_private_set (char *sp, void *priv, size_t size)
|
|
|
|
{
|
|
|
|
char *dest;
|
|
|
|
|
|
|
|
#if PTH_STACK_GROWTH > 0
|
2002-02-02 19:07:10 +00:00
|
|
|
dest = (char*) ((gulong)sp | (_cothreads_config_global->chunk_size / _cothreads_config_global->blocks_per_chunk - 1))
|
2002-01-29 05:49:57 +00:00
|
|
|
- size + 1 - getpagesize();
|
|
|
|
#else
|
2002-02-02 19:07:10 +00:00
|
|
|
dest = (char*) ((gulong)sp &~ (_cothreads_config_global->chunk_size / _cothreads_config_global->blocks_per_chunk - 1))
|
2002-01-29 05:49:57 +00:00
|
|
|
+ getpagesize();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memcpy (dest, priv, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cothread_private_get (char *sp, void *priv, size_t size)
|
|
|
|
{
|
|
|
|
char *src;
|
|
|
|
|
|
|
|
#if PTH_STACK_GROWTH > 0
|
2002-02-02 19:07:10 +00:00
|
|
|
src = (char*) ((gulong)sp | (_cothreads_config_global->chunk_size / _cothreads_config_global->blocks_per_chunk - 1))
|
2002-01-29 05:49:57 +00:00
|
|
|
- size + 1 - getpagesize();
|
|
|
|
#else
|
2002-02-02 19:07:10 +00:00
|
|
|
src = (char*) ((gulong)sp &~ (_cothreads_config_global->chunk_size / _cothreads_config_global->blocks_per_chunk - 1))
|
2002-01-29 05:49:57 +00:00
|
|
|
+ getpagesize();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memcpy (priv, src, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cothread_stub (void)
|
|
|
|
{
|
|
|
|
cothread_private priv;
|
|
|
|
|
|
|
|
cothread_private_get (CURRENT_STACK_FRAME, &priv, sizeof (priv));
|
|
|
|
|
|
|
|
priv.func (priv.argc, priv.argv);
|
|
|
|
|
|
|
|
g_warning ("we really shouldn't get here");
|
|
|
|
}
|
|
|
|
|