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;
|
|
|
|
|
|
|
|
cothread_state *cothread_create(cothread_context *ctx) {
|
|
|
|
cothread_state *s;
|
|
|
|
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: pthread_self() %ld\n",pthread_self());
|
2000-09-07 20:35:15 +00:00
|
|
|
//if (pthread_self() == 0) {
|
|
|
|
if (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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cothread_setfunc(cothread_state *thread,cothread_func func,int argc,char **argv) {
|
|
|
|
thread->func = func;
|
|
|
|
thread->argc = argc;
|
|
|
|
thread->argv = argv;
|
|
|
|
thread->pc = (int *)func;
|
|
|
|
}
|
|
|
|
|
|
|
|
cothread_context *cothread_init() {
|
|
|
|
cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
|
|
|
|
|
|
|
|
if (_cothread_key == -1) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
pthread_setspecific(_cothread_key,ctx);
|
|
|
|
|
|
|
|
memset(ctx->threads,0,sizeof(ctx->threads));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
cothread_state *cothread_main(cothread_context *ctx) {
|
|
|
|
// fprintf(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
|
|
|
|
return ctx->threads[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
void cothread_stub() {
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
void cothread_switch(cothread_state *thread) {
|
|
|
|
cothread_context *ctx;
|
|
|
|
cothread_state *current;
|
2000-09-16 10:58:23 +00:00
|
|
|
int enter;
|
2000-01-30 09:03:00 +00:00
|
|
|
// int i;
|
|
|
|
|
|
|
|
if (thread == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx = thread->ctx;
|
|
|
|
|
|
|
|
current = ctx->threads[ctx->current];
|
|
|
|
if (current == NULL) {
|
2000-09-16 10:58:23 +00:00
|
|
|
g_print("cothread: there's no current thread, help!\n");
|
2000-01-30 09:03:00 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2000-09-17 22:08:09 +00:00
|
|
|
/*
|
2000-01-30 09:03:00 +00:00
|
|
|
if (current == thread) {
|
2000-09-16 10:58:23 +00:00
|
|
|
g_print("cothread: trying to switch to same thread, legal but not necessary\n");
|
2000-09-17 22:08:09 +00:00
|
|
|
//return;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
2000-09-17 22:08:09 +00:00
|
|
|
*/
|
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-01-30 09:03:00 +00:00
|
|
|
enter = setjmp(current->jmp);
|
2000-09-16 10:58:23 +00:00
|
|
|
DEBUG("cothread: after thread #%d %d\n",ctx->current, enter);
|
|
|
|
if (enter != 0) {
|
2000-01-30 09:03:00 +00:00
|
|
|
return;
|
2000-09-16 10:58:23 +00:00
|
|
|
}
|
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
|
|
|
|
longjmp(thread->jmp,1);
|
|
|
|
} else {
|
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
|
|
|
//JUMP(cothread_stub);
|
|
|
|
cothread_stub();
|
2000-09-17 22:08:09 +00:00
|
|
|
DEBUG("cothread: exit thread \n");
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
}
|