initial checkin
[platform/upstream/gstreamer.git] / gst / cothreads.c
1 #include <sys/time.h>
2 #include <linux/linkage.h>
3 #include <stdio.h>   
4 #include <stdlib.h>
5 #include <signal.h>   
6 #include <setjmp.h>
7 #include <pthread.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10
11 #include "cothreads.h"
12
13 pthread_key_t _cothread_key = -1;
14
15 cothread_state *cothread_create(cothread_context *ctx) {
16   cothread_state *s;
17
18   if (pthread_self() == 0) {
19     s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
20   } else {
21     char *sp = CURRENT_STACK_FRAME;
22     unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
23       ~(STACK_SIZE - 1));
24     s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
25                            COTHREAD_STACKSIZE));
26     if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
27              PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANONYMOUS,
28              -1,0) < 0) {
29       perror("mmap'ing cothread stack space");
30       return NULL;
31     }
32   }
33
34   s->ctx = ctx;
35   s->threadnum = ctx->nthreads;
36   s->flags = 0;
37   s->sp = (int *)(s + COTHREAD_STACKSIZE);
38
39   ctx->threads[ctx->nthreads++] = s;
40
41 //  printf("created cothread at %p\n",s);
42
43   return s;
44 }
45
46 void cothread_setfunc(cothread_state *thread,cothread_func func,int argc,char **argv) {
47   thread->func = func;
48   thread->argc = argc;
49   thread->argv = argv;
50   thread->pc = (int *)func;
51 }
52
53 cothread_context *cothread_init() {
54   cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
55
56   if (_cothread_key == -1) {
57     if (pthread_key_create(&_cothread_key,NULL) != 0) {
58       perror("pthread_key_create");
59       return;
60     }
61   }
62   pthread_setspecific(_cothread_key,ctx);
63
64   memset(ctx->threads,0,sizeof(ctx->threads));
65
66   ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
67   ctx->threads[0]->ctx = ctx;
68   ctx->threads[0]->threadnum = 0;
69   ctx->threads[0]->func = NULL;
70   ctx->threads[0]->argc = 0;
71   ctx->threads[0]->argv = NULL;
72   ctx->threads[0]->flags = COTHREAD_STARTED;
73   ctx->threads[0]->sp = CURRENT_STACK_FRAME;
74   ctx->threads[0]->pc = 0;
75
76 //  fprintf(stderr,"0th thread is at %p\n",ctx->threads[0]);
77
78   // we consider the initiating process to be cothread 0
79   ctx->nthreads = 1;
80   ctx->current = 0;
81
82   return ctx;
83 }
84
85 cothread_state *cothread_main(cothread_context *ctx) {
86 //  fprintf(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
87   return ctx->threads[0];
88 }
89
90 void cothread_stub() {
91   cothread_context *ctx = pthread_getspecific(_cothread_key);
92   register cothread_state *thread = ctx->threads[ctx->current];
93
94   thread->flags |= COTHREAD_STARTED;
95   thread->func(thread->argc,thread->argv);
96   thread->flags &= ~COTHREAD_STARTED;
97   thread->pc = 0;
98 //  printf("uh, yeah, we shouldn't be here, but we should deal anyway\n");
99 }
100
101 void cothread_switch(cothread_state *thread) {
102   cothread_context *ctx;
103   cothread_state *current;
104   int enter = 0;
105 //  int i;
106
107   if (thread == NULL)
108     return;
109
110   ctx = thread->ctx;
111
112   current = ctx->threads[ctx->current];
113   if (current == NULL) {
114     fprintf(stderr,"there's no current thread, help!\n");
115     exit(2);
116   }
117
118   if (current == thread) {
119     fprintf(stderr,"trying to switch to same thread, legal but not necessary\n");
120     return;
121   }
122
123   // find the number of the thread to switch to
124   ctx->current = thread->threadnum;
125 //  fprintf(stderr,"about to switch to thread #%d\n",ctx->current);
126
127   /* save the current stack pointer, frame pointer, and pc */
128   __asm__("movl %%esp, %0" : "=m"(current->sp) : : "esp", "ebp");
129   enter = setjmp(current->jmp);
130   if (enter != 0)
131     return;
132   enter = 1;
133
134   /* restore stack pointer and other stuff of new cothread */
135   __asm__("movl %0, %%esp\n" : "=m"(thread->sp));
136   if (thread->flags & COTHREAD_STARTED) {
137     // switch to it
138     longjmp(thread->jmp,1);
139   } else {
140     // start it
141     __asm__("jmp " SYMBOL_NAME_STR(cothread_stub));
142   }
143 }