signals: Ensure we ref handler in emission fast path
[platform/upstream/glib.git] / gobject / gsourceclosure.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gsourceclosure.h"
23 #include "gboxed.h"
24 #include "genums.h"
25 #include "gmarshal.h"
26 #include "gvalue.h"
27 #include "gvaluetypes.h"
28
29 G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
30
31 GType
32 g_io_condition_get_type (void)
33 {
34   static GType etype = 0;
35   if (etype == 0)
36     {
37       static const GFlagsValue values[] = {
38         { G_IO_IN,   "G_IO_IN",   "in" },
39         { G_IO_OUT,  "G_IO_OUT",  "out" },
40         { G_IO_PRI,  "G_IO_PRI",  "pri" },
41         { G_IO_ERR,  "G_IO_ERR",  "err" },
42         { G_IO_HUP,  "G_IO_HUP",  "hup" },
43         { G_IO_NVAL, "G_IO_NVAL", "nval" },
44         { 0, NULL, NULL }
45       };
46       etype = g_flags_register_static ("GIOCondition", values);
47     }
48   return etype;
49 }
50
51 /* We need to hand-write this marshaler, since it doesn't have an
52  * instance object.
53  */
54 static void
55 source_closure_marshal_BOOLEAN__VOID (GClosure     *closure,
56                                       GValue       *return_value,
57                                       guint         n_param_values,
58                                       const GValue *param_values,
59                                       gpointer      invocation_hint,
60                                       gpointer      marshal_data)
61 {
62   GSourceFunc callback;
63   GCClosure *cc = (GCClosure*) closure;
64   gboolean v_return;
65
66   g_return_if_fail (return_value != NULL);
67   g_return_if_fail (n_param_values == 0);
68
69   callback = (GSourceFunc) (marshal_data ? marshal_data : cc->callback);
70
71   v_return = callback (closure->data);
72
73   g_value_set_boolean (return_value, v_return);
74 }
75
76 static gboolean
77 io_watch_closure_callback (GIOChannel   *channel,
78                            GIOCondition  condition,
79                            gpointer      data)
80 {
81   GClosure *closure = data;
82
83   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
84   GValue result_value = G_VALUE_INIT;
85   gboolean result;
86
87   g_value_init (&result_value, G_TYPE_BOOLEAN);
88   g_value_init (&params[0], G_TYPE_IO_CHANNEL);
89   g_value_set_boxed (&params[0], channel);
90                      
91   g_value_init (&params[1], G_TYPE_IO_CONDITION);
92   g_value_set_flags (&params[1], condition);
93
94   g_closure_invoke (closure, &result_value, 2, params, NULL);
95
96   result = g_value_get_boolean (&result_value);
97   g_value_unset (&result_value);
98   g_value_unset (&params[0]);
99   g_value_unset (&params[1]);
100
101   return result;
102 }
103
104 static gboolean
105 source_closure_callback (gpointer data)
106 {
107   GClosure *closure = data;
108   GValue result_value = G_VALUE_INIT;
109   gboolean result;
110
111   g_value_init (&result_value, G_TYPE_BOOLEAN);
112   
113   g_closure_invoke (closure, &result_value, 0, NULL, NULL);
114
115   result = g_value_get_boolean (&result_value);
116   g_value_unset (&result_value);
117
118   return result;
119 }
120
121 static void
122 closure_callback_get (gpointer     cb_data,
123                       GSource     *source,
124                       GSourceFunc *func,
125                       gpointer    *data)
126 {
127   GSourceFunc closure_callback = source->source_funcs->closure_callback;
128
129   if (!closure_callback)
130     {
131       if (source->source_funcs == &g_io_watch_funcs)
132         closure_callback = (GSourceFunc)io_watch_closure_callback;
133       else if (source->source_funcs == &g_timeout_funcs ||
134                source->source_funcs == &g_idle_funcs)
135         closure_callback = source_closure_callback;
136     }
137
138   *func = closure_callback;
139   *data = cb_data;
140 }
141
142 static GSourceCallbackFuncs closure_callback_funcs = {
143   (void (*) (gpointer)) g_closure_ref,
144   (void (*) (gpointer)) g_closure_unref,
145   closure_callback_get
146 };
147
148 static void
149 closure_invalidated (gpointer  user_data,
150                      GClosure *closure)
151 {
152   g_source_destroy (user_data);
153 }
154
155 /**
156  * g_source_set_closure:
157  * @source: the source
158  * @closure: a #GClosure
159  *
160  * Set the callback for a source as a #GClosure.
161  *
162  * If the source is not one of the standard GLib types, the @closure_callback
163  * and @closure_marshal fields of the #GSourceFuncs structure must have been
164  * filled in with pointers to appropriate functions.
165  */
166 void
167 g_source_set_closure (GSource  *source,
168                       GClosure *closure)
169 {
170   g_return_if_fail (source != NULL);
171   g_return_if_fail (closure != NULL);
172
173   if (!source->source_funcs->closure_callback &&
174       source->source_funcs != &g_io_watch_funcs &&
175       source->source_funcs != &g_timeout_funcs &&
176       source->source_funcs != &g_idle_funcs)
177     {
178       g_critical (G_STRLOC ": closure can not be set on closure without GSourceFuncs::closure_callback\n");
179       return;
180     }
181
182   g_closure_ref (closure);
183   g_closure_sink (closure);
184   g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
185
186   g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
187
188   if (G_CLOSURE_NEEDS_MARSHAL (closure))
189     {
190       GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
191       if (!marshal)
192         {
193           if (source->source_funcs == &g_idle_funcs ||
194               source->source_funcs == &g_timeout_funcs)
195             marshal = source_closure_marshal_BOOLEAN__VOID;
196           else if (source->source_funcs == &g_io_watch_funcs)
197             marshal = g_cclosure_marshal_BOOLEAN__FLAGS;
198         }
199       if (marshal)
200         g_closure_set_marshal (closure, marshal);
201     }
202 }
203
204 static void
205 dummy_closure_marshal (GClosure     *closure,
206                        GValue       *return_value,
207                        guint         n_param_values,
208                        const GValue *param_values,
209                        gpointer      invocation_hint,
210                        gpointer      marshal_data)
211 {
212   if (G_VALUE_HOLDS_BOOLEAN (return_value))
213     g_value_set_boolean (return_value, TRUE);
214 }
215
216 /**
217  * g_source_set_dummy_callback:
218  * @source: the source
219  *
220  * Sets a dummy callback for @source. The callback will do nothing, and
221  * if the source expects a #gboolean return value, it will return %TRUE.
222  * (If the source expects any other type of return value, it will return
223  * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
224  * that type.)
225  *
226  * If the source is not one of the standard GLib types, the
227  * @closure_callback and @closure_marshal fields of the #GSourceFuncs
228  * structure must have been filled in with pointers to appropriate
229  * functions.
230  */
231 void
232 g_source_set_dummy_callback (GSource *source)
233 {
234   GClosure *closure;
235
236   closure = g_closure_new_simple (sizeof (GClosure), NULL);
237   g_closure_set_meta_marshal (closure, NULL, dummy_closure_marshal);
238   g_source_set_closure (source, closure);
239 }