<commit_before>/* Gnome-Streamer
 * Copyright (C) <1999> <NAME> <<EMAIL>>
 *
 * 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.
 */

#include <unistd.h>

#include "gstthread.h"
#include "gstdebug.h"

GstElementDetails gst_thread_details = {
  "Threaded container",
  "Bin",
  "Container that creates/manages a thread",
  VERSION,
  "<NAME> <<EMAIL>>",
  "(C) 1999",
};


/* Thread signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_CREATE_THREAD,
};


static void 			gst_thread_class_init		(GstThreadClass *klass);
static void 			gst_thread_init			(GstThread *thread);

static void 			gst_thread_set_arg		(GtkObject *object,GtkArg *arg,guint id);
static void 			gst_thread_get_arg		(GtkObject *object,GtkArg *arg,guint id);

static GstElementStateReturn 	gst_thread_change_state		(GstElement *element);

static xmlNodePtr 		gst_thread_save_thyself		(GstElement *element,xmlNodePtr parent);
static void 			gst_thread_restore_thyself	(GstElement *element,xmlNodePtr parent, 
								 GHashTable *elements);

static void 			gst_thread_signal_thread	(GstThread *thread);
static void 			gst_thread_create_plan_dummy	(GstBin *bin);

static void*			gst_thread_main_loop		(void *arg);

static GstBinClass *parent_class = NULL;
//static guint gst_thread_signals[LAST_SIGNAL] = { 0 };

GtkType
gst_thread_get_type(void) {
  static GtkType thread_type = 0;

  if (!thread_type) {
    static const GtkTypeInfo thread_info = {
      "GstThread",
      sizeof(GstThread),
      sizeof(GstThreadClass),
      (GtkClassInitFunc)gst_thread_class_init,
      (GtkObjectInitFunc)gst_thread_init,
      (GtkArgSetFunc)NULL,
      (GtkArgGetFunc)NULL,
      (GtkClassInitFunc)NULL,
    };
    thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
  }
  return thread_type;
}

static void
gst_thread_class_init (GstThreadClass *klass) 
{
  GtkObjectClass *gtkobject_class;
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
  GstBinClass *gstbin_class;

  gtkobject_class = 	(GtkObjectClass*)klass;
  gstobject_class = 	(GstObjectClass*)klass;
  gstelement_class = 	(GstElementClass*)klass;
  gstbin_class = 	(GstBinClass*)klass;

  parent_class = gtk_type_class (GST_TYPE_BIN);

  gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
                           GTK_ARG_READWRITE, ARG_CREATE_THREAD);

  gstelement_class->change_state = 	gst_thread_change_state;
  gstelement_class->save_thyself = 	gst_thread_save_thyself;
  gstelement_class->restore_thyself = 	gst_thread_restore_thyself;

  gstbin_class->create_plan = gst_thread_create_plan_dummy;

  gtkobject_class->set_arg = gst_thread_set_arg;
  gtkobject_class->get_arg = gst_thread_get_arg;

}

static void 
gst_thread_init (GstThread *thread) 
{
  // we're a manager by default
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);

  // default is to create a thread
  GST_FLAG_SET (thread, GST_THREAD_CREATE);
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

  thread->lock = g_mutex_new();
  thread->cond = g_cond_new();
}

static void 
gst_thread_create_plan_dummy (GstBin *bin) 
{
  g_return_if_fail (GST_IS_THREAD (bin));

  if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING)) 
    gst_info("gstthread: create plan delayed until thread starts\n");
}

static void 
gst_thread_set_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id) 
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch(id) {
    case ARG_CREATE_THREAD:
      if (GTK_VALUE_BOOL (*arg)) {
        gst_info("gstthread: turning ON the creation of the thread\n");
        GST_FLAG_SET (object, GST_THREAD_CREATE);
        gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      } else {
        gst_info("gstthread: turning OFF the creation of the thread\n");
        GST_FLAG_UNSET (object, GST_THREAD_CREATE);
        gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      }
      break;
    default:
      break;
  }
}

static void 
gst_thread_get_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id) 
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch (id) {
    case ARG_CREATE_THREAD:
      GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
      break;
    default:
      break;
  }
}


/**
 * gst_thread_new:
 * @name: the name of the thread
 *
 * Create a new thrad with the given name
 *
 * Returns; The new thread
 */
GstElement*
gst_thread_new (guchar *name) 
{
  return gst_elementfactory_make ("bin", name);
}



static GstElementStateReturn 
gst_thread_change_state (GstElement *element) 
{
  GstThread *thread;
  gboolean stateset = GST_STATE_SUCCESS;
  gint pending, transition;

  g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
  DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));

  thread = GST_THREAD (element);

  gst_info("gstthread: thread \"%s\" change state %d\n",
               gst_element_get_name (GST_ELEMENT (element)), 
	       GST_STATE_PENDING (element));<commit_msg>Changed "bin" to "thread" in get_thread_new.<commit_after>/* Gnome-Streamer
 * Copyright (C) <1999> <NAME> <<EMAIL>>
 *
 * 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.
 */

#include <unistd.h>

