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.
33 #include "gst_private.h"
35 #include "cothreads.h"
40 #ifdef HAVE_UCONTEXT_H
44 /* older glibc's have MAP_ANON instead of MAP_ANONYMOUS */
46 #define MAP_ANONYMOUS MAP_ANON
49 #define STACK_SIZE 0x200000
51 #define COTHREAD_MAGIC_NUMBER 0xabcdef
53 #define COTHREAD_MAXTHREADS 16
54 #define COTHREAD_STACKSIZE (STACK_SIZE/COTHREAD_MAXTHREADS)
56 static void cothread_destroy (cothread_state *cothread);
58 struct _cothread_context
60 cothread_state *cothreads[COTHREAD_MAXTHREADS]; /* array of cothread states */
63 unsigned long stack_top;
68 /* Disabling this define allows you to shut off a few checks in
69 * cothread_switch. This likely will speed things up fractionally */
70 #define COTHREAD_PARANOID
73 /* this _cothread_ctx_key is used as a GThread key to the thread's context
74 * a GThread key is a "pointer" to memory space that is/can be different
75 * (ie. private) for each thread. The key itself is shared among threads,
76 * so it only needs to be initialized once.
78 static GStaticPrivate _cothread_ctx_key = G_STATIC_PRIVATE_INIT;
81 * This should only after context init, since we do checking.
83 static cothread_context *
84 cothread_get_current_context (void)
86 cothread_context *ctx;
88 ctx = g_static_private_get (&_cothread_ctx_key);
91 #ifdef COTHREAD_PARANOID
92 g_assert (ctx->thread == g_thread_self());
99 * cothread_context_init:
101 * Create and initialize a new cothread context
103 * Returns: the new cothread context
106 cothread_context_init (void)
109 void *current_stack_frame = &__csf; /* Get pointer inside current stack frame */
110 cothread_context *ctx;
112 /* if there already is a cotread context for this thread,
114 ctx = g_static_private_get (&_cothread_ctx_key);
116 GST_INFO (GST_CAT_COTHREADS,
117 "returning private _cothread_ctx_key %p", ctx);
122 * initalize the whole of the cothreads context
124 ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
126 /* we consider the initiating process to be cothread 0 */
129 ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
130 ctx->thread = g_thread_self();
132 GST_INFO (GST_CAT_COTHREADS, "initializing cothreads");
134 /* set this thread's context pointer */
135 GST_INFO (GST_CAT_COTHREADS, "setting private _cothread_ctx_key to %p in thread %p",
136 ctx,g_thread_self());
137 g_static_private_set (&_cothread_ctx_key, ctx, NULL);
139 g_assert(ctx == cothread_get_current_context());
141 /* clear the cothread data */
142 memset (ctx->cothreads, 0, sizeof (ctx->cothreads));
144 /* FIXME this may not be 64bit clean
145 * could use casts to uintptr_t from inttypes.h
146 * if only all platforms had inttypes.h
148 /* stack_top is the address of the first byte past our stack segment. */
149 /* FIXME: an assumption is made that the stack segment is STACK_SIZE
151 ctx->stack_top = ((gulong) current_stack_frame | (STACK_SIZE - 1)) + 1;
152 GST_DEBUG (GST_CAT_COTHREADS, "stack top is 0x%08lx", ctx->stack_top);
155 * initialize the 0th cothread
157 ctx->cothreads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
158 ctx->cothreads[0]->ctx = ctx;
159 ctx->cothreads[0]->cothreadnum = 0;
160 ctx->cothreads[0]->func = NULL;
161 ctx->cothreads[0]->argc = 0;
162 ctx->cothreads[0]->argv = NULL;
163 ctx->cothreads[0]->priv = NULL;
164 ctx->cothreads[0]->flags = COTHREAD_STARTED;
165 ctx->cothreads[0]->sp = (void *) current_stack_frame;
167 GST_INFO (GST_CAT_COTHREADS, "0th cothread is %p at sp:%p",
168 ctx->cothreads[0], ctx->cothreads[0]->sp);
174 * cothread_context_free:
175 * @ctx: the cothread context to free
177 * Free the cothread context.
180 cothread_context_free (cothread_context *ctx)
184 g_return_if_fail (ctx != NULL);
185 g_assert (ctx->thread == g_thread_self());
186 g_assert (ctx->current == 0);
188 GST_INFO (GST_CAT_COTHREADS, "free cothread context");
190 for (i = 1; i < COTHREAD_MAXTHREADS; i++) {
191 if (ctx->cothreads[i]) {
192 cothread_destroy (ctx->cothreads[i]);
195 if (ctx->cothreads[0]) {
196 g_free(ctx->cothreads[0]);
197 ctx->cothreads[0] = NULL;
199 g_hash_table_destroy (ctx->data);
200 /* make sure we free the private key for cothread context */
201 GST_INFO (GST_CAT_COTHREADS, "setting private _cothread_ctx_key to NULL in thread %p",
203 g_static_private_set (&_cothread_ctx_key, NULL, NULL);
209 * @ctx: the cothread context
211 * Create a new cothread state in the given context
213 * Returns: the new cothread state or NULL on error
216 cothread_create (cothread_context *ctx)
218 cothread_state *cothread;
221 unsigned long page_size;
223 g_return_val_if_fail (ctx != NULL, NULL);
224 g_assert (ctx->thread == g_thread_self());
226 if (ctx->ncothreads == COTHREAD_MAXTHREADS) {
227 /* this is pretty fatal */
228 g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS");
231 /* find a free spot in the stack, note slot 0 has the main thread */
232 for (slot = 1; slot < ctx->ncothreads; slot++) {
233 if (ctx->cothreads[slot] == NULL)
235 else if (ctx->cothreads[slot]->flags & COTHREAD_DESTROYED &&
236 slot != ctx->current) {
237 cothread_destroy (ctx->cothreads[slot]);
242 GST_DEBUG (GST_CAT_COTHREADS, "Found free cothread slot %d", slot);
244 /* cothread stack space of the thread is mapped in reverse, with cothread 0
245 * stack space at the top */
246 cothread = (cothread_state *) (ctx->stack_top - (slot + 1) * COTHREAD_STACKSIZE);
247 GST_DEBUG (GST_CAT_COTHREADS, "cothread pointer is %p", cothread);
250 /* This tests to see whether or not we can grow down the stack */
253 for(ptr=ctx->stack_top - 4096; ptr > (unsigned long)cothread; ptr -= 4096){
254 GST_DEBUG (GST_CAT_COTHREADS, "touching location 0x%08lx", ptr);
255 *(volatile unsigned int *)ptr = *(volatile unsigned int *)ptr;
256 GST_DEBUG (GST_CAT_COTHREADS, "ok (0x%08x)", *(unsigned int *)ptr);
262 page_size = sysconf(_SC_PAGESIZE);
264 page_size = getpagesize();
267 /* The mmap is necessary on Linux/i386, and possibly others, since the
268 * kernel is picky about when we can expand our stack. */
269 GST_DEBUG (GST_CAT_COTHREADS, "mmaping %p, size 0x%08x", cothread,
271 /* Remap with a guard page. This decreases our stack size by 8 kB (for
272 * 4 kB pages) and also wastes almost 4 kB for the cothreads
274 munmap((void *)cothread, COTHREAD_STACKSIZE);
275 mmaped = mmap ((void *) cothread, page_size,
276 PROT_READ | PROT_WRITE,
277 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
278 mmaped = mmap (((void *) cothread) + page_size * 2,
279 COTHREAD_STACKSIZE - page_size * 2,
280 PROT_READ | PROT_WRITE | PROT_EXEC,
281 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
282 GST_DEBUG (GST_CAT_COTHREADS, "coming out of mmap");
283 if (mmaped == MAP_FAILED) {
284 perror ("mmap'ing cothread stack space");
287 if (mmaped != (void *)cothread + page_size * 2) {
288 g_warning ("could not mmap requested memory for cothread");
292 cothread->magic_number = COTHREAD_MAGIC_NUMBER;
293 GST_DEBUG (GST_CAT_COTHREADS, "create cothread %d with magic number 0x%x",
294 slot, cothread->magic_number);
296 cothread->cothreadnum = slot;
298 cothread->priv = NULL;
299 cothread->sp = ((guchar *) cothread + COTHREAD_STACKSIZE);
300 cothread->stack_size = COTHREAD_STACKSIZE - page_size * 2;
301 cothread->stack_base = (void *)cothread + 2 * page_size;
303 GST_INFO (GST_CAT_COTHREADS,
304 "created cothread #%d in slot %d: %p at sp:%p",
305 ctx->ncothreads, slot, cothread, cothread->sp);
307 ctx->cothreads[slot] = cothread;
315 * @cothread: the cothread state
317 * Free the given cothread state
320 cothread_free (cothread_state *cothread)
322 g_return_if_fail (cothread != NULL);
324 GST_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction",
325 cothread->cothreadnum);
327 /* we simply flag the cothread for destruction here */
329 cothread->flags |= COTHREAD_DESTROYED;
331 g_warning ("somebody set up us the bomb");
335 cothread_destroy (cothread_state *cothread)
337 cothread_context *ctx;
340 g_return_if_fail (cothread != NULL);
342 cothreadnum = cothread->cothreadnum;
344 g_assert (ctx->thread == g_thread_self());
345 g_assert (ctx == cothread_get_current_context());
347 GST_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d",
348 cothreadnum, cothread, ctx->current);
350 /* cothread 0 needs to be destroyed specially */
351 g_assert(cothreadnum != 0);
353 /* we have to unlock here because we might be switched out
354 * with the lock held */
355 cothread_unlock (cothread);
357 /* doing cleanups of the cothread create */
358 GST_DEBUG (GST_CAT_COTHREADS, "destroy cothread %d with magic number 0x%x",
359 cothreadnum, cothread->magic_number);
360 g_assert (cothread->magic_number == COTHREAD_MAGIC_NUMBER);
362 g_assert (cothread->priv == NULL);
364 memset(cothread,0,sizeof(*cothread));
366 ctx->cothreads[cothreadnum] = NULL;
372 * @cothread: the cothread state
373 * @func: the function to call
374 * @argc: argument count for the cothread function
375 * @argv: arguments for the cothread function
377 * Set the cothread function
380 cothread_setfunc (cothread_state* cothread, cothread_func func, int argc, char **argv)
382 cothread->func = func;
383 cothread->argc = argc;
384 cothread->argv = argv;
389 * @cothread: the cothread to stop
391 * Stop the cothread and reset the stack and program counter.
394 cothread_stop (cothread_state* cothread)
396 cothread->flags &= ~COTHREAD_STARTED;
401 * @ctx: cothread context to find main cothread of.
403 * Gets the main thread.
405 * Returns: the #cothread_state of the main (0th) cothread.
408 cothread_main (cothread_context* ctx)
410 g_assert (ctx->thread == g_thread_self());
412 GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread",
414 return ctx->cothreads[0];
418 * cothread_current_main:
420 * Get the main thread in the current GThread.
422 * Returns: the #cothread_state of the main (0th) thread in the current GThread
425 cothread_current_main (void)
427 cothread_context *ctx = cothread_get_current_context();
429 return ctx->cothreads[0];
435 * Get the currenttly executing cothread
437 * Returns: the #cothread_state of the current cothread
440 cothread_current (void)
442 cothread_context *ctx = cothread_get_current_context();
444 return ctx->cothreads[ctx->current];
450 cothread_context *ctx = cothread_get_current_context();
451 cothread_state *cothread = ctx->cothreads[ctx->current];
453 void *current_stack_frame = &__csf;
455 GST_DEBUG_ENTER ("");
457 GST_DEBUG (GST_CAT_COTHREADS, "stack addr %p", &ctx);
459 cothread->flags |= COTHREAD_STARTED;
462 cothread->func (cothread->argc, cothread->argv);
464 GST_DEBUG (GST_CAT_COTHREADS, "cothread[%d] thread->func exited", ctx->current);
466 GST_DEBUG (GST_CAT_COTHREADS, "sp=%p", current_stack_frame);
467 GST_DEBUG (GST_CAT_COTHREADS, "ctx=%p current=%p", ctx,cothread_get_current_context());
468 g_assert (ctx == cothread_get_current_context());
470 g_assert (ctx->current != 0);
472 /* we do this to avoid ever returning, we just switch to 0th thread */
473 cothread_switch (cothread_main (ctx));
475 GST_DEBUG_LEAVE ("");
479 * cothread_getcurrent:
481 * Get the current cothread id
483 * Returns: the current cothread id
485 int cothread_getcurrent (void) G_GNUC_NO_INSTRUMENT;
488 cothread_getcurrent (void)
490 cothread_context *ctx = cothread_get_current_context();
499 * cothread_set_private:
500 * @cothread: the cothread state
503 * set private data for the cothread.
506 cothread_set_private (cothread_state *cothread, gpointer data)
508 cothread->priv = data;
512 * cothread_context_set_data:
513 * @cothread: the cothread state
514 * @key: a key for the data
517 * adds data to a cothread
520 cothread_context_set_data (cothread_state *cothread, gchar *key, gpointer data)
522 cothread_context *ctx = cothread_get_current_context();
524 g_hash_table_insert (ctx->data, key, data);
528 * cothread_get_private:
529 * @cothread: the cothread state
531 * get the private data from the cothread
533 * Returns: the private data of the cothread
536 cothread_get_private (cothread_state *cothread)
538 return cothread->priv;
542 * cothread_context_get_data:
543 * @cothread: the cothread state
544 * @key: a key for the data
546 * get data from the cothread
548 * Returns: the data associated with the key
551 cothread_context_get_data (cothread_state *cothread, gchar *key)
553 cothread_context *ctx = cothread_get_current_context();
555 return g_hash_table_lookup (ctx->data, key);
560 * @cothread: cothread state to switch to
562 * Switches to the given cothread state
565 cothread_switch (cothread_state *cothread)
567 cothread_context *ctx;
568 cothread_state *current;
571 #ifdef COTHREAD_PARANOID
572 if (cothread == NULL)
577 /* paranoia check to make sure we're in the right thread */
578 g_assert (ctx->thread == g_thread_self());
580 #ifdef COTHREAD_PARANOID
585 current = ctx->cothreads[ctx->current];
586 #ifdef COTHREAD_PARANOID
590 if (current == cothread)
594 /* find the number of the thread to switch to */
595 GST_INFO (GST_CAT_COTHREAD_SWITCH,
596 "switching from cothread #%d to cothread #%d",
597 ctx->current, cothread->cothreadnum);
598 ctx->current = cothread->cothreadnum;
600 g_static_private_set (&_gst_debug_cothread_index,
601 GINT_TO_POINTER(ctx->current), NULL);
603 /* save the current stack pointer, frame pointer, and pc */
604 #ifdef GST_ARCH_PRESETJMP
605 GST_ARCH_PRESETJMP ();
607 enter = setjmp (current->jmp);
609 GST_DEBUG (GST_CAT_COTHREADS,
610 "enter cothread #%d %d sp=%p jmpbuf=%p",
611 current->cothreadnum, enter, current->sp, current->jmp);
614 GST_DEBUG (GST_CAT_COTHREADS, "exit cothread #%d %d sp=%p jmpbuf=%p",
615 current->cothreadnum, enter, current->sp, current->jmp);
618 if (current->flags & COTHREAD_DESTROYED) {
619 cothread_destroy (current);
622 GST_DEBUG (GST_CAT_COTHREADS, "set stack to %p", cothread->sp);
623 /* restore stack pointer and other stuff of new cothread */
624 if (cothread->flags & COTHREAD_STARTED) {
625 GST_DEBUG (GST_CAT_COTHREADS, "via longjmp() jmpbuf %p", cothread->jmp);
627 longjmp (cothread->jmp, 1);
630 #ifdef HAVE_MAKECONTEXT
633 GST_DEBUG (GST_CAT_COTHREADS, "making context");
635 g_assert (cothread != cothread_main(ctx));
638 ucp.uc_stack.ss_sp = (void *)cothread->stack_base;
639 ucp.uc_stack.ss_size = cothread->stack_size;
640 makecontext (&ucp, cothread_stub, 0);
643 GST_ARCH_SETUP_STACK ((char*)cothread->sp);
644 GST_ARCH_SET_SP (cothread->sp);
646 GST_ARCH_CALL (cothread_stub);
649 GST_DEBUG (GST_CAT_COTHREADS, "exit thread ");
655 #ifdef COTHREAD_PARANOID
657 g_warning ("cothread: can't switch to NULL cothread!");
660 g_warning ("cothread: there's no context, help!");
663 g_warning ("cothread: there's no current thread, help!");
665 #endif /* COTHREAD_PARANOID */
667 g_warning ("cothread: trying to switch to same thread, legal but not necessary");
673 * @cothread: cothread state to lock
675 * Locks the cothread state.
678 cothread_lock (cothread_state *cothread)
684 * @cothread: cothread state to try to lock
686 * Try to lock the cothread state
688 * Returns: TRUE if the cothread could be locked.
691 cothread_trylock (cothread_state *cothread)
698 * @cothread: cothread state to unlock
700 * Unlock the cothread state.
703 cothread_unlock (cothread_state *cothread)