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