LCOV - code coverage report
Current view: top level - glib/gobject - gsourceclosure.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 116 119 97.5 %
Date: 2023-11-28 05:14:24 Functions: 13 13 100.0 %
Branches: 46 58 79.3 %

           Branch data     Line data    Source code
       1                 :            : /* GObject - GLib Type, Object, Parameter and Signal Library
       2                 :            :  * Copyright (C) 2001 Red Hat, Inc.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 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                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General
      17                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      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                 :            : #ifdef G_OS_UNIX
      29                 :            : #include "glib-unix.h"
      30                 :            : #endif
      31                 :            : 
      32   [ +  -  +  -  :          4 : G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
                   +  - ]
      33                 :            : 
      34                 :            : GType
      35                 :       4941 : g_io_condition_get_type (void)
      36                 :            : {
      37                 :            :   static GType etype = 0;
      38                 :            : 
      39   [ +  +  +  -  :       4941 :   if (g_once_init_enter_pointer (&etype))
                   +  + ]
      40                 :            :     {
      41                 :            :       static const GFlagsValue values[] = {
      42                 :            :         { G_IO_IN,   "G_IO_IN",   "in" },
      43                 :            :         { G_IO_OUT,  "G_IO_OUT",  "out" },
      44                 :            :         { G_IO_PRI,  "G_IO_PRI",  "pri" },
      45                 :            :         { G_IO_ERR,  "G_IO_ERR",  "err" },
      46                 :            :         { G_IO_HUP,  "G_IO_HUP",  "hup" },
      47                 :            :         { G_IO_NVAL, "G_IO_NVAL", "nval" },
      48                 :            :         { 0, NULL, NULL }
      49                 :            :       };
      50                 :         13 :       GType type_id = g_flags_register_static ("GIOCondition", values);
      51                 :         13 :       g_once_init_leave_pointer (&etype, type_id);
      52                 :            :     }
      53                 :       4941 :   return etype;
      54                 :            : }
      55                 :            : 
      56                 :            : /* We need to hand-write this marshaler, since it doesn't have an
      57                 :            :  * instance object.
      58                 :            :  */
      59                 :            : static void
      60                 :          3 : source_closure_marshal_BOOLEAN__VOID (GClosure     *closure,
      61                 :            :                                       GValue       *return_value,
      62                 :            :                                       guint         n_param_values,
      63                 :            :                                       const GValue *param_values,
      64                 :            :                                       gpointer      invocation_hint,
      65                 :            :                                       gpointer      marshal_data)
      66                 :            : {
      67                 :            :   GSourceFunc callback;
      68                 :          3 :   GCClosure *cc = (GCClosure*) closure;
      69                 :            :   gboolean v_return;
      70                 :            : 
      71                 :          3 :   g_return_if_fail (return_value != NULL);
      72                 :          3 :   g_return_if_fail (n_param_values == 0);
      73                 :            : 
      74         [ +  - ]:          3 :   callback = (GSourceFunc) (marshal_data ? marshal_data : cc->callback);
      75                 :            : 
      76                 :          3 :   v_return = callback (closure->data);
      77                 :            : 
      78                 :          3 :   g_value_set_boolean (return_value, v_return);
      79                 :            : }
      80                 :            : 
      81                 :            : static gboolean
      82                 :          1 : io_watch_closure_callback (GIOChannel   *channel,
      83                 :            :                            GIOCondition  condition,
      84                 :            :                            gpointer      data)
      85                 :            : {
      86                 :          1 :   GClosure *closure = data;
      87                 :            : 
      88                 :          1 :   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
      89                 :          1 :   GValue result_value = G_VALUE_INIT;
      90                 :            :   gboolean result;
      91                 :            : 
      92                 :          1 :   g_value_init (&result_value, G_TYPE_BOOLEAN);
      93                 :          1 :   g_value_init (&params[0], G_TYPE_IO_CHANNEL);
      94                 :          1 :   g_value_set_boxed (&params[0], channel);
      95                 :            :                      
      96                 :          1 :   g_value_init (&params[1], G_TYPE_IO_CONDITION);
      97                 :          1 :   g_value_set_flags (&params[1], condition);
      98                 :            : 
      99                 :          1 :   g_closure_invoke (closure, &result_value, 2, params, NULL);
     100                 :            : 
     101                 :          1 :   result = g_value_get_boolean (&result_value);
     102                 :          1 :   g_value_unset (&result_value);
     103                 :          1 :   g_value_unset (&params[0]);
     104                 :          1 :   g_value_unset (&params[1]);
     105                 :            : 
     106                 :          1 :   return result;
     107                 :            : }
     108                 :            : 
     109                 :            : static gboolean
     110                 :          1 : g_child_watch_closure_callback (GPid     pid,
     111                 :            :                                 gint     status,
     112                 :            :                                 gpointer data)
     113                 :            : {
     114                 :          1 :   GClosure *closure = data;
     115                 :            : 
     116                 :          1 :   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
     117                 :          1 :   GValue result_value = G_VALUE_INIT;
     118                 :            :   gboolean result;
     119                 :            : 
     120                 :          1 :   g_value_init (&result_value, G_TYPE_BOOLEAN);
     121                 :            : 
     122                 :            : #ifdef G_OS_UNIX
     123                 :          1 :   g_value_init (&params[0], G_TYPE_ULONG);
     124                 :          1 :   g_value_set_ulong (&params[0], pid);
     125                 :            : #endif
     126                 :            : #ifdef G_OS_WIN32
     127                 :            :   g_value_init (&params[0], G_TYPE_POINTER);
     128                 :            :   g_value_set_pointer (&params[0], pid);
     129                 :            : #endif
     130                 :            : 
     131                 :          1 :   g_value_init (&params[1], G_TYPE_INT);
     132                 :          1 :   g_value_set_int (&params[1], status);
     133                 :            : 
     134                 :          1 :   g_closure_invoke (closure, &result_value, 2, params, NULL);
     135                 :            : 
     136                 :          1 :   result = g_value_get_boolean (&result_value);
     137                 :          1 :   g_value_unset (&result_value);
     138                 :          1 :   g_value_unset (&params[0]);
     139                 :          1 :   g_value_unset (&params[1]);
     140                 :            : 
     141                 :          1 :   return result;
     142                 :            : }
     143                 :            : 
     144                 :            : #ifdef G_OS_UNIX
     145                 :            : static gboolean
     146                 :       4754 : g_unix_fd_source_closure_callback (int           fd,
     147                 :            :                                    GIOCondition  condition,
     148                 :            :                                    gpointer      data)
     149                 :            : {
     150                 :       4754 :   GClosure *closure = data;
     151                 :            : 
     152                 :       4754 :   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
     153                 :       4754 :   GValue result_value = G_VALUE_INIT;
     154                 :            :   gboolean result;
     155                 :            : 
     156                 :       4754 :   g_value_init (&result_value, G_TYPE_BOOLEAN);
     157                 :            : 
     158                 :       4754 :   g_value_init (&params[0], G_TYPE_INT);
     159                 :       4754 :   g_value_set_int (&params[0], fd);
     160                 :            : 
     161                 :       4754 :   g_value_init (&params[1], G_TYPE_IO_CONDITION);
     162                 :       4754 :   g_value_set_flags (&params[1], condition);
     163                 :            : 
     164                 :       4754 :   g_closure_invoke (closure, &result_value, 2, params, NULL);
     165                 :            : 
     166                 :       4754 :   result = g_value_get_boolean (&result_value);
     167                 :       4754 :   g_value_unset (&result_value);
     168                 :       4754 :   g_value_unset (&params[0]);
     169                 :       4754 :   g_value_unset (&params[1]);
     170                 :            : 
     171                 :       4754 :   return result;
     172                 :            : }
     173                 :            : #endif
     174                 :            : 
     175                 :            : static gboolean
     176                 :          3 : source_closure_callback (gpointer data)
     177                 :            : {
     178                 :          3 :   GClosure *closure = data;
     179                 :          3 :   GValue result_value = G_VALUE_INIT;
     180                 :            :   gboolean result;
     181                 :            : 
     182                 :          3 :   g_value_init (&result_value, G_TYPE_BOOLEAN);
     183                 :            :   
     184                 :          3 :   g_closure_invoke (closure, &result_value, 0, NULL, NULL);
     185                 :            : 
     186                 :          3 :   result = g_value_get_boolean (&result_value);
     187                 :          3 :   g_value_unset (&result_value);
     188                 :            : 
     189                 :          3 :   return result;
     190                 :            : }
     191                 :            : 
     192                 :            : static void
     193                 :       6472 : closure_callback_get (gpointer     cb_data,
     194                 :            :                       GSource     *source,
     195                 :            :                       GSourceFunc *func,
     196                 :            :                       gpointer    *data)
     197                 :            : {
     198                 :       6472 :   GSourceFunc closure_callback = source->source_funcs->closure_callback;
     199                 :            : 
     200         [ +  + ]:       6472 :   if (!closure_callback)
     201                 :            :     {
     202         [ +  + ]:       4759 :       if (source->source_funcs == &g_io_watch_funcs)
     203                 :          1 :         closure_callback = (GSourceFunc)io_watch_closure_callback;
     204         [ +  + ]:       4758 :       else if (source->source_funcs == &g_child_watch_funcs)
     205                 :          1 :         closure_callback = (GSourceFunc)g_child_watch_closure_callback;
     206                 :            : #ifdef G_OS_UNIX
     207         [ +  + ]:       4757 :       else if (source->source_funcs == &g_unix_fd_source_funcs)
     208                 :       4754 :         closure_callback = (GSourceFunc)g_unix_fd_source_closure_callback;
     209                 :            : #endif
     210         [ +  + ]:          3 :       else if (source->source_funcs == &g_timeout_funcs ||
     211                 :            : #ifdef G_OS_UNIX
     212         [ +  + ]:          2 :                source->source_funcs == &g_unix_signal_funcs ||
     213                 :            : #endif
     214         [ +  - ]:          1 :                source->source_funcs == &g_idle_funcs)
     215                 :          3 :         closure_callback = source_closure_callback;
     216                 :            :     }
     217                 :            : 
     218                 :       6472 :   *func = closure_callback;
     219                 :       6472 :   *data = cb_data;
     220                 :       6472 : }
     221                 :            : 
     222                 :            : static GSourceCallbackFuncs closure_callback_funcs = {
     223                 :            :   (void (*) (gpointer)) g_closure_ref,
     224                 :            :   (void (*) (gpointer)) g_closure_unref,
     225                 :            :   closure_callback_get
     226                 :            : };
     227                 :            : 
     228                 :            : static void
     229                 :      18643 : closure_invalidated (gpointer  user_data,
     230                 :            :                      GClosure *closure)
     231                 :            : {
     232                 :      18643 :   g_source_destroy (user_data);
     233                 :      18643 : }
     234                 :            : 
     235                 :            : /**
     236                 :            :  * g_source_set_closure:
     237                 :            :  * @source: the source
     238                 :            :  * @closure: a #GClosure
     239                 :            :  *
     240                 :            :  * Set the callback for a source as a #GClosure.
     241                 :            :  *
     242                 :            :  * If the source is not one of the standard GLib types, the @closure_callback
     243                 :            :  * and @closure_marshal fields of the #GSourceFuncs structure must have been
     244                 :            :  * filled in with pointers to appropriate functions.
     245                 :            :  */
     246                 :            : void
     247                 :      18706 : g_source_set_closure (GSource  *source,
     248                 :            :                       GClosure *closure)
     249                 :            : {
     250                 :      18706 :   g_return_if_fail (source != NULL);
     251                 :      18706 :   g_return_if_fail (closure != NULL);
     252                 :            : 
     253         [ +  + ]:      18706 :   if (!source->source_funcs->closure_callback &&
     254                 :            : #ifdef G_OS_UNIX
     255         [ +  + ]:       4898 :       source->source_funcs != &g_unix_fd_source_funcs &&
     256         [ +  + ]:          5 :       source->source_funcs != &g_unix_signal_funcs &&
     257                 :            : #endif
     258         [ +  + ]:          4 :       source->source_funcs != &g_child_watch_funcs &&
     259         [ +  + ]:          3 :       source->source_funcs != &g_io_watch_funcs &&
     260         [ +  + ]:          2 :       source->source_funcs != &g_timeout_funcs &&
     261         [ -  + ]:          1 :       source->source_funcs != &g_idle_funcs)
     262                 :            :     {
     263                 :          0 :       g_critical (G_STRLOC ": closure cannot be set on GSource without GSourceFuncs::closure_callback");
     264                 :          0 :       return;
     265                 :            :     }
     266                 :            : 
     267                 :      18706 :   g_closure_ref (closure);
     268                 :      18706 :   g_closure_sink (closure);
     269                 :      18706 :   g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
     270                 :            : 
     271                 :      18706 :   g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
     272                 :            : 
     273         [ +  - ]:      18706 :   if (G_CLOSURE_NEEDS_MARSHAL (closure))
     274                 :            :     {
     275                 :      18706 :       GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
     276         [ -  + ]:      18706 :       if (marshal)
     277                 :          0 :         g_closure_set_marshal (closure, marshal);
     278         [ +  + ]:      18706 :       else if (source->source_funcs == &g_idle_funcs ||
     279                 :            : #ifdef G_OS_UNIX
     280         [ +  + ]:      18705 :                source->source_funcs == &g_unix_signal_funcs ||
     281                 :            : #endif
     282         [ +  + ]:      18704 :                source->source_funcs == &g_timeout_funcs)
     283                 :          3 :         g_closure_set_marshal (closure, source_closure_marshal_BOOLEAN__VOID);
     284                 :            :       else
     285                 :      18703 :         g_closure_set_marshal (closure, g_cclosure_marshal_generic);
     286                 :            :     }
     287                 :            : }
     288                 :            : 
     289                 :            : static void
     290                 :       6466 : dummy_closure_marshal (GClosure     *closure,
     291                 :            :                        GValue       *return_value,
     292                 :            :                        guint         n_param_values,
     293                 :            :                        const GValue *param_values,
     294                 :            :                        gpointer      invocation_hint,
     295                 :            :                        gpointer      marshal_data)
     296                 :            : {
     297   [ -  +  +  -  :       6466 :   if (G_VALUE_HOLDS_BOOLEAN (return_value))
                   +  - ]
     298                 :       6466 :     g_value_set_boolean (return_value, TRUE);
     299                 :       6466 : }
     300                 :            : 
     301                 :            : /**
     302                 :            :  * g_source_set_dummy_callback:
     303                 :            :  * @source: the source
     304                 :            :  *
     305                 :            :  * Sets a dummy callback for @source. The callback will do nothing, and
     306                 :            :  * if the source expects a #gboolean return value, it will return %TRUE.
     307                 :            :  * (If the source expects any other type of return value, it will return
     308                 :            :  * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
     309                 :            :  * that type.)
     310                 :            :  *
     311                 :            :  * If the source is not one of the standard GLib types, the
     312                 :            :  * @closure_callback and @closure_marshal fields of the #GSourceFuncs
     313                 :            :  * structure must have been filled in with pointers to appropriate
     314                 :            :  * functions.
     315                 :            :  */
     316                 :            : void
     317                 :      18700 : g_source_set_dummy_callback (GSource *source)
     318                 :            : {
     319                 :            :   GClosure *closure;
     320                 :            : 
     321                 :      18700 :   closure = g_closure_new_simple (sizeof (GClosure), NULL);
     322                 :      18700 :   g_closure_set_meta_marshal (closure, NULL, dummy_closure_marshal);
     323                 :      18700 :   g_source_set_closure (source, closure);
     324                 :      18700 : }

Generated by: LCOV version 1.14