2000-11-11 15:13:50 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2000-09-07 20:35:15 +00:00
|
|
|
#include <pthread.h>
|
2000-01-30 09:03:00 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
//#define DEBUG_ENABLED
|
|
|
|
#include "gst/gst.h"
|
2000-01-30 09:03:00 +00:00
|
|
|
#include "cothreads.h"
|
2000-09-16 10:58:23 +00:00
|
|
|
#include "gst/gstarch.h"
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
pthread_key_t _cothread_key = -1;
|
|
|
|
|
2000-11-29 10:05:47 +00:00
|
|
|
/* Disablig this define allows you to shut off a few checks in
|
|
|
|
* cothread_switch. This likely will speed things up fractionally */
|
|
|
|
#define COTHREAD_PARANOID
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_create:
|
|
|
|
* @ctx: the cothread context
|
|
|
|
*
|
|
|
|
* create a new cotread state in the given context
|
|
|
|
*
|
|
|
|
* Returns: the new cothread state
|
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
cothread_state*
|
|
|
|
cothread_create (cothread_context *ctx)
|
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
cothread_state *s;
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: pthread_self() %ld\n",pthread_self());
|
2000-11-01 22:11:48 +00:00
|
|
|
//if (0) {
|
|
|
|
if (pthread_self() == 0) {
|
2000-01-30 09:03:00 +00:00
|
|
|
s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: new stack at %p\n",s);
|
2000-01-30 09:03:00 +00:00
|
|
|
} else {
|
|
|
|
char *sp = CURRENT_STACK_FRAME;
|
|
|
|
unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
|
|
|
|
~(STACK_SIZE - 1));
|
|
|
|
s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
|
|
|
|
COTHREAD_STACKSIZE));
|
|
|
|
if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANONYMOUS,
|
|
|
|
-1,0) < 0) {
|
|
|
|
perror("mmap'ing cothread stack space");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->ctx = ctx;
|
|
|
|
s->threadnum = ctx->nthreads;
|
|
|
|
s->flags = 0;
|
2000-09-07 20:35:15 +00:00
|
|
|
s->sp = ((int *)s + COTHREAD_STACKSIZE);
|
2000-09-22 23:35:14 +00:00
|
|
|
s->top_sp = s->sp;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
ctx->threads[ctx->nthreads++] = s;
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: created cothread at %p %p\n",s, s->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_setfunc:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @func: the function to call
|
|
|
|
* @argc: the argument count for the cothread function
|
|
|
|
* @argv: the arguments for the cothread function
|
|
|
|
*
|
|
|
|
* Set the cothread function
|
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
void
|
|
|
|
cothread_setfunc (cothread_state *thread,
|
|
|
|
cothread_func func,
|
|
|
|
int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->func = func;
|
|
|
|
thread->argc = argc;
|
|
|
|
thread->argv = argv;
|
|
|
|
thread->pc = (int *)func;
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_init:
|
|
|
|
*
|
|
|
|
* create and initialize a new cotread context
|
|
|
|
*
|
|
|
|
* Returns: the new cothread context
|
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
cothread_context*
|
|
|
|
cothread_init (void)
|
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
|
|
|
|
|
|
|
|
if (_cothread_key == -1) {
|
2000-11-04 18:54:07 +00:00
|
|
|
if (pthread_key_create (&_cothread_key,NULL) != 0) {
|
|
|
|
perror ("pthread_key_create");
|
2000-02-27 23:18:38 +00:00
|
|
|
return NULL;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
2000-11-04 18:54:07 +00:00
|
|
|
pthread_setspecific (_cothread_key,ctx);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
memset (ctx->threads,0,sizeof(ctx->threads));
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
|
|
|
|
ctx->threads[0]->ctx = ctx;
|
|
|
|
ctx->threads[0]->threadnum = 0;
|
|
|
|
ctx->threads[0]->func = NULL;
|
|
|
|
ctx->threads[0]->argc = 0;
|
|
|
|
ctx->threads[0]->argv = NULL;
|
|
|
|
ctx->threads[0]->flags = COTHREAD_STARTED;
|
2000-09-07 20:35:15 +00:00
|
|
|
ctx->threads[0]->sp = (int *)CURRENT_STACK_FRAME;
|
2000-01-30 09:03:00 +00:00
|
|
|
ctx->threads[0]->pc = 0;
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: 0th thread is at %p %p\n",ctx->threads[0], ctx->threads[0]->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
// we consider the initiating process to be cothread 0
|
|
|
|
ctx->nthreads = 1;
|
|
|
|
ctx->current = 0;
|
2000-11-06 00:15:51 +00:00
|
|
|
ctx->data = g_hash_table_new(g_str_hash, g_str_equal);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_main:
|
|
|
|
* @ctx: the cothread context
|
|
|
|
*
|
|
|
|
* Returns: the new cothread state
|
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
cothread_state*
|
|
|
|
cothread_main(cothread_context *ctx)
|
|
|
|
{
|
|
|
|
DEBUG(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
|
2000-01-30 09:03:00 +00:00
|
|
|
return ctx->threads[0];
|
|
|
|
}
|
|
|
|
|
2000-11-04 18:54:07 +00:00
|
|
|
void
|
|
|
|
cothread_stub (void)
|
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific(_cothread_key);
|
|
|
|
register cothread_state *thread = ctx->threads[ctx->current];
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: cothread_stub() entered\n");
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->flags |= COTHREAD_STARTED;
|
2000-09-07 20:35:15 +00:00
|
|
|
if (thread->func)
|
|
|
|
thread->func(thread->argc,thread->argv);
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->flags &= ~COTHREAD_STARTED;
|
|
|
|
thread->pc = 0;
|
2000-09-22 23:35:14 +00:00
|
|
|
thread->sp = thread->top_sp;
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: cothread_stub() exit\n");
|
2000-09-07 20:35:15 +00:00
|
|
|
//printf("uh, yeah, we shouldn't be here, but we should deal anyway\n");
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_set_data:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @key: a key for the data
|
|
|
|
* @data: the data
|
|
|
|
*
|
|
|
|
* adds data to a cothread
|
|
|
|
*/
|
2000-11-06 00:15:51 +00:00
|
|
|
void
|
|
|
|
cothread_set_data (cothread_state *thread,
|
|
|
|
gchar *key,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
cothread_context *ctx = pthread_getspecific(_cothread_key);
|
|
|
|
|
|
|
|
g_hash_table_insert(ctx->data, key, data);
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_get_data:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @key: a key for the data
|
|
|
|
*
|
|
|
|
* get data from the cothread
|
|
|
|
*
|
|
|
|
* Returns: the data assiciated with the key
|
|
|
|
*/
|
2000-11-06 00:15:51 +00:00
|
|
|
gpointer
|
|
|
|
cothread_get_data (cothread_state *thread,
|
|
|
|
gchar *key)
|
|
|
|
{
|
|
|
|
cothread_context *ctx = pthread_getspecific(_cothread_key);
|
|
|
|
|
|
|
|
return g_hash_table_lookup(ctx->data, key);
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_switch:
|
|
|
|
* @thread: the cothread state
|
|
|
|
*
|
|
|
|
* switches to the given cothread state
|
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
void
|
|
|
|
cothread_switch (cothread_state *thread)
|
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
cothread_context *ctx;
|
|
|
|
cothread_state *current;
|
2000-09-16 10:58:23 +00:00
|
|
|
int enter;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
|
|
|
if (thread == NULL) goto nothread;
|
|
|
|
#endif
|
2000-01-30 09:03:00 +00:00
|
|
|
ctx = thread->ctx;
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
|
|
|
if (ctx == NULL) goto nocontext;
|
|
|
|
#endif
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
current = ctx->threads[ctx->current];
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
|
|
|
if (current == NULL) goto nocurrent;
|
|
|
|
#endif
|
|
|
|
if (current == thread) goto selfswitch;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
// find the number of the thread to switch to
|
|
|
|
ctx->current = thread->threadnum;
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: about to switch to thread #%d\n",ctx->current);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
/* save the current stack pointer, frame pointer, and pc */
|
2000-09-15 22:44:10 +00:00
|
|
|
GET_SP(current->sp);
|
2000-11-01 22:11:48 +00:00
|
|
|
enter = setjmp(current->jmp);
|
2000-09-16 10:58:23 +00:00
|
|
|
if (enter != 0) {
|
2000-09-22 23:35:14 +00:00
|
|
|
DEBUG("cothread: enter thread #%d %d %p<->%p (%d)\n",current->threadnum, enter,
|
|
|
|
current->sp, current->top_sp, current->top_sp-current->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
return;
|
2000-09-16 10:58:23 +00:00
|
|
|
}
|
2000-09-22 23:35:14 +00:00
|
|
|
DEBUG("cothread: exit thread #%d %d %p<->%p (%d)\n",current->threadnum, enter,
|
|
|
|
current->sp, current->top_sp, current->top_sp-current->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
enter = 1;
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: set stack to %p\n", thread->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
/* restore stack pointer and other stuff of new cothread */
|
|
|
|
if (thread->flags & COTHREAD_STARTED) {
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: in thread \n");
|
2000-09-15 22:44:10 +00:00
|
|
|
SET_SP(thread->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
// switch to it
|
2000-11-01 22:11:48 +00:00
|
|
|
longjmp(thread->jmp,1);
|
2000-01-30 09:03:00 +00:00
|
|
|
} else {
|
2000-09-21 00:57:09 +00:00
|
|
|
SETUP_STACK(thread->sp);
|
2000-09-16 10:58:23 +00:00
|
|
|
SET_SP(thread->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
// start it
|
2000-09-16 10:58:23 +00:00
|
|
|
cothread_stub();
|
2000-09-17 22:08:09 +00:00
|
|
|
DEBUG("cothread: exit thread \n");
|
2000-09-22 23:35:14 +00:00
|
|
|
ctx->current = 0;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
2000-11-29 10:05:47 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef COTHREAD_PARANOID
|
|
|
|
nothread:
|
|
|
|
g_print("cothread: there's no thread, strange...\n");
|
|
|
|
return;
|
|
|
|
nocontext:
|
|
|
|
g_print("cothread: there's no context, help!\n");
|
|
|
|
exit(2);
|
|
|
|
nocurrent:
|
|
|
|
g_print("cothread: there's no current thread, help!\n");
|
|
|
|
exit(2);
|
|
|
|
#endif /* COTHREAD_PARANOID */
|
|
|
|
selfswitch:
|
|
|
|
g_print("cothread: trying to switch to same thread, legal but not necessary\n");
|
|
|
|
return;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|