LCOV - code coverage report
Current view: top level - gio - gioscheduler.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 0.0 % 106 0
Test Date: 2024-11-26 05:23:01 Functions: 0.0 % 8 0
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GIO - GLib Input, Output and Streaming Library
       2                 :             :  * 
       3                 :             :  * Copyright (C) 2006-2007 Red Hat, Inc.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :             :  *
       7                 :             :  * This library is free software; you can redistribute it and/or
       8                 :             :  * modify it under the terms of the GNU Lesser General Public
       9                 :             :  * License as published by the Free Software Foundation; either
      10                 :             :  * version 2.1 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                 :             :  * Lesser General Public License for more details.
      16                 :             :  *
      17                 :             :  * You should have received a copy of the GNU Lesser General
      18                 :             :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :             :  *
      20                 :             :  * Author: Alexander Larsson <alexl@redhat.com>
      21                 :             :  */
      22                 :             : 
      23                 :             : #include "config.h"
      24                 :             : 
      25                 :             : #include "gioscheduler.h"
      26                 :             : #include "gcancellable.h"
      27                 :             : #include "gtask.h"
      28                 :             : 
      29                 :             : struct _GIOSchedulerJob {
      30                 :             :   GList *active_link;
      31                 :             :   GTask *task;
      32                 :             : 
      33                 :             :   GIOSchedulerJobFunc job_func;
      34                 :             :   gpointer data;
      35                 :             :   GDestroyNotify destroy_notify;
      36                 :             : 
      37                 :             :   GCancellable *cancellable;
      38                 :             :   gulong cancellable_id;
      39                 :             :   GMainContext *context;
      40                 :             : };
      41                 :             : 
      42                 :             : G_LOCK_DEFINE_STATIC(active_jobs);
      43                 :             : static GList *active_jobs = NULL;
      44                 :             : 
      45                 :             : static void
      46                 :           0 : g_io_job_free (GIOSchedulerJob *job)
      47                 :             : {
      48                 :           0 :   if (job->destroy_notify)
      49                 :           0 :     job->destroy_notify (job->data);
      50                 :             : 
      51                 :           0 :   G_LOCK (active_jobs);
      52                 :           0 :   active_jobs = g_list_delete_link (active_jobs, job->active_link);
      53                 :           0 :   G_UNLOCK (active_jobs);
      54                 :             : 
      55                 :           0 :   if (job->cancellable)
      56                 :           0 :     g_object_unref (job->cancellable);
      57                 :           0 :   g_main_context_unref (job->context);
      58                 :           0 :   g_slice_free (GIOSchedulerJob, job);
      59                 :           0 : }
      60                 :             : 
      61                 :             : static void
      62                 :           0 : io_job_thread (GTask         *task,
      63                 :             :                gpointer       source_object,
      64                 :             :                gpointer       task_data,
      65                 :             :                GCancellable  *cancellable)
      66                 :             : {
      67                 :           0 :   GIOSchedulerJob *job = task_data;
      68                 :             :   gboolean result;
      69                 :             : 
      70                 :           0 :   if (job->cancellable)
      71                 :           0 :     g_cancellable_push_current (job->cancellable);
      72                 :             : 
      73                 :             :   do 
      74                 :             :     {
      75                 :           0 :       result = job->job_func (job, job->cancellable, job->data);
      76                 :             :     }
      77                 :           0 :   while (result);
      78                 :             : 
      79                 :           0 :   if (job->cancellable)
      80                 :           0 :     g_cancellable_pop_current (job->cancellable);
      81                 :           0 : }
      82                 :             : 
      83                 :             : /**
      84                 :             :  * g_io_scheduler_push_job:
      85                 :             :  * @job_func: a #GIOSchedulerJobFunc.
      86                 :             :  * @user_data: data to pass to @job_func
      87                 :             :  * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
      88                 :             :  * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority)
      89                 :             :  * of the request.
      90                 :             :  * @cancellable: optional #GCancellable object, %NULL to ignore.
      91                 :             :  *
      92                 :             :  * Schedules the I/O job to run in another thread.
      93                 :             :  *
      94                 :             :  * @notify will be called on @user_data after @job_func has returned,
      95                 :             :  * regardless whether the job was cancelled or has run to completion.
      96                 :             :  * 
      97                 :             :  * If @cancellable is not %NULL, it can be used to cancel the I/O job
      98                 :             :  * by calling g_cancellable_cancel() or by calling 
      99                 :             :  * g_io_scheduler_cancel_all_jobs().
     100                 :             :  *
     101                 :             :  * Deprecated: 2.36: use #GThreadPool or g_task_run_in_thread()
     102                 :             :  **/
     103                 :             : void
     104                 :           0 : g_io_scheduler_push_job (GIOSchedulerJobFunc  job_func,
     105                 :             :                          gpointer             user_data,
     106                 :             :                          GDestroyNotify       notify,
     107                 :             :                          gint                 io_priority,
     108                 :             :                          GCancellable        *cancellable)
     109                 :             : {
     110                 :             :   GIOSchedulerJob *job;
     111                 :             :   GTask *task;
     112                 :             : 
     113                 :           0 :   g_return_if_fail (job_func != NULL);
     114                 :             : 
     115                 :           0 :   job = g_slice_new0 (GIOSchedulerJob);
     116                 :           0 :   job->job_func = job_func;
     117                 :           0 :   job->data = user_data;
     118                 :           0 :   job->destroy_notify = notify;
     119                 :             : 
     120                 :           0 :   if (cancellable)
     121                 :           0 :     job->cancellable = g_object_ref (cancellable);
     122                 :             : 
     123                 :           0 :   job->context = g_main_context_ref_thread_default ();
     124                 :             : 
     125                 :           0 :   G_LOCK (active_jobs);
     126                 :           0 :   active_jobs = g_list_prepend (active_jobs, job);
     127                 :           0 :   job->active_link = active_jobs;
     128                 :           0 :   G_UNLOCK (active_jobs);
     129                 :             : 
     130                 :           0 :   task = g_task_new (NULL, cancellable, NULL, NULL);
     131                 :             : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
     132                 :           0 :   g_task_set_source_tag (task, g_io_scheduler_push_job);
     133                 :             : G_GNUC_END_IGNORE_DEPRECATIONS
     134                 :           0 :   g_task_set_task_data (task, job, (GDestroyNotify)g_io_job_free);
     135                 :           0 :   g_task_set_priority (task, io_priority);
     136                 :           0 :   g_task_run_in_thread (task, io_job_thread);
     137                 :           0 :   g_object_unref (task);
     138                 :             : }
     139                 :             : 
     140                 :             : /**
     141                 :             :  * g_io_scheduler_cancel_all_jobs:
     142                 :             :  * 
     143                 :             :  * Cancels all cancellable I/O jobs. 
     144                 :             :  *
     145                 :             :  * A job is cancellable if a #GCancellable was passed into
     146                 :             :  * g_io_scheduler_push_job().
     147                 :             :  *
     148                 :             :  * Deprecated: 2.36: You should never call this function, since you don't
     149                 :             :  * know how other libraries in your program might be making use of
     150                 :             :  * gioscheduler.
     151                 :             :  **/
     152                 :             : void
     153                 :           0 : g_io_scheduler_cancel_all_jobs (void)
     154                 :             : {
     155                 :             :   GList *cancellable_list, *l;
     156                 :             :   
     157                 :           0 :   G_LOCK (active_jobs);
     158                 :           0 :   cancellable_list = NULL;
     159                 :           0 :   for (l = active_jobs; l != NULL; l = l->next)
     160                 :             :     {
     161                 :           0 :       GIOSchedulerJob *job = l->data;
     162                 :           0 :       if (job->cancellable)
     163                 :           0 :         cancellable_list = g_list_prepend (cancellable_list,
     164                 :           0 :                                            g_object_ref (job->cancellable));
     165                 :             :     }
     166                 :           0 :   G_UNLOCK (active_jobs);
     167                 :             : 
     168                 :           0 :   for (l = cancellable_list; l != NULL; l = l->next)
     169                 :             :     {
     170                 :           0 :       GCancellable *c = l->data;
     171                 :           0 :       g_cancellable_cancel (c);
     172                 :           0 :       g_object_unref (c);
     173                 :             :     }
     174                 :           0 :   g_list_free (cancellable_list);
     175                 :           0 : }
     176                 :             : 
     177                 :             : typedef struct {
     178                 :             :   GSourceFunc func;
     179                 :             :   gboolean ret_val;
     180                 :             :   gpointer data;
     181                 :             :   GDestroyNotify notify;
     182                 :             : 
     183                 :             :   GMutex ack_lock;
     184                 :             :   GCond ack_condition;
     185                 :             :   gboolean ack;
     186                 :             : } MainLoopProxy;
     187                 :             : 
     188                 :             : static gboolean
     189                 :           0 : mainloop_proxy_func (gpointer data)
     190                 :             : {
     191                 :           0 :   MainLoopProxy *proxy = data;
     192                 :             : 
     193                 :           0 :   proxy->ret_val = proxy->func (proxy->data);
     194                 :             : 
     195                 :           0 :   if (proxy->notify)
     196                 :           0 :     proxy->notify (proxy->data);
     197                 :             : 
     198                 :           0 :   g_mutex_lock (&proxy->ack_lock);
     199                 :           0 :   proxy->ack = TRUE;
     200                 :           0 :   g_cond_signal (&proxy->ack_condition);
     201                 :           0 :   g_mutex_unlock (&proxy->ack_lock);
     202                 :             : 
     203                 :           0 :   return FALSE;
     204                 :             : }
     205                 :             : 
     206                 :             : static void
     207                 :           0 : mainloop_proxy_free (MainLoopProxy *proxy)
     208                 :             : {
     209                 :           0 :   g_mutex_clear (&proxy->ack_lock);
     210                 :           0 :   g_cond_clear (&proxy->ack_condition);
     211                 :           0 :   g_free (proxy);
     212                 :           0 : }
     213                 :             : 
     214                 :             : /**
     215                 :             :  * g_io_scheduler_job_send_to_mainloop:
     216                 :             :  * @job: a #GIOSchedulerJob
     217                 :             :  * @func: a #GSourceFunc callback that will be called in the original thread
     218                 :             :  * @user_data: data to pass to @func
     219                 :             :  * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
     220                 :             :  * 
     221                 :             :  * Used from an I/O job to send a callback to be run in the thread
     222                 :             :  * that the job was started from, waiting for the result (and thus
     223                 :             :  * blocking the I/O job).
     224                 :             :  *
     225                 :             :  * Returns: The return value of @func
     226                 :             :  *
     227                 :             :  * Deprecated: 2.36: Use g_main_context_invoke().
     228                 :             :  **/
     229                 :             : gboolean
     230                 :           0 : g_io_scheduler_job_send_to_mainloop (GIOSchedulerJob *job,
     231                 :             :                                      GSourceFunc      func,
     232                 :             :                                      gpointer         user_data,
     233                 :             :                                      GDestroyNotify   notify)
     234                 :             : {
     235                 :             :   GSource *source;
     236                 :             :   MainLoopProxy *proxy;
     237                 :             :   gboolean ret_val;
     238                 :             : 
     239                 :           0 :   g_return_val_if_fail (job != NULL, FALSE);
     240                 :           0 :   g_return_val_if_fail (func != NULL, FALSE);
     241                 :             : 
     242                 :           0 :   proxy = g_new0 (MainLoopProxy, 1);
     243                 :           0 :   proxy->func = func;
     244                 :           0 :   proxy->data = user_data;
     245                 :           0 :   proxy->notify = notify;
     246                 :           0 :   g_mutex_init (&proxy->ack_lock);
     247                 :           0 :   g_cond_init (&proxy->ack_condition);
     248                 :           0 :   g_mutex_lock (&proxy->ack_lock);
     249                 :             : 
     250                 :           0 :   source = g_idle_source_new ();
     251                 :           0 :   g_source_set_priority (source, G_PRIORITY_DEFAULT);
     252                 :           0 :   g_source_set_callback (source, mainloop_proxy_func, proxy,
     253                 :             :                          NULL);
     254                 :           0 :   g_source_set_static_name (source, "[gio] mainloop_proxy_func");
     255                 :             : 
     256                 :           0 :   g_source_attach (source, job->context);
     257                 :           0 :   g_source_unref (source);
     258                 :             : 
     259                 :           0 :   while (!proxy->ack)
     260                 :           0 :     g_cond_wait (&proxy->ack_condition, &proxy->ack_lock);
     261                 :           0 :   g_mutex_unlock (&proxy->ack_lock);
     262                 :             : 
     263                 :           0 :   ret_val = proxy->ret_val;
     264                 :           0 :   mainloop_proxy_free (proxy);
     265                 :             :   
     266                 :           0 :   return ret_val;
     267                 :             : }
     268                 :             : 
     269                 :             : /**
     270                 :             :  * g_io_scheduler_job_send_to_mainloop_async:
     271                 :             :  * @job: a #GIOSchedulerJob
     272                 :             :  * @func: a #GSourceFunc callback that will be called in the original thread
     273                 :             :  * @user_data: data to pass to @func
     274                 :             :  * @notify: (nullable): a #GDestroyNotify for @user_data, or %NULL
     275                 :             :  * 
     276                 :             :  * Used from an I/O job to send a callback to be run asynchronously in
     277                 :             :  * the thread that the job was started from. The callback will be run
     278                 :             :  * when the main loop is available, but at that time the I/O job might
     279                 :             :  * have finished. The return value from the callback is ignored.
     280                 :             :  *
     281                 :             :  * Note that if you are passing the @user_data from g_io_scheduler_push_job()
     282                 :             :  * on to this function you have to ensure that it is not freed before
     283                 :             :  * @func is called, either by passing %NULL as @notify to 
     284                 :             :  * g_io_scheduler_push_job() or by using refcounting for @user_data.
     285                 :             :  *
     286                 :             :  * Deprecated: 2.36: Use g_main_context_invoke().
     287                 :             :  **/
     288                 :             : void
     289                 :           0 : g_io_scheduler_job_send_to_mainloop_async (GIOSchedulerJob *job,
     290                 :             :                                            GSourceFunc      func,
     291                 :             :                                            gpointer         user_data,
     292                 :             :                                            GDestroyNotify   notify)
     293                 :             : {
     294                 :             :   GSource *source;
     295                 :             :   MainLoopProxy *proxy;
     296                 :             : 
     297                 :           0 :   g_return_if_fail (job != NULL);
     298                 :           0 :   g_return_if_fail (func != NULL);
     299                 :             : 
     300                 :           0 :   proxy = g_new0 (MainLoopProxy, 1);
     301                 :           0 :   proxy->func = func;
     302                 :           0 :   proxy->data = user_data;
     303                 :           0 :   proxy->notify = notify;
     304                 :           0 :   g_mutex_init (&proxy->ack_lock);
     305                 :           0 :   g_cond_init (&proxy->ack_condition);
     306                 :             : 
     307                 :           0 :   source = g_idle_source_new ();
     308                 :           0 :   g_source_set_priority (source, G_PRIORITY_DEFAULT);
     309                 :           0 :   g_source_set_callback (source, mainloop_proxy_func, proxy,
     310                 :             :                          (GDestroyNotify)mainloop_proxy_free);
     311                 :           0 :   g_source_set_static_name (source, "[gio] mainloop_proxy_func");
     312                 :             : 
     313                 :           0 :   g_source_attach (source, job->context);
     314                 :           0 :   g_source_unref (source);
     315                 :             : }
        

Generated by: LCOV version 2.0-1