2003-03-11 21:01:31 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
|
|
|
|
*
|
|
|
|
* gthread-cothreads.c: cothreads implemented via GThread for compatibility
|
|
|
|
* They're probably slooooooow
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2004-11-07 19:31:39 +00:00
|
|
|
#ifndef __GTHREAD_COTHREADS_H__
|
|
|
|
#define __GTHREAD_COTHREADS_H__
|
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
#include <glib.h>
|
2003-04-21 18:37:13 +00:00
|
|
|
#include <gst/gstthread.h>
|
2003-03-11 21:01:31 +00:00
|
|
|
|
|
|
|
/* the name of this cothreads */
|
2004-03-28 02:29:24 +00:00
|
|
|
#define COTHREADS_TYPE gthread
|
2003-03-11 21:01:31 +00:00
|
|
|
#define COTHREADS_NAME "gthread"
|
|
|
|
#define COTHREADS_NAME_CAPITAL "GThread"
|
|
|
|
|
2003-05-18 15:03:34 +00:00
|
|
|
/*
|
2003-03-11 21:01:31 +00:00
|
|
|
* Theory of operation:
|
|
|
|
* Instead of using cothreads, GThreads and 1 mutex are used.
|
|
|
|
* Every thread may only run if it holds the mutex. Otherwise it holds its own
|
|
|
|
* cond which has to be signaled to wakeit up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* define "cothread", "cothread_context" and "cothread_func" */
|
2003-05-18 23:34:12 +00:00
|
|
|
typedef int (*cothread_func) (int, char **);
|
2003-03-11 21:01:31 +00:00
|
|
|
|
|
|
|
typedef struct _cothread cothread;
|
|
|
|
typedef struct _cothread_context cothread_context;
|
|
|
|
|
2004-03-15 14:43:34 +00:00
|
|
|
struct _cothread_context {
|
|
|
|
GSList * cothreads; /* contains all threads but main */
|
|
|
|
cothread * main;
|
|
|
|
cothread * current;
|
|
|
|
GMutex * mutex;
|
|
|
|
GstThread * gst_thread; /* the GstThread we're running from */
|
2003-03-11 21:01:31 +00:00
|
|
|
};
|
|
|
|
|
2004-03-15 14:43:34 +00:00
|
|
|
struct _cothread {
|
|
|
|
GThread * thread;
|
|
|
|
GCond * cond;
|
|
|
|
cothread_func run;
|
|
|
|
int argc;
|
|
|
|
char ** argv;
|
|
|
|
cothread * creator;
|
|
|
|
gboolean die;
|
|
|
|
cothread_context * context;
|
2003-03-11 21:01:31 +00:00
|
|
|
};
|
|
|
|
|
2004-11-07 19:31:39 +00:00
|
|
|
#ifndef GTHREAD_COTHREADS_NO_DEFINITIONS
|
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
/* define functions
|
|
|
|
* Functions starting with "do_" are used by the scheduler.
|
|
|
|
*/
|
2004-03-15 14:43:34 +00:00
|
|
|
static void do_cothreads_init (void *unused);
|
|
|
|
static cothread_context *do_cothread_context_init (void);
|
|
|
|
static void do_cothread_context_destroy (cothread_context *context);
|
|
|
|
static cothread * cothread_create (cothread_context *context,
|
|
|
|
cothread_func func,
|
|
|
|
int argc,
|
|
|
|
char **argv);
|
2003-03-11 21:01:31 +00:00
|
|
|
#define do_cothread_create(new_cothread, context, func, argc, argv) \
|
|
|
|
G_STMT_START{ \
|
|
|
|
new_cothread = cothread_create ((context), (func), argc, (char**) (argv)); \
|
|
|
|
}G_STMT_END
|
2004-03-15 14:43:34 +00:00
|
|
|
static void do_cothread_switch (cothread *to);
|
|
|
|
static void do_cothread_destroy (cothread *thread);
|
2003-03-11 21:01:31 +00:00
|
|
|
#define do_cothread_get_current(context) ((context)->current)
|
|
|
|
#define do_cothread_get_main(context) ((context)->main)
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_cothreads_init (void *unused)
|
|
|
|
{
|
2004-03-15 14:43:34 +00:00
|
|
|
if (!g_thread_supported ()) g_thread_init (NULL);
|
2003-03-11 21:01:31 +00:00
|
|
|
}
|
|
|
|
static cothread_context *
|
|
|
|
do_cothread_context_init (void)
|
|
|
|
{
|
|
|
|
cothread_context *ret = g_new0 (cothread_context, 1);
|
|
|
|
|
|
|
|
ret->main = g_new0 (cothread, 1);
|
|
|
|
ret->main->thread = g_thread_self ();
|
|
|
|
ret->main->cond = g_cond_new ();
|
|
|
|
ret->main->die = FALSE;
|
|
|
|
ret->main->context = ret;
|
|
|
|
ret->mutex = g_mutex_new ();
|
|
|
|
ret->cothreads = NULL;
|
|
|
|
ret->current = ret->main;
|
2004-03-15 14:43:34 +00:00
|
|
|
ret->gst_thread = gst_thread_get_current();
|
2003-03-11 21:01:31 +00:00
|
|
|
g_mutex_lock (ret->mutex);
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
static void
|
2004-03-15 14:43:34 +00:00
|
|
|
do_cothread_context_destroy (cothread_context *context)
|
2003-03-11 21:01:31 +00:00
|
|
|
{
|
2004-03-15 14:43:34 +00:00
|
|
|
g_assert (g_thread_self() == context->main->thread);
|
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
while (context->cothreads) {
|
2003-03-11 21:37:36 +00:00
|
|
|
do_cothread_destroy ((cothread *) context->cothreads->data);
|
2003-03-11 21:01:31 +00:00
|
|
|
}
|
2003-03-13 23:59:36 +00:00
|
|
|
g_mutex_unlock (context->mutex);
|
2003-03-12 21:55:09 +00:00
|
|
|
g_mutex_free (context->mutex);
|
2004-04-30 02:16:29 +00:00
|
|
|
g_cond_free (context->main->cond);
|
|
|
|
g_free (context->main);
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
g_free (context);
|
|
|
|
}
|
|
|
|
static void
|
2004-03-15 14:43:34 +00:00
|
|
|
die (cothread *to_die) {
|
2003-03-11 21:01:31 +00:00
|
|
|
g_cond_free (to_die->cond);
|
2004-03-15 14:43:34 +00:00
|
|
|
to_die->context->cothreads = g_slist_remove (to_die->context->cothreads, to_die);
|
2003-03-11 21:01:31 +00:00
|
|
|
g_free (to_die);
|
|
|
|
g_thread_exit (to_die);
|
2003-03-11 21:37:36 +00:00
|
|
|
/* don't unlock the mutex here, the thread waiting for us to die is gonna take it */
|
2003-03-11 21:01:31 +00:00
|
|
|
}
|
|
|
|
static gpointer
|
|
|
|
run_new_thread (gpointer data)
|
|
|
|
{
|
|
|
|
cothread *self = (cothread *) data;
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
g_mutex_lock (self->context->mutex);
|
2003-04-21 18:37:13 +00:00
|
|
|
g_private_set (gst_thread_current, self->context->gst_thread);
|
2003-03-11 21:01:31 +00:00
|
|
|
g_cond_signal (self->creator->cond);
|
|
|
|
g_cond_wait (self->cond, self->context->mutex);
|
|
|
|
if (self->die)
|
|
|
|
die (self);
|
|
|
|
while (TRUE) {
|
|
|
|
self->run (self->argc, self->argv);
|
|
|
|
/* compatibility */
|
|
|
|
do_cothread_switch (do_cothread_get_main (self->context));
|
|
|
|
}
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
static cothread *
|
2004-03-15 14:43:34 +00:00
|
|
|
cothread_create (cothread_context *context, cothread_func func, int argc, char **argv)
|
2003-03-11 21:01:31 +00:00
|
|
|
{
|
|
|
|
cothread *ret;
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
if ((ret = g_new (cothread, 1)) == NULL) {
|
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
ret->cond = g_cond_new ();
|
|
|
|
ret->run = func;
|
|
|
|
ret->argc = argc;
|
|
|
|
ret->argv = argv;
|
|
|
|
ret->creator = do_cothread_get_current (context);
|
|
|
|
ret->die = FALSE;
|
2004-03-15 14:43:34 +00:00
|
|
|
ret->context = context;
|
|
|
|
context->cothreads = g_slist_prepend (context->cothreads, ret);
|
2003-03-11 21:01:31 +00:00
|
|
|
ret->thread = g_thread_create (run_new_thread, ret, TRUE, NULL);
|
2004-03-15 14:43:34 +00:00
|
|
|
if (ret->thread == NULL) goto out2;
|
2003-03-11 21:01:31 +00:00
|
|
|
g_cond_wait (do_cothread_get_current (context)->cond, context->mutex);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
out2:
|
2003-03-13 23:59:36 +00:00
|
|
|
context->cothreads = g_slist_remove (context->cothreads, ret);
|
2003-03-11 21:01:31 +00:00
|
|
|
g_free (ret);
|
|
|
|
out1:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-15 14:43:34 +00:00
|
|
|
static void do_cothread_switch (cothread *to)
|
2003-03-11 21:01:31 +00:00
|
|
|
{
|
2004-03-15 14:43:34 +00:00
|
|
|
cothread *self = do_cothread_get_current(to->context);
|
|
|
|
|
2004-04-09 22:43:06 +00:00
|
|
|
if (self != to) {
|
2003-03-11 21:01:31 +00:00
|
|
|
self->context->current = to;
|
|
|
|
g_cond_signal (to->cond);
|
|
|
|
g_cond_wait (self->cond, self->context->mutex);
|
|
|
|
if (self->die)
|
|
|
|
die (self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-28 02:29:24 +00:00
|
|
|
#define do_cothread_setfunc(thread,context,_func,_argc,_argv) G_STMT_START {\
|
|
|
|
((cothread *)(thread))->run = (_func); \
|
|
|
|
((cothread *)(thread))->argc = _argc; \
|
|
|
|
((cothread *)(thread))->argv = _argv; \
|
|
|
|
}G_STMT_END
|
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
static void
|
2004-03-15 14:43:34 +00:00
|
|
|
do_cothread_destroy (cothread *thread)
|
2003-03-11 21:01:31 +00:00
|
|
|
{
|
2003-03-11 21:37:36 +00:00
|
|
|
GThread *join;
|
|
|
|
cothread_context *context;
|
2003-03-11 21:01:31 +00:00
|
|
|
g_return_if_fail (thread != thread->context->main);
|
2003-03-11 21:37:36 +00:00
|
|
|
g_return_if_fail (thread != thread->context->current);
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
thread->die = TRUE;
|
2003-03-11 21:37:36 +00:00
|
|
|
join = thread->thread;
|
|
|
|
context = thread->context;
|
2003-03-11 21:01:31 +00:00
|
|
|
g_cond_signal (thread->cond);
|
|
|
|
g_mutex_unlock (thread->context->mutex);
|
2003-03-11 21:37:36 +00:00
|
|
|
g_thread_join (join);
|
|
|
|
/* the mutex was locked by the thread that we joined, no need to lock again */
|
2003-03-11 21:01:31 +00:00
|
|
|
}
|
2004-03-15 14:43:34 +00:00
|
|
|
|
2003-03-11 21:01:31 +00:00
|
|
|
#define do_cothread_get_current(context) ((context)->current)
|
|
|
|
#define do_cothread_get_main(context) ((context)->main)
|
2004-11-07 19:31:39 +00:00
|
|
|
|
|
|
|
#endif /* GTHREAD_COTHREADS_NO_DEFINITIONS */
|
|
|
|
|
|
|
|
#endif /* __GTHREAD_COTHREADS_H__ */
|