gst/gsttask.*: Detect and warn for obvious deadlocks. fixes #320340
[platform/upstream/gstreamer.git] / gst / gsttask.h
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *               <2005> Wim Taymans <wim@fluendo.com>
4  *
5  * gsttask.h: Streaming tasks
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifndef __GST_TASK_H__
24 #define __GST_TASK_H__
25
26 #include <gst/gstobject.h>
27
28 G_BEGIN_DECLS
29
30 /**
31  * GstTaskFunction:
32  * @data: user data passed to the function
33  *
34  * A function that will repeadedly be called in the thread created by
35  * a GstTask. 
36  */
37 typedef void         (*GstTaskFunction)          (void *data);
38
39 /* --- standard type macros --- */
40 #define GST_TYPE_TASK                   (gst_task_get_type ())
41 #define GST_TASK(task)                  (G_TYPE_CHECK_INSTANCE_CAST ((task), GST_TYPE_TASK, GstTask))
42 #define GST_IS_TASK(task)               (G_TYPE_CHECK_INSTANCE_TYPE ((task), GST_TYPE_TASK))
43 #define GST_TASK_CLASS(tclass)          (G_TYPE_CHECK_CLASS_CAST ((tclass), GST_TYPE_TASK, GstTaskClass))
44 #define GST_IS_TASK_CLASS(tclass)       (G_TYPE_CHECK_CLASS_TYPE ((tclass), GST_TYPE_TASK))
45 #define GST_TASK_GET_CLASS(task)        (G_TYPE_INSTANCE_GET_CLASS ((task), GST_TYPE_TASK, GstTaskClass))
46 #define GST_TASK_CAST(task)             ((GstTask*)(task))
47
48 typedef struct _GstTask GstTask;
49 typedef struct _GstTaskClass GstTaskClass;
50
51 /**
52  * GstTaskState:
53  * @GST_TASK_STARTED: the task is started and running
54  * @GST_TASK_STOPPED:  the task is stopped
55  * @GST_TASK_PAUSED: the task is paused
56  *
57  * The different states a task can be in
58  */
59 typedef enum {
60   GST_TASK_STARTED,
61   GST_TASK_STOPPED,
62   GST_TASK_PAUSED,
63 } GstTaskState;
64
65 /**
66  * GST_TASK_STATE:
67  * @task: Task to get the state of
68  *
69  * Get access to the state of the task.
70  */
71 #define GST_TASK_STATE(task)            (GST_TASK_CAST(task)->state)
72
73 /**
74  * GST_TASK_GET_COND:
75  * @task: Task to get the cond of
76  *
77  * Get access to the cond of the task.
78  */
79 #define GST_TASK_GET_COND(task)         (GST_TASK_CAST(task)->cond)
80 /**
81  * GST_TASK_WAIT:
82  * @task: Task to wait for
83  *
84  * Wait for the task cond to be signalled
85  */
86 #define GST_TASK_WAIT(task)             g_cond_wait(GST_TASK_GET_COND (task), GST_OBJECT_GET_LOCK (task))
87 /**
88  * GST_TASK_SIGNAL:
89  * @task: Task to signal
90  *
91  * Signal the task cond
92  */
93 #define GST_TASK_SIGNAL(task)           g_cond_signal(GST_TASK_GET_COND (task))
94 /**
95  * GST_TASK_BROADCAST:
96  * @task: Task to broadcast
97  *
98  * Send a broadcast signal to all waiting task conds
99  */
100 #define GST_TASK_BROADCAST(task)        g_cond_breadcast(GST_TASK_GET_COND (task))
101
102 /**
103  * GST_TASK_GET_LOCK:
104  * @task: Task to get the lock of
105  *
106  * Get access to the task lock.
107  */
108 #define GST_TASK_GET_LOCK(task)         (GST_TASK_CAST(task)->lock)
109
110 /**
111  * GstTask:
112  * @state: the state of the task
113  * @cond: used to pause/resume the task
114  * @lock: The lock taken when iterating the taskfunction
115  * @func: the function executed by this task
116  * @data: data passed to the task function
117  * @running: a flag indicating that the task is running.
118  *
119  * The #GstTask object.
120  */
121 struct _GstTask {
122   GstObject      object;
123
124   /*< public >*/ /* with LOCK */
125   GstTaskState     state;
126   GCond           *cond;
127
128   GStaticRecMutex *lock;
129
130   GstTaskFunction  func;
131   gpointer         data;
132
133   gboolean         running;
134
135   /*< private >*/
136   union {
137     struct {
138       /* thread this task is currently running in */
139       GThread  *thread;
140     } ABI;
141     /* adding + 0 to mark ABI change to be undone later */
142     gpointer _gst_reserved[GST_PADDING + 0];
143   } abidata;
144
145 };
146
147 struct _GstTaskClass {
148   GstObjectClass parent_class;
149
150   /*< private >*/
151   GThreadPool *pool;
152
153   /*< private >*/
154   gpointer _gst_reserved[GST_PADDING];
155 };
156
157 void            gst_task_cleanup_all    (void);
158
159 GType           gst_task_get_type       (void);
160
161 GstTask*        gst_task_create         (GstTaskFunction func, gpointer data);
162 void            gst_task_set_lock       (GstTask *task, GStaticRecMutex *mutex);
163
164 GstTaskState    gst_task_get_state      (GstTask *task);
165
166 gboolean        gst_task_start          (GstTask *task);
167 gboolean        gst_task_stop           (GstTask *task);
168 gboolean        gst_task_pause          (GstTask *task);
169
170 gboolean        gst_task_join           (GstTask *task);
171
172 G_END_DECLS
173
174 #endif /* __GST_TASK_H__ */
175