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:
Thomas Vander Stichele 2002-09-12 18:56:31 +00:00
parent b5b28a3c8b
commit 3a5b408709
4 changed files with 234 additions and 132 deletions

View file

@ -52,14 +52,19 @@ static void cothread_destroy (cothread_state *thread);
struct _cothread_context struct _cothread_context
{ {
cothread_state *threads[COTHREAD_MAXTHREADS]; cothread_state *cothreads[COTHREAD_MAXTHREADS]; /* array of cothread states */
int nthreads; int ncothreads;
int current; int current;
GHashTable *data; 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 /* Disabling this define allows you to shut off a few checks in
* cothread_switch. This likely will speed things up fractionally */ * cothread_switch. This likely will speed things up fractionally */
@ -75,37 +80,49 @@ static pthread_key_t _cothread_key = -1;
cothread_context * cothread_context *
cothread_context_init (void) cothread_context_init (void)
{ {
/*
* initalize the whole of the cothreads context
*/
cothread_context *ctx = (cothread_context *) g_malloc (sizeof (cothread_context)); cothread_context *ctx = (cothread_context *) g_malloc (sizeof (cothread_context));
/* we consider the initiating process to be cothread 0 */ /* we consider the initiating process to be cothread 0 */
ctx->nthreads = 1; ctx->ncothreads = 1;
ctx->current = 0; ctx->current = 0;
ctx->data = g_hash_table_new (g_str_hash, g_str_equal); ctx->data = g_hash_table_new (g_str_hash, g_str_equal);
GST_INFO (GST_CAT_COTHREADS, "initializing cothreads"); GST_INFO (GST_CAT_COTHREADS, "initializing cothreads");
if (_cothread_key == (pthread_key_t)-1) { /* initialize the cothread key (for pthread space) if not done yet */
if (pthread_key_create (&_cothread_key, NULL) != 0) { if (_cothread_ctx_key == (pthread_key_t) -1) {
if (pthread_key_create (&_cothread_ctx_key, NULL) != 0) {
perror ("pthread_key_create"); perror ("pthread_key_create");
return NULL; 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)); /* clear the cothread data */
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;
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; return ctx;
} }
@ -126,8 +143,8 @@ cothread_context_free (cothread_context *ctx)
GST_INFO (GST_CAT_COTHREADS, "free cothread context"); GST_INFO (GST_CAT_COTHREADS, "free cothread context");
for (i = 0; i < COTHREAD_MAXTHREADS; i++) { for (i = 0; i < COTHREAD_MAXTHREADS; i++) {
if (ctx->threads[i]) { if (ctx->cothreads[i]) {
cothread_destroy (ctx->threads[i]); cothread_destroy (ctx->cothreads[i]);
} }
} }
g_hash_table_destroy (ctx->data); g_hash_table_destroy (ctx->data);
@ -145,7 +162,7 @@ cothread_context_free (cothread_context *ctx)
cothread_state* cothread_state*
cothread_create (cothread_context *ctx) cothread_create (cothread_context *ctx)
{ {
cothread_state *thread; cothread_state *cothread;
void *sp; void *sp;
void *mmaped = 0; void *mmaped = 0;
guchar *stack_end; guchar *stack_end;
@ -153,41 +170,44 @@ cothread_create (cothread_context *ctx)
g_return_val_if_fail (ctx != NULL, NULL); g_return_val_if_fail (ctx != NULL, NULL);
if (ctx->nthreads == COTHREAD_MAXTHREADS) { if (ctx->ncothreads == COTHREAD_MAXTHREADS) {
/* this is pretty fatal */ /* this is pretty fatal */
g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS\n"); g_warning ("cothread_create: attempt to create > COTHREAD_MAXTHREADS\n");
return NULL; return NULL;
} }
/* find a free spot in the stack, note slot 0 has the main thread */ /* find a free spot in the stack, note slot 0 has the main thread */
for (slot = 1; slot < ctx->nthreads; slot++) { for (slot = 1; slot < ctx->ncothreads; slot++) {
if (ctx->threads[slot] == NULL) if (ctx->cothreads[slot] == NULL)
break; break;
else if (ctx->threads[slot]->flags & COTHREAD_DESTROYED && else if (ctx->cothreads[slot]->flags & COTHREAD_DESTROYED &&
slot != ctx->current) { slot != ctx->current) {
cothread_destroy (ctx->threads[slot]); cothread_destroy (ctx->cothreads[slot]);
break; 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; sp = CURRENT_STACK_FRAME;
/* FIXME this may not be 64bit clean /* FIXME this may not be 64bit clean
* could use casts to uintptr_t from inttypes.h * could use casts to uintptr_t from inttypes.h
* if only all platforms had 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)); 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, GST_DEBUG (GST_CAT_COTHREADS,
"mmap cothread slot stack from %p to %p (size 0x%lx)", "mmap cothread slot stack from %p to %p (size 0x%lx)",
thread, thread + COTHREAD_STACKSIZE, cothread, cothread + COTHREAD_STACKSIZE,
(long) COTHREAD_STACKSIZE); (long) COTHREAD_STACKSIZE);
GST_DEBUG (GST_CAT_COTHREADS, "going into mmap"); GST_DEBUG (GST_CAT_COTHREADS, "going into mmap");
/* the mmap is used to reserve part of the stack /* the mmap is used to reserve part of the stack
* ie. we state explicitly that we are going to use it */ * 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, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
GST_DEBUG (GST_CAT_COTHREADS, "coming out of mmap"); GST_DEBUG (GST_CAT_COTHREADS, "coming out of mmap");
@ -195,70 +215,74 @@ cothread_create (cothread_context *ctx)
perror ("mmap'ing cothread stack space"); perror ("mmap'ing cothread stack space");
return NULL; return NULL;
} }
if (mmaped != thread) { if (mmaped != cothread) {
g_warning ("could not mmap requested memory"); g_warning ("could not mmap requested memory for cothread");
return NULL; 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", GST_DEBUG (GST_CAT_COTHREADS, "create cothread %d with magic number 0x%x",
slot, thread->magic_number); slot, cothread->magic_number);
thread->ctx = ctx; cothread->ctx = ctx;
thread->threadnum = slot; cothread->cothreadnum = slot;
thread->flags = 0; cothread->flags = 0;
thread->priv = NULL; cothread->priv = NULL;
thread->sp = ((guchar *) thread + COTHREAD_STACKSIZE); cothread->sp = ((guchar *) cothread + COTHREAD_STACKSIZE);
thread->top_sp = thread->sp; /* for debugging purposes to detect stack overruns */ cothread->top_sp = cothread->sp; /* for debugging purposes
to detect stack overruns */
GST_INFO (GST_CAT_COTHREADS, GST_INFO (GST_CAT_COTHREADS,
"created cothread #%d in slot %d: %p at sp:%p", "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->cothreads[slot] = cothread;
ctx->nthreads++; ctx->ncothreads++;
return thread; return cothread;
} }
/** /**
* cothread_free: * cothread_free:
* @thread: the cothread state * @cothread: the cothread state
* *
* Free the given cothread state * Free the given cothread state
*/ */
void 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 */ /* we simply flag the cothread for destruction here */
thread->flags |= COTHREAD_DESTROYED; cothread->flags |= COTHREAD_DESTROYED;
} }
static void static void
cothread_destroy (cothread_state *thread) cothread_destroy (cothread_state *cothread)
{ {
cothread_context *ctx; cothread_context *ctx;
gint threadnum; gint cothreadnum;
g_return_if_fail (thread != NULL); g_return_if_fail (cothread != NULL);
threadnum = thread->threadnum; cothreadnum = cothread->cothreadnum;
ctx = thread->ctx; 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 */ /* we have to unlock here because we might be switched out
cothread_unlock (thread); * with the lock held */
cothread_unlock (cothread);
if (threadnum == 0) if (cothreadnum == 0)
{ {
GST_INFO (GST_CAT_COTHREADS, GST_INFO (GST_CAT_COTHREADS,
"trying to destroy cothread 0 with %d cothreads left", "trying to destroy cothread 0 with %d cothreads left",
ctx->nthreads); ctx->ncothreads);
if (ctx->nthreads > 1) if (ctx->ncothreads > 1)
{ {
/* we're trying to destroy cothread 0 when there are still cothreads /* we're trying to destroy cothread 0 when there are still cothreads
* active, so kill those first */ * active, so kill those first */
@ -266,17 +290,17 @@ cothread_destroy (cothread_state *thread)
for (i = 1; i < COTHREAD_MAXTHREADS; ++i) 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, GST_INFO (GST_CAT_COTHREADS,
"destroyed cothread %d, %d cothreads left\n", "destroyed cothread %d, %d cothreads left\n",
i, ctx->nthreads); i, ctx->ncothreads);
} }
} }
} }
g_assert (ctx->nthreads == 1); g_assert (ctx->ncothreads == 1);
g_free (thread); g_free (cothread);
} }
else { else {
/* int res; /* int res;
@ -285,14 +309,14 @@ cothread_destroy (cothread_state *thread)
/* doing cleanups of the cothread create */ /* doing cleanups of the cothread create */
GST_DEBUG (GST_CAT_COTHREADS, "destroy cothread %d with magic number 0x%x", GST_DEBUG (GST_CAT_COTHREADS, "destroy cothread %d with magic number 0x%x",
threadnum, thread->magic_number); cothreadnum, cothread->magic_number);
g_assert (thread->magic_number == 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, GST_DEBUG (GST_CAT_COTHREADS,
"munmap cothread slot stack from %p to %p (size 0x%lx)", "munmap cothread slot stack from %p to %p (size 0x%lx)",
thread, thread + COTHREAD_STACKSIZE, cothread, cothread + COTHREAD_STACKSIZE,
(long) COTHREAD_STACKSIZE); (long) COTHREAD_STACKSIZE);
/* res = munmap (thread, COTHREAD_STACKSIZE); /* res = munmap (thread, COTHREAD_STACKSIZE);
* Commented out waiting for resolution for cothread issue */ * Commented out waiting for resolution for cothread issue */
@ -302,18 +326,19 @@ cothread_destroy (cothread_state *thread)
{ {
case EINVAL: case EINVAL:
g_warning ("munmap doesn't like start %p or length %d\n", g_warning ("munmap doesn't like start %p or length %d\n",
thread, COTHREAD_STACKSIZE); cothread, COTHREAD_STACKSIZE);
break; break;
default: 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; break;
} }
} }
} }
GST_DEBUG (GST_CAT_COTHREADS, "munmap done\n"); GST_DEBUG (GST_CAT_COTHREADS, "munmap done\n");
ctx->threads[threadnum] = NULL; ctx->cothreads[cothreadnum] = NULL;
ctx->nthreads--; ctx->ncothreads--;
} }
/** /**
@ -350,17 +375,18 @@ cothread_stop (cothread_state * thread)
/** /**
* cothread_main: * 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_state *
cothread_main (cothread_context * ctx) cothread_main (cothread_context * ctx)
{ {
GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread", ctx->threads[0]); GST_DEBUG (GST_CAT_COTHREADS, "returning %p, the 0th cothread",
return ctx->threads[0]; ctx->cothreads[0]);
return ctx->cothreads[0];
} }
/** /**
@ -373,9 +399,9 @@ cothread_main (cothread_context * ctx)
cothread_state * cothread_state *
cothread_current_main (void) 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_state *
cothread_current (void) 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 static void
cothread_stub (void) cothread_stub (void)
{ {
cothread_context *ctx = pthread_getspecific (_cothread_key); cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
register cothread_state *thread = ctx->threads[ctx->current]; register cothread_state *thread = ctx->cothreads[ctx->current];
GST_DEBUG_ENTER (""); GST_DEBUG_ENTER ("");
@ -422,7 +448,7 @@ int cothread_getcurrent (void) __attribute__ ((no_instrument_function));
int int
cothread_getcurrent (void) cothread_getcurrent (void)
{ {
cothread_context *ctx = pthread_getspecific (_cothread_key); cothread_context *ctx = pthread_getspecific (_cothread_ctx_key);
if (!ctx) if (!ctx)
return -1; return -1;
@ -453,7 +479,7 @@ cothread_set_private (cothread_state *thread, gpointer data)
void void
cothread_context_set_data (cothread_state *thread, gchar *key, gpointer data) 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); g_hash_table_insert (ctx->data, key, data);
} }
@ -484,7 +510,7 @@ cothread_get_private (cothread_state *thread)
gpointer gpointer
cothread_context_get_data (cothread_state * thread, gchar * key) 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); return g_hash_table_lookup (ctx->data, key);
} }
@ -499,7 +525,13 @@ cothread_context_get_data (cothread_state * thread, gchar * key)
gboolean gboolean
cothread_stackquery (void **stack, glong* stacksize) 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 #ifdef HAVE_POSIX_MEMALIGN
int retval = posix_memalign (stack, STACK_SIZE, STACK_SIZE); int retval = posix_memalign (stack, STACK_SIZE, STACK_SIZE);
@ -516,7 +548,7 @@ cothread_stackquery (void **stack, glong* stacksize)
return FALSE; return FALSE;
} }
GST_DEBUG (GST_CAT_THREAD, "have posix_memalign at %p of size %d", GST_DEBUG (GST_CAT_THREAD, "have posix_memalign at %p of size %d",
(void *) *stack, STACK_SIZE); (void *) *stack, STACK_SIZE);
#else #else
if ((*stack = valloc (STACK_SIZE)) != 0) if ((*stack = valloc (STACK_SIZE)) != 0)
{ {
@ -524,7 +556,7 @@ cothread_stackquery (void **stack, glong* stacksize)
return FALSE; return FALSE;
} }
GST_DEBUG (GST_CAT_THREAD, "have valloc at %p of size %d", GST_DEBUG (GST_CAT_THREAD, "have valloc at %p of size %d",
(void *) *stack, STACK_SIZE); (void *) *stack, STACK_SIZE);
#endif #endif
GST_DEBUG (GST_CAT_COTHREADS, GST_DEBUG (GST_CAT_COTHREADS,
@ -557,7 +589,7 @@ cothread_switch (cothread_state * thread)
goto nocontext; goto nocontext;
#endif #endif
current = ctx->threads[ctx->current]; current = ctx->cothreads[ctx->current];
#ifdef COTHREAD_PARANOID #ifdef COTHREAD_PARANOID
if (current == NULL) if (current == NULL)
goto nocurrent; goto nocurrent;
@ -569,8 +601,8 @@ cothread_switch (cothread_state * thread)
/* find the number of the thread to switch to */ /* find the number of the thread to switch to */
GST_INFO (GST_CAT_COTHREAD_SWITCH, GST_INFO (GST_CAT_COTHREAD_SWITCH,
"switching from cothread #%d to cothread #%d", "switching from cothread #%d to cothread #%d",
ctx->current, thread->threadnum); ctx->current, thread->cothreadnum);
ctx->current = thread->threadnum; ctx->current = thread->cothreadnum;
/* save the current stack pointer, frame pointer, and pc */ /* save the current stack pointer, frame pointer, and pc */
#ifdef GST_ARCH_PRESETJMP #ifdef GST_ARCH_PRESETJMP
@ -579,14 +611,14 @@ cothread_switch (cothread_state * thread)
enter = setjmp (current->jmp); enter = setjmp (current->jmp);
if (enter != 0) { if (enter != 0) {
GST_DEBUG (GST_CAT_COTHREADS, GST_DEBUG (GST_CAT_COTHREADS,
"enter thread #%d %d %p<->%p (%d) %p", "enter cothread #%d %d %p<->%p (%d) %p",
current->threadnum, enter, current->sp, current->top_sp, current->cothreadnum, enter, current->sp, current->top_sp,
(char*) current->top_sp - (char*) current->sp, current->jmp); (char*) current->top_sp - (char*) current->sp, current->jmp);
return; return;
} }
GST_DEBUG (GST_CAT_COTHREADS, "exit thread #%d %d %p<->%p (%d) %p", GST_DEBUG (GST_CAT_COTHREADS, "exit cothread #%d %d %p<->%p (%d) %p",
current->threadnum, enter, current->sp, current->top_sp, current->cothreadnum, enter, current->sp, current->top_sp,
(char*) current->top_sp - (char*) current->sp, current->jmp); (char *) current->top_sp - (char *) current->sp, current->jmp);
enter = 1; enter = 1;
if (current->flags & COTHREAD_DESTROYED) { if (current->flags & COTHREAD_DESTROYED) {

View file

@ -26,6 +26,7 @@
#include <glib.h> #include <glib.h>
#include <setjmp.h> #include <setjmp.h>
/* this bit is lifted out of glibc */
#ifndef CURRENT_STACK_FRAME #ifndef CURRENT_STACK_FRAME
#define CURRENT_STACK_FRAME ({ char __csf; &__csf; }) #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
#endif /* CURRENT_STACK_FRAME */ #endif /* CURRENT_STACK_FRAME */
@ -40,7 +41,7 @@ typedef int (*cothread_func) (int argc,char **argv);
struct _cothread_state { struct _cothread_state {
cothread_context *ctx; cothread_context *ctx;
int threadnum; int cothreadnum;
gpointer priv; gpointer priv;
cothread_func func; cothread_func func;

View file

@ -96,11 +96,11 @@ gst_scheduler_setup (GstScheduler *sched)
/** /**
* gst_scheduler_get_preferred_stack: * gst_scheduler_get_preferred_stack:
* @sched: the scheduler * @sched: a #GstScheduler to query.
* @stack: a pointer to the location of the preferred stack * @stack: the pointer to store the location of the preferred stack in.
* @size: a pointer to the size of the preferred stack * @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. * 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: * gst_scheduler_reset:
* @sched: the scheduler * @sched: a #GstScheduler to reset.
* *
* Reset the scheduler * Reset the schedulers.
*/ */
void void
gst_scheduler_reset (GstScheduler *sched) gst_scheduler_reset (GstScheduler *sched)

View file

@ -181,6 +181,7 @@ gst_thread_init (GstThread *thread)
thread->thread_id = (pthread_t) -1; thread->thread_id = (pthread_t) -1;
thread->sched_policy = SCHED_OTHER; thread->sched_policy = SCHED_OTHER;
thread->priority = 0; thread->priority = 0;
thread->stack = NULL;
} }
static void static void
@ -261,17 +262,24 @@ gst_thread_new (const gchar *name)
return gst_element_factory_make ("thread", 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...) \ #define THR_INFO(format,args...) \
GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \ GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
GST_DEBUG_THREAD_ARGS(thread->pid) , ## args ) GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
#define THR_DEBUG(format,args...) \ #define THR_DEBUG(format,args...) \
GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \ GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
GST_DEBUG_THREAD_ARGS(thread->pid) , ## args ) 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...) \ #define THR_INFO_MAIN(format,args...) \
GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \ GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args ) GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
#define THR_DEBUG_MAIN(format,args...) \ #define THR_DEBUG_MAIN(format,args...) \
GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \ GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args ) GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
@ -279,13 +287,17 @@ gst_thread_new (const gchar *name)
static GstElementStateReturn static GstElementStateReturn
gst_thread_update_state (GstThread *thread) gst_thread_update_state (GstThread *thread)
{ {
GST_DEBUG_ELEMENT (GST_CAT_THREAD, thread, "updating state of thread");
/* check for state change */ /* 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 */ /* punt and change state on all the children */
if (GST_ELEMENT_CLASS (parent_class)->change_state) 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; return GST_STATE_SUCCESS;
} }
@ -324,15 +336,36 @@ gst_thread_change_state (GstElement * element)
THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (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); 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) if (pthread_attr_init (&thread->attr) != 0)
g_warning ("pthread_attr_init returned an error !"); 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), if (gst_scheduler_get_preferred_stack (GST_ELEMENT_SCHED (element),
&thread->stack, &stacksize)) { &thread->stack, &stacksize)) {
#ifdef HAVE_PTHREAD_ATTR_SETSTACK #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"); g_warning ("pthread_attr_setstack failed\n");
return GST_STATE_FAILURE; 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", GST_DEBUG (GST_CAT_THREAD, "pthread attr set stack at %p of size %ld",
thread->stack, stacksize); thread->stack, stacksize);
} }
else {
g_warning ("scheduler did not return a preferred stack");
}
/* create the thread */ /* create a new pthread
THR_DEBUG ("going to pthread_create..."); * use the specified attributes
if (pthread_create (&thread->thread_id, &thread->attr, gst_thread_main_loop, thread) != 0) { * make it execute gst_thread_main_loop (thread)
THR_DEBUG ("pthread create failed"); */
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); 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; return GST_STATE_FAILURE;
} }
THR_DEBUG ("pthread created"); GST_DEBUG (GST_CAT_THREAD, "pthread created");
/* wait for it to 'spin up' */ /* wait for it to 'spin up' */
THR_DEBUG ("waiting for child thread spinup"); THR_DEBUG ("waiting for child thread spinup");
@ -477,19 +518,38 @@ gst_thread_change_state (GstElement * element)
THR_DEBUG ("waiting for ack"); THR_DEBUG ("waiting for ack");
g_cond_wait (thread->cond, thread->lock); g_cond_wait (thread->cond, thread->lock);
THR_DEBUG ("got ack"); 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) if (pthread_join (thread->thread_id, NULL) != 0)
g_warning ("pthread_join has failed !\n"); 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) { 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); free (thread->stack);
thread->stack = NULL; 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_REAPING);
GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED); GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING); GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
@ -531,6 +591,7 @@ gst_thread_main_loop (void *arg)
thread = GST_THREAD (arg); thread = GST_THREAD (arg);
g_mutex_lock (thread->lock); g_mutex_lock (thread->lock);
/* handle scheduler policy */
if (thread->sched_policy != SCHED_OTHER) { if (thread->sched_policy != SCHED_OTHER) {
struct sched_param sched_param; 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"); 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_scheduler_setup (GST_ELEMENT_SCHED (thread));
GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING); GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
thread->pid = getpid(); 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 */ /* first we need to change the state of all the children */
if (GST_ELEMENT_CLASS (parent_class)->change_state) { if (GST_ELEMENT_CLASS (parent_class)->change_state) {
@ -560,7 +624,6 @@ gst_thread_main_loop (void *arg)
} }
} }
THR_DEBUG_MAIN ("indicating spinup"); THR_DEBUG_MAIN ("indicating spinup");
g_cond_signal (thread->cond); g_cond_signal (thread->cond);
/* don't unlock the mutex because we hold it into the top of the while loop */ /* 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_READY),
gst_element_state_get_name (GST_STATE_NULL), gst_element_state_get_name (GST_STATE_NULL),
gst_element_state_get_name (GST_STATE_PAUSED)); 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 */ /* this must have happened by a state change in the thread context */
if (GST_STATE_PENDING (thread) != GST_STATE_NULL && 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 */ /* been signaled, we need to state transition now and signal back */
gst_thread_update_state (thread); 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); g_cond_signal (thread->cond);
/* now we decide what to do next */ /* now we decide what to do next */
if (GST_STATE (thread) == GST_STATE_NULL) { if (GST_STATE (thread) == GST_STATE_NULL) {
/* REAPING must be set, we can simply break this iteration */ /* 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); GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
} }
continue; continue;
case GST_STATE_PAUSED: case GST_STATE_PAUSED:
/* wait to be set to either the READY or PLAYING states */ /* wait to be set to either the READY or PLAYING states */
THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s", 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_PAUSED),
gst_element_state_get_name (GST_STATE_READY), gst_element_state_get_name (GST_STATE_READY),
gst_element_state_get_name (GST_STATE_PLAYING)); gst_element_state_get_name (GST_STATE_PLAYING));
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 */ /* 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: case GST_STATE_PLAYING:
/* wait to be set to PAUSED */ /* wait to be set to PAUSED */
THR_DEBUG_MAIN ("thread in %s state, waiting for %s", 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_PLAYING),
gst_element_state_get_name(GST_STATE_PAUSED)); gst_element_state_get_name (GST_STATE_PAUSED));
g_cond_wait (thread->cond,thread->lock); g_cond_wait (thread->cond,thread->lock);
/* been signaled, we need to state transition now and signal back */ /* been signaled, we need to state transition now and signal back */
@ -663,7 +729,7 @@ gst_thread_main_loop (void *arg)
continue; continue;
case GST_STATE_NULL: case GST_STATE_NULL:
THR_DEBUG_MAIN ("thread in %s state, preparing to die", 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); GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
break; break;
default: default:
@ -671,6 +737,9 @@ gst_thread_main_loop (void *arg)
break; break;
} }
} }
/* THREAD HAS STOPPED RUNNING */
/* we need to destroy the scheduler here because it has mapped it's /* we need to destroy the scheduler here because it has mapped it's
* stack into the threads stack space */ * stack into the threads stack space */
gst_scheduler_reset (GST_ELEMENT_SCHED (thread)); gst_scheduler_reset (GST_ELEMENT_SCHED (thread));