source: trunk/third/gstreamer/testsuite/threads/signals.c @ 21448

Revision 21448, 5.2 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21447, which included commits to RCS files with non-trunk default branches.
Line 
1#include <unistd.h>
2#include <gst/gst.h>
3
4#define GST_TYPE_TEST               (gst_test_get_type ())
5#define GST_TEST(test)              (G_TYPE_CHECK_INSTANCE_CAST ((test), GST_TYPE_TEST, GstTest))
6#define GST_IS_TEST(test)           (G_TYPE_CHECK_INSTANCE_TYPE ((test), GST_TYPE_TEST))
7#define GST_TEST_CLASS(tclass)      (G_TYPE_CHECK_CLASS_CAST ((tclass), GST_TYPE_TEST, GstTestClass))
8#define GST_IS_TEST_CLASS(tclass)   (G_TYPE_CHECK_CLASS_TYPE ((tclass), GST_TYPE_TEST))
9#define GST_TEST_GET_CLASS(test)    (G_TYPE_INSTANCE_GET_CLASS ((test), GST_TYPE_TEST, GstTestClass))
10
11typedef struct _GstTest GstTest;
12typedef struct _GstTestClass GstTestClass;
13
14struct _GstTest
15{
16  GstObject object;
17};
18
19struct _GstTestClass
20{
21  GstObjectClass parent_class;
22
23  void (*test_signal1) (GstTest * test, gint an_int);
24  void (*test_signal2) (GstTest * test, gint an_int);
25};
26
27static GType gst_test_get_type (void);
28
29/* Element signals and args */
30enum
31{
32  TEST_SIGNAL1,
33  TEST_SIGNAL2,
34  /* add more above */
35  LAST_SIGNAL
36};
37
38enum
39{
40  ARG_0,
41  ARG_TEST_PROP
42};
43
44static void gst_test_class_init (GstTestClass * klass);
45static void gst_test_init (GstTest * test);
46static void gst_test_dispose (GObject * object);
47
48static void signal2_handler (GstTest * test, gint anint);
49
50static void gst_test_set_property (GObject * object, guint prop_id,
51    const GValue * value, GParamSpec * pspec);
52static void gst_test_get_property (GObject * object, guint prop_id,
53    GValue * value, GParamSpec * pspec);
54
55static GstObjectClass *parent_class = NULL;
56
57static guint gst_test_signals[LAST_SIGNAL] = { 0 };
58
59static GType
60gst_test_get_type (void)
61{
62  static GType test_type = 0;
63
64  if (!test_type) {
65    static const GTypeInfo test_info = {
66      sizeof (GstTestClass),
67      NULL,
68      NULL,
69      (GClassInitFunc) gst_test_class_init,
70      NULL,
71      NULL,
72      sizeof (GstTest),
73      0,
74      (GInstanceInitFunc) gst_test_init,
75      NULL
76    };
77
78    test_type = g_type_register_static (GST_TYPE_OBJECT, "GstTest",
79        &test_info, 0);
80  }
81  return test_type;
82}
83
84static void
85gst_test_class_init (GstTestClass * klass)
86{
87  GObjectClass *gobject_class;
88  GstObjectClass *gstobject_class;
89
90  gobject_class = (GObjectClass *) klass;
91  gstobject_class = (GstObjectClass *) klass;
92
93  parent_class = g_type_class_ref (GST_TYPE_OBJECT);
94
95  if (!g_thread_supported ())
96    g_thread_init (NULL);
97
98  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_test_dispose);
99  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_test_set_property);
100  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_test_get_property);
101
102  gst_test_signals[TEST_SIGNAL1] =
103      g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass),
104      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstTestClass, test_signal1), NULL,
105      NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
106  gst_test_signals[TEST_SIGNAL2] =
107      g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass),
108      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstTestClass, test_signal2), NULL,
109      NULL, gst_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
110
111  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP,
112      g_param_spec_int ("test-prop", "Test Prop", "Test property",
113          0, 1, 0, G_PARAM_READWRITE));
114
115  klass->test_signal2 = signal2_handler;
116}
117
118static void
119gst_test_init (GstTest * test)
120{
121}
122
123static void
124gst_test_dispose (GObject * object)
125{
126  GstTest *test;
127
128  test = GST_TEST (object);
129
130  G_OBJECT_CLASS (parent_class)->dispose (object);
131}
132
133static void
134gst_test_set_property (GObject * object, guint prop_id,
135    const GValue * value, GParamSpec * pspec)
136{
137  GstTest *test;
138
139  test = GST_TEST (object);
140
141  switch (prop_id) {
142    case ARG_TEST_PROP:
143      g_value_get_int (value);
144      break;
145    default:
146      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147      break;
148  }
149}
150
151static void
152gst_test_get_property (GObject * object, guint prop_id,
153    GValue * value, GParamSpec * pspec)
154{
155  GstTest *test;
156
157  test = GST_TEST (object);
158
159  switch (prop_id) {
160    case ARG_TEST_PROP:
161      g_value_set_int (value, 0);
162      break;
163    default:
164      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165      break;
166  }
167}
168
169static void
170gst_test_do_signal1 (GstTest * test)
171{
172  g_signal_emit (G_OBJECT (test), gst_test_signals[TEST_SIGNAL1], 0, 0);
173}
174
175static void
176signal2_handler (GstTest * test, gint anint)
177{
178}
179
180static void
181gst_test_do_signal2 (GstTest * test)
182{
183  g_signal_emit (G_OBJECT (test), gst_test_signals[TEST_SIGNAL2], 0, 0);
184}
185
186static void
187gst_test_do_prop (GstTest * test)
188{
189  g_object_notify (G_OBJECT (test), "test-prop");
190}
191
192static gpointer
193run_thread (GstTest * test)
194{
195  gint i = 0;
196
197  while (TRUE) {
198    if (TESTNUM == 1)
199      gst_test_do_signal1 (test);
200    if (TESTNUM == 2)
201      gst_test_do_signal2 (test);
202    if (TESTNUM == 3)
203      gst_test_do_prop (test);
204    if ((i++ % 10000) == 0) {
205      g_print (".");
206      g_usleep(1); /* context switch */
207    }
208  }
209
210  return NULL;
211}
212
213int
214main (int argc, char **argv)
215{
216  gint i;
217  GstTest *test1, *test2;
218
219  gst_init (&argc, &argv);
220
221  test1 = g_object_new (GST_TYPE_TEST, NULL);
222  test2 = g_object_new (GST_TYPE_TEST, NULL);
223
224  for (i = 0; i < 20; i++) {
225    g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL);
226    g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL);
227  }
228  sleep (5);
229
230  return 0;
231}
Note: See TracBrowser for help on using the repository browser.