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 /* older glibc's have MAP_ANON instead of MAP_ANONYMOUS */
42 #define MAP_ANONYMOUS MAP_ANON
45 #define STACK_SIZE 0x200000
47 #define COTHREAD_MAGIC_NUMBER 0xabcdef
49 #define COTHREAD_MAXTHREADS 16
50 #define COTHREAD_STACKSIZE (STACK_SIZE/COTHREAD_MAXTHREADS)
52 static void cothread_destroy (cothread_state *thread);
54 struct _cothread_context
56 cothread_state *cothreads[COTHREAD_MAXTHREADS]; /* array of cothread states */
59 unsigned long stack_top;
64 /* Disabling this define allows you to shut off a few checks in
65 * cothread_switch. This likely will speed things up fractionally */
66 #define COTHREAD_PARANOID
69 /* this _cothread_ctx_key is used as a GThread key to the thread's context
70 * a GThread key is a "pointer" to memory space that is/can be different
71 * (ie. private) for each thread. The key itself is shared among threads,
72 * so it only needs to be initialized once.
74 static GStaticPrivate _cothread_ctx_key = G_STATIC_PRIVATE_INIT;
77 * This should only after context init, since we do checking.
79 static cothread_context *
80 cothread_get_current_context (void)
82 cothread_context *ctx;
84 ctx = g_static_private_get (&_cothread_ctx_key);
87 #ifdef COTHREAD_PARANOID
88 g_assert (ctx->thread == g_thread_self());
95 * cothread_context_init:
97 * Create and initialize a new cothread context
99 * Returns: the new cothread context
102 cothread_context_init (void)
104 cothread_context *ctx;
107 /* if there already is a cotread context for this thread,
109 ctx = g_static_private_get (&_cothread_ctx_key);
111 GST_INFO (GST_CAT_COTHREADS,
112 "returning private _cothread_ctx_key %p", ctx);
117 * initalize the whole of the cothreads context
119 ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
121 /* we consider the initiating process to be cothread 0 */
124 ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
125 ctx->thread = g_thread_self();
127 GST_INFO (GST_CAT_COTHREADS, "initializing cothreads");
129 /* set this thread's context pointer */
130 GST_INFO (GST_CAT_COTHREADS, "setting private _cothread_ctx_key to %p",
132 g_static_private_set (&_cothread_ctx_key, ctx, NULL);
134 /* clear the cothread data */
135 memset (ctx->cothreads, 0, sizeof (ctx->cothreads));
137 sp = CURRENT_STACK_FRAME;
138 /* FIXME this may not be 64bit clean
139 * could use casts to uintptr_t from inttypes.h
140 * if only all platforms had inttypes.h
142 /* stack_top is the address of the first byte past our stack segment. */
143 /* FIXME: an assumption is made that the stack segment is STACK_SIZE
145 ctx->stack_top = ((gulong) sp | (STACK_SIZE - 1)) + 1;
146 GST_DEBUG (GST_CAT_COTHREADS, "stack top is 0x%08lx", ctx->stack_top);
149 * initialize the 0th cothread
151 ctx->cothreads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
152 ctx->cothreads[0]->ctx = ctx;
153 ctx->cothreads[0]->cothreadnum = 0;
154 ctx->cothreads[0]->func = NULL;
155 ctx->cothreads[0]->argc = 0;
156 ctx->cothreads[0]->argv = NULL;
157 ctx->cothreads[0]->priv = NULL;
158 ctx->cothreads[0]->flags = COTHREAD_STARTED;
159 ctx->cothreads[0]->sp = (void *) CURRENT_STACK_FRAME;
161 GST_INFO (GST_CAT_COTHREADS, "0th cothread is %p at sp:%p",
162 ctx->cothreads[0], ctx->cothreads[0]->sp);
168 * cothread_context_free:
169 * @ctx: the cothread context to free
171 * Free the cothread context.
174 cothread_context_free (cothread_context *ctx)
178 g_return_if_fail (ctx != NULL);
179 g_assert (ctx->thread == g_thread_self());
181 GST_INFO (GST_CAT_COTHREADS, "free cothread context");
183 for (i = 0; i < COTHREAD_MAXTHREADS; i++) {
184 if (ctx->cothreads[i]) {
185 cothread_destroy (ctx->cothreads[i]);
188 g_hash_table_destroy (ctx->data);
189 /* make sure we free the private key for cothread context */
190 g_static_private_set (&_cothread_ctx_key, NULL, NULL);
196 * @ctx: the cothread context
198 * Create a new cothread state in the given context
200 * Returns: the new cothread state or NULL on error
203 cothread_create (cothread_context *ctx)
205 cothread_state *cothread;
208 unsigned long page_size;
210 g_return_val_if_fail (ctx != NULL, NULL);
211 g_assert (ctx->thread == g_thread_self());
213 if (ctx->ncothreads == COTHREAD_MAXTHREADS) {
214 /* this is pretty fatal */
215 g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS\n");
218 /* find a free spot in the stack, note slot 0 has the main thread */
219 for (slot = 1; slot < ctx->ncothreads; slot++) {
220 if (ctx->cothreads[slot] == NULL)
222 else if (ctx->cothreads[slot]->flags & COTHREAD_DESTROYED &&
223 slot != ctx->current) {
224 cothread_destroy (ctx->cothreads[slot]);
229 GST_DEBUG (GST_CAT_COTHREADS, "Found free cothread slot %d", slot);
231 /* cothread stack space of the thread is mapped in reverse, with cothread 0
232 * stack space at the top */
233 cothread = (cothread_state *) (ctx->stack_top - (slot + 1) * COTHREAD_STACKSIZE);
234 GST_DEBUG (GST_CAT_COTHREADS, "cothread pointer is %p", cothread);
237 /* This tests to see whether or not we can grow down the stack */
240 for(ptr=ctx->stack_top - 4096; ptr > (unsigned long)cothread; ptr -= 4096){
241 GST_DEBUG (GST_CAT_COTHREADS, "touching location 0x%08lx", ptr);
242 *(volatile unsigned int *)ptr = *(volatile unsigned int *)ptr;
243 GST_DEBUG (GST_CAT_COTHREADS, "ok (0x%08x)", *(unsigned int *)ptr);
249 page_size = sysconf(_SC_PAGESIZE);
251 page_size = getpagesize();
254 /* The mmap is necessary on Linux/i386, and possibly others, since the
255 * kernel is picky about when we can expand our stack. */
256 GST_DEBUG (GST_CAT_COTHREADS, "mmaping %p, size 0x%08x", cothread,
258 /* Remap with a guard page. This decreases our stack size by 8 kB (for
259 * 4 kB pages) and also wastes almost 4 kB for the cothreads
261 munmap((void *)cothread, COTHREAD_STACKSIZE);
262 mmaped = mmap ((void *) cothread, page_size,
263 PROT_READ | PROT_WRITE,
264 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
265 mmaped = mmap (((void *) cothread) + page_size * 2,
266 COTHREAD_STACKSIZE - page_size * 2,
267 PROT_READ | PROT_WRITE | PROT_EXEC,
268 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
269 GST_DEBUG (GST_CAT_COTHREADS, "coming out of mmap");
270 if (mmaped == MAP_FAILED) {
271 perror ("mmap'ing cothread stack space");
274 if (mmaped != (void *)cothread + page_size * 2) {
275 g_warning ("could not mmap requested memory for cothread");
279 cothread->magic_number = COTHREAD_MAGIC_NUMBER;
280 GST_DEBUG (GST_CAT_COTHREADS, "create cothread %d with magic number 0x%x",
281 slot, cothread->magic_number);
283 cothread->cothreadnum = slot;
285 cothread->priv = NULL;
286 cothread->sp = ((guchar *) cothread + COTHREAD_STACKSIZE);
287 cothread->stack_size = COTHREAD_STACKSIZE - page_size * 2;
288 cothread->stack_base = (void *)cothread + 2 * page_size;
290 GST_INFO (GST_CAT_COTHREADS,
291 "created cothread #%d in slot %d: %p at sp:%p",
292 ctx->ncothreads, slot, cothread, cothread->sp);
294 ctx->cothreads[slot] = cothread;
302 * @cothread: the cothread state
304 * Free the given cothread state
307 cothread_free (cothread_state *cothread)
309 g_return_if_fail (cothread != NULL);
311 GST_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction",
312 cothread->cothreadnum);
314 /* we simply flag the cothread for destruction here */
316 cothread->flags |= COTHREAD_DESTROYED;
318 g_warning ("somebody set up us the bomb");
322 cothread_destroy (cothread_state *cothread)
324 cothread_context *ctx;
327 g_return_if_fail (cothread != NULL);
329 cothreadnum = cothread->cothreadnum;
331 g_assert (ctx->thread == g_thread_self());
333 GST_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d",
334 cothreadnum, cothread, ctx->current);
336 /* we have to unlock here because we might be switched out
337 * with the lock held */
338 cothread_unlock (cothread);
340 if (cothreadnum == 0)
342 GST_INFO (GST_CAT_COTHREADS,
343 "trying to destroy cothread 0 with %d cothreads left",
345 if (ctx->ncothreads > 1)
347 /* we're trying to destroy cothread 0 when there are still cothreads
348 * active, so kill those first */
351 for (i = 1; i < COTHREAD_MAXTHREADS; ++i)
353 if (ctx->cothreads[i] != NULL)
355 cothread_destroy (ctx->cothreads[i]);
356 GST_INFO (GST_CAT_COTHREADS,
357 "destroyed cothread %d, %d cothreads left",
362 g_assert (ctx->ncothreads == 1);
363 GST_INFO (GST_CAT_COTHREADS, "freeing 0th cothread");
368 * Replaced with version below until cothreads issues solved */
371 /* doing cleanups of the cothread create */
372 GST_DEBUG (GST_CAT_COTHREADS, "destroy cothread %d with magic number 0x%x",
373 cothreadnum, cothread->magic_number);
374 g_assert (cothread->magic_number == COTHREAD_MAGIC_NUMBER);
376 g_assert (cothread->priv == NULL);
378 GST_DEBUG (GST_CAT_COTHREADS,
379 "munmap cothread slot stack from %p to %p (size 0x%lx)",
380 cothread, cothread + COTHREAD_STACKSIZE,
381 (long) COTHREAD_STACKSIZE);
382 /* res = munmap (thread, COTHREAD_STACKSIZE);
383 * Commented out waiting for resolution for cothread issue */
389 g_warning ("munmap doesn't like start %p or length %d\n",
390 cothread, COTHREAD_STACKSIZE);
393 g_warning ("Thomas was too lazy to check for all errors, "
394 "so I can't tell you what is wrong.\n");
399 GST_DEBUG (GST_CAT_COTHREADS, "munmap done");
401 ctx->cothreads[cothreadnum] = NULL;
407 * @thread: the cothread state
408 * @func: the function to call
409 * @argc: argument count for the cothread function
410 * @argv: arguments for the cothread function
412 * Set the cothread function
415 cothread_setfunc (cothread_state * thread, cothread_func func, int argc, char **argv)
424 * @thread: the cothread to stop
426 * Stop the cothread and reset the stack and program counter.
429 cothread_stop (cothread_state * thread)
431 thread->flags &= ~COTHREAD_STARTED;
436 * @ctx: cothread context to find main cothread of.
438 * Gets the main thread.
440 * Returns: the #cothread_state of the main (0th) cothread.
443 cothread_main (cothread_context * ctx)
445 GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread",
447 return ctx->cothreads[0];
451 * cothread_current_main:
453 * Get the main thread in the current GThread.
455 * Returns: the #cothread_state of the main (0th) thread in the current GThread
458 cothread_current_main (void)
460 cothread_context *ctx = cothread_get_current_context();
462 return ctx->cothreads[0];
468 * Get the currenttly executing cothread
470 * Returns: the #cothread_state of the current cothread
473 cothread_current (void)
475 cothread_context *ctx = cothread_get_current_context();
477 return ctx->cothreads[ctx->current];
483 cothread_context *ctx = cothread_get_current_context();
484 register cothread_state *thread = ctx->cothreads[ctx->current];
486 GST_DEBUG_ENTER ("");
488 GST_DEBUG (GST_CAT_COTHREADS, "stack addr %p\n", &ctx);
490 thread->flags |= COTHREAD_STARTED;
493 thread->func (thread->argc, thread->argv);
494 /* we do this to avoid ever returning, we just switch to 0th thread */
495 cothread_switch (cothread_main (ctx));
497 GST_DEBUG_LEAVE ("");
501 * cothread_getcurrent:
503 * Get the current cothread id
505 * Returns: the current cothread id
507 int cothread_getcurrent (void) G_GNUC_NO_INSTRUMENT;
510 cothread_getcurrent (void)
512 cothread_context *ctx = cothread_get_current_context();
520 * cothread_set_private:
521 * @thread: the cothread state
524 * set private data for the cothread.
527 cothread_set_private (cothread_state *thread, gpointer data)
533 * cothread_context_set_data:
534 * @thread: the cothread state
535 * @key: a key for the data
538 * adds data to a cothread
541 cothread_context_set_data (cothread_state *thread, gchar *key, gpointer data)
543 cothread_context *ctx = cothread_get_current_context();
545 g_hash_table_insert (ctx->data, key, data);
549 * cothread_get_private:
550 * @thread: the cothread state
552 * get the private data from the cothread
554 * Returns: the private data of the cothread
557 cothread_get_private (cothread_state *thread)
563 * cothread_context_get_data:
564 * @thread: the cothread state
565 * @key: a key for the data
567 * get data from the cothread
569 * Returns: the data associated with the key
572 cothread_context_get_data (cothread_state * thread, gchar * key)
574 cothread_context *ctx = cothread_get_current_context();
576 return g_hash_table_lookup (ctx->data, key);
581 * @thread: cothread state to switch to
583 * Switches to the given cothread state
586 cothread_switch (cothread_state * thread)
588 cothread_context *ctx;
589 cothread_state *current;
592 #ifdef COTHREAD_PARANOID
598 /* paranoia check to make sure we're in the right thread */
599 g_assert (ctx->thread == g_thread_self());
601 #ifdef COTHREAD_PARANOID
606 current = ctx->cothreads[ctx->current];
607 #ifdef COTHREAD_PARANOID
611 if (current == thread)
615 /* find the number of the thread to switch to */
616 GST_INFO (GST_CAT_COTHREAD_SWITCH,
617 "switching from cothread #%d to cothread #%d",
618 ctx->current, thread->cothreadnum);
619 ctx->current = thread->cothreadnum;
621 /* save the current stack pointer, frame pointer, and pc */
622 #ifdef GST_ARCH_PRESETJMP
623 GST_ARCH_PRESETJMP ();
625 enter = setjmp (current->jmp);
627 GST_DEBUG (GST_CAT_COTHREADS,
628 "enter cothread #%d %d sp=%p jmpbuf=%p",
629 current->cothreadnum, enter, current->sp, current->jmp);
632 GST_DEBUG (GST_CAT_COTHREADS, "exit cothread #%d %d sp=%p jmpbuf=%p",
633 current->cothreadnum, enter, current->sp, current->jmp);
636 if (current->flags & COTHREAD_DESTROYED) {
637 cothread_destroy (current);
640 GST_DEBUG (GST_CAT_COTHREADS, "set stack to %p", thread->sp);
641 /* restore stack pointer and other stuff of new cothread */
642 if (thread->flags & COTHREAD_STARTED) {
643 GST_DEBUG (GST_CAT_COTHREADS, "via longjmp() jmpbuf %p", thread->jmp);
645 longjmp (thread->jmp, 1);
648 #ifdef HAVE_MAKECONTEXT
651 GST_DEBUG (GST_CAT_COTHREADS, "making context");
653 g_assert(thread != cothread_main(ctx));
656 ucp.uc_stack.ss_sp = (void *)thread->stack_base;
657 ucp.uc_stack.ss_size = thread->stack_size;
658 makecontext(&ucp, cothread_stub, 0);
661 GST_ARCH_SETUP_STACK ((char*)thread->sp);
662 GST_ARCH_SET_SP (thread->sp);
664 GST_ARCH_CALL (cothread_stub);
667 GST_DEBUG (GST_CAT_COTHREADS, "exit thread ");
673 #ifdef COTHREAD_PARANOID
675 g_warning ("cothread: can't switch to NULL cothread!\n");
678 g_warning ("cothread: there's no context, help!\n");
681 g_warning ("cothread: there's no current thread, help!\n");
683 #endif /* COTHREAD_PARANOID */
685 g_warning ("cothread: trying to switch to same thread, legal but not necessary\n");
691 * @thread: cothread state to lock
693 * Locks the cothread state.
696 cothread_lock (cothread_state * thread)
702 * @thread: cothread state to try to lock
704 * Try to lock the cothread state
706 * Returns: TRUE if the cothread could be locked.
709 cothread_trylock (cothread_state * thread)
716 * @thread: cothread state to unlock
718 * Unlock the cothread state.
721 cothread_unlock (cothread_state * thread)