2000-11-11 15:13:50 +00:00
|
|
|
/* GStreamer
|
2000-12-28 22:12:02 +00:00
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
*
|
|
|
|
* cothreads.c: Cothreading routines
|
2000-11-11 15:13:50 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2000-09-07 20:35:15 +00:00
|
|
|
#include <pthread.h>
|
2001-12-20 02:41:34 +00:00
|
|
|
#include <stdio.h>
|
2000-01-30 09:03:00 +00:00
|
|
|
#include <stdlib.h>
|
2001-12-20 02:41:34 +00:00
|
|
|
#include <signal.h>
|
2000-01-30 09:03:00 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
#include "gst_private.h"
|
Changes made to the DEBUG system. New header file gstdebug.h holds the stuff to keep it out of gst.h's hair. DEBUG ...
Original commit message from CVS:
Changes made to the DEBUG system. New header file gstdebug.h holds the
stuff to keep it out of gst.h's hair. DEBUG prints out the process id,
cothread id, source filename and line number. Two new macros DEBUG_ENTER
and DEBUG_LEAVE are used to show the entry and exit of a given function.
This eventually might be used to construct call trace graphs, even taking
cothreads into account. This would be quite useful in visualizing the
scheduling mechanism.
Minor changes to various debug messages.
Also sitting in gstdebug.h is a prototypical DEBUG_ENTER that's capable of
performing DEBUG_LEAVE automatically. It does this by utilizing a
little-known GCC extension that allows one to call a function with the
same parameters as the current function. The macro uses this to basically
call itself. A boolean is used to ensure that when it calls itself it
actually runs the body of the function. In the meantime it prints stuff
out before and after the real function, as well as constructing a
debugging string. This can be used eventually to provide call-wide data
on the DEBUG lines, instead of having to replicate data on each call to
DEBUG. More research is needed into how this would most cleanly be fit
into some other chunk of code, like GStreamer (I think of this DEBUG trick
as a separate project, sorta).
Unfortunately, the aforementioned DEBUG trick interacts quite poorly with
cothreads. Almost any time it's used in a function that has anything
remotely to do with a cothread context (as in, it runs in one), a segfault
results from the __builtin_apply call, which is the heart of the whole
thing. If someone who really knows assembly could analyze the resulting
code to see what's really going on, we might find a way to fix either the
macro or the cothreads (I'm thinking that there's something we missed in
constructing the cothreads themselves) so this works in all cases.
In the meantime, please insert both DEBUG_ENTER and DEBUG_LEAVE in your
functions. Be sure to put DEBUG_ENTER after your variable declarations
and before any functional code, not to put the function name in any DEBUG
strings (it's already there, trust me), and put a DEBUG_LEAVE if you care
enough.
Changes are going to happen in the way DEBUGs and other printouts occur,
so stay tuned.
2000-12-04 09:35:08 +00:00
|
|
|
|
2000-12-15 01:57:34 +00:00
|
|
|
#include "cothreads.h"
|
|
|
|
#include "gstarch.h"
|
2001-12-20 02:41:34 +00:00
|
|
|
#include "gstlog.h"
|
2002-05-29 14:59:48 +00:00
|
|
|
#include "gstutils.h"
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-12-28 22:12:02 +00:00
|
|
|
|
2001-05-10 22:10:19 +00:00
|
|
|
#define STACK_SIZE 0x200000
|
|
|
|
|
2001-06-06 18:29:57 +00:00
|
|
|
#define COTHREAD_MAXTHREADS 16
|
2001-06-04 16:02:48 +00:00
|
|
|
#define COTHREAD_STACKSIZE (STACK_SIZE/COTHREAD_MAXTHREADS)
|
2001-05-10 22:10:19 +00:00
|
|
|
|
2001-12-20 20:03:10 +00:00
|
|
|
static void cothread_destroy (cothread_state *thread);
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
struct _cothread_context
|
|
|
|
{
|
2001-05-10 22:35:50 +00:00
|
|
|
cothread_state *threads[COTHREAD_MAXTHREADS];
|
|
|
|
int nthreads;
|
|
|
|
int current;
|
|
|
|
GHashTable *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
static pthread_key_t _cothread_key = -1;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-29 10:05:47 +00:00
|
|
|
/* Disablig this define allows you to shut off a few checks in
|
|
|
|
* cothread_switch. This likely will speed things up fractionally */
|
|
|
|
#define COTHREAD_PARANOID
|
|
|
|
|
2001-01-04 10:54:27 +00:00
|
|
|
/**
|
2001-12-20 02:41:34 +00:00
|
|
|
* cothread_context_init:
|
2001-01-04 10:54:27 +00:00
|
|
|
*
|
2001-01-04 22:16:06 +00:00
|
|
|
* Create and initialize a new cothread context
|
2001-01-04 10:54:27 +00:00
|
|
|
*
|
|
|
|
* Returns: the new cothread context
|
|
|
|
*/
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *
|
|
|
|
cothread_context_init (void)
|
2001-01-04 10:54:27 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
|
2001-01-04 10:54:27 +00:00
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* we consider the initiating process to be cothread 0 */
|
2001-05-25 21:00:07 +00:00
|
|
|
ctx->nthreads = 1;
|
|
|
|
ctx->current = 0;
|
2001-12-20 02:41:34 +00:00
|
|
|
ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
|
2001-05-25 21:00:07 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREADS, "initializing cothreads");
|
2001-01-04 10:54:27 +00:00
|
|
|
|
2001-12-23 14:27:48 +00:00
|
|
|
if (_cothread_key == (pthread_key_t)-1) {
|
2001-12-20 02:41:34 +00:00
|
|
|
if (pthread_key_create (&_cothread_key, NULL) != 0) {
|
2001-01-04 10:54:27 +00:00
|
|
|
perror ("pthread_key_create");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-12-20 02:41:34 +00:00
|
|
|
pthread_setspecific (_cothread_key, ctx);
|
2001-01-04 10:54:27 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
memset (ctx->threads, 0, sizeof (ctx->threads));
|
2001-01-04 10:54:27 +00:00
|
|
|
|
2002-05-29 14:59:48 +00:00
|
|
|
ctx->threads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
|
2001-01-04 10:54:27 +00:00
|
|
|
ctx->threads[0]->ctx = ctx;
|
|
|
|
ctx->threads[0]->threadnum = 0;
|
|
|
|
ctx->threads[0]->func = NULL;
|
|
|
|
ctx->threads[0]->argc = 0;
|
|
|
|
ctx->threads[0]->argv = NULL;
|
2002-02-24 19:08:30 +00:00
|
|
|
ctx->threads[0]->priv = NULL;
|
2001-01-04 10:54:27 +00:00
|
|
|
ctx->threads[0]->flags = COTHREAD_STARTED;
|
2001-12-20 02:41:34 +00:00
|
|
|
ctx->threads[0]->sp = (void *) CURRENT_STACK_FRAME;
|
2001-01-04 10:54:27 +00:00
|
|
|
ctx->threads[0]->pc = 0;
|
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* initialize the lock */
|
2001-05-25 21:00:07 +00:00
|
|
|
#ifdef COTHREAD_ATOMIC
|
|
|
|
atomic_set (&ctx->threads[0]->lock, 0);
|
|
|
|
#else
|
2001-12-20 02:41:34 +00:00
|
|
|
ctx->threads[0]->lock = g_mutex_new ();
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
2001-01-04 10:54:27 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREADS, "0th thread is %p at sp:%p", ctx->threads[0], ctx->threads[0]->sp);
|
2001-01-04 10:54:27 +00:00
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
/**
|
|
|
|
* cothread_context_free:
|
2001-12-28 20:20:26 +00:00
|
|
|
* @ctx: the cothread context to free
|
2001-12-20 02:41:34 +00:00
|
|
|
*
|
|
|
|
* Free the cothread context.
|
|
|
|
*/
|
2001-12-18 16:49:13 +00:00
|
|
|
void
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context_free (cothread_context *ctx)
|
2001-12-18 16:49:13 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
2001-12-23 14:27:48 +00:00
|
|
|
g_return_if_fail (ctx != NULL);
|
|
|
|
|
2001-12-20 20:03:10 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREADS, "free cothread context");
|
|
|
|
|
2001-12-23 14:27:48 +00:00
|
|
|
for (i = 0; i < COTHREAD_MAXTHREADS; i++) {
|
2001-12-20 02:41:34 +00:00
|
|
|
if (ctx->threads[i]) {
|
2001-12-23 14:27:48 +00:00
|
|
|
cothread_destroy (ctx->threads[i]);
|
2001-12-19 19:22:53 +00:00
|
|
|
}
|
2001-12-18 16:49:13 +00:00
|
|
|
}
|
|
|
|
g_hash_table_destroy (ctx->data);
|
|
|
|
g_free (ctx);
|
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_create:
|
|
|
|
* @ctx: the cothread context
|
|
|
|
*
|
2001-01-04 22:16:06 +00:00
|
|
|
* Create a new cothread state in the given context
|
2000-11-11 15:13:50 +00:00
|
|
|
*
|
2001-05-16 07:16:47 +00:00
|
|
|
* Returns: the new cothread state or NULL on error
|
2000-11-11 15:13:50 +00:00
|
|
|
*/
|
2000-11-04 18:54:07 +00:00
|
|
|
cothread_state*
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_create (cothread_context *ctx)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_state *thread;
|
|
|
|
void *sp;
|
|
|
|
guchar *stack_end;
|
|
|
|
gint slot = 0;
|
|
|
|
|
|
|
|
g_return_val_if_fail (ctx != NULL, NULL);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-05-16 07:16:47 +00:00
|
|
|
if (ctx->nthreads == COTHREAD_MAXTHREADS) {
|
2001-12-20 02:41:34 +00:00
|
|
|
/* this is pretty fatal */
|
|
|
|
g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS\n");
|
2001-05-16 07:16:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-12-23 14:27:48 +00:00
|
|
|
/* find a free spot in the stack, note slot 0 has the main thread */
|
2001-12-20 02:41:34 +00:00
|
|
|
for (slot = 1; slot < ctx->nthreads; slot++) {
|
|
|
|
if (ctx->threads[slot] == NULL)
|
|
|
|
break;
|
2001-12-22 23:19:17 +00:00
|
|
|
else if (ctx->threads[slot]->flags & COTHREAD_DESTROYED &&
|
|
|
|
slot != ctx->current) {
|
2001-12-20 20:03:10 +00:00
|
|
|
cothread_destroy (ctx->threads[slot]);
|
|
|
|
break;
|
|
|
|
}
|
2001-12-20 02:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sp = CURRENT_STACK_FRAME;
|
|
|
|
/* FIXME this may not be 64bit clean
|
|
|
|
* could use casts to uintptr_t from inttypes.h
|
|
|
|
* if only all platforms had inttypes.h
|
|
|
|
*/
|
|
|
|
stack_end = (guchar *) ((gulong) sp & ~(STACK_SIZE - 1));
|
|
|
|
|
|
|
|
thread = (cothread_state *) (stack_end + ((slot - 1) * COTHREAD_STACKSIZE));
|
2002-03-24 22:07:09 +00:00
|
|
|
GST_DEBUG (0, "new stack at %p", thread);
|
2001-12-20 02:41:34 +00:00
|
|
|
|
|
|
|
if (mmap ((void *) thread, COTHREAD_STACKSIZE,
|
2001-12-23 14:27:48 +00:00
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == MAP_FAILED) {
|
2001-12-20 02:41:34 +00:00
|
|
|
perror ("mmap'ing cothread stack space");
|
|
|
|
return NULL;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
thread->ctx = ctx;
|
|
|
|
thread->threadnum = slot;
|
|
|
|
thread->flags = 0;
|
2002-02-24 19:08:30 +00:00
|
|
|
thread->priv = NULL;
|
2001-12-20 02:41:34 +00:00
|
|
|
thread->sp = ((guchar *) thread + COTHREAD_STACKSIZE);
|
|
|
|
thread->top_sp = thread->sp; /* for debugging purposes to detect stack overruns */
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* initialize the lock */
|
2001-05-25 21:00:07 +00:00
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-20 02:41:34 +00:00
|
|
|
atomic_set (thread->lock, 0);
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-12-20 02:41:34 +00:00
|
|
|
thread->lock = g_mutex_new ();
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREADS, "created cothread #%d in slot %d: %p at sp:%p lock:%p",
|
|
|
|
ctx->nthreads, slot, thread, thread->sp, thread->lock);
|
|
|
|
|
|
|
|
ctx->threads[slot] = thread;
|
|
|
|
ctx->nthreads++;
|
|
|
|
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cothread_free:
|
|
|
|
* @thread: the cothread state
|
|
|
|
*
|
|
|
|
* Free the given cothread state
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cothread_free (cothread_state *thread)
|
|
|
|
{
|
|
|
|
g_return_if_fail (thread != NULL);
|
|
|
|
|
2001-12-22 23:19:17 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction", thread->threadnum);
|
2001-12-20 20:03:10 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
/* we simply flag the cothread for destruction here */
|
|
|
|
thread->flags |= COTHREAD_DESTROYED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cothread_destroy (cothread_state *thread)
|
|
|
|
{
|
|
|
|
cothread_context *ctx;
|
2001-12-23 14:27:48 +00:00
|
|
|
gint threadnum;
|
2001-12-20 02:41:34 +00:00
|
|
|
|
|
|
|
g_return_if_fail (thread != NULL);
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-12-23 14:27:48 +00:00
|
|
|
threadnum = thread->threadnum;
|
2001-12-20 02:41:34 +00:00
|
|
|
ctx = thread->ctx;
|
2001-12-23 14:27:48 +00:00
|
|
|
|
|
|
|
GST_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d", threadnum, thread, ctx->current);
|
|
|
|
|
2001-12-23 14:50:44 +00:00
|
|
|
/* we have to unlock here because we might be switched out with the lock held */
|
|
|
|
cothread_unlock (thread);
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
#ifndef COTHREAD_ATOMIC
|
|
|
|
g_mutex_free (thread->lock);
|
|
|
|
#endif
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-12-23 14:27:48 +00:00
|
|
|
if (threadnum == 0) {
|
|
|
|
g_free (thread);
|
|
|
|
}
|
|
|
|
else {
|
2001-12-23 23:36:44 +00:00
|
|
|
/* this doesn't seem to work very well */
|
|
|
|
/* munmap ((void *) thread, COTHREAD_STACKSIZE); */
|
2001-12-23 14:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->threads[threadnum] = NULL;
|
2001-12-20 02:41:34 +00:00
|
|
|
ctx->nthreads--;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_setfunc:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @func: the function to call
|
2001-01-04 22:16:06 +00:00
|
|
|
* @argc: argument count for the cothread function
|
|
|
|
* @argv: arguments for the cothread function
|
2000-11-11 15:13:50 +00:00
|
|
|
*
|
|
|
|
* Set the cothread function
|
|
|
|
*/
|
2001-12-20 02:41:34 +00:00
|
|
|
void
|
|
|
|
cothread_setfunc (cothread_state * thread, cothread_func func, int argc, char **argv)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->func = func;
|
|
|
|
thread->argc = argc;
|
|
|
|
thread->argv = argv;
|
2001-12-20 02:41:34 +00:00
|
|
|
thread->pc = (void *) func;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2001-12-28 20:20:26 +00:00
|
|
|
/**
|
|
|
|
* cothread_stop:
|
|
|
|
* @thread: the cothread to stop
|
|
|
|
*
|
|
|
|
* Stop the cothread and reset the stack and program counter.
|
|
|
|
*/
|
2001-12-25 02:15:46 +00:00
|
|
|
void
|
|
|
|
cothread_stop (cothread_state * thread)
|
|
|
|
{
|
|
|
|
thread->flags &= ~COTHREAD_STARTED;
|
|
|
|
thread->pc = 0;
|
|
|
|
thread->sp = thread->top_sp;
|
|
|
|
}
|
|
|
|
|
2001-01-04 22:16:06 +00:00
|
|
|
/**
|
|
|
|
* cothread_main:
|
|
|
|
* @ctx: cothread context to find main thread of
|
|
|
|
*
|
2001-10-21 18:00:31 +00:00
|
|
|
* Get the main thread.
|
|
|
|
*
|
2001-01-04 22:16:06 +00:00
|
|
|
* Returns: the #cothread_state of the main (0th) thread
|
2001-01-05 00:28:09 +00:00
|
|
|
*/
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_state *
|
|
|
|
cothread_main (cothread_context * ctx)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2002-03-24 22:07:09 +00:00
|
|
|
GST_DEBUG (0, "returning %p, the 0th cothread", ctx->threads[0]);
|
2000-01-30 09:03:00 +00:00
|
|
|
return ctx->threads[0];
|
|
|
|
}
|
|
|
|
|
2001-05-25 21:00:07 +00:00
|
|
|
/**
|
2001-05-27 14:37:29 +00:00
|
|
|
* cothread_current_main:
|
2001-05-25 21:00:07 +00:00
|
|
|
*
|
2001-10-21 18:00:31 +00:00
|
|
|
* Get the main thread in the current pthread.
|
|
|
|
*
|
2001-05-25 21:00:07 +00:00
|
|
|
* Returns: the #cothread_state of the main (0th) thread in the current pthread
|
|
|
|
*/
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_state *
|
2001-10-21 18:00:31 +00:00
|
|
|
cothread_current_main (void)
|
2001-05-25 21:00:07 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
|
|
|
|
2001-05-25 21:00:07 +00:00
|
|
|
return ctx->threads[0];
|
|
|
|
}
|
|
|
|
|
2001-12-27 00:47:41 +00:00
|
|
|
/**
|
|
|
|
* cothread_current:
|
|
|
|
*
|
|
|
|
* Get the currenttly executing cothread
|
|
|
|
*
|
|
|
|
* Returns: the #cothread_state of the current cothread
|
|
|
|
*/
|
|
|
|
cothread_state *
|
|
|
|
cothread_current (void)
|
|
|
|
{
|
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
|
|
|
|
|
|
|
return ctx->threads[ctx->current];
|
|
|
|
}
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
static void
|
|
|
|
cothread_stub (void)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
2000-01-30 09:03:00 +00:00
|
|
|
register cothread_state *thread = ctx->threads[ctx->current];
|
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_DEBUG_ENTER ("");
|
2001-01-19 07:48:43 +00:00
|
|
|
|
2000-01-30 09:03:00 +00:00
|
|
|
thread->flags |= COTHREAD_STARTED;
|
2002-04-19 10:26:56 +00:00
|
|
|
/*
|
|
|
|
* ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
* do something here to lock
|
2002-04-19 10:26:56 +00:00
|
|
|
* else
|
2001-12-14 22:59:21 +00:00
|
|
|
* g_mutex_lock(thread->lock);
|
2002-04-19 10:26:56 +00:00
|
|
|
* endif
|
2001-12-14 22:59:21 +00:00
|
|
|
*/
|
2002-04-19 10:26:56 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
while (TRUE) {
|
|
|
|
thread->func (thread->argc, thread->argv);
|
2001-12-14 22:59:21 +00:00
|
|
|
/* we do this to avoid ever returning, we just switch to 0th thread */
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_switch (cothread_main (ctx));
|
2001-01-19 07:48:43 +00:00
|
|
|
}
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_DEBUG_LEAVE ("");
|
2000-12-03 00:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* cothread_getcurrent:
|
|
|
|
*
|
2001-10-21 18:00:31 +00:00
|
|
|
* Get the current cothread id
|
|
|
|
*
|
2000-12-03 00:17:52 +00:00
|
|
|
* Returns: the current cothread id
|
|
|
|
*/
|
2001-10-21 18:00:31 +00:00
|
|
|
int cothread_getcurrent (void) __attribute__ ((no_instrument_function));
|
2001-12-20 02:41:34 +00:00
|
|
|
int
|
|
|
|
cothread_getcurrent (void)
|
2001-10-21 18:00:31 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
|
|
|
|
|
|
|
if (!ctx)
|
|
|
|
return -1;
|
2000-12-03 00:17:52 +00:00
|
|
|
return ctx->current;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
2002-03-31 14:04:50 +00:00
|
|
|
* cothread_set_private:
|
2000-11-11 15:13:50 +00:00
|
|
|
* @thread: the cothread state
|
|
|
|
* @data: the data
|
|
|
|
*
|
2002-03-31 14:04:50 +00:00
|
|
|
* set private data for the cothread.
|
2000-11-11 15:13:50 +00:00
|
|
|
*/
|
2000-11-06 00:15:51 +00:00
|
|
|
void
|
2002-02-23 13:41:17 +00:00
|
|
|
cothread_set_private (cothread_state *thread, gpointer data)
|
|
|
|
{
|
|
|
|
thread->priv = data;
|
|
|
|
}
|
|
|
|
|
2002-03-31 14:04:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_context_set_data:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @key: a key for the data
|
|
|
|
* @data: the data
|
|
|
|
*
|
|
|
|
* adds data to a cothread
|
|
|
|
*/
|
2002-02-23 13:41:17 +00:00
|
|
|
void
|
|
|
|
cothread_context_set_data (cothread_state *thread, gchar *key, gpointer data)
|
2000-11-06 00:15:51 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
2000-11-06 00:15:51 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
g_hash_table_insert (ctx->data, key, data);
|
2000-11-06 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
2002-03-31 14:04:50 +00:00
|
|
|
* cothread_get_private:
|
2000-11-11 15:13:50 +00:00
|
|
|
* @thread: the cothread state
|
|
|
|
*
|
2002-03-31 14:04:50 +00:00
|
|
|
* get the private data from the cothread
|
2000-11-11 15:13:50 +00:00
|
|
|
*
|
2002-03-31 14:04:50 +00:00
|
|
|
* Returns: the private data of the cothread
|
2000-11-11 15:13:50 +00:00
|
|
|
*/
|
2000-11-06 00:15:51 +00:00
|
|
|
gpointer
|
2002-02-23 13:41:17 +00:00
|
|
|
cothread_get_private (cothread_state *thread)
|
|
|
|
{
|
|
|
|
return thread->priv;
|
|
|
|
}
|
|
|
|
|
2002-03-31 14:04:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_context_get_data:
|
|
|
|
* @thread: the cothread state
|
|
|
|
* @key: a key for the data
|
|
|
|
*
|
|
|
|
* get data from the cothread
|
|
|
|
*
|
|
|
|
* Returns: the data associated with the key
|
|
|
|
*/
|
2002-02-23 13:41:17 +00:00
|
|
|
gpointer
|
|
|
|
cothread_context_get_data (cothread_state * thread, gchar * key)
|
2000-11-06 00:15:51 +00:00
|
|
|
{
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
2000-11-06 00:15:51 +00:00
|
|
|
|
2001-12-20 02:41:34 +00:00
|
|
|
return g_hash_table_lookup (ctx->data, key);
|
2000-11-06 00:15:51 +00:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:13:50 +00:00
|
|
|
/**
|
|
|
|
* cothread_switch:
|
2001-01-04 22:16:06 +00:00
|
|
|
* @thread: cothread state to switch to
|
2000-11-11 15:13:50 +00:00
|
|
|
*
|
2001-01-04 22:16:06 +00:00
|
|
|
* Switches to the given cothread state
|
2000-11-11 15:13:50 +00:00
|
|
|
*/
|
2001-12-20 02:41:34 +00:00
|
|
|
void
|
|
|
|
cothread_switch (cothread_state * thread)
|
2000-11-04 18:54:07 +00:00
|
|
|
{
|
2000-01-30 09:03:00 +00:00
|
|
|
cothread_context *ctx;
|
|
|
|
cothread_state *current;
|
2000-09-16 10:58:23 +00:00
|
|
|
int enter;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
2001-12-20 02:41:34 +00:00
|
|
|
if (thread == NULL)
|
|
|
|
goto nothread;
|
2000-11-29 10:05:47 +00:00
|
|
|
#endif
|
2000-01-30 09:03:00 +00:00
|
|
|
ctx = thread->ctx;
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
2001-12-20 02:41:34 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
goto nocontext;
|
2000-11-29 10:05:47 +00:00
|
|
|
#endif
|
2000-01-30 09:03:00 +00:00
|
|
|
|
|
|
|
current = ctx->threads[ctx->current];
|
2000-11-29 10:05:47 +00:00
|
|
|
#ifdef COTHREAD_PARANOID
|
2001-12-20 02:41:34 +00:00
|
|
|
if (current == NULL)
|
|
|
|
goto nocurrent;
|
2000-11-29 10:05:47 +00:00
|
|
|
#endif
|
2001-12-20 02:41:34 +00:00
|
|
|
if (current == thread)
|
|
|
|
goto selfswitch;
|
2000-01-30 09:03:00 +00:00
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* unlock the current thread, we're out of that context now */
|
2001-05-25 21:00:07 +00:00
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
/* do something to unlock the cothread */
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-12-20 02:41:34 +00:00
|
|
|
g_mutex_unlock (current->lock);
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* lock the next cothread before we even switch to it */
|
2001-05-25 21:00:07 +00:00
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
/* do something to lock the cothread */
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-12-20 02:41:34 +00:00
|
|
|
g_mutex_lock (thread->lock);
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
|
2001-12-14 22:59:21 +00:00
|
|
|
/* find the number of the thread to switch to */
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_INFO (GST_CAT_COTHREAD_SWITCH, "switching from cothread #%d to cothread #%d",
|
|
|
|
ctx->current, thread->threadnum);
|
2000-01-30 09:03:00 +00:00
|
|
|
ctx->current = thread->threadnum;
|
|
|
|
|
|
|
|
/* save the current stack pointer, frame pointer, and pc */
|
2001-03-02 18:30:37 +00:00
|
|
|
#ifdef GST_ARCH_PRESETJMP
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_ARCH_PRESETJMP ();
|
2001-03-02 18:30:37 +00:00
|
|
|
#endif
|
2001-12-23 14:50:44 +00:00
|
|
|
enter = setjmp (current->jmp);
|
2000-09-16 10:58:23 +00:00
|
|
|
if (enter != 0) {
|
2002-05-29 14:59:48 +00:00
|
|
|
GST_DEBUG (0, "enter thread #%d %d %p<->%p (%d) %p", current->threadnum, enter,
|
|
|
|
current->sp, current->top_sp, (char*)current->top_sp - (char*)current->sp, current->jmp);
|
2000-01-30 09:03:00 +00:00
|
|
|
return;
|
2000-09-16 10:58:23 +00:00
|
|
|
}
|
2002-05-29 14:59:48 +00:00
|
|
|
GST_DEBUG (0, "exit thread #%d %d %p<->%p (%d) %p", current->threadnum, enter,
|
|
|
|
current->sp, current->top_sp, (char*)current->top_sp - (char*)current->sp, current->jmp);
|
2000-01-30 09:03:00 +00:00
|
|
|
enter = 1;
|
|
|
|
|
2002-05-29 14:59:48 +00:00
|
|
|
if (current->flags & COTHREAD_DESTROYED) {
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_destroy (current);
|
2002-05-29 14:59:48 +00:00
|
|
|
}
|
2001-12-20 02:41:34 +00:00
|
|
|
|
2002-03-24 22:07:09 +00:00
|
|
|
GST_DEBUG (0, "set stack to %p", thread->sp);
|
2000-01-30 09:03:00 +00:00
|
|
|
/* restore stack pointer and other stuff of new cothread */
|
|
|
|
if (thread->flags & COTHREAD_STARTED) {
|
2002-05-29 14:59:48 +00:00
|
|
|
GST_DEBUG (0, "in thread %p", thread->jmp);
|
2001-12-14 22:59:21 +00:00
|
|
|
/* switch to it */
|
2001-12-23 14:50:44 +00:00
|
|
|
longjmp (thread->jmp, 1);
|
2001-12-20 02:41:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2001-12-23 14:27:48 +00:00
|
|
|
GST_ARCH_SETUP_STACK ((char*)thread->sp);
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_ARCH_SET_SP (thread->sp);
|
2001-12-14 22:59:21 +00:00
|
|
|
/* start it */
|
2001-12-20 02:41:34 +00:00
|
|
|
GST_ARCH_CALL (cothread_stub);
|
2002-03-24 22:07:09 +00:00
|
|
|
GST_DEBUG (0, "exit thread ");
|
2000-09-22 23:35:14 +00:00
|
|
|
ctx->current = 0;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
2000-11-29 10:05:47 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef COTHREAD_PARANOID
|
|
|
|
nothread:
|
2001-12-20 02:41:34 +00:00
|
|
|
g_print ("cothread: can't switch to NULL cothread!\n");
|
2000-11-29 10:05:47 +00:00
|
|
|
return;
|
|
|
|
nocontext:
|
2001-12-20 02:41:34 +00:00
|
|
|
g_print ("cothread: there's no context, help!\n");
|
|
|
|
exit (2);
|
2000-11-29 10:05:47 +00:00
|
|
|
nocurrent:
|
2001-12-20 02:41:34 +00:00
|
|
|
g_print ("cothread: there's no current thread, help!\n");
|
|
|
|
exit (2);
|
2000-11-29 10:05:47 +00:00
|
|
|
#endif /* COTHREAD_PARANOID */
|
|
|
|
selfswitch:
|
2001-12-20 02:41:34 +00:00
|
|
|
g_print ("cothread: trying to switch to same thread, legal but not necessary\n");
|
2000-11-29 10:05:47 +00:00
|
|
|
return;
|
2000-01-30 09:03:00 +00:00
|
|
|
}
|
2001-05-25 21:00:07 +00:00
|
|
|
|
2001-05-27 14:37:29 +00:00
|
|
|
/**
|
|
|
|
* cothread_lock:
|
|
|
|
* @thread: cothread state to lock
|
|
|
|
*
|
|
|
|
* Locks the cothread state.
|
|
|
|
*/
|
2001-05-25 21:00:07 +00:00
|
|
|
void
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_lock (cothread_state * thread)
|
2001-05-25 21:00:07 +00:00
|
|
|
{
|
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
/* do something to lock the cothread */
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-05-26 22:58:15 +00:00
|
|
|
if (thread->lock)
|
2001-12-20 02:41:34 +00:00
|
|
|
g_mutex_lock (thread->lock);
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-05-27 14:37:29 +00:00
|
|
|
/**
|
|
|
|
* cothread_trylock:
|
|
|
|
* @thread: cothread state to try to lock
|
|
|
|
*
|
|
|
|
* Try to lock the cothread state
|
|
|
|
*
|
|
|
|
* Returns: TRUE if the cothread could be locked.
|
|
|
|
*/
|
2001-05-25 21:00:07 +00:00
|
|
|
gboolean
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_trylock (cothread_state * thread)
|
2001-05-25 21:00:07 +00:00
|
|
|
{
|
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
/* do something to try to lock the cothread */
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-05-26 22:58:15 +00:00
|
|
|
if (thread->lock)
|
2001-12-20 02:41:34 +00:00
|
|
|
return g_mutex_trylock (thread->lock);
|
2001-05-26 22:58:15 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-05-27 14:37:29 +00:00
|
|
|
/**
|
|
|
|
* cothread_unlock:
|
|
|
|
* @thread: cothread state to unlock
|
|
|
|
*
|
|
|
|
* Unlock the cothread state.
|
|
|
|
*/
|
2001-05-25 21:00:07 +00:00
|
|
|
void
|
2001-12-20 02:41:34 +00:00
|
|
|
cothread_unlock (cothread_state * thread)
|
2001-05-25 21:00:07 +00:00
|
|
|
{
|
|
|
|
#ifdef COTHREAD_ATOMIC
|
2001-12-14 22:59:21 +00:00
|
|
|
/* do something to unlock the cothread */
|
2001-05-25 21:00:07 +00:00
|
|
|
#else
|
2001-05-26 22:58:15 +00:00
|
|
|
if (thread->lock)
|
2001-12-20 02:41:34 +00:00
|
|
|
g_mutex_unlock (thread->lock);
|
2001-05-25 21:00:07 +00:00
|
|
|
#endif
|
|
|
|
}
|