2de2b3ee61cf3e95b90ad107a2657a66dc06e5b4
[platform/upstream/gstreamer.git] / gst / cothreads.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * cothreads.c: Cothreading routines
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <pthread.h>
24 #include <stdio.h>   
25 #include <stdlib.h>
26 #include <signal.h>   
27 #include <setjmp.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30
31 /* we make too much noise for normal debugging... */
32 //#define GST_DEBUG_FORCE_DISABLE
33 #include "gst_private.h"
34
35 #include "cothreads.h"
36 #include "gstarch.h"
37
38
39 #define COTHREAD_STACKSIZE 16384
40 #define COTHREAD_MAXTHREADS 128
41 #define STACK_SIZE 0x200000
42
43
44 struct _cothread_context {
45   cothread_state *threads[COTHREAD_MAXTHREADS];
46   int nthreads;
47   int current;
48   GHashTable *data;
49 };
50
51
52 pthread_key_t _cothread_key = -1;
53
54 /* Disablig this define allows you to shut off a few checks in
55  * cothread_switch.  This likely will speed things up fractionally */
56 #define COTHREAD_PARANOID
57
58 /**
59  * cothread_init:
60  *
61  * Create and initialize a new cothread context 
62  *
63  * Returns: the new cothread context
64  */
65 cothread_context*
66 cothread_init (void) 
67 {
68   cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
69
70   GST_INFO (GST_CAT_COTHREADS,"initializing cothreads");
71
72   if (_cothread_key == -1) {
73     if (pthread_key_create (&_cothread_key,NULL) != 0) {
74       perror ("pthread_key_create");
75       return NULL;
76     }
77   }
78   pthread_setspecific (_cothread_key,ctx);
79
80   memset (ctx->threads,0,sizeof(ctx->threads));
81
82   ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
83   ctx->threads[0]->ctx = ctx;
84   ctx->threads[0]->threadnum = 0;
85   ctx->threads[0]->func = NULL;
86   ctx->threads[0]->argc = 0;
87   ctx->threads[0]->argv = NULL;
88   ctx->threads[0]->flags = COTHREAD_STARTED;
89   ctx->threads[0]->sp = (void *)CURRENT_STACK_FRAME;
90   ctx->threads[0]->pc = 0;
91
92   GST_INFO (GST_CAT_COTHREADS,"0th thread is %p at sp:%p",ctx->threads[0], ctx->threads[0]->sp);
93
94   // we consider the initiating process to be cothread 0
95   ctx->nthreads = 1;
96   ctx->current = 0;
97   ctx->data = g_hash_table_new(g_str_hash, g_str_equal);
98
99   return ctx;
100 }
101
102 /**
103  * cothread_create:
104  * @ctx: the cothread context
105  *
106  * Create a new cothread state in the given context
107  *
108  * Returns: the new cothread state or NULL on error
109  */
110 cothread_state*
111 cothread_create (cothread_context *ctx) 
112 {
113   cothread_state *s;
114
115   if (ctx->nthreads == COTHREAD_MAXTHREADS) {
116     GST_DEBUG (0, "attempt to create > COTHREAD_MAXTHREADS\n");
117     return NULL;
118   }
119   GST_DEBUG (0,"pthread_self() %ld\n",pthread_self());
120   //if (0) {
121   if (pthread_self() == 0) {
122     s = (cothread_state *)malloc(COTHREAD_STACKSIZE);
123     GST_DEBUG (0,"new stack (case 1) at %p\n",s);
124   } else {
125     void *sp = CURRENT_STACK_FRAME;
126     // FIXME this may not be 64bit clean
127     //       could use casts to uintptr_t from inttypes.h
128     //       if only all platforms had inttypes.h
129     void *stack_end = (void *)((unsigned long)sp & ~(STACK_SIZE - 1));
130     s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
131                            COTHREAD_STACKSIZE));
132     GST_DEBUG (0,"new stack (case 2) at %p\n",s);
133     if (mmap((void *)s,COTHREAD_STACKSIZE,
134              PROT_READ|PROT_WRITE|PROT_EXEC,MAP_FIXED|MAP_PRIVATE|MAP_ANON,
135              -1,0) < 0) {
136       perror("mmap'ing cothread stack space");
137       return NULL;
138     }
139   }
140
141   s->ctx = ctx;
142   s->threadnum = ctx->nthreads;
143   s->flags = 0;
144   s->sp = ((void *)s + COTHREAD_STACKSIZE);
145   // is this needed anymore?
146   s->top_sp = s->sp;
147
148   GST_INFO (GST_CAT_COTHREADS,"created cothread #%d: %p at sp:%p", ctx->nthreads, s, s->sp);
149
150   ctx->threads[ctx->nthreads++] = s;
151
152   return s;
153 }
154
155 /**
156  * cothread_setfunc:
157  * @thread: the cothread state
158  * @func: the function to call
159  * @argc: argument count for the cothread function
160  * @argv: arguments for the cothread function
161  *
162  * Set the cothread function
163  */
164 void 
165 cothread_setfunc (cothread_state *thread,
166                   cothread_func func,
167                   int argc,
168                   char **argv) 
169 {
170   thread->func = func;
171   thread->argc = argc;
172   thread->argv = argv;
173   thread->pc = (void *)func;
174 }
175
176 /**
177  * cothread_main:
178  * @ctx: cothread context to find main thread of
179  *
180  * Returns: the #cothread_state of the main (0th) thread
181  */
182 cothread_state*
183 cothread_main(cothread_context *ctx) 
184 {
185   GST_DEBUG (0,"returning %p, the 0th cothread\n",ctx->threads[0]);
186   return ctx->threads[0];
187 }
188
189 static void 
190 cothread_stub (void) 
191 {
192   cothread_context *ctx = pthread_getspecific(_cothread_key);
193   register cothread_state *thread = ctx->threads[ctx->current];
194
195   GST_DEBUG_ENTER("");
196
197   thread->flags |= COTHREAD_STARTED;
198   while (1) {
199     thread->func(thread->argc,thread->argv);
200     // we do this to avoid ever returning, we just switch to 0th thread
201     cothread_switch(cothread_main(ctx));
202   }
203   thread->flags &= ~COTHREAD_STARTED;
204   thread->pc = 0;
205   thread->sp = thread->top_sp;
206   fprintf(stderr,"uh, yeah, we shouldn't be here, but we should deal anyway\n");
207   GST_DEBUG_LEAVE("");
208 }
209
210 /**
211  * cothread_getcurrent:
212  *
213  * Returns: the current cothread id
214  */
215 int cothread_getcurrent(void) {
216   cothread_context *ctx = pthread_getspecific(_cothread_key);
217   if (!ctx) return -1;
218   return ctx->current;
219 }
220
221 /**
222  * cothread_set_data:
223  * @thread: the cothread state
224  * @key: a key for the data
225  * @data: the data
226  *
227  * adds data to a cothread
228  */
229 void
230 cothread_set_data (cothread_state *thread, 
231                    gchar *key,
232                    gpointer data)
233 {
234   cothread_context *ctx = pthread_getspecific(_cothread_key);
235
236   g_hash_table_insert(ctx->data, key, data);
237 }
238
239 /**
240  * cothread_get_data:
241  * @thread: the cothread state
242  * @key: a key for the data
243  *
244  * get data from the cothread
245  *
246  * Returns: the data assiciated with the key
247  */
248 gpointer
249 cothread_get_data (cothread_state *thread, 
250                    gchar *key)
251 {
252   cothread_context *ctx = pthread_getspecific(_cothread_key);
253
254   return g_hash_table_lookup(ctx->data, key);
255 }
256
257 /**
258  * cothread_switch:
259  * @thread: cothread state to switch to
260  *
261  * Switches to the given cothread state
262  */
263 void 
264 cothread_switch (cothread_state *thread) 
265 {
266   cothread_context *ctx;
267   cothread_state *current;
268   int enter;
269
270 #ifdef COTHREAD_PARANOID
271   if (thread == NULL) goto nothread;
272 #endif
273   ctx = thread->ctx;
274 #ifdef COTHREAD_PARANOID
275   if (ctx == NULL) goto nocontext;
276 #endif
277
278   current = ctx->threads[ctx->current];
279 #ifdef COTHREAD_PARANOID
280   if (current == NULL) goto nocurrent;
281 #endif
282   if (current == thread) goto selfswitch;
283
284   // find the number of the thread to switch to
285   GST_INFO (GST_CAT_COTHREAD_SWITCH,"switching from cothread %d to to cothread #%d",
286             ctx->current,thread->threadnum);
287   ctx->current = thread->threadnum;
288
289   /* save the current stack pointer, frame pointer, and pc */
290 #ifdef GST_ARCH_PRESETJMP
291   GST_ARCH_PRESETJMP();
292 #endif
293   enter = setjmp(current->jmp);
294   if (enter != 0) {
295     GST_DEBUG (0,"enter thread #%d %d %p<->%p (%d)\n",current->threadnum, enter, 
296                     current->sp, current->top_sp, current->top_sp-current->sp);
297     return;
298   }
299   GST_DEBUG (0,"exit thread #%d %d %p<->%p (%d)\n",current->threadnum, enter, 
300                     current->sp, current->top_sp, current->top_sp-current->sp);
301   enter = 1;
302
303   GST_DEBUG (0,"set stack to %p\n", thread->sp);
304   /* restore stack pointer and other stuff of new cothread */
305   if (thread->flags & COTHREAD_STARTED) {
306     GST_DEBUG (0,"in thread \n");
307     // switch to it
308     longjmp(thread->jmp,1);
309   } else {
310     GST_ARCH_SETUP_STACK(thread->sp);
311     GST_ARCH_SET_SP(thread->sp);
312     // start it
313     GST_ARCH_CALL(cothread_stub);
314     GST_DEBUG (0,"exit thread \n");
315     ctx->current = 0;
316   }
317
318   return;
319
320 #ifdef COTHREAD_PARANOID
321 nothread:
322   g_print("cothread: there's no thread, strange...\n");
323   return;
324 nocontext:
325   g_print("cothread: there's no context, help!\n");
326   exit(2);
327 nocurrent:
328   g_print("cothread: there's no current thread, help!\n");
329   exit(2);
330 #endif /* COTHREAD_PARANOID */
331 selfswitch:
332   g_print("cothread: trying to switch to same thread, legal but not necessary\n");
333   return;
334 }