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 STACK_SIZE 0x200000
41 #define COTHREAD_MAXTHREADS 16
42 #define COTHREAD_STACKSIZE (STACK_SIZE/COTHREAD_MAXTHREADS)
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 /* we consider the initiating process to be cothread 0 */
73 ctx->data = g_hash_table_new(g_str_hash, g_str_equal);
75 GST_INFO (GST_CAT_COTHREADS,"initializing cothreads");
77 if (_cothread_key == -1) {
78 if (pthread_key_create (&_cothread_key,NULL) != 0) {
79 perror ("pthread_key_create");
83 pthread_setspecific (_cothread_key,ctx);
85 memset (ctx->threads,0,sizeof(ctx->threads));
87 ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
88 ctx->threads[0]->ctx = ctx;
89 ctx->threads[0]->threadnum = 0;
90 ctx->threads[0]->func = NULL;
91 ctx->threads[0]->argc = 0;
92 ctx->threads[0]->argv = NULL;
93 ctx->threads[0]->flags = COTHREAD_STARTED;
94 ctx->threads[0]->sp = (void *)CURRENT_STACK_FRAME;
95 ctx->threads[0]->pc = 0;
97 /* initialize the lock */
98 #ifdef COTHREAD_ATOMIC
99 atomic_set (&ctx->threads[0]->lock, 0);
101 ctx->threads[0]->lock = g_mutex_new();
104 GST_INFO (GST_CAT_COTHREADS,"0th thread is %p at sp:%p",ctx->threads[0], ctx->threads[0]->sp);
110 cothread_free (cothread_context *ctx)
114 for (i=0; i<ctx->nthreads; i++) {
115 #ifndef COTHREAD_ATOMIC
116 if (ctx->threads[i]->lock) {
117 g_mutex_unlock(ctx->threads[i]->lock);
118 g_mutex_free (ctx->threads[i]->lock);
119 ctx->threads[i]->lock = NULL;
123 g_free (ctx->threads[i]);
124 ctx->threads[i] = NULL;
127 g_hash_table_destroy (ctx->data);
133 * @ctx: the cothread context
135 * Create a new cothread state in the given context
137 * Returns: the new cothread state or NULL on error
140 cothread_create (cothread_context *ctx)
144 if (ctx->nthreads == COTHREAD_MAXTHREADS) {
145 GST_DEBUG (0, "attempt to create > COTHREAD_MAXTHREADS\n");
148 GST_DEBUG (0,"pthread_self() %ld\n",pthread_self());
150 if (pthread_self() == 0) { /* FIXME uh, what does this test really do? */
151 s = (cothread_state *)malloc(COTHREAD_STACKSIZE);
152 GST_DEBUG (0,"new stack (case 1) at %p\n",s);
154 void *sp = CURRENT_STACK_FRAME;
155 /* FIXME this may not be 64bit clean
156 * could use casts to uintptr_t from inttypes.h
157 * if only all platforms had inttypes.h
159 guchar *stack_end = (guchar *)((unsigned long)sp & ~(STACK_SIZE - 1));
160 s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
161 COTHREAD_STACKSIZE));
162 GST_DEBUG (0,"new stack (case 2) at %p\n",s);
163 if (mmap((void *)s,COTHREAD_STACKSIZE,
164 PROT_READ|PROT_WRITE|PROT_EXEC,MAP_FIXED|MAP_PRIVATE|MAP_ANON,
166 perror("mmap'ing cothread stack space");
172 s->threadnum = ctx->nthreads;
174 s->sp = ((guchar *)s + COTHREAD_STACKSIZE);
175 /* is this needed anymore? */
178 /* initialize the lock */
179 #ifdef COTHREAD_ATOMIC
180 atomic_set (s->lock, 0);
182 s->lock = g_mutex_new();
185 GST_INFO (GST_CAT_COTHREADS,"created cothread #%d: %p at sp:%p lock:%p", ctx->nthreads,
188 ctx->threads[ctx->nthreads++] = s;
195 * @thread: the cothread state
196 * @func: the function to call
197 * @argc: argument count for the cothread function
198 * @argv: arguments for the cothread function
200 * Set the cothread function
203 cothread_setfunc (cothread_state *thread,
211 thread->pc = (void *)func;
216 * @ctx: cothread context to find main thread of
218 * Get the main thread.
220 * Returns: the #cothread_state of the main (0th) thread
223 cothread_main (cothread_context *ctx)
225 GST_DEBUG (0,"returning %p, the 0th cothread\n",ctx->threads[0]);
226 return ctx->threads[0];
230 * cothread_current_main:
232 * Get the main thread in the current pthread.
234 * Returns: the #cothread_state of the main (0th) thread in the current pthread
237 cothread_current_main (void)
239 cothread_context *ctx = pthread_getspecific(_cothread_key);
240 return ctx->threads[0];
246 cothread_context *ctx = pthread_getspecific(_cothread_key);
247 register cothread_state *thread = ctx->threads[ctx->current];
251 thread->flags |= COTHREAD_STARTED;
252 /* #ifdef COTHREAD_ATOMIC
253 * do something here to lock
255 * g_mutex_lock(thread->lock);
259 thread->func(thread->argc,thread->argv);
260 /* we do this to avoid ever returning, we just switch to 0th thread */
261 cothread_switch(cothread_main(ctx));
263 thread->flags &= ~COTHREAD_STARTED;
265 thread->sp = thread->top_sp;
266 fprintf(stderr,"uh, yeah, we shouldn't be here, but we should deal anyway\n");
271 * cothread_getcurrent:
273 * Get the current cothread id
275 * Returns: the current cothread id
277 int cothread_getcurrent (void) __attribute__ ((no_instrument_function));
279 cothread_getcurrent (void)
281 cothread_context *ctx = pthread_getspecific(_cothread_key);
288 * @thread: the cothread state
289 * @key: a key for the data
292 * adds data to a cothread
295 cothread_set_data (cothread_state *thread,
299 cothread_context *ctx = pthread_getspecific(_cothread_key);
301 g_hash_table_insert(ctx->data, key, data);
306 * @thread: the cothread state
307 * @key: a key for the data
309 * get data from the cothread
311 * Returns: the data assiciated with the key
314 cothread_get_data (cothread_state *thread,
317 cothread_context *ctx = pthread_getspecific(_cothread_key);
319 return g_hash_table_lookup(ctx->data, key);
324 * @thread: cothread state to switch to
326 * Switches to the given cothread state
329 cothread_switch (cothread_state *thread)
331 cothread_context *ctx;
332 cothread_state *current;
335 #ifdef COTHREAD_PARANOID
336 if (thread == NULL) goto nothread;
339 #ifdef COTHREAD_PARANOID
340 if (ctx == NULL) goto nocontext;
343 current = ctx->threads[ctx->current];
344 #ifdef COTHREAD_PARANOID
345 if (current == NULL) goto nocurrent;
347 if (current == thread) goto selfswitch;
349 /* unlock the current thread, we're out of that context now */
350 #ifdef COTHREAD_ATOMIC
351 /* do something to unlock the cothread */
353 g_mutex_unlock(current->lock);
356 /* lock the next cothread before we even switch to it */
357 #ifdef COTHREAD_ATOMIC
358 /* do something to lock the cothread */
360 g_mutex_lock(thread->lock);
363 /* find the number of the thread to switch to */
364 GST_INFO (GST_CAT_COTHREAD_SWITCH,"switching from cothread #%d to cothread #%d",
365 ctx->current,thread->threadnum);
366 ctx->current = thread->threadnum;
368 /* save the current stack pointer, frame pointer, and pc */
369 #ifdef GST_ARCH_PRESETJMP
370 GST_ARCH_PRESETJMP();
372 enter = sigsetjmp(current->jmp, 1);
374 GST_DEBUG (0,"enter thread #%d %d %p<->%p (%d)\n",current->threadnum, enter,
375 current->sp, current->top_sp, current->top_sp-current->sp);
378 GST_DEBUG (0,"exit thread #%d %d %p<->%p (%d)\n",current->threadnum, enter,
379 current->sp, current->top_sp, current->top_sp-current->sp);
382 GST_DEBUG (0,"set stack to %p\n", thread->sp);
383 /* restore stack pointer and other stuff of new cothread */
384 if (thread->flags & COTHREAD_STARTED) {
385 GST_DEBUG (0,"in thread \n");
387 siglongjmp(thread->jmp,1);
389 GST_ARCH_SETUP_STACK(thread->sp);
390 GST_ARCH_SET_SP(thread->sp);
392 GST_ARCH_CALL(cothread_stub);
393 GST_DEBUG (0,"exit thread \n");
399 #ifdef COTHREAD_PARANOID
401 g_print("cothread: can't switch to NULL cothread!\n");
404 g_print("cothread: there's no context, help!\n");
407 g_print("cothread: there's no current thread, help!\n");
409 #endif /* COTHREAD_PARANOID */
411 g_print("cothread: trying to switch to same thread, legal but not necessary\n");
417 * @thread: cothread state to lock
419 * Locks the cothread state.
422 cothread_lock (cothread_state *thread)
424 #ifdef COTHREAD_ATOMIC
425 /* do something to lock the cothread */
428 g_mutex_lock(thread->lock);
434 * @thread: cothread state to try to lock
436 * Try to lock the cothread state
438 * Returns: TRUE if the cothread could be locked.
441 cothread_trylock (cothread_state *thread)
443 #ifdef COTHREAD_ATOMIC
444 /* do something to try to lock the cothread */
447 return g_mutex_trylock(thread->lock);
455 * @thread: cothread state to unlock
457 * Unlock the cothread state.
460 cothread_unlock (cothread_state *thread)
462 #ifdef COTHREAD_ATOMIC
463 /* do something to unlock the cothread */
466 g_mutex_unlock(thread->lock);