1 /* GStreamer
2 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include <pthread.h>
21
22 #include "testrtpool.h"
23
24 static void test_rt_pool_finalize (GObject * object);
25
26 typedef struct
27 {
28 pthread_t thread;
29 } TestRTId;
30
31 G_DEFINE_TYPE (TestRTPool, test_rt_pool, GST_TYPE_TASK_POOL);
32
33 static void
default_prepare(GstTaskPool * pool,GError ** error)34 default_prepare (GstTaskPool * pool, GError ** error)
35 {
36 /* we don't do anything here. We could construct a pool of threads here that
37 * we could reuse later but we don't */
38 g_message ("prepare Realtime pool %p", pool);
39 }
40
41 static void
default_cleanup(GstTaskPool * pool)42 default_cleanup (GstTaskPool * pool)
43 {
44 g_message ("cleanup Realtime pool %p", pool);
45 }
46
47 static gpointer
default_push(GstTaskPool * pool,GstTaskPoolFunction func,gpointer data,GError ** error)48 default_push (GstTaskPool * pool, GstTaskPoolFunction func, gpointer data,
49 GError ** error)
50 {
51 TestRTId *tid;
52 gint res;
53 pthread_attr_t attr;
54 struct sched_param param;
55
56 g_message ("pushing Realtime pool %p, %p", pool, func);
57
58 tid = g_slice_new0 (TestRTId);
59
60 g_message ("set policy");
61 pthread_attr_init (&attr);
62 if ((res = pthread_attr_setschedpolicy (&attr, SCHED_RR)) != 0)
63 g_warning ("setschedpolicy: failure: %p", g_strerror (res));
64
65 g_message ("set prio");
66 param.sched_priority = 50;
67 if ((res = pthread_attr_setschedparam (&attr, ¶m)) != 0)
68 g_warning ("setschedparam: failure: %p", g_strerror (res));
69
70 g_message ("set inherit");
71 if ((res = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0)
72 g_warning ("setinheritsched: failure: %p", g_strerror (res));
73
74 g_message ("create thread");
75 res = pthread_create (&tid->thread, &attr, (void *(*)(void *)) func, data);
76
77 if (res != 0) {
78 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
79 "Error creating thread: %s", g_strerror (res));
80 g_slice_free (TestRTId, tid);
81 tid = NULL;
82 }
83
84 return tid;
85 }
86
87 static void
default_join(GstTaskPool * pool,gpointer id)88 default_join (GstTaskPool * pool, gpointer id)
89 {
90 TestRTId *tid = (TestRTId *) id;
91
92 g_message ("joining Realtime pool %p", pool);
93
94 pthread_join (tid->thread, NULL);
95
96 g_slice_free (TestRTId, tid);
97 }
98
99 static void
test_rt_pool_class_init(TestRTPoolClass * klass)100 test_rt_pool_class_init (TestRTPoolClass * klass)
101 {
102 GObjectClass *gobject_class;
103 GstTaskPoolClass *gsttaskpool_class;
104
105 gobject_class = (GObjectClass *) klass;
106 gsttaskpool_class = (GstTaskPoolClass *) klass;
107
108 gobject_class->finalize = GST_DEBUG_FUNCPTR (test_rt_pool_finalize);
109
110 gsttaskpool_class->prepare = default_prepare;
111 gsttaskpool_class->cleanup = default_cleanup;
112 gsttaskpool_class->push = default_push;
113 gsttaskpool_class->join = default_join;
114 }
115
116 static void
test_rt_pool_init(TestRTPool * pool)117 test_rt_pool_init (TestRTPool * pool)
118 {
119 }
120
121 static void
test_rt_pool_finalize(GObject * object)122 test_rt_pool_finalize (GObject * object)
123 {
124 G_OBJECT_CLASS (test_rt_pool_parent_class)->finalize (object);
125 }
126
127 GstTaskPool *
test_rt_pool_new(void)128 test_rt_pool_new (void)
129 {
130 GstTaskPool *pool;
131
132 pool = g_object_new (TEST_TYPE_RT_POOL, NULL);
133
134 return pool;
135 }
136