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.
34 #include "gst_private.h"
36 #include "cothreads.h"
41 #ifdef HAVE_UCONTEXT_H
47 /* older glibc's have MAP_ANON instead of MAP_ANONYMOUS */
48 #define MAP_ANONYMOUS MAP_ANON
50 /* make due without. If this fails, we need to open and map /dev/zero */
51 #define MAP_ANONYMOUS 0
55 #define STACK_SIZE 0x200000
57 #define COTHREAD_MAGIC_NUMBER 0xabcdef
59 #define COTHREAD_MAXTHREADS 16
60 #define COTHREAD_STACKSIZE (STACK_SIZE/COTHREAD_MAXTHREADS)
62 static void cothread_destroy (cothread_state * cothread);
64 struct _cothread_context
66 cothread_state *cothreads[COTHREAD_MAXTHREADS]; /* array of cothread states */
69 unsigned long stack_top;
74 /* Disabling this define allows you to shut off a few checks in
75 * cothread_switch. This likely will speed things up fractionally */
76 #define COTHREAD_PARANOID
79 /* this _cothread_ctx_key is used as a GThread key to the thread's context
80 * a GThread key is a "pointer" to memory space that is/can be different
81 * (ie. private) for each thread. The key itself is shared among threads,
82 * so it only needs to be initialized once.
84 static GStaticPrivate _cothread_ctx_key = G_STATIC_PRIVATE_INIT;
87 * This should only after context init, since we do checking.
89 static cothread_context *
90 cothread_get_current_context (void)
92 cothread_context *ctx;
94 ctx = g_static_private_get (&_cothread_ctx_key);
97 #ifdef COTHREAD_PARANOID
98 g_assert (ctx->thread == g_thread_self ());
105 * cothread_context_init:
107 * Create and initialize a new cothread context
109 * Returns: the new cothread context
112 cothread_context_init (void)
115 void *current_stack_frame = &__csf; /* Get pointer inside current stack frame */
116 cothread_context *ctx;
118 /* if there already is a cotread context for this thread,
120 ctx = g_static_private_get (&_cothread_ctx_key);
122 GST_CAT_INFO (GST_CAT_COTHREADS,
123 "returning private _cothread_ctx_key %p", ctx);
128 * initalize the whole of the cothreads context
130 ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
132 /* we consider the initiating process to be cothread 0 */
135 ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
136 ctx->thread = g_thread_self ();
138 GST_CAT_INFO (GST_CAT_COTHREADS, "initializing cothreads");
140 /* set this thread's context pointer */
141 GST_CAT_INFO (GST_CAT_COTHREADS,
142 "setting private _cothread_ctx_key to %p in thread %p", ctx,
144 g_static_private_set (&_cothread_ctx_key, ctx, NULL);
146 g_assert (ctx == cothread_get_current_context ());
148 /* clear the cothread data */
149 memset (ctx->cothreads, 0, sizeof (ctx->cothreads));
151 /* FIXME this may not be 64bit clean
152 * could use casts to uintptr_t from inttypes.h
153 * if only all platforms had inttypes.h
155 /* stack_top is the address of the first byte past our stack segment. */
156 /* FIXME: an assumption is made that the stack segment is STACK_SIZE
158 ctx->stack_top = ((gulong) current_stack_frame | (STACK_SIZE - 1)) + 1;
159 GST_CAT_DEBUG (GST_CAT_COTHREADS, "stack top is 0x%08lx", ctx->stack_top);
162 * initialize the 0th cothread
164 ctx->cothreads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
165 ctx->cothreads[0]->ctx = ctx;
166 ctx->cothreads[0]->cothreadnum = 0;
167 ctx->cothreads[0]->func = NULL;
168 ctx->cothreads[0]->argc = 0;
169 ctx->cothreads[0]->argv = NULL;
170 ctx->cothreads[0]->priv = NULL;
171 ctx->cothreads[0]->flags = COTHREAD_STARTED;
172 ctx->cothreads[0]->sp = (void *) current_stack_frame;
174 GST_CAT_INFO (GST_CAT_COTHREADS, "0th cothread is %p at sp:%p",
175 ctx->cothreads[0], ctx->cothreads[0]->sp);
181 * cothread_context_free:
182 * @ctx: the cothread context to free
184 * Free the cothread context.
187 cothread_context_free (cothread_context * ctx)
191 g_return_if_fail (ctx != NULL);
192 g_assert (ctx->thread == g_thread_self ());
193 g_assert (ctx->current == 0);
195 GST_CAT_INFO (GST_CAT_COTHREADS, "free cothread context");
197 for (i = 1; i < COTHREAD_MAXTHREADS; i++) {
198 if (ctx->cothreads[i]) {
199 cothread_destroy (ctx->cothreads[i]);
202 if (ctx->cothreads[0]) {
203 g_free (ctx->cothreads[0]);
204 ctx->cothreads[0] = NULL;
206 g_hash_table_destroy (ctx->data);
207 /* make sure we free the private key for cothread context */
208 GST_CAT_INFO (GST_CAT_COTHREADS,
209 "setting private _cothread_ctx_key to NULL in thread %p",
211 g_static_private_set (&_cothread_ctx_key, NULL, NULL);
217 * @ctx: the cothread context
219 * Create a new cothread state in the given context
221 * Returns: the new cothread state or NULL on error
224 cothread_create (cothread_context * ctx)
226 cothread_state *cothread;
229 unsigned long page_size;
231 g_return_val_if_fail (ctx != NULL, NULL);
233 GST_CAT_DEBUG (GST_CAT_COTHREADS, "manager sef %p, cothread self %p",
234 ctx->thread, g_thread_self ());
236 if (ctx->ncothreads == COTHREAD_MAXTHREADS) {
237 /* this is pretty fatal */
238 g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS");
241 /* find a free spot in the stack, note slot 0 has the main thread */
242 for (slot = 1; slot < ctx->ncothreads; slot++) {
243 if (ctx->cothreads[slot] == NULL)
245 else if (ctx->cothreads[slot]->flags & COTHREAD_DESTROYED &&
246 slot != ctx->current) {
247 cothread_destroy (ctx->cothreads[slot]);
252 GST_CAT_DEBUG (GST_CAT_COTHREADS, "Found free cothread slot %d", slot);
254 /* cothread stack space of the thread is mapped in reverse, with cothread 0
255 * stack space at the top */
257 (cothread_state *) (ctx->stack_top - (slot + 1) * COTHREAD_STACKSIZE);
258 GST_CAT_DEBUG (GST_CAT_COTHREADS, "cothread pointer is %p", cothread);
261 /* This tests to see whether or not we can grow down the stack */
265 for (ptr = ctx->stack_top - 4096; ptr > (unsigned long) cothread;
267 GST_CAT_DEBUG (GST_CAT_COTHREADS, "touching location 0x%08lx", ptr);
268 *(volatile unsigned int *) ptr = *(volatile unsigned int *) ptr;
269 GST_CAT_DEBUG (GST_CAT_COTHREADS, "ok (0x%08x)", *(unsigned int *) ptr);
275 page_size = sysconf (_SC_PAGESIZE);
277 page_size = getpagesize ();
280 /* The mmap is necessary on Linux/i386, and possibly others, since the
281 * kernel is picky about when we can expand our stack. */
282 GST_CAT_DEBUG (GST_CAT_COTHREADS, "mmaping %p, size 0x%08x", cothread,
284 /* Remap with a guard page. This decreases our stack size by 8 kB (for
285 * 4 kB pages) and also wastes almost 4 kB for the cothreads
287 munmap ((void *) cothread, COTHREAD_STACKSIZE);
288 mmaped = mmap ((void *) cothread, page_size,
289 PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
290 mmaped = mmap (((void *) cothread) + page_size * 2,
291 COTHREAD_STACKSIZE - page_size * 2,
292 PROT_READ | PROT_WRITE | PROT_EXEC,
293 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
294 GST_CAT_DEBUG (GST_CAT_COTHREADS, "coming out of mmap");
295 if (mmaped == MAP_FAILED) {
296 perror ("mmap'ing cothread stack space");
299 if (mmaped != (void *) cothread + page_size * 2) {
300 g_warning ("could not mmap requested memory for cothread");
304 cothread->magic_number = COTHREAD_MAGIC_NUMBER;
305 GST_CAT_DEBUG (GST_CAT_COTHREADS,
306 "create cothread %d with magic number 0x%x", slot,
307 cothread->magic_number);
309 cothread->cothreadnum = slot;
311 cothread->priv = NULL;
312 cothread->sp = ((guchar *) cothread + COTHREAD_STACKSIZE);
313 cothread->stack_size = COTHREAD_STACKSIZE - page_size * 2;
314 cothread->stack_base = (void *) cothread + 2 * page_size;
316 GST_CAT_INFO (GST_CAT_COTHREADS,
317 "created cothread #%d in slot %d: %p at sp:%p",
318 ctx->ncothreads, slot, cothread, cothread->sp);
320 ctx->cothreads[slot] = cothread;
328 * @cothread: the cothread state
330 * Free the given cothread state
333 cothread_free (cothread_state * cothread)
335 g_return_if_fail (cothread != NULL);
337 GST_CAT_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction",
338 cothread->cothreadnum);
340 /* we simply flag the cothread for destruction here */
342 cothread->flags |= COTHREAD_DESTROYED;
344 g_warning ("somebody set up us the bomb");
348 cothread_destroy (cothread_state * cothread)
350 cothread_context *ctx;
353 g_return_if_fail (cothread != NULL);
355 cothreadnum = cothread->cothreadnum;
357 g_assert (ctx->thread == g_thread_self ());
358 g_assert (ctx == cothread_get_current_context ());
360 GST_CAT_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d",
361 cothreadnum, cothread, ctx->current);
363 /* cothread 0 needs to be destroyed specially */
364 g_assert (cothreadnum != 0);
366 /* we have to unlock here because we might be switched out
367 * with the lock held */
368 cothread_unlock (cothread);
370 /* doing cleanups of the cothread create */
371 GST_CAT_DEBUG (GST_CAT_COTHREADS,
372 "destroy cothread %d with magic number 0x%x", cothreadnum,
373 cothread->magic_number);
374 g_assert (cothread->magic_number == COTHREAD_MAGIC_NUMBER);
376 g_assert (cothread->priv == NULL);
378 memset (cothread, 0, sizeof (*cothread));
380 ctx->cothreads[cothreadnum] = NULL;
386 * @cothread: the cothread state
387 * @func: the function to call
388 * @argc: argument count for the cothread function
389 * @argv: arguments for the cothread function
391 * Set the cothread function
394 cothread_setfunc (cothread_state * cothread, cothread_func func, int argc,
397 cothread->func = func;
398 cothread->argc = argc;
399 cothread->argv = argv;
404 * @cothread: the cothread to stop
406 * Stop the cothread and reset the stack and program counter.
409 cothread_stop (cothread_state * cothread)
411 cothread->flags &= ~COTHREAD_STARTED;
416 * @ctx: cothread context to find main cothread of.
418 * Gets the main thread.
420 * Returns: the #cothread_state of the main (0th) cothread.
423 cothread_main (cothread_context * ctx)
425 g_assert (ctx->thread == g_thread_self ());
427 GST_CAT_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread",
429 return ctx->cothreads[0];
433 * cothread_current_main:
435 * Get the main thread in the current GThread.
437 * Returns: the #cothread_state of the main (0th) thread in the current GThread
440 cothread_current_main (void)
442 cothread_context *ctx = cothread_get_current_context ();
444 return ctx->cothreads[0];
450 * Get the currenttly executing cothread
452 * Returns: the #cothread_state of the current cothread
455 cothread_current (void)
457 cothread_context *ctx = cothread_get_current_context ();
459 return ctx->cothreads[ctx->current];
465 cothread_context *ctx = cothread_get_current_context ();
466 cothread_state *cothread = ctx->cothreads[ctx->current];
468 #ifndef GST_DISABLE_GST_DEBUG
470 void *current_stack_frame = &__csf;
473 GST_CAT_DEBUG (GST_CAT_COTHREADS, "stack addr %p", &ctx);
475 cothread->flags |= COTHREAD_STARTED;
478 cothread->func (cothread->argc, cothread->argv);
480 GST_CAT_DEBUG (GST_CAT_COTHREADS, "cothread[%d] thread->func exited",
483 GST_CAT_DEBUG (GST_CAT_COTHREADS, "sp=%p", current_stack_frame);
484 GST_CAT_DEBUG (GST_CAT_COTHREADS, "ctx=%p current=%p", ctx,
485 cothread_get_current_context ());
486 g_assert (ctx == cothread_get_current_context ());
488 g_assert (ctx->current != 0);
490 /* we do this to avoid ever returning, we just switch to 0th thread */
491 cothread_switch (cothread_main (ctx));
496 * cothread_getcurrent:
498 * Get the current cothread id
500 * Returns: the current cothread id
503 cothread_getcurrent (void)
504 G_GNUC_NO_INSTRUMENT;
506 int cothread_getcurrent (void)
508 cothread_context *ctx = cothread_get_current_context ();
517 * cothread_set_private:
518 * @cothread: the cothread state
521 * set private data for the cothread.
524 cothread_set_private (cothread_state * cothread, gpointer data)
526 cothread->priv = data;
530 * cothread_context_set_data:
531 * @cothread: the cothread state
532 * @key: a key for the data
535 * adds data to a cothread
538 cothread_context_set_data (cothread_state * cothread, gchar * key,
541 cothread_context *ctx = cothread_get_current_context ();
543 g_hash_table_insert (ctx->data, key, data);
547 * cothread_get_private:
548 * @cothread: the cothread state
550 * get the private data from the cothread
552 * Returns: the private data of the cothread
555 cothread_get_private (cothread_state * cothread)
557 return cothread->priv;
561 * cothread_context_get_data:
562 * @cothread: the cothread state
563 * @key: a key for the data
565 * get data from the cothread
567 * Returns: the data associated with the key
570 cothread_context_get_data (cothread_state * cothread, gchar * key)
572 cothread_context *ctx = cothread_get_current_context ();
574 return g_hash_table_lookup (ctx->data, key);
579 * @cothread: cothread state to switch to
581 * Switches to the given cothread state
584 cothread_switch (cothread_state * cothread)
586 cothread_context *ctx;
587 cothread_state *current;
590 #ifdef COTHREAD_PARANOID
591 if (cothread == NULL)
596 /* paranoia check to make sure we're in the right thread */
597 g_assert (ctx->thread == g_thread_self ());
599 #ifdef COTHREAD_PARANOID
604 current = ctx->cothreads[ctx->current];
605 #ifdef COTHREAD_PARANOID
609 if (current == cothread)
613 /* find the number of the thread to switch to */
614 GST_CAT_INFO (GST_CAT_COTHREAD_SWITCH,
615 "switching from cothread #%d to cothread #%d",
616 ctx->current, cothread->cothreadnum);
617 ctx->current = cothread->cothreadnum;
619 /* save the current stack pointer, frame pointer, and pc */
620 #ifdef GST_ARCH_PRESETJMP
621 GST_ARCH_PRESETJMP ();
623 enter = setjmp (current->jmp);
625 GST_CAT_DEBUG (GST_CAT_COTHREADS,
626 "enter cothread #%d %d sp=%p jmpbuf=%p",
627 current->cothreadnum, enter, current->sp, current->jmp);
630 GST_CAT_DEBUG (GST_CAT_COTHREADS, "exit cothread #%d %d sp=%p jmpbuf=%p",
631 current->cothreadnum, enter, current->sp, current->jmp);
634 if (current->flags & COTHREAD_DESTROYED) {
635 cothread_destroy (current);
638 GST_CAT_DEBUG (GST_CAT_COTHREADS, "set stack to %p", cothread->sp);
639 /* restore stack pointer and other stuff of new cothread */
640 if (cothread->flags & COTHREAD_STARTED) {
641 GST_CAT_DEBUG (GST_CAT_COTHREADS, "via longjmp() jmpbuf %p", cothread->jmp);
643 longjmp (cothread->jmp, 1);
645 #ifdef HAVE_MAKECONTEXT
648 GST_CAT_DEBUG (GST_CAT_COTHREADS, "making context");
650 g_assert (cothread != cothread_main (ctx));
653 ucp.uc_stack.ss_sp = (void *) cothread->stack_base;
654 ucp.uc_stack.ss_size = cothread->stack_size;
655 makecontext (&ucp, cothread_stub, 0);
658 GST_ARCH_SETUP_STACK ((char *) cothread->sp);
659 GST_ARCH_SET_SP (cothread->sp);
661 GST_ARCH_CALL (cothread_stub);
664 GST_CAT_DEBUG (GST_CAT_COTHREADS, "exit thread ");
670 #ifdef COTHREAD_PARANOID
672 g_warning ("cothread: can't switch to NULL cothread!");
675 g_warning ("cothread: there's no context, help!");
678 g_warning ("cothread: there's no current thread, help!");
680 #endif /* COTHREAD_PARANOID */
683 ("cothread: trying to switch to same thread, legal but not necessary");
689 * @cothread: cothread state to lock
691 * Locks the cothread state.
694 cothread_lock (cothread_state * cothread)
700 * @cothread: cothread state to try to lock
702 * Try to lock the cothread state
704 * Returns: TRUE if the cothread could be locked.
707 cothread_trylock (cothread_state * cothread)
714 * @cothread: cothread state to unlock
716 * Unlock the cothread state.
719 cothread_unlock (cothread_state * cothread)