mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-03 15:06:34 +00:00
useful comments change thread -> cothread in varnames where it's applicable code cleanups
Original commit message from CVS: * useful comments * change thread -> cothread in varnames where it's applicable * code cleanups
This commit is contained in:
parent
b5b28a3c8b
commit
3a5b408709
4 changed files with 234 additions and 132 deletions
230
gst/cothreads.c
230
gst/cothreads.c
|
@ -52,14 +52,19 @@ static void cothread_destroy (cothread_state *thread);
|
|||
|
||||
struct _cothread_context
|
||||
{
|
||||
cothread_state *threads[COTHREAD_MAXTHREADS];
|
||||
int nthreads;
|
||||
cothread_state *cothreads[COTHREAD_MAXTHREADS]; /* array of cothread states */
|
||||
int ncothreads;
|
||||
int current;
|
||||
GHashTable *data;
|
||||
};
|
||||
|
||||
|
||||
static pthread_key_t _cothread_key = -1;
|
||||
/* this _cothread_ctx_key is used as a pthread key to the thread's context
|
||||
* a pthread key is a "pointer" to memory space that is/can be different
|
||||
* (ie. private) for each thread. The key itself is shared among threads,
|
||||
* so it only needs to be initialized once.
|
||||
*/
|
||||
static pthread_key_t _cothread_ctx_key = -1;
|
||||
|
||||
/* Disabling this define allows you to shut off a few checks in
|
||||
* cothread_switch. This likely will speed things up fractionally */
|
||||
|
@ -75,37 +80,49 @@ static pthread_key_t _cothread_key = -1;
|
|||
cothread_context *
|
||||
cothread_context_init (void)
|
||||
{
|
||||
/*
|
||||
* initalize the whole of the cothreads context
|
||||
*/
|
||||
cothread_context *ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
|
||||
|
||||
/* we consider the initiating process to be cothread 0 */
|
||||
ctx->nthreads = 1;
|
||||
ctx->ncothreads = 1;
|
||||
ctx->current = 0;
|
||||
ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS, "initializing cothreads");
|
||||
|
||||
if (_cothread_key == (pthread_key_t)-1) {
|
||||
if (pthread_key_create (&_cothread_key, NULL) != 0) {
|
||||
/* initialize the cothread key (for pthread space) if not done yet */
|
||||
if (_cothread_ctx_key == (pthread_key_t) -1) {
|
||||
if (pthread_key_create (&_cothread_ctx_key, NULL) != 0) {
|
||||
perror ("pthread_key_create");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
pthread_setspecific (_cothread_key, ctx);
|
||||
|
||||
memset (ctx->threads, 0, sizeof (ctx->threads));
|
||||
/* set this thread's context pointer */
|
||||
pthread_setspecific (_cothread_ctx_key, ctx);
|
||||
|
||||
ctx->threads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
|
||||
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;
|
||||
ctx->threads[0]->priv = NULL;
|
||||
ctx->threads[0]->flags = COTHREAD_STARTED;
|
||||
ctx->threads[0]->sp = (void *) CURRENT_STACK_FRAME;
|
||||
ctx->threads[0]->pc = 0;
|
||||
/* clear the cothread data */
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS, "0th thread is %p at sp:%p", ctx->threads[0], ctx->threads[0]->sp);
|
||||
memset (ctx->cothreads, 0, sizeof (ctx->cothreads));
|
||||
|
||||
/*
|
||||
* initialize the 0th cothread
|
||||
*/
|
||||
ctx->cothreads[0] = (cothread_state *) g_malloc0 (sizeof (cothread_state));
|
||||
ctx->cothreads[0]->ctx = ctx;
|
||||
ctx->cothreads[0]->cothreadnum = 0;
|
||||
ctx->cothreads[0]->func = NULL;
|
||||
ctx->cothreads[0]->argc = 0;
|
||||
ctx->cothreads[0]->argv = NULL;
|
||||
ctx->cothreads[0]->priv = NULL;
|
||||
ctx->cothreads[0]->flags = COTHREAD_STARTED;
|
||||
ctx->cothreads[0]->sp = (void *) CURRENT_STACK_FRAME;
|
||||
ctx->cothreads[0]->pc = 0;
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS, "0th cothread is %p at sp:%p",
|
||||
ctx->cothreads[0], ctx->cothreads[0]->sp);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
@ -126,8 +143,8 @@ cothread_context_free (cothread_context *ctx)
|
|||
GST_INFO (GST_CAT_COTHREADS, "free cothread context");
|
||||
|
||||
for (i = 0; i < COTHREAD_MAXTHREADS; i++) {
|
||||
if (ctx->threads[i]) {
|
||||
cothread_destroy (ctx->threads[i]);
|
||||
if (ctx->cothreads[i]) {
|
||||
cothread_destroy (ctx->cothreads[i]);
|
||||
}
|
||||
}
|
||||
g_hash_table_destroy (ctx->data);
|
||||
|
@ -145,7 +162,7 @@ cothread_context_free (cothread_context *ctx)
|
|||
cothread_state*
|
||||
cothread_create (cothread_context *ctx)
|
||||
{
|
||||
cothread_state *thread;
|
||||
cothread_state *cothread;
|
||||
void *sp;
|
||||
void *mmaped = 0;
|
||||
guchar *stack_end;
|
||||
|
@ -153,41 +170,44 @@ cothread_create (cothread_context *ctx)
|
|||
|
||||
g_return_val_if_fail (ctx != NULL, NULL);
|
||||
|
||||
if (ctx->nthreads == COTHREAD_MAXTHREADS) {
|
||||
if (ctx->ncothreads == COTHREAD_MAXTHREADS) {
|
||||
/* this is pretty fatal */
|
||||
g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS\n");
|
||||
return NULL;
|
||||
}
|
||||
/* find a free spot in the stack, note slot 0 has the main thread */
|
||||
for (slot = 1; slot < ctx->nthreads; slot++) {
|
||||
if (ctx->threads[slot] == NULL)
|
||||
for (slot = 1; slot < ctx->ncothreads; slot++) {
|
||||
if (ctx->cothreads[slot] == NULL)
|
||||
break;
|
||||
else if (ctx->threads[slot]->flags & COTHREAD_DESTROYED &&
|
||||
else if (ctx->cothreads[slot]->flags & COTHREAD_DESTROYED &&
|
||||
slot != ctx->current) {
|
||||
cothread_destroy (ctx->threads[slot]);
|
||||
cothread_destroy (ctx->cothreads[slot]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
GST_DEBUG(GST_CAT_COTHREADS, "Found free cothread slot %d", slot);
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "Found free cothread slot %d", slot);
|
||||
|
||||
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
|
||||
*/
|
||||
/* FIXME: a little explanation on what this REALLY means would be nice ;) */
|
||||
stack_end = (guchar *) ((gulong) sp & ~(STACK_SIZE - 1));
|
||||
|
||||
thread = (cothread_state *) (stack_end + ((slot - 1) * COTHREAD_STACKSIZE));
|
||||
/* cothread stack space of the thread is mapped in reverse, with cothread 0
|
||||
* stack space at the top */
|
||||
cothread = (cothread_state *) (stack_end + ((slot - 1) * COTHREAD_STACKSIZE));
|
||||
GST_DEBUG (GST_CAT_COTHREADS,
|
||||
"mmap cothread slot stack from %p to %p (size 0x%lx)",
|
||||
thread, thread + COTHREAD_STACKSIZE,
|
||||
cothread, cothread + COTHREAD_STACKSIZE,
|
||||
(long) COTHREAD_STACKSIZE);
|
||||
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "going into mmap");
|
||||
/* the mmap is used to reserve part of the stack
|
||||
* ie. we state explicitly that we are going to use it */
|
||||
mmaped = mmap ((void *) thread, COTHREAD_STACKSIZE,
|
||||
mmaped = mmap ((void *) cothread, COTHREAD_STACKSIZE,
|
||||
PROT_READ | PROT_WRITE | PROT_EXEC,
|
||||
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "coming out of mmap");
|
||||
|
@ -195,70 +215,74 @@ cothread_create (cothread_context *ctx)
|
|||
perror ("mmap'ing cothread stack space");
|
||||
return NULL;
|
||||
}
|
||||
if (mmaped != thread) {
|
||||
g_warning ("could not mmap requested memory");
|
||||
if (mmaped != cothread) {
|
||||
g_warning ("could not mmap requested memory for cothread");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
thread->magic_number = COTHREAD_MAGIC_NUMBER;
|
||||
cothread->magic_number = COTHREAD_MAGIC_NUMBER;
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "create cothread %d with magic number 0x%x",
|
||||
slot, thread->magic_number);
|
||||
thread->ctx = ctx;
|
||||
thread->threadnum = slot;
|
||||
thread->flags = 0;
|
||||
thread->priv = NULL;
|
||||
thread->sp = ((guchar *) thread + COTHREAD_STACKSIZE);
|
||||
thread->top_sp = thread->sp; /* for debugging purposes to detect stack overruns */
|
||||
slot, cothread->magic_number);
|
||||
cothread->ctx = ctx;
|
||||
cothread->cothreadnum = slot;
|
||||
cothread->flags = 0;
|
||||
cothread->priv = NULL;
|
||||
cothread->sp = ((guchar *) cothread + COTHREAD_STACKSIZE);
|
||||
cothread->top_sp = cothread->sp; /* for debugging purposes
|
||||
to detect stack overruns */
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS,
|
||||
"created cothread #%d in slot %d: %p at sp:%p",
|
||||
ctx->nthreads, slot, thread, thread->sp);
|
||||
ctx->ncothreads, slot, cothread, cothread->sp);
|
||||
|
||||
ctx->threads[slot] = thread;
|
||||
ctx->nthreads++;
|
||||
ctx->cothreads[slot] = cothread;
|
||||
ctx->ncothreads++;
|
||||
|
||||
return thread;
|
||||
return cothread;
|
||||
}
|
||||
|
||||
/**
|
||||
* cothread_free:
|
||||
* @thread: the cothread state
|
||||
* @cothread: the cothread state
|
||||
*
|
||||
* Free the given cothread state
|
||||
*/
|
||||
void
|
||||
cothread_free (cothread_state *thread)
|
||||
cothread_free (cothread_state *cothread)
|
||||
{
|
||||
g_return_if_fail (thread != NULL);
|
||||
g_return_if_fail (cothread != NULL);
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction", thread->threadnum);
|
||||
GST_INFO (GST_CAT_COTHREADS, "flag cothread %d for destruction",
|
||||
cothread->cothreadnum);
|
||||
|
||||
/* we simply flag the cothread for destruction here */
|
||||
thread->flags |= COTHREAD_DESTROYED;
|
||||
cothread->flags |= COTHREAD_DESTROYED;
|
||||
}
|
||||
|
||||
static void
|
||||
cothread_destroy (cothread_state *thread)
|
||||
cothread_destroy (cothread_state *cothread)
|
||||
{
|
||||
cothread_context *ctx;
|
||||
gint threadnum;
|
||||
gint cothreadnum;
|
||||
|
||||
g_return_if_fail (thread != NULL);
|
||||
g_return_if_fail (cothread != NULL);
|
||||
|
||||
threadnum = thread->threadnum;
|
||||
ctx = thread->ctx;
|
||||
cothreadnum = cothread->cothreadnum;
|
||||
ctx = cothread->ctx;
|
||||
|
||||
GST_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d", threadnum, thread, ctx->current);
|
||||
GST_INFO (GST_CAT_COTHREADS, "destroy cothread %d %p %d",
|
||||
cothreadnum, cothread, ctx->current);
|
||||
|
||||
/* we have to unlock here because we might be switched out with the lock held */
|
||||
cothread_unlock (thread);
|
||||
/* we have to unlock here because we might be switched out
|
||||
* with the lock held */
|
||||
cothread_unlock (cothread);
|
||||
|
||||
if (threadnum == 0)
|
||||
if (cothreadnum == 0)
|
||||
{
|
||||
GST_INFO (GST_CAT_COTHREADS,
|
||||
"trying to destroy cothread 0 with %d cothreads left",
|
||||
ctx->nthreads);
|
||||
if (ctx->nthreads > 1)
|
||||
ctx->ncothreads);
|
||||
if (ctx->ncothreads > 1)
|
||||
{
|
||||
/* we're trying to destroy cothread 0 when there are still cothreads
|
||||
* active, so kill those first */
|
||||
|
@ -266,17 +290,17 @@ cothread_destroy (cothread_state *thread)
|
|||
|
||||
for (i = 1; i < COTHREAD_MAXTHREADS; ++i)
|
||||
{
|
||||
if (ctx->threads[i] != NULL)
|
||||
if (ctx->cothreads[i] != NULL)
|
||||
{
|
||||
cothread_destroy (ctx->threads[i]);
|
||||
cothread_destroy (ctx->cothreads[i]);
|
||||
GST_INFO (GST_CAT_COTHREADS,
|
||||
"destroyed cothread %d, %d cothreads left\n",
|
||||
i, ctx->nthreads);
|
||||
i, ctx->ncothreads);
|
||||
}
|
||||
}
|
||||
}
|
||||
g_assert (ctx->nthreads == 1);
|
||||
g_free (thread);
|
||||
g_assert (ctx->ncothreads == 1);
|
||||
g_free (cothread);
|
||||
}
|
||||
else {
|
||||
/* int res;
|
||||
|
@ -285,14 +309,14 @@ cothread_destroy (cothread_state *thread)
|
|||
|
||||
/* doing cleanups of the cothread create */
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "destroy cothread %d with magic number 0x%x",
|
||||
threadnum, thread->magic_number);
|
||||
g_assert (thread->magic_number == COTHREAD_MAGIC_NUMBER);
|
||||
cothreadnum, cothread->magic_number);
|
||||
g_assert (cothread->magic_number == COTHREAD_MAGIC_NUMBER);
|
||||
|
||||
g_assert (thread->priv == NULL);
|
||||
g_assert (cothread->priv == NULL);
|
||||
|
||||
GST_DEBUG (GST_CAT_COTHREADS,
|
||||
"munmap cothread slot stack from %p to %p (size 0x%lx)",
|
||||
thread, thread + COTHREAD_STACKSIZE,
|
||||
cothread, cothread + COTHREAD_STACKSIZE,
|
||||
(long) COTHREAD_STACKSIZE);
|
||||
/* res = munmap (thread, COTHREAD_STACKSIZE);
|
||||
* Commented out waiting for resolution for cothread issue */
|
||||
|
@ -302,18 +326,19 @@ cothread_destroy (cothread_state *thread)
|
|||
{
|
||||
case EINVAL:
|
||||
g_warning ("munmap doesn't like start %p or length %d\n",
|
||||
thread, COTHREAD_STACKSIZE);
|
||||
cothread, COTHREAD_STACKSIZE);
|
||||
break;
|
||||
default:
|
||||
g_warning ("Thomas was too lazy to check for all errors, so I can't tell you what is wrong.\n");
|
||||
g_warning ("Thomas was too lazy to check for all errors, "
|
||||
"so I can't tell you what is wrong.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "munmap done\n");
|
||||
|
||||
ctx->threads[threadnum] = NULL;
|
||||
ctx->nthreads--;
|
||||
ctx->cothreads[cothreadnum] = NULL;
|
||||
ctx->ncothreads--;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -350,17 +375,18 @@ cothread_stop (cothread_state * thread)
|
|||
|
||||
/**
|
||||
* cothread_main:
|
||||
* @ctx: cothread context to find main thread of
|
||||
* @ctx: cothread context to find main cothread of.
|
||||
*
|
||||
* Get the main thread.
|
||||
* Gets the main thread.
|
||||
*
|
||||
* Returns: the #cothread_state of the main (0th) thread
|
||||
* Returns: the #cothread_state of the main (0th) cothread.
|
||||
*/
|
||||
cothread_state *
|
||||
cothread_main (cothread_context * ctx)
|
||||
{
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread", ctx->threads[0]);
|
||||
return ctx->threads[0];
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread",
|
||||
ctx->cothreads[0]);
|
||||
return ctx->cothreads[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -373,9 +399,9 @@ cothread_main (cothread_context * ctx)
|
|||
cothread_state *
|
||||
cothread_current_main (void)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
|
||||
return ctx->threads[0];
|
||||
return ctx->cothreads[0];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -388,16 +414,16 @@ cothread_current_main (void)
|
|||
cothread_state *
|
||||
cothread_current (void)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
|
||||
return ctx->threads[ctx->current];
|
||||
return ctx->cothreads[ctx->current];
|
||||
}
|
||||
|
||||
static void
|
||||
cothread_stub (void)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
register cothread_state *thread = ctx->threads[ctx->current];
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
register cothread_state *thread = ctx->cothreads[ctx->current];
|
||||
|
||||
GST_DEBUG_ENTER ("");
|
||||
|
||||
|
@ -422,7 +448,7 @@ int cothread_getcurrent (void) __attribute__ ((no_instrument_function));
|
|||
int
|
||||
cothread_getcurrent (void)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
|
||||
if (!ctx)
|
||||
return -1;
|
||||
|
@ -453,7 +479,7 @@ cothread_set_private (cothread_state *thread, gpointer data)
|
|||
void
|
||||
cothread_context_set_data (cothread_state *thread, gchar *key, gpointer data)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
|
||||
g_hash_table_insert (ctx->data, key, data);
|
||||
}
|
||||
|
@ -484,7 +510,7 @@ cothread_get_private (cothread_state *thread)
|
|||
gpointer
|
||||
cothread_context_get_data (cothread_state * thread, gchar * key)
|
||||
{
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_key);
|
||||
cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
|
||||
|
||||
return g_hash_table_lookup (ctx->data, key);
|
||||
}
|
||||
|
@ -499,7 +525,13 @@ cothread_context_get_data (cothread_state * thread, gchar * key)
|
|||
gboolean
|
||||
cothread_stackquery (void **stack, glong* stacksize)
|
||||
{
|
||||
/* wingo says: use posix_memalign to allocate a 2M-aligned, 2M stack */
|
||||
/* use either
|
||||
* - posix_memalign to allocate a 2M-aligned, 2M stack
|
||||
* or
|
||||
* - valloc
|
||||
*
|
||||
* memory allocated by either of these two can be freed using free ()
|
||||
* FIXME: define how the stack grows */
|
||||
|
||||
#ifdef HAVE_POSIX_MEMALIGN
|
||||
int retval = posix_memalign (stack, STACK_SIZE, STACK_SIZE);
|
||||
|
@ -516,7 +548,7 @@ cothread_stackquery (void **stack, glong* stacksize)
|
|||
return FALSE;
|
||||
}
|
||||
GST_DEBUG (GST_CAT_THREAD, "have posix_memalign at %p of size %d",
|
||||
(void *) *stack, STACK_SIZE);
|
||||
(void *) *stack, STACK_SIZE);
|
||||
#else
|
||||
if ((*stack = valloc (STACK_SIZE)) != 0)
|
||||
{
|
||||
|
@ -524,7 +556,7 @@ cothread_stackquery (void **stack, glong* stacksize)
|
|||
return FALSE;
|
||||
}
|
||||
GST_DEBUG (GST_CAT_THREAD, "have valloc at %p of size %d",
|
||||
(void *) *stack, STACK_SIZE);
|
||||
(void *) *stack, STACK_SIZE);
|
||||
#endif
|
||||
|
||||
GST_DEBUG (GST_CAT_COTHREADS,
|
||||
|
@ -557,7 +589,7 @@ cothread_switch (cothread_state * thread)
|
|||
goto nocontext;
|
||||
#endif
|
||||
|
||||
current = ctx->threads[ctx->current];
|
||||
current = ctx->cothreads[ctx->current];
|
||||
#ifdef COTHREAD_PARANOID
|
||||
if (current == NULL)
|
||||
goto nocurrent;
|
||||
|
@ -569,8 +601,8 @@ cothread_switch (cothread_state * thread)
|
|||
/* find the number of the thread to switch to */
|
||||
GST_INFO (GST_CAT_COTHREAD_SWITCH,
|
||||
"switching from cothread #%d to cothread #%d",
|
||||
ctx->current, thread->threadnum);
|
||||
ctx->current = thread->threadnum;
|
||||
ctx->current, thread->cothreadnum);
|
||||
ctx->current = thread->cothreadnum;
|
||||
|
||||
/* save the current stack pointer, frame pointer, and pc */
|
||||
#ifdef GST_ARCH_PRESETJMP
|
||||
|
@ -579,14 +611,14 @@ cothread_switch (cothread_state * thread)
|
|||
enter = setjmp (current->jmp);
|
||||
if (enter != 0) {
|
||||
GST_DEBUG (GST_CAT_COTHREADS,
|
||||
"enter thread #%d %d %p<->%p (%d) %p",
|
||||
current->threadnum, enter, current->sp, current->top_sp,
|
||||
"enter cothread #%d %d %p<->%p (%d) %p",
|
||||
current->cothreadnum, enter, current->sp, current->top_sp,
|
||||
(char*) current->top_sp - (char*) current->sp, current->jmp);
|
||||
return;
|
||||
}
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "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);
|
||||
GST_DEBUG (GST_CAT_COTHREADS, "exit cothread #%d %d %p<->%p (%d) %p",
|
||||
current->cothreadnum, enter, current->sp, current->top_sp,
|
||||
(char *) current->top_sp - (char *) current->sp, current->jmp);
|
||||
enter = 1;
|
||||
|
||||
if (current->flags & COTHREAD_DESTROYED) {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
#include <glib.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
/* this bit is lifted out of glibc */
|
||||
#ifndef CURRENT_STACK_FRAME
|
||||
#define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
|
||||
#endif /* CURRENT_STACK_FRAME */
|
||||
|
@ -40,7 +41,7 @@ typedef int (*cothread_func) (int argc,char **argv);
|
|||
|
||||
struct _cothread_state {
|
||||
cothread_context *ctx;
|
||||
int threadnum;
|
||||
int cothreadnum;
|
||||
gpointer priv;
|
||||
|
||||
cothread_func func;
|
||||
|
|
|
@ -96,11 +96,11 @@ gst_scheduler_setup (GstScheduler *sched)
|
|||
|
||||
/**
|
||||
* gst_scheduler_get_preferred_stack:
|
||||
* @sched: the scheduler
|
||||
* @stack: a pointer to the location of the preferred stack
|
||||
* @size: a pointer to the size of the preferred stack
|
||||
* @sched: a #GstScheduler to query.
|
||||
* @stack: the pointer to store the location of the preferred stack in.
|
||||
* @size: the pointer to store the size of the preferred stack in.
|
||||
*
|
||||
* Get the preferred stack location and size of this scheduler.
|
||||
* Gets the preferred stack location and size of this scheduler.
|
||||
*
|
||||
* Returns: TRUE if the scheduler suggested a preferred stacksize and location.
|
||||
*/
|
||||
|
@ -117,9 +117,9 @@ gst_scheduler_get_preferred_stack (GstScheduler *sched, gpointer *stack, gulong
|
|||
|
||||
/**
|
||||
* gst_scheduler_reset:
|
||||
* @sched: the scheduler
|
||||
* @sched: a #GstScheduler to reset.
|
||||
*
|
||||
* Reset the scheduler
|
||||
* Reset the schedulers.
|
||||
*/
|
||||
void
|
||||
gst_scheduler_reset (GstScheduler *sched)
|
||||
|
|
121
gst/gstthread.c
121
gst/gstthread.c
|
@ -181,6 +181,7 @@ gst_thread_init (GstThread *thread)
|
|||
thread->thread_id = (pthread_t) -1;
|
||||
thread->sched_policy = SCHED_OTHER;
|
||||
thread->priority = 0;
|
||||
thread->stack = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -261,17 +262,24 @@ gst_thread_new (const gchar *name)
|
|||
return gst_element_factory_make ("thread", name);
|
||||
}
|
||||
|
||||
/* these two macros are used for debug/info from the state_change function */
|
||||
|
||||
#define THR_INFO(format,args...) \
|
||||
GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
|
||||
GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
|
||||
|
||||
#define THR_DEBUG(format,args...) \
|
||||
GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
|
||||
GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
|
||||
|
||||
/* these two macros are used for debug/info from the gst_thread_main_loop
|
||||
* function
|
||||
*/
|
||||
|
||||
#define THR_INFO_MAIN(format,args...) \
|
||||
GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
|
||||
GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
|
||||
|
||||
#define THR_DEBUG_MAIN(format,args...) \
|
||||
GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
|
||||
GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
|
||||
|
@ -279,13 +287,17 @@ gst_thread_new (const gchar *name)
|
|||
static GstElementStateReturn
|
||||
gst_thread_update_state (GstThread *thread)
|
||||
{
|
||||
GST_DEBUG_ELEMENT (GST_CAT_THREAD, thread, "updating state of thread");
|
||||
/* check for state change */
|
||||
if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
|
||||
if (GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING) {
|
||||
/* punt and change state on all the children */
|
||||
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
||||
return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
|
||||
return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (thread));
|
||||
}
|
||||
|
||||
/* FIXME: in the case of no change_state function in the parent's class,
|
||||
* shouldn't we actually change the thread's state ? */
|
||||
g_warning ("thread's parent doesn't have change_state, returning success");
|
||||
return GST_STATE_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -324,15 +336,36 @@ gst_thread_change_state (GstElement * element)
|
|||
|
||||
THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
|
||||
|
||||
/* this bit of code handles creation of pthreads
|
||||
* this is therefor tricky code
|
||||
* compare it with the block of code that handles the destruction
|
||||
* in GST_STATE_READY_TO_NULL below
|
||||
*/
|
||||
g_mutex_lock (thread->lock);
|
||||
|
||||
/* create attribute struct for pthread
|
||||
* and assign stack pointer and size to it
|
||||
*
|
||||
* the default state of a pthread is PTHREAD_CREATE_JOINABLE
|
||||
* (see man pthread_attr_init)
|
||||
* - other thread can sync on termination
|
||||
* - thread resources are kept allocated until other thread performs
|
||||
* pthread_join
|
||||
*/
|
||||
|
||||
if (pthread_attr_init (&thread->attr) != 0)
|
||||
g_warning ("pthread_attr_init returned an error !");
|
||||
|
||||
/* this function should return a newly allocated stack
|
||||
* (using whatever method)
|
||||
* which we can initiate the pthreads with
|
||||
* the stack should be freed in
|
||||
*/
|
||||
if (gst_scheduler_get_preferred_stack (GST_ELEMENT_SCHED (element),
|
||||
&thread->stack, &stacksize)) {
|
||||
#ifdef HAVE_PTHREAD_ATTR_SETSTACK
|
||||
if (pthread_attr_setstack (&thread->attr, thread->stack, stacksize) != 0) {
|
||||
if (pthread_attr_setstack (&thread->attr,
|
||||
thread->stack, stacksize) != 0) {
|
||||
g_warning ("pthread_attr_setstack failed\n");
|
||||
return GST_STATE_FAILURE;
|
||||
}
|
||||
|
@ -349,16 +382,24 @@ gst_thread_change_state (GstElement * element)
|
|||
GST_DEBUG (GST_CAT_THREAD, "pthread attr set stack at %p of size %ld",
|
||||
thread->stack, stacksize);
|
||||
}
|
||||
else {
|
||||
g_warning ("scheduler did not return a preferred stack");
|
||||
}
|
||||
|
||||
/* create the thread */
|
||||
THR_DEBUG ("going to pthread_create...");
|
||||
if (pthread_create (&thread->thread_id, &thread->attr, gst_thread_main_loop, thread) != 0) {
|
||||
THR_DEBUG ("pthread create failed");
|
||||
/* create a new pthread
|
||||
* use the specified attributes
|
||||
* make it execute gst_thread_main_loop (thread)
|
||||
*/
|
||||
GST_DEBUG (GST_CAT_THREAD, "going to pthread_create...");
|
||||
if (pthread_create (&thread->thread_id, &thread->attr,
|
||||
gst_thread_main_loop, thread) != 0) {
|
||||
GST_DEBUG (GST_CAT_THREAD, "pthread_create failed");
|
||||
g_mutex_unlock (thread->lock);
|
||||
THR_DEBUG ("could not create thread \"%s\"", GST_ELEMENT_NAME (element));
|
||||
GST_DEBUG (GST_CAT_THREAD, "could not create thread \"%s\"",
|
||||
GST_ELEMENT_NAME (element));
|
||||
return GST_STATE_FAILURE;
|
||||
}
|
||||
THR_DEBUG ("pthread created");
|
||||
GST_DEBUG (GST_CAT_THREAD, "pthread created");
|
||||
|
||||
/* wait for it to 'spin up' */
|
||||
THR_DEBUG ("waiting for child thread spinup");
|
||||
|
@ -477,19 +518,38 @@ gst_thread_change_state (GstElement * element)
|
|||
THR_DEBUG ("waiting for ack");
|
||||
g_cond_wait (thread->cond, thread->lock);
|
||||
THR_DEBUG ("got ack");
|
||||
|
||||
/* this block of code is very tricky
|
||||
* basically, we try to clean up the whole thread and
|
||||
* everything related to it in the right order without
|
||||
* triggering segfaults
|
||||
* compare this block to the block
|
||||
*/
|
||||
|
||||
|
||||
/* in glibc 2.2.5, pthread_attr_destroy does nothing more
|
||||
* than return 0 */
|
||||
if (pthread_attr_destroy (&thread->attr) != 0)
|
||||
g_warning ("pthread_attr_destroy has failed !");
|
||||
|
||||
GST_DEBUG (GST_CAT_THREAD, "joining pthread %ld", thread->thread_id);
|
||||
if (pthread_join (thread->thread_id, NULL) != 0)
|
||||
g_warning ("pthread_join has failed !\n");
|
||||
if (pthread_attr_destroy (&thread->attr) != 0)
|
||||
g_warning ("pthread_attr_destroy has failed !\n");
|
||||
thread->thread_id = -1;
|
||||
g_mutex_unlock (thread->lock);
|
||||
|
||||
thread->thread_id = -1;
|
||||
|
||||
/* the stack was allocated when we created the thread
|
||||
* using scheduler->get_preferred_stack */
|
||||
if (thread->stack) {
|
||||
GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)", thread->stack);
|
||||
GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)",
|
||||
thread->stack);
|
||||
free (thread->stack);
|
||||
thread->stack = NULL;
|
||||
}
|
||||
|
||||
|
||||
THR_DEBUG ("unlocking mutex");
|
||||
g_mutex_unlock (thread->lock);
|
||||
|
||||
GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
|
||||
GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
|
||||
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
|
||||
|
@ -531,6 +591,7 @@ gst_thread_main_loop (void *arg)
|
|||
thread = GST_THREAD (arg);
|
||||
g_mutex_lock (thread->lock);
|
||||
|
||||
/* handle scheduler policy */
|
||||
if (thread->sched_policy != SCHED_OTHER) {
|
||||
struct sched_param sched_param;
|
||||
|
||||
|
@ -544,12 +605,15 @@ gst_thread_main_loop (void *arg)
|
|||
GST_DEBUG (GST_CAT_THREAD, "not running with real-time priority");
|
||||
}
|
||||
}
|
||||
else
|
||||
g_warning ("thread has SCHED_OTHER policy, unhandled !");
|
||||
|
||||
/* set up the element's scheduler */
|
||||
gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
|
||||
GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
|
||||
|
||||
thread->pid = getpid();
|
||||
THR_INFO_MAIN("thread is running");
|
||||
THR_INFO_MAIN ("thread is running");
|
||||
|
||||
/* first we need to change the state of all the children */
|
||||
if (GST_ELEMENT_CLASS (parent_class)->change_state) {
|
||||
|
@ -560,7 +624,6 @@ gst_thread_main_loop (void *arg)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
THR_DEBUG_MAIN ("indicating spinup");
|
||||
g_cond_signal (thread->cond);
|
||||
/* don't unlock the mutex because we hold it into the top of the while loop */
|
||||
|
@ -581,7 +644,7 @@ gst_thread_main_loop (void *arg)
|
|||
gst_element_state_get_name (GST_STATE_READY),
|
||||
gst_element_state_get_name (GST_STATE_NULL),
|
||||
gst_element_state_get_name (GST_STATE_PAUSED));
|
||||
g_cond_wait (thread->cond,thread->lock);
|
||||
g_cond_wait (thread->cond, thread->lock);
|
||||
|
||||
/* this must have happened by a state change in the thread context */
|
||||
if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
|
||||
|
@ -592,20 +655,23 @@ gst_thread_main_loop (void *arg)
|
|||
|
||||
/* been signaled, we need to state transition now and signal back */
|
||||
gst_thread_update_state (thread);
|
||||
THR_DEBUG_MAIN ("done with state transition, signaling back to parent process");
|
||||
THR_DEBUG_MAIN ("done with state transition, "
|
||||
"signaling back to parent process");
|
||||
g_cond_signal (thread->cond);
|
||||
/* now we decide what to do next */
|
||||
if (GST_STATE (thread) == GST_STATE_NULL) {
|
||||
/* REAPING must be set, we can simply break this iteration */
|
||||
THR_DEBUG_MAIN ("set GST_THREAD_STATE_REAPING");
|
||||
GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
|
||||
}
|
||||
continue;
|
||||
|
||||
case GST_STATE_PAUSED:
|
||||
/* wait to be set to either the READY or PLAYING states */
|
||||
THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s",
|
||||
gst_element_state_get_name (GST_STATE_PAUSED),
|
||||
gst_element_state_get_name (GST_STATE_READY),
|
||||
gst_element_state_get_name (GST_STATE_PLAYING));
|
||||
THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
|
||||
gst_element_state_get_name (GST_STATE_PAUSED),
|
||||
gst_element_state_get_name (GST_STATE_READY),
|
||||
gst_element_state_get_name (GST_STATE_PLAYING));
|
||||
g_cond_wait (thread->cond, thread->lock);
|
||||
|
||||
/* this must have happened by a state change in the thread context */
|
||||
|
@ -651,8 +717,8 @@ gst_thread_main_loop (void *arg)
|
|||
case GST_STATE_PLAYING:
|
||||
/* wait to be set to PAUSED */
|
||||
THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
|
||||
gst_element_state_get_name(GST_STATE_PLAYING),
|
||||
gst_element_state_get_name(GST_STATE_PAUSED));
|
||||
gst_element_state_get_name (GST_STATE_PLAYING),
|
||||
gst_element_state_get_name (GST_STATE_PAUSED));
|
||||
g_cond_wait (thread->cond,thread->lock);
|
||||
|
||||
/* been signaled, we need to state transition now and signal back */
|
||||
|
@ -663,7 +729,7 @@ gst_thread_main_loop (void *arg)
|
|||
continue;
|
||||
case GST_STATE_NULL:
|
||||
THR_DEBUG_MAIN ("thread in %s state, preparing to die",
|
||||
gst_element_state_get_name(GST_STATE_NULL));
|
||||
gst_element_state_get_name (GST_STATE_NULL));
|
||||
GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
|
||||
break;
|
||||
default:
|
||||
|
@ -671,6 +737,9 @@ gst_thread_main_loop (void *arg)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* THREAD HAS STOPPED RUNNING */
|
||||
|
||||
/* we need to destroy the scheduler here because it has mapped it's
|
||||
* stack into the threads stack space */
|
||||
gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
|
||||
|
|
Loading…
Reference in a new issue