#include "gstthread.h"
#include "gstdebug.h"

GstElementDetails gst_thread_details = {
  "Threaded container",
  "Bin",
  "Container that creates/manages a thread",
  VERSION,
  "<NAME> <<EMAIL>>",
  "(C) 1999",
};


/* Thread signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  ARG_CREATE_THREAD,
};


static void 			gst_thread_class_init		(GstThreadClass *klass);
static void 			gst_thread_init			(GstThread *thread);

static void 			gst_thread_set_arg		(GtkObject *object,GtkArg *arg,guint id);
static void 			gst_thread_get_arg		(GtkObject *object,GtkArg *arg,guint id);

static GstElementStateReturn 	gst_thread_change_state		(GstElement *element);

static xmlNodePtr 		gst_thread_save_thyself		(GstElement *element,xmlNodePtr parent);
static void 			gst_thread_restore_thyself	(GstElement *element,xmlNodePtr parent, 
								 GHashTable *elements);

static void 			gst_thread_signal_thread	(GstThread *thread);
static void 			gst_thread_create_plan_dummy	(GstBin *bin);

static void*			gst_thread_main_loop		(void *arg);

static GstBinClass *parent_class = NULL;
//static guint gst_thread_signals[LAST_SIGNAL] = { 0 };

GtkType
gst_thread_get_type(void) {
  static GtkType thread_type = 0;

  if (!thread_type) {
    static const GtkTypeInfo thread_info = {
      "GstThread",
      sizeof(GstThread),
      sizeof(GstThreadClass),
      (GtkClassInitFunc)gst_thread_class_init,
      (GtkObjectInitFunc)gst_thread_init,
      (GtkArgSetFunc)NULL,
      (GtkArgGetFunc)NULL,
      (GtkClassInitFunc)NULL,
    };
    thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
  }
  return thread_type;
}

static void
gst_thread_class_init (GstThreadClass *klass) 
{
  GtkObjectClass *gtkobject_class;
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
  GstBinClass *gstbin_class;

  gtkobject_class = 	(GtkObjectClass*)klass;
  gstobject_class = 	(GstObjectClass*)klass;
  gstelement_class = 	(GstElementClass*)klass;
  gstbin_class = 	(GstBinClass*)klass;

  parent_class = gtk_type_class (GST_TYPE_BIN);

  gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
                           GTK_ARG_READWRITE, ARG_CREATE_THREAD);

  gstelement_class->change_state = 	gst_thread_change_state;
  gstelement_class->save_thyself = 	gst_thread_save_thyself;
  gstelement_class->restore_thyself = 	gst_thread_restore_thyself;

  gstbin_class->create_plan = gst_thread_create_plan_dummy;

  gtkobject_class->set_arg = gst_thread_set_arg;
  gtkobject_class->get_arg = gst_thread_get_arg;

}

static void 
gst_thread_init (GstThread *thread) 
{
  // we're a manager by default
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);

  // default is to create a thread
  GST_FLAG_SET (thread, GST_THREAD_CREATE);
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

  thread->lock = g_mutex_new();
  thread->cond = g_cond_new();
}

static void 
gst_thread_create_plan_dummy (GstBin *bin) 
{
  g_return_if_fail (GST_IS_THREAD (bin));

  if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING)) 
    gst_info("gstthread: create plan delayed until thread starts\n");
}

static void 
gst_thread_set_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id) 
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch(id) {
    case ARG_CREATE_THREAD:
      if (GTK_VALUE_BOOL (*arg)) {
        gst_info("gstthread: turning ON the creation of the thread\n");
        GST_FLAG_SET (object, GST_THREAD_CREATE);
        gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      } else {
        gst_info("gstthread: turning OFF the creation of the thread\n");
        GST_FLAG_UNSET (object, GST_THREAD_CREATE);
        gst_info("gstthread: flags are 0x%08x\n", GST_FLAGS (object));
      }
      break;
    default:
      break;
  }
}

static void 
gst_thread_get_arg (GtkObject *object,
		    GtkArg *arg,
		    guint id) 
{
  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_THREAD (object));

  switch (id) {
    case ARG_CREATE_THREAD:
      GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
      break;
    default:
      break;
  }
}


/**
 * gst_thread_new:
 * @name: the name of the thread
 *
 * Create a new thrad with the given name
 *
 * Returns; The new thread
 */
GstElement*
gst_thread_new (guchar *name) 
{
  return gst_elementfactory_make ("thread", name);
}



static GstElementStateReturn 
gst_thread_change_state (GstElement *element) 
{
  GstThread *thread;
  gboolean stateset = GST_STATE_SUCCESS;
  gint pending, transition;

  g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
  DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));

  thread = GST_THREAD (element);

  gst_info("gstthread: thread \"%s\" change state %d\n",
               gst_element_get_name (GST_ELEMENT (element)), 
	       GST_STATE_PENDING (element));
{'fraction_non_alphanumeric': 0.09502067539627843, 'fraction_numerical': 0.005513439007580979, 'mean_word_length': 4.116350815337153, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3658559', 'n_tokens_mistral': 4346, 'n_tokens_neox': 4169, 'n_words': 1002}