2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * cothreads.c: Cothreading routines
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.
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.
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.
31 /* we make too much noise for normal debugging... */
32 //#define GST_DEBUG_FORCE_DISABLE
33 #include "gst_private.h"
35 #include "cothreads.h"
39 #define COTHREAD_STACKSIZE 16384
40 #define COTHREAD_MAXTHREADS 128
41 #define STACK_SIZE 0x200000
44 struct _cothread_context {
45 cothread_state *threads[COTHREAD_MAXTHREADS];
52 pthread_key_t _cothread_key = -1;
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
61 * Create and initialize a new cothread context
63 * Returns: the new cothread context
68 cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
70 GST_INFO (GST_CAT_COTHREADS,"initializing cothreads");
72 if (_cothread_key == -1) {
73 if (pthread_key_create (&_cothread_key,NULL) != 0) {
74 perror ("pthread_key_create");
78 pthread_setspecific (_cothread_key,ctx);
80 memset (ctx->threads,0,sizeof(ctx->threads));
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;
92 GST_INFO (GST_CAT_COTHREADS,"0th thread is %p at sp:%p",ctx->threads[0], ctx->threads[0]->sp);
94 // we consider the initiating process to be cothread 0
97 ctx->data = g_hash_table_new(g_str_hash, g_str_equal);
104 * @ctx: the cothread context
106 * Create a new cothread state in the given context
108 * Returns: the new cothread state or NULL on error
111 cothread_create (cothread_context *ctx)
115 if (ctx->nthreads == COTHREAD_MAXTHREADS) {
116 GST_DEBUG (0, "attempt to create > COTHREAD_MAXTHREADS\n");
119 GST_DEBUG (0,"pthread_self() %ld\n",pthread_self());
121 if (pthread_self() == 0) {
122 s = (cothread_state *)malloc(COTHREAD_STACKSIZE);
123 GST_DEBUG (0,"new stack (case 1) at %p\n",s);
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,
136 perror("mmap'ing cothread stack space");
142 s->threadnum = ctx->nthreads;
144 s->sp = ((void *)s + COTHREAD_STACKSIZE);
145 // is this needed anymore?
148 GST_INFO (GST_CAT_COTHREADS,"created cothread #%d: %p at sp:%p", ctx->nthreads, s, s->sp);
150 ctx->threads[ctx->nthreads++] = s;
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
162 * Set the cothread function
165 cothread_setfunc (cothread_state *thread,
173 thread->pc = (void *)func;
178 * @ctx: cothread context to find main thread of
180 * Returns: the #cothread_state of the main (0th) thread
183 cothread_main(cothread_context *ctx)
185 GST_DEBUG (0,"returning %p, the 0th cothread\n",ctx->threads[0]);
186 return ctx->threads[0];
192 cothread_context *ctx = pthread_getspecific(_cothread_key);
193 register cothread_state *thread = ctx->threads[ctx->current];
197 thread->flags |= COTHREAD_STARTED;
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));
203 thread->flags &= ~COTHREAD_STARTED;
205 thread->sp = thread->top_sp;
206 fprintf(stderr,"uh, yeah, we shouldn't be here, but we should deal anyway\n");
211 * cothread_getcurrent:
213 * Returns: the current cothread id
215 int cothread_getcurrent(void) {
216 cothread_context *ctx = pthread_getspecific(_cothread_key);
223 * @thread: the cothread state
224 * @key: a key for the data
227 * adds data to a cothread
230 cothread_set_data (cothread_state *thread,
234 cothread_context *ctx = pthread_getspecific(_cothread_key);
236 g_hash_table_insert(ctx->data, key, data);
241 * @thread: the cothread state
242 * @key: a key for the data
244 * get data from the cothread
246 * Returns: the data assiciated with the key
249 cothread_get_data (cothread_state *thread,
252 cothread_context *ctx = pthread_getspecific(_cothread_key);
254 return g_hash_table_lookup(ctx->data, key);
259 * @thread: cothread state to switch to
261 * Switches to the given cothread state
264 cothread_switch (cothread_state *thread)
266 cothread_context *ctx;
267 cothread_state *current;
270 #ifdef COTHREAD_PARANOID
271 if (thread == NULL) goto nothread;
274 #ifdef COTHREAD_PARANOID
275 if (ctx == NULL) goto nocontext;
278 current = ctx->threads[ctx->current];
279 #ifdef COTHREAD_PARANOID
280 if (current == NULL) goto nocurrent;
282 if (current == thread) goto selfswitch;
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;
289 /* save the current stack pointer, frame pointer, and pc */
290 #ifdef GST_ARCH_PRESETJMP
291 GST_ARCH_PRESETJMP();
293 enter = setjmp(current->jmp);
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);
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);
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");
308 longjmp(thread->jmp,1);
310 GST_ARCH_SETUP_STACK(thread->sp);
311 GST_ARCH_SET_SP(thread->sp);
313 GST_ARCH_CALL(cothread_stub);
314 GST_DEBUG (0,"exit thread \n");
320 #ifdef COTHREAD_PARANOID
322 g_print("cothread: there's no thread, strange...\n");
325 g_print("cothread: there's no context, help!\n");
328 g_print("cothread: there's no current thread, help!\n");
330 #endif /* COTHREAD_PARANOID */
332 g_print("cothread: trying to switch to same thread, legal but not necessary\n");