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