miniobject: add lock functionality to GstMiniObject
[platform/upstream/gstreamer.git] / gst / gstminiobject.h
1 /* GStreamer
2  * Copyright (C) 2005 David Schleef <ds@schleef.org>
3  *
4  * gstminiobject.h: Header for GstMiniObject
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifndef __GST_MINI_OBJECT_H__
24 #define __GST_MINI_OBJECT_H__
25
26 #include <gst/gstconfig.h>
27
28 #include <glib-object.h>
29
30 G_BEGIN_DECLS
31
32 #define GST_IS_MINI_OBJECT_TYPE(obj,type)  ((obj) && GST_MINI_OBJECT_TYPE(obj) == (type))
33 #define GST_MINI_OBJECT_CAST(obj)          ((GstMiniObject*)(obj))
34 #define GST_MINI_OBJECT_CONST_CAST(obj)    ((const GstMiniObject*)(obj))
35 #define GST_MINI_OBJECT(obj)               (GST_MINI_OBJECT_CAST(obj))
36
37 typedef struct _GstMiniObject GstMiniObject;
38
39 /**
40  * GstMiniObjectCopyFunction:
41  * @obj: MiniObject to copy
42  *
43  * Function prototype for methods to create copies of instances.
44  *
45  * Returns: reference to cloned instance.
46  */
47 typedef GstMiniObject * (*GstMiniObjectCopyFunction) (const GstMiniObject *obj);
48 /**
49  * GstMiniObjectDisposeFunction:
50  * @obj: MiniObject to dispose
51  *
52  * Function prototype for when a miniobject has lost its last refcount.
53  * Implementation of the mini object are allowed to revive the
54  * passed object by doing a gst_mini_object_ref(). If the object is not
55  * revived after the dispose function, the function should return %TRUE
56  * and the memory associated with the object is freed.
57  *
58  * Returns: %TRUE if the object should be cleaned up.
59  */
60 typedef gboolean (*GstMiniObjectDisposeFunction) (GstMiniObject *obj);
61 /**
62  * GstMiniObjectFreeFunction:
63  * @obj: MiniObject to free
64  *
65  * Virtual function prototype for methods to free ressources used by
66  * mini-objects.
67  */
68 typedef void (*GstMiniObjectFreeFunction) (GstMiniObject *obj);
69
70  /**
71  * GstMiniObjectNotify:
72  * @user_data: data that was provided when the notify was added
73  * @obj: the mini object
74  *
75  * A #GstMiniObjectNotify function can be added to a mini object as a
76  * callback that gets triggered with gst_mini_object_notify().
77  */
78 typedef void (*GstMiniObjectNotify) (gpointer user_data, GstMiniObject * obj);
79
80 /**
81  * GST_MINI_OBJECT_TYPE:
82  * @obj: MiniObject to return type for.
83  *
84  * This macro returns the type of the mini-object.
85  */
86 #define GST_MINI_OBJECT_TYPE(obj)  (GST_MINI_OBJECT_CAST(obj)->type)
87
88 /**
89  * GST_MINI_OBJECT_FLAGS:
90  * @obj: MiniObject to return flags for.
91  *
92  * This macro returns the entire set of flags for the mini-object.
93  */
94 #define GST_MINI_OBJECT_FLAGS(obj)  (GST_MINI_OBJECT_CAST(obj)->flags)
95 /**
96  * GST_MINI_OBJECT_FLAG_IS_SET:
97  * @obj: MiniObject to check for flags.
98  * @flag: Flag to check for
99  *
100  * This macro checks to see if the given flag is set.
101  */
102 #define GST_MINI_OBJECT_FLAG_IS_SET(obj,flag)        !!(GST_MINI_OBJECT_FLAGS (obj) & (flag))
103 /**
104  * GST_MINI_OBJECT_FLAG_SET:
105  * @obj: MiniObject to set flag in.
106  * @flag: Flag to set, can by any number of bits in guint32.
107  *
108  * This macro sets the given bits.
109  */
110 #define GST_MINI_OBJECT_FLAG_SET(obj,flag)           (GST_MINI_OBJECT_FLAGS (obj) |= (flag))
111 /**
112  * GST_MINI_OBJECT_FLAG_UNSET:
113  * @obj: MiniObject to unset flag in.
114  * @flag: Flag to set, must be a single bit in guint32.
115  *
116  * This macro usets the given bits.
117  */
118 #define GST_MINI_OBJECT_FLAG_UNSET(obj,flag)         (GST_MINI_OBJECT_FLAGS (obj) &= ~(flag))
119
120 /**
121  * GstMiniObjectFlags:
122  * @GST_MINI_OBJECT_FLAG_LOCKABLE: the object can be locked and unlocked with
123  * gst_mini_object_lock() and gst_mini_object_unlock().
124  * @GST_MINI_OBJECT_FLAG_LOCK_READONLY: the object is permanently locked in
125  * READONLY mode. Only read locks can be performed on the object.
126  * @GST_MINI_OBJECT_FLAG_LAST: first flag that can be used by subclasses.
127  *
128  * Flags for the mini object
129  */
130 typedef enum
131 {
132   GST_MINI_OBJECT_FLAG_LOCKABLE      = (1 << 0),
133   GST_MINI_OBJECT_FLAG_LOCK_READONLY = (1 << 1),
134   /* padding */
135   GST_MINI_OBJECT_FLAG_LAST          = (1 << 4)
136 } GstMiniObjectFlags;
137
138 /**
139  * GST_MINI_OBJECT_IS_LOCKABLE:
140  * @obj: a #GstMiniObject
141  *
142  * Check if @obj is lockable. A lockable object can be locked and unlocked with
143  * gst_mini_object_lock() and gst_mini_object_unlock().
144  */
145 #define GST_MINI_OBJECT_IS_LOCKABLE(obj)  GST_MINI_OBJECT_FLAG_IS_SET(obj, GST_MINI_OBJECT_FLAG_LOCKABLE)
146
147 /**
148  * GstLockFlags:
149  * @GST_LOCK_FLAG_READ: lock for read access
150  * @GST_LOCK_FLAG_WRITE: lock for write access
151  * @GST_LOCK_FLAG_EXCLUSIVE: lock for exclusive access
152  * @GST_LOCK_FLAG_LAST: first flag that can be used for custom purposes
153  *
154  * Flags used when locking miniobjects
155  */
156 typedef enum {
157   GST_LOCK_FLAG_READ      = (1 << 0),
158   GST_LOCK_FLAG_WRITE     = (1 << 1),
159   GST_LOCK_FLAG_EXCLUSIVE = (1 << 2),
160
161   GST_LOCK_FLAG_LAST      = (1 << 4)
162 } GstLockFlags;
163
164 /**
165  * GST_LOCK_FLAG_READWRITE:
166  *
167  * GstLockFlags value alias for GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE
168  */
169 #define GST_LOCK_FLAG_READWRITE  (GST_LOCK_FLAG_READ | GST_LOCK_FLAG_WRITE)
170
171 /**
172  * GST_MINI_OBJECT_REFCOUNT:
173  * @obj: a #GstMiniObject
174  *
175  * Get access to the reference count field of the mini-object.
176  */
177 #define GST_MINI_OBJECT_REFCOUNT(obj)           ((GST_MINI_OBJECT_CAST(obj))->refcount)
178 /**
179  * GST_MINI_OBJECT_REFCOUNT_VALUE:
180  * @obj: a #GstMiniObject
181  *
182  * Get the reference count value of the mini-object.
183  */
184 #define GST_MINI_OBJECT_REFCOUNT_VALUE(obj)     (g_atomic_int_get (&(GST_MINI_OBJECT_CAST(obj))->refcount))
185
186 /**
187  * GstMiniObject:
188  * @type: the GType of the object
189  * @refcount: atomic refcount
190  * @lockstate: atomic state of the locks
191  * @flags: extra flags.
192  * @copy: a copy function
193  * @dispose: a dispose function
194  * @free: the free function
195  *
196  * Base class for refcounted lightweight objects.
197  * Ref Func: gst_mini_object_ref
198  * Unref Func: gst_mini_object_unref
199  * Set Value Func: g_value_set_boxed
200  * Get Value Func: g_value_get_boxed
201  */
202 struct _GstMiniObject {
203   GType   type;
204
205   /*< public >*/ /* with COW */
206   gint    refcount;
207   gint    lockstate;
208   guint   flags;
209
210   GstMiniObjectCopyFunction copy;
211   GstMiniObjectDisposeFunction dispose;
212   GstMiniObjectFreeFunction free;
213
214   /* < private > */
215   /* Used to keep track of weak ref notifies and qdata */
216   guint n_qdata;
217   gpointer qdata;
218 };
219
220 void            gst_mini_object_init (GstMiniObject *mini_object,
221                                       guint flags, GType type,
222                                       GstMiniObjectCopyFunction copy_func,
223                                       GstMiniObjectDisposeFunction dispose_func,
224                                       GstMiniObjectFreeFunction free_func);
225
226
227 /* refcounting */
228 GstMiniObject * gst_mini_object_ref             (GstMiniObject *mini_object);
229 void            gst_mini_object_unref           (GstMiniObject *mini_object);
230
231 void            gst_mini_object_weak_ref        (GstMiniObject *object,
232                                                  GstMiniObjectNotify notify,
233                                                  gpointer data);
234 void            gst_mini_object_weak_unref      (GstMiniObject *object,
235                                                  GstMiniObjectNotify notify,
236                                                  gpointer data);
237
238 /* locking */
239 gboolean        gst_mini_object_lock            (GstMiniObject *object, GstLockFlags flags);
240 void            gst_mini_object_unlock          (GstMiniObject *object, GstLockFlags flags);
241
242 gboolean        gst_mini_object_is_writable     (const GstMiniObject *mini_object);
243 GstMiniObject * gst_mini_object_make_writable   (GstMiniObject *mini_object);
244
245 /* copy */
246 GstMiniObject * gst_mini_object_copy            (const GstMiniObject *mini_object) G_GNUC_MALLOC;
247
248
249 void            gst_mini_object_set_qdata       (GstMiniObject *object, GQuark quark,
250                                                  gpointer data, GDestroyNotify destroy);
251 gpointer        gst_mini_object_get_qdata       (GstMiniObject *object, GQuark quark);
252 gpointer        gst_mini_object_steal_qdata     (GstMiniObject *object, GQuark quark);
253
254
255 gboolean        gst_mini_object_replace         (GstMiniObject **olddata, GstMiniObject *newdata);
256 gboolean        gst_mini_object_take            (GstMiniObject **olddata, GstMiniObject *newdata);
257 GstMiniObject * gst_mini_object_steal           (GstMiniObject **olddata);
258
259 /**
260  * GST_DEFINE_MINI_OBJECT_TYPE:
261  * @TypeName: name of the new type in CamelCase
262  * @type_name: name of the new type
263  *
264  * Define a new mini-object type with the given name
265  */
266 #define GST_DEFINE_MINI_OBJECT_TYPE(TypeName,type_name) \
267    G_DEFINE_BOXED_TYPE(TypeName,type_name,              \
268        (GBoxedCopyFunc) gst_mini_object_ref,            \
269        (GBoxedFreeFunc) gst_mini_object_unref)
270
271 G_END_DECLS
272
273 #endif
274