LCOV - code coverage report
Current view: top level - glib - gmain.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 89.1 % 2062 1838
Test Date: 2026-11-10 05:12:13 Functions: 96.4 % 168 162
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* GLIB - Library of useful routines for C programming
       2                 :             :  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       3                 :             :  *
       4                 :             :  * gmain.c: Main loop abstraction, timeouts, and idle functions
       5                 :             :  * Copyright 1998 Owen Taylor
       6                 :             :  *
       7                 :             :  * SPDX-License-Identifier: LGPL-2.1-or-later
       8                 :             :  *
       9                 :             :  * This library is free software; you can redistribute it and/or
      10                 :             :  * modify it under the terms of the GNU Lesser General Public
      11                 :             :  * License as published by the Free Software Foundation; either
      12                 :             :  * version 2.1 of the License, or (at your option) any later version.
      13                 :             :  *
      14                 :             :  * This library is distributed in the hope that it will be useful,
      15                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :             :  * Lesser General Public License for more details.
      18                 :             :  *
      19                 :             :  * You should have received a copy of the GNU Lesser General Public
      20                 :             :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      21                 :             :  */
      22                 :             : 
      23                 :             : /*
      24                 :             :  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
      25                 :             :  * file for a list of people on the GLib Team.  See the ChangeLog
      26                 :             :  * files for a list of changes.  These files are distributed with
      27                 :             :  * GLib at ftp://ftp.gtk.org/pub/gtk/.
      28                 :             :  */
      29                 :             : 
      30                 :             : /*
      31                 :             :  * MT safe
      32                 :             :  */
      33                 :             : 
      34                 :             : #include "config.h"
      35                 :             : 
      36                 :             : /* We need to include this as early as possible, because on some
      37                 :             :  * platforms like AIX, <poll.h> redefines the names we use for
      38                 :             :  * GPollFD struct members.
      39                 :             :  * See https://gitlab.gnome.org/GNOME/glib/-/issues/3500 */
      40                 :             : 
      41                 :             : #ifdef HAVE_POLL_H
      42                 :             : #include <poll.h>
      43                 :             : #endif
      44                 :             : 
      45                 :             : #include "glib.h"
      46                 :             : #include "glibconfig.h"
      47                 :             : #include "glib_trace.h"
      48                 :             : 
      49                 :             : /* Uncomment the next line (and the corresponding line in gpoll.c) to
      50                 :             :  * enable debugging printouts if the environment variable
      51                 :             :  * G_MAIN_POLL_DEBUG is set to some value.
      52                 :             :  */
      53                 :             : /* #define G_MAIN_POLL_DEBUG */
      54                 :             : 
      55                 :             : #ifdef _WIN32
      56                 :             : /* Always enable debugging printout on Windows, as it is more often
      57                 :             :  * needed there...
      58                 :             :  */
      59                 :             : #define G_MAIN_POLL_DEBUG
      60                 :             : #endif
      61                 :             : 
      62                 :             : 
      63                 :             : #ifdef G_OS_UNIX
      64                 :             : #include "glib-unix.h"
      65                 :             : #include <pthread.h>
      66                 :             : #ifdef HAVE_EVENTFD
      67                 :             : #include <sys/eventfd.h>
      68                 :             : #endif
      69                 :             : #endif
      70                 :             : 
      71                 :             : #include <signal.h>
      72                 :             : #include <sys/types.h>
      73                 :             : #include <time.h>
      74                 :             : #include <stdlib.h>
      75                 :             : #ifdef HAVE_SYS_TIME_H
      76                 :             : #include <sys/time.h>
      77                 :             : #endif /* HAVE_SYS_TIME_H */
      78                 :             : #ifdef G_OS_UNIX
      79                 :             : #include <unistd.h>
      80                 :             : #endif /* G_OS_UNIX */
      81                 :             : #include <errno.h>
      82                 :             : #include <string.h>
      83                 :             : #include <inttypes.h>
      84                 :             : 
      85                 :             : #ifdef HAVE_PIDFD
      86                 :             : #include <sys/syscall.h>
      87                 :             : #include <sys/wait.h>
      88                 :             : #include <linux/wait.h>  /* P_PIDFD */
      89                 :             : #ifndef W_EXITCODE
      90                 :             : #define W_EXITCODE(ret, sig) ((ret) << 8 | (sig))
      91                 :             : #endif
      92                 :             : #ifndef W_STOPCODE
      93                 :             : #define W_STOPCODE(sig)      ((sig) << 8 | 0x7f)
      94                 :             : #endif
      95                 :             : #ifndef WCOREFLAG
      96                 :             : /* musl doesn’t define WCOREFLAG while glibc does. Unfortunately, there’s no way
      97                 :             :  * to detect we’re building against musl, so just define it and hope.
      98                 :             :  * See https://git.musl-libc.org/cgit/musl/tree/include/sys/wait.h#n51 */
      99                 :             : #define WCOREFLAG 0x80
     100                 :             : #endif
     101                 :             : #ifndef __W_CONTINUED
     102                 :             : /* Same as above, for musl */
     103                 :             : #define __W_CONTINUED 0xffff
     104                 :             : #endif
     105                 :             : #endif  /* HAVE_PIDFD */
     106                 :             : 
     107                 :             : #ifdef G_OS_WIN32
     108                 :             : #include <windows.h>
     109                 :             : #endif
     110                 :             : 
     111                 :             : #ifdef HAVE_MACH_MACH_TIME_H
     112                 :             : #include <mach/mach_time.h>
     113                 :             : #endif
     114                 :             : 
     115                 :             : #include "glib_trace.h"
     116                 :             : 
     117                 :             : #include "gmain.h"
     118                 :             : 
     119                 :             : #include "garray.h"
     120                 :             : #include "giochannel.h"
     121                 :             : #include "ghash.h"
     122                 :             : #include "ghook.h"
     123                 :             : #include "gqueue.h"
     124                 :             : #include "gstrfuncs.h"
     125                 :             : #include "gtestutils.h"
     126                 :             : #include "gthreadprivate.h"
     127                 :             : #include "gtrace-private.h"
     128                 :             : 
     129                 :             : #ifdef G_OS_WIN32
     130                 :             : #include "gwin32.h"
     131                 :             : #endif
     132                 :             : 
     133                 :             : #ifdef  G_MAIN_POLL_DEBUG
     134                 :             : #include "gtimer.h"
     135                 :             : #endif
     136                 :             : 
     137                 :             : #include "gwakeup.h"
     138                 :             : #include "gmain-internal.h"
     139                 :             : #include "glib-init.h"
     140                 :             : #include "glib-private.h"
     141                 :             : 
     142                 :             : /* Types */
     143                 :             : 
     144                 :             : typedef struct _GIdleSource GIdleSource;
     145                 :             : typedef struct _GTimeoutSource GTimeoutSource;
     146                 :             : typedef struct _GChildWatchSource GChildWatchSource;
     147                 :             : typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource;
     148                 :             : typedef struct _GPollRec GPollRec;
     149                 :             : typedef struct _GSourceCallback GSourceCallback;
     150                 :             : 
     151                 :             : typedef enum
     152                 :             : {
     153                 :             :   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
     154                 :             :   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1),
     155                 :             :   G_SOURCE_BLOCKED = 1 << (G_HOOK_FLAG_USER_SHIFT + 2)
     156                 :             : } G_GNUC_FLAG_ENUM GSourceFlags;
     157                 :             : 
     158                 :             : typedef struct _GSourceList GSourceList;
     159                 :             : 
     160                 :             : struct _GSourceList
     161                 :             : {
     162                 :             :   GList link;
     163                 :             :   GSource *head, *tail;
     164                 :             :   gint priority;
     165                 :             : };
     166                 :             : 
     167                 :             : typedef struct _GMainWaiter GMainWaiter;
     168                 :             : 
     169                 :             : struct _GMainWaiter
     170                 :             : {
     171                 :             :   GCond *cond;
     172                 :             :   GMutex *mutex;
     173                 :             : };
     174                 :             : 
     175                 :             : typedef struct _GMainDispatch GMainDispatch;
     176                 :             : 
     177                 :             : struct _GMainDispatch
     178                 :             : {
     179                 :             :   gint depth;
     180                 :             :   GSource *source;
     181                 :             : };
     182                 :             : 
     183                 :             : #ifdef G_MAIN_POLL_DEBUG
     184                 :             : gboolean _g_main_poll_debug = FALSE;
     185                 :             : #endif
     186                 :             : 
     187                 :             : struct _GMainContext
     188                 :             : {
     189                 :             :   /* The following lock is used for both the list of sources
     190                 :             :    * and the list of poll records
     191                 :             :    */
     192                 :             :   GMutex mutex;
     193                 :             :   GCond cond;
     194                 :             :   GThread *owner;
     195                 :             :   guint owner_count;
     196                 :             :   GMainContextFlags flags;
     197                 :             :   GSList *waiters;
     198                 :             : 
     199                 :             :   gint ref_count;  /* (atomic) */
     200                 :             : 
     201                 :             :   GHashTable *sources;              /* guint -> GSource */
     202                 :             : 
     203                 :             :   GPtrArray *pending_dispatches;
     204                 :             :   uint64_t timeout_ns; /* Timeout for current iteration */
     205                 :             :   gboolean has_timeout;
     206                 :             : 
     207                 :             :   guint next_id;
     208                 :             :   GQueue source_lists;
     209                 :             :   gint in_check_or_prepare;
     210                 :             : 
     211                 :             :   GPollRec *poll_records;
     212                 :             :   guint n_poll_records;
     213                 :             :   GPollFD *cached_poll_array;
     214                 :             :   guint cached_poll_array_size;
     215                 :             : 
     216                 :             :   GWakeup *wakeup;
     217                 :             : 
     218                 :             :   GPollFD wake_up_rec;
     219                 :             : 
     220                 :             : /* Flag indicating whether the set of fd's changed during a poll */
     221                 :             :   gboolean poll_changed;
     222                 :             : 
     223                 :             :   GPollFunc poll_func;
     224                 :             : 
     225                 :             :   uint64_t time_ns;
     226                 :             :   gboolean time_is_fresh;
     227                 :             : };
     228                 :             : 
     229                 :             : struct _GSourceCallback
     230                 :             : {
     231                 :             :   gint ref_count;  /* (atomic) */
     232                 :             :   GSourceFunc func;
     233                 :             :   gpointer    data;
     234                 :             :   GDestroyNotify notify;
     235                 :             : };
     236                 :             : 
     237                 :             : struct _GMainLoop
     238                 :             : {
     239                 :             :   GMainContext *context;
     240                 :             :   gboolean is_running; /* (atomic) */
     241                 :             :   gint ref_count;  /* (atomic) */
     242                 :             : };
     243                 :             : 
     244                 :             : struct _GIdleSource
     245                 :             : {
     246                 :             :   GSource  source;
     247                 :             :   gboolean one_shot;
     248                 :             : };
     249                 :             : 
     250                 :             : struct _GTimeoutSource
     251                 :             : {
     252                 :             :   GSource     source;
     253                 :             :   /* Measured in seconds if 'seconds' is TRUE, or nanoseconds otherwise. */
     254                 :             :   uint64_t    interval;
     255                 :             :   gboolean    seconds;
     256                 :             :   gboolean    one_shot;
     257                 :             : };
     258                 :             : 
     259                 :             : struct _GChildWatchSource
     260                 :             : {
     261                 :             :   GSource     source;
     262                 :             :   GPid        pid;
     263                 :             :   /* @poll is always used on Windows.
     264                 :             :    * On Unix, poll.fd will be negative if PIDFD is unavailable. */
     265                 :             :   GPollFD     poll;
     266                 :             : #ifndef G_OS_WIN32
     267                 :             :   gboolean child_maybe_exited; /* (atomic) */
     268                 :             : #endif /* G_OS_WIN32 */
     269                 :             : };
     270                 :             : 
     271                 :             : struct _GUnixSignalWatchSource
     272                 :             : {
     273                 :             :   GSource     source;
     274                 :             :   int         signum;
     275                 :             :   gboolean    pending; /* (atomic) */
     276                 :             : };
     277                 :             : 
     278                 :             : struct _GPollRec
     279                 :             : {
     280                 :             :   GPollFD *fd;
     281                 :             :   GPollRec *prev;
     282                 :             :   GPollRec *next;
     283                 :             :   gint priority;
     284                 :             : };
     285                 :             : 
     286                 :             : struct _GSourcePrivate
     287                 :             : {
     288                 :             :   GSList *child_sources;
     289                 :             :   GSource *parent_source;
     290                 :             : 
     291                 :             :   uint64_t ready_time_ns;
     292                 :             : 
     293                 :             :   /* This is currently only used on UNIX, but we always declare it (and
     294                 :             :    * let it remain empty on Windows) to avoid #ifdef all over the place.
     295                 :             :    */
     296                 :             :   GSList *fds;
     297                 :             : 
     298                 :             :   GSourceDisposeFunc dispose;
     299                 :             : 
     300                 :             :   gboolean static_name;
     301                 :             :   gboolean has_ready_time;
     302                 :             : };
     303                 :             : 
     304                 :             : typedef struct _GSourceIter
     305                 :             : {
     306                 :             :   GMainContext *context;
     307                 :             :   gboolean may_modify;
     308                 :             :   GList *current_list;
     309                 :             :   GSource *source;
     310                 :             : } GSourceIter;
     311                 :             : 
     312                 :             : #define LOCK_CONTEXT(context) g_mutex_lock (&context->mutex)
     313                 :             : #define UNLOCK_CONTEXT(context) g_mutex_unlock (&context->mutex)
     314                 :             : #define G_THREAD_SELF g_thread_self ()
     315                 :             : 
     316                 :             : #define SOURCE_DESTROYED(source) \
     317                 :             :   ((g_atomic_int_get (&((source)->flags)) & G_HOOK_FLAG_ACTIVE) == 0)
     318                 :             : #define SOURCE_BLOCKED(source) \
     319                 :             :   ((g_atomic_int_get (&((source)->flags)) & G_SOURCE_BLOCKED) != 0)
     320                 :             : 
     321                 :             : /* Forward declarations */
     322                 :             : 
     323                 :             : static void g_source_unref_internal             (GSource      *source,
     324                 :             :                                                  GMainContext *context,
     325                 :             :                                                  gboolean      have_lock);
     326                 :             : static void g_source_destroy_internal           (GSource      *source,
     327                 :             :                                                  GMainContext *context,
     328                 :             :                                                  gboolean      have_lock);
     329                 :             : static void g_source_set_priority_unlocked      (GSource      *source,
     330                 :             :                                                  GMainContext *context,
     331                 :             :                                                  gint          priority);
     332                 :             : static void g_child_source_remove_internal      (GSource      *child_source,
     333                 :             :                                                  GMainContext *context);
     334                 :             : 
     335                 :             : static gboolean g_main_context_acquire_unlocked (GMainContext *context);
     336                 :             : static void g_main_context_release_unlocked     (GMainContext *context);
     337                 :             : static gboolean g_main_context_prepare_unlocked (GMainContext *context,
     338                 :             :                                                  gint         *priority);
     339                 :             : static gint g_main_context_query_unlocked       (GMainContext *context,
     340                 :             :                                                  gint          max_priority,
     341                 :             :                                                  gboolean     *has_timeout,
     342                 :             :                                                  uint64_t     *timeout_ns,
     343                 :             :                                                  GPollFD      *fds,
     344                 :             :                                                  gint          n_fds);
     345                 :             : static gboolean g_main_context_check_unlocked   (GMainContext *context,
     346                 :             :                                                  gint          max_priority,
     347                 :             :                                                  GPollFD      *fds,
     348                 :             :                                                  gint          n_fds);
     349                 :             : static void g_main_context_dispatch_unlocked    (GMainContext *context);
     350                 :             : static void g_main_context_poll_unlocked        (GMainContext *context,
     351                 :             :                                                  gboolean      has_timeout,
     352                 :             :                                                  uint64_t      timeout_ns,
     353                 :             :                                                  int           priority,
     354                 :             :                                                  GPollFD      *fds,
     355                 :             :                                                  int           n_fds);
     356                 :             : static void g_main_context_add_poll_unlocked    (GMainContext *context,
     357                 :             :                                                  gint          priority,
     358                 :             :                                                  GPollFD      *fd);
     359                 :             : static void g_main_context_remove_poll_unlocked (GMainContext *context,
     360                 :             :                                                  GPollFD      *fd);
     361                 :             : 
     362                 :             : static void     g_source_iter_init  (GSourceIter   *iter,
     363                 :             :                                      GMainContext  *context,
     364                 :             :                                      gboolean       may_modify);
     365                 :             : static gboolean g_source_iter_next  (GSourceIter   *iter,
     366                 :             :                                      GSource      **source);
     367                 :             : static void     g_source_iter_clear (GSourceIter   *iter);
     368                 :             : 
     369                 :             : static gboolean g_timeout_dispatch (GSource     *source,
     370                 :             :                                     GSourceFunc  callback,
     371                 :             :                                     gpointer     user_data);
     372                 :             : static gboolean g_child_watch_prepare  (GSource     *source,
     373                 :             :                                         gint        *timeout);
     374                 :             : static gboolean g_child_watch_check    (GSource     *source);
     375                 :             : static gboolean g_child_watch_dispatch (GSource     *source,
     376                 :             :                                         GSourceFunc  callback,
     377                 :             :                                         gpointer     user_data);
     378                 :             : static void     g_child_watch_finalize (GSource     *source);
     379                 :             : 
     380                 :             : #ifndef G_OS_WIN32
     381                 :             : static void unref_unix_signal_handler_unlocked (int signum);
     382                 :             : #endif
     383                 :             : 
     384                 :             : #ifdef G_OS_UNIX
     385                 :             : static void g_unix_signal_handler (int signum);
     386                 :             : static gboolean g_unix_signal_watch_prepare  (GSource     *source,
     387                 :             :                                               gint        *timeout);
     388                 :             : static gboolean g_unix_signal_watch_check    (GSource     *source);
     389                 :             : static gboolean g_unix_signal_watch_dispatch (GSource     *source,
     390                 :             :                                               GSourceFunc  callback,
     391                 :             :                                               gpointer     user_data);
     392                 :             : static void     g_unix_signal_watch_finalize  (GSource     *source);
     393                 :             : #endif
     394                 :             : static gboolean g_idle_prepare     (GSource     *source,
     395                 :             :                                     gint        *timeout);
     396                 :             : static gboolean g_idle_check       (GSource     *source);
     397                 :             : static gboolean g_idle_dispatch    (GSource     *source,
     398                 :             :                                     GSourceFunc  callback,
     399                 :             :                                     gpointer     user_data);
     400                 :             : 
     401                 :             : static void block_source (GSource      *source,
     402                 :             :                           GMainContext *context);
     403                 :             : static GMainContext *source_dup_main_context (GSource *source);
     404                 :             : 
     405                 :             : /* Lock for serializing access for safe execution of
     406                 :             :  * g_main_context_unref() with concurrent use of
     407                 :             :  * g_source_destroy() and g_source_unref().
     408                 :             :  *
     409                 :             :  * Locking order is source_destroy_lock, then context lock.
     410                 :             :  */
     411                 :             : static GRWLock source_destroy_lock;
     412                 :             : 
     413                 :             : static GMainContext *glib_worker_context;
     414                 :             : 
     415                 :             : #ifndef G_OS_WIN32
     416                 :             : 
     417                 :             : 
     418                 :             : /* UNIX signals work by marking one of these variables then waking the
     419                 :             :  * worker context to check on them and dispatch accordingly.
     420                 :             :  *
     421                 :             :  * Both variables must be accessed using atomic primitives, unless those atomic
     422                 :             :  * primitives are implemented using fallback mutexes (as those aren’t safe in
     423                 :             :  * an interrupt context).
     424                 :             :  *
     425                 :             :  * If using atomic primitives, the variables must be of type `int` (so they’re
     426                 :             :  * the right size for the atomic primitives). Otherwise, use `sig_atomic_t` if
     427                 :             :  * it’s available, which is guaranteed to be async-signal-safe (but it’s *not*
     428                 :             :  * guaranteed to be thread-safe, which is why we use atomic primitives if
     429                 :             :  * possible).
     430                 :             :  *
     431                 :             :  * Typically, `sig_atomic_t` is a typedef to `int`, but that’s not the case on
     432                 :             :  * FreeBSD, so we can’t use it unconditionally if it’s defined.
     433                 :             :  */
     434                 :             : #if (defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) || !defined(HAVE_SIG_ATOMIC_T)
     435                 :             : static volatile int unix_signal_pending[NSIG];
     436                 :             : static volatile int any_unix_signal_pending;
     437                 :             : #else
     438                 :             : static volatile sig_atomic_t unix_signal_pending[NSIG];
     439                 :             : static volatile sig_atomic_t any_unix_signal_pending;
     440                 :             : #endif
     441                 :             : 
     442                 :             : /* Guards all the data below */
     443                 :             : G_LOCK_DEFINE_STATIC (unix_signal_lock);
     444                 :             : static guint unix_signal_refcount[NSIG];
     445                 :             : static GSList *unix_signal_watches;
     446                 :             : static GSList *unix_child_watches;
     447                 :             : 
     448                 :             : GSourceFuncs g_unix_signal_funcs =
     449                 :             : {
     450                 :             :   g_unix_signal_watch_prepare,
     451                 :             :   g_unix_signal_watch_check,
     452                 :             :   g_unix_signal_watch_dispatch,
     453                 :             :   g_unix_signal_watch_finalize,
     454                 :             :   NULL, NULL
     455                 :             : };
     456                 :             : #endif /* !G_OS_WIN32 */
     457                 :             : 
     458                 :             : GSourceFuncs g_timeout_funcs =
     459                 :             : {
     460                 :             :   NULL, /* prepare */
     461                 :             :   NULL, /* check */
     462                 :             :   g_timeout_dispatch,
     463                 :             :   NULL, NULL, NULL
     464                 :             : };
     465                 :             : 
     466                 :             : GSourceFuncs g_child_watch_funcs =
     467                 :             : {
     468                 :             :   g_child_watch_prepare,
     469                 :             :   g_child_watch_check,
     470                 :             :   g_child_watch_dispatch,
     471                 :             :   g_child_watch_finalize,
     472                 :             :   NULL, NULL
     473                 :             : };
     474                 :             : 
     475                 :             : GSourceFuncs g_idle_funcs =
     476                 :             : {
     477                 :             :   g_idle_prepare,
     478                 :             :   g_idle_check,
     479                 :             :   g_idle_dispatch,
     480                 :             :   NULL, NULL, NULL
     481                 :             : };
     482                 :             : 
     483                 :             : /**
     484                 :             :  * g_main_context_ref:
     485                 :             :  * @context: (not nullable): a main context
     486                 :             :  * 
     487                 :             :  * Increases the reference count on a [struct@GLib.MainContext] object by one.
     488                 :             :  *
     489                 :             :  * Returns: the @context that was passed in (since 2.6)
     490                 :             :  **/
     491                 :             : GMainContext *
     492                 :      544717 : g_main_context_ref (GMainContext *context)
     493                 :             : {
     494                 :             :   int old_ref_count;
     495                 :             : 
     496                 :      544717 :   g_return_val_if_fail (context != NULL, NULL);
     497                 :             : 
     498                 :      544717 :   old_ref_count = g_atomic_int_add (&context->ref_count, 1);
     499                 :      544717 :   g_return_val_if_fail (old_ref_count > 0, NULL);
     500                 :             : 
     501                 :      544717 :   return context;
     502                 :       18606 : }
     503                 :             : 
     504                 :             : static inline void
     505                 :       29762 : poll_rec_list_free (GMainContext *context,
     506                 :             :                     GPollRec     *list)
     507                 :             : {
     508                 :       29762 :   g_slice_free_chain (GPollRec, list, next);
     509                 :       29762 : }
     510                 :             : 
     511                 :             : /**
     512                 :             :  * g_main_context_unref:
     513                 :             :  * @context: (not nullable): a main context
     514                 :             :  * 
     515                 :             :  * Decreases the reference count on a [struct@GLib.MainContext] object by one.
     516                 :             :  * If
     517                 :             :  * the result is zero, free the context and free all associated memory.
     518                 :             :  **/
     519                 :             : void
     520                 :     2341847 : g_main_context_unref (GMainContext *context)
     521                 :             : {
     522                 :             :   GSourceIter iter;
     523                 :             :   GSource *source;
     524                 :             :   GList *sl_iter;
     525                 :     2341847 :   GSList *s_iter, *remaining_sources = NULL;
     526                 :             :   GSourceList *list;
     527                 :             :   guint i;
     528                 :             :   guint old_ref;
     529                 :             :   GSource **pending_dispatches;
     530                 :             :   gsize pending_dispatches_len;
     531                 :             : 
     532                 :     4143019 :   g_return_if_fail (context != NULL);
     533                 :     2341847 :   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 
     534                 :             : 
     535                 :     1819054 : retry_decrement:
     536                 :     2342439 :   old_ref = g_atomic_int_get (&context->ref_count);
     537                 :     2342439 :   if (old_ref > 1)
     538                 :             :     {
     539                 :     2312677 :       if (!g_atomic_int_compare_and_exchange (&context->ref_count, old_ref, old_ref - 1))
     540                 :         592 :         goto retry_decrement;
     541                 :             : 
     542                 :     2312085 :       return;
     543                 :             :     }
     544                 :             : 
     545                 :       29762 :   g_rw_lock_writer_lock (&source_destroy_lock);
     546                 :             : 
     547                 :             :   /* if a weak ref got to the source_destroy lock first, we need to retry */
     548                 :       29762 :   old_ref = g_atomic_int_add (&context->ref_count, -1);
     549                 :       29762 :   if (old_ref != 1)
     550                 :             :     {
     551                 :           0 :       g_rw_lock_writer_unlock (&source_destroy_lock);
     552                 :           0 :       return;
     553                 :             :     }
     554                 :             : 
     555                 :       29762 :   LOCK_CONTEXT (context);
     556                 :       29762 :   pending_dispatches = (GSource **) g_ptr_array_steal (context->pending_dispatches, &pending_dispatches_len);
     557                 :       29762 :   UNLOCK_CONTEXT (context);
     558                 :             : 
     559                 :             :   /* Free pending dispatches */
     560                 :       29764 :   for (i = 0; i < pending_dispatches_len; i++)
     561                 :           2 :     g_source_unref_internal (pending_dispatches[i], context, FALSE);
     562                 :             : 
     563                 :       29762 :   g_clear_pointer (&pending_dispatches, g_free);
     564                 :             : 
     565                 :             :   /* g_source_iter_next() assumes the context is locked. */
     566                 :       29762 :   LOCK_CONTEXT (context);
     567                 :             : 
     568                 :             :   /* First collect all remaining sources from the sources lists and store a
     569                 :             :    * new reference in a separate list. Also set the context of the sources
     570                 :             :    * to NULL so that they can't access a partially destroyed context anymore.
     571                 :             :    *
     572                 :             :    * We have to do this first so that we have a strong reference to all
     573                 :             :    * sources and destroying them below does not also free them, and so that
     574                 :             :    * none of the sources can access the context from their finalize/dispose
     575                 :             :    * functions. */
     576                 :       29762 :   g_source_iter_init (&iter, context, FALSE);
     577                 :       30035 :   while (g_source_iter_next (&iter, &source))
     578                 :             :     {
     579                 :         273 :       source->context = NULL;
     580                 :         273 :       remaining_sources = g_slist_prepend (remaining_sources, g_source_ref (source));
     581                 :             :     }
     582                 :       29762 :   g_source_iter_clear (&iter);
     583                 :             : 
     584                 :       29762 :   g_rw_lock_writer_unlock (&source_destroy_lock);
     585                 :             : 
     586                 :             :   /* Next destroy all sources. As we still hold a reference to all of them,
     587                 :             :    * this won't cause any of them to be freed yet and especially prevents any
     588                 :             :    * source that unrefs another source from its finalize function to be freed.
     589                 :             :    */
     590                 :       30035 :   for (s_iter = remaining_sources; s_iter; s_iter = s_iter->next)
     591                 :             :     {
     592                 :         273 :       source = s_iter->data;
     593                 :         273 :       g_source_destroy_internal (source, context, TRUE);
     594                 :         136 :     }
     595                 :             : 
     596                 :             :   /* the context is going to die now */
     597                 :       29762 :   g_return_if_fail (old_ref > 0);
     598                 :             : 
     599                 :       29762 :   sl_iter = context->source_lists.head;
     600                 :       29795 :   while (sl_iter != NULL)
     601                 :             :     {
     602                 :          33 :       list = sl_iter->data;
     603                 :          33 :       sl_iter = sl_iter->next;
     604                 :          33 :       g_slice_free (GSourceList, list);
     605                 :             :     }
     606                 :             : 
     607                 :       29762 :   g_hash_table_remove_all (context->sources);
     608                 :             : 
     609                 :       29762 :   UNLOCK_CONTEXT (context);
     610                 :             : 
     611                 :             :   /* if the object has been reffed meanwhile by an internal weak ref, keep the
     612                 :             :    * resources alive until the last reference is gone.
     613                 :             :    */
     614                 :       29762 :   if (old_ref == 1)
     615                 :             :     {
     616                 :       29762 :       g_mutex_clear (&context->mutex);
     617                 :             : 
     618                 :       29762 :       g_ptr_array_free (context->pending_dispatches, TRUE);
     619                 :       29762 :       g_free (context->cached_poll_array);
     620                 :             : 
     621                 :       29762 :       poll_rec_list_free (context, context->poll_records);
     622                 :             : 
     623                 :       29762 :       g_wakeup_free (context->wakeup);
     624                 :       29762 :       g_cond_clear (&context->cond);
     625                 :             : 
     626                 :       29762 :       g_hash_table_unref (context->sources);
     627                 :             : 
     628                 :       29762 :       g_free (context);
     629                 :       12380 :     }
     630                 :             : 
     631                 :             :   /* And now finally get rid of our references to the sources. This will cause
     632                 :             :    * them to be freed unless something else still has a reference to them. Due
     633                 :             :    * to setting the context pointers in the sources to NULL above, this won't
     634                 :             :    * ever access the context or the internal linked list inside the GSource.
     635                 :             :    * We already removed the sources completely from the context above. */
     636                 :       30035 :   for (s_iter = remaining_sources; s_iter; s_iter = s_iter->next)
     637                 :             :     {
     638                 :         273 :       source = s_iter->data;
     639                 :         273 :       g_source_unref_internal (source, NULL, FALSE);
     640                 :         136 :     }
     641                 :       29762 :   g_slist_free (remaining_sources);
     642                 :      523293 : }
     643                 :             : 
     644                 :             : /* Helper function used by mainloop/overflow test.
     645                 :             :  */
     646                 :             : GMainContext *
     647                 :           2 : g_main_context_new_with_next_id (guint next_id)
     648                 :             : {
     649                 :           2 :   GMainContext *ret = g_main_context_new ();
     650                 :             :   
     651                 :           2 :   ret->next_id = next_id;
     652                 :             :   
     653                 :           2 :   return ret;
     654                 :             : }
     655                 :             : 
     656                 :             : /**
     657                 :             :  * g_main_context_new:
     658                 :             :  *
     659                 :             :  * Creates a new [struct@GLib.MainContext] structure.
     660                 :             :  *
     661                 :             :  * Returns: (transfer full): the new main context
     662                 :             :  **/
     663                 :             : GMainContext *
     664                 :       30492 : g_main_context_new (void)
     665                 :             : {
     666                 :       30492 :   return g_main_context_new_with_flags (G_MAIN_CONTEXT_FLAGS_NONE);
     667                 :             : }
     668                 :             : 
     669                 :             : /**
     670                 :             :  * g_main_context_new_with_flags:
     671                 :             :  * @flags: a bitwise-OR combination of flags that can only be set at creation
     672                 :             :  *   time
     673                 :             :  *
     674                 :             :  * Creates a new [struct@GLib.MainContext] structure.
     675                 :             :  *
     676                 :             :  * Returns: (transfer full): the new main context
     677                 :             :  * Since: 2.72
     678                 :             :  */
     679                 :             : GMainContext *
     680                 :       30496 : g_main_context_new_with_flags (GMainContextFlags flags)
     681                 :             : {
     682                 :             :   static gsize initialised;
     683                 :             :   GMainContext *context;
     684                 :             : 
     685                 :       30496 :   if (g_once_init_enter (&initialised))
     686                 :             :     {
     687                 :             : #ifdef G_MAIN_POLL_DEBUG
     688                 :         130 :       if (g_getenv ("G_MAIN_POLL_DEBUG") != NULL)
     689                 :           0 :         _g_main_poll_debug = TRUE;
     690                 :             : #endif
     691                 :             : 
     692                 :         446 :       g_once_init_leave (&initialised, TRUE);
     693                 :         130 :     }
     694                 :             : 
     695                 :       30496 :   context = g_new0 (GMainContext, 1);
     696                 :             : 
     697                 :       17963 :   TRACE (GLIB_MAIN_CONTEXT_NEW (context));
     698                 :             : 
     699                 :       30496 :   g_mutex_init (&context->mutex);
     700                 :       30496 :   g_cond_init (&context->cond);
     701                 :             : 
     702                 :       30496 :   context->sources = g_hash_table_new (g_uint_hash, g_uint_equal);
     703                 :       30496 :   context->owner = NULL;
     704                 :       30496 :   context->flags = flags;
     705                 :       30496 :   context->waiters = NULL;
     706                 :             : 
     707                 :       30496 :   context->ref_count = 1;
     708                 :             : 
     709                 :       30496 :   context->next_id = 1;
     710                 :             : 
     711                 :       30496 :   context->poll_func = g_poll;
     712                 :             :   
     713                 :       30496 :   context->cached_poll_array = NULL;
     714                 :       30496 :   context->cached_poll_array_size = 0;
     715                 :             :   
     716                 :       30496 :   context->pending_dispatches = g_ptr_array_new ();
     717                 :             :   
     718                 :       30496 :   context->time_is_fresh = FALSE;
     719                 :             :   
     720                 :       30496 :   context->wakeup = g_wakeup_new ();
     721                 :       30496 :   g_wakeup_get_pollfd (context->wakeup, &context->wake_up_rec);
     722                 :       30496 :   g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
     723                 :             : 
     724                 :             : #ifdef G_MAIN_POLL_DEBUG
     725                 :       12533 :   if (_g_main_poll_debug)
     726                 :           0 :     g_print ("created context=%p\n", context);
     727                 :             : #endif
     728                 :             : 
     729                 :       30496 :   return context;
     730                 :             : }
     731                 :             : 
     732                 :             : /**
     733                 :             :  * g_main_context_default:
     734                 :             :  *
     735                 :             :  * Returns the global-default main context.
     736                 :             :  *
     737                 :             :  * This is the main context
     738                 :             :  * used for main loop functions when a main loop is not explicitly
     739                 :             :  * specified, and corresponds to the ‘main’ main loop. See also
     740                 :             :  * [func@GLib.MainContext.get_thread_default].
     741                 :             :  *
     742                 :             :  * Returns: (transfer none): the global-default main context.
     743                 :             :  **/
     744                 :             : GMainContext *
     745                 :      884800 : g_main_context_default (void)
     746                 :             : {
     747                 :             :   static GMainContext *default_main_context = NULL;
     748                 :             : 
     749                 :      884800 :   if (g_once_init_enter_pointer (&default_main_context))
     750                 :             :     {
     751                 :             :       GMainContext *context;
     752                 :             : 
     753                 :         295 :       context = g_main_context_new ();
     754                 :             : 
     755                 :         211 :       TRACE (GLIB_MAIN_CONTEXT_DEFAULT (context));
     756                 :             : 
     757                 :             : #ifdef G_MAIN_POLL_DEBUG
     758                 :          84 :       if (_g_main_poll_debug)
     759                 :           0 :         g_print ("global-default main context=%p\n", context);
     760                 :             : #endif
     761                 :             : 
     762                 :         295 :       g_once_init_leave_pointer (&default_main_context, context);
     763                 :          84 :     }
     764                 :             : 
     765                 :      884800 :   return default_main_context;
     766                 :             : }
     767                 :             : 
     768                 :             : static void
     769                 :           3 : free_context (gpointer data)
     770                 :             : {
     771                 :           3 :   GMainContext *context = data;
     772                 :             : 
     773                 :           3 :   TRACE (GLIB_MAIN_CONTEXT_FREE (context));
     774                 :             : 
     775                 :           3 :   g_main_context_release (context);
     776                 :           3 :   if (context)
     777                 :           3 :     g_main_context_unref (context);
     778                 :           3 : }
     779                 :             : 
     780                 :             : static void
     781                 :        1118 : free_context_stack (gpointer data)
     782                 :             : {
     783                 :        1118 :   g_queue_free_full((GQueue *) data, (GDestroyNotify) free_context);
     784                 :        1118 : }
     785                 :             : 
     786                 :             : static GPrivate thread_context_stack = G_PRIVATE_INIT (free_context_stack);
     787                 :             : 
     788                 :             : /**
     789                 :             :  * g_main_context_push_thread_default:
     790                 :             :  * @context: (nullable): a main context, or `NULL` for the global-default
     791                 :             :  *   main context
     792                 :             :  *
     793                 :             :  * Acquires @context and sets it as the thread-default context for the
     794                 :             :  * current thread. This will cause certain asynchronous operations
     795                 :             :  * (such as most [Gio](../gio/index.html)-based I/O) which are
     796                 :             :  * started in this thread to run under @context and deliver their
     797                 :             :  * results to its main loop, rather than running under the global
     798                 :             :  * default main context in the main thread. Note that calling this function
     799                 :             :  * changes the context returned by [func@GLib.MainContext.get_thread_default],
     800                 :             :  * not the one returned by [func@GLib.MainContext.default], so it does not
     801                 :             :  * affect the context used by functions like [func@GLib.idle_add].
     802                 :             :  *
     803                 :             :  * Normally you would call this function shortly after creating a new
     804                 :             :  * thread, passing it a [struct@GLib.MainContext] which will be run by a
     805                 :             :  * [struct@GLib.MainLoop] in that thread, to set a new default context for all
     806                 :             :  * async operations in that thread. In this case you may not need to
     807                 :             :  * ever call [method@GLib.MainContext.pop_thread_default], assuming you want
     808                 :             :  * the new [struct@GLib.MainContext] to be the default for the whole lifecycle
     809                 :             :  * of the thread.
     810                 :             :  *
     811                 :             :  * If you don’t have control over how the new thread was created (e.g.
     812                 :             :  * in the new thread isn’t newly created, or if the thread life
     813                 :             :  * cycle is managed by a #GThreadPool), it is always suggested to wrap
     814                 :             :  * the logic that needs to use the new [struct@GLib.MainContext] inside a
     815                 :             :  * [method@GLib.MainContext.push_thread_default] /
     816                 :             :  * [method@GLib.MainContext.pop_thread_default] pair, otherwise threads that
     817                 :             :  * are re-used will end up never explicitly releasing the
     818                 :             :  * [struct@GLib.MainContext] reference they hold.
     819                 :             :  *
     820                 :             :  * In some cases you may want to schedule a single operation in a
     821                 :             :  * non-default context, or temporarily use a non-default context in
     822                 :             :  * the main thread. In that case, you can wrap the call to the
     823                 :             :  * asynchronous operation inside a
     824                 :             :  * [method@GLib.MainContext.push_thread_default] /
     825                 :             :  * [method@GLib.MainContext.pop_thread_default] pair, but it is up to you to
     826                 :             :  * ensure that no other asynchronous operations accidentally get
     827                 :             :  * started while the non-default context is active.
     828                 :             :  *
     829                 :             :  * Beware that libraries that predate this function may not correctly
     830                 :             :  * handle being used from a thread with a thread-default context. For example,
     831                 :             :  * see `g_file_supports_thread_contexts()`.
     832                 :             :  *
     833                 :             :  * Since: 2.22
     834                 :             :  **/
     835                 :             : void
     836                 :      125409 : g_main_context_push_thread_default (GMainContext *context)
     837                 :             : {
     838                 :             :   GQueue *stack;
     839                 :             :   gboolean acquired_context;
     840                 :             : 
     841                 :      125409 :   acquired_context = g_main_context_acquire (context);
     842                 :      125409 :   g_return_if_fail (acquired_context);
     843                 :             : 
     844                 :      125409 :   if (context == g_main_context_default ())
     845                 :       37133 :     context = NULL;
     846                 :       88276 :   else if (context)
     847                 :       88274 :     g_main_context_ref (context);
     848                 :             : 
     849                 :      125409 :   stack = g_private_get (&thread_context_stack);
     850                 :      125409 :   if (!stack)
     851                 :             :     {
     852                 :        1431 :       stack = g_queue_new ();
     853                 :        1431 :       g_private_set (&thread_context_stack, stack);
     854                 :          72 :     }
     855                 :             : 
     856                 :      125409 :   g_queue_push_head (stack, context);
     857                 :             : 
     858                 :      120368 :   TRACE (GLIB_MAIN_CONTEXT_PUSH_THREAD_DEFAULT (context));
     859                 :        5041 : }
     860                 :             : 
     861                 :             : /**
     862                 :             :  * g_main_context_pop_thread_default:
     863                 :             :  * @context: (nullable): a main context, or `NULL` for the global-default
     864                 :             :  *   main context
     865                 :             :  *
     866                 :             :  * Pops @context off the thread-default context stack (verifying that
     867                 :             :  * it was on the top of the stack).
     868                 :             :  *
     869                 :             :  * Since: 2.22
     870                 :             :  **/
     871                 :             : void
     872                 :      125301 : g_main_context_pop_thread_default (GMainContext *context)
     873                 :             : {
     874                 :             :   GQueue *stack;
     875                 :             : 
     876                 :      125301 :   if (context == g_main_context_default ())
     877                 :       37133 :     context = NULL;
     878                 :             : 
     879                 :      125301 :   stack = g_private_get (&thread_context_stack);
     880                 :             : 
     881                 :      125301 :   g_return_if_fail (stack != NULL);
     882                 :      125301 :   g_return_if_fail (g_queue_peek_head (stack) == context);
     883                 :             : 
     884                 :      120265 :   TRACE (GLIB_MAIN_CONTEXT_POP_THREAD_DEFAULT (context));
     885                 :             : 
     886                 :      125301 :   g_queue_pop_head (stack);
     887                 :             : 
     888                 :      125301 :   g_main_context_release (context);
     889                 :      125301 :   if (context)
     890                 :       88166 :     g_main_context_unref (context);
     891                 :        5036 : }
     892                 :             : 
     893                 :             : /**
     894                 :             :  * g_main_context_get_thread_default:
     895                 :             :  *
     896                 :             :  * Gets the thread-default main context for this thread.
     897                 :             :  *
     898                 :             :  * Asynchronous operations that want to be able to be run in contexts other than
     899                 :             :  * the default one should call this method or
     900                 :             :  * [func@GLib.MainContext.ref_thread_default] to get a
     901                 :             :  * [struct@GLib.MainContext] to add their [struct@GLib.Source]s to. (Note that
     902                 :             :  * even in single-threaded programs applications may sometimes want to
     903                 :             :  * temporarily push a non-default context, so it is not safe to assume that
     904                 :             :  * this will always return `NULL` if you are running in the default thread.)
     905                 :             :  *
     906                 :             :  * If you need to hold a reference on the context, use
     907                 :             :  * [func@GLib.MainContext.ref_thread_default] instead.
     908                 :             :  *
     909                 :             :  * Returns: (transfer none) (nullable): the thread-default main context, or
     910                 :             :  *   `NULL` if the thread-default context is the global-default main context
     911                 :             :  * Since: 2.22
     912                 :             :  **/
     913                 :             : GMainContext *
     914                 :      429609 : g_main_context_get_thread_default (void)
     915                 :             : {
     916                 :             :   GQueue *stack;
     917                 :             : 
     918                 :      429609 :   stack = g_private_get (&thread_context_stack);
     919                 :      429609 :   if (stack)
     920                 :      327134 :     return g_queue_peek_head (stack);
     921                 :             :   else
     922                 :      102475 :     return NULL;
     923                 :        6608 : }
     924                 :             : 
     925                 :             : /**
     926                 :             :  * g_main_context_ref_thread_default:
     927                 :             :  *
     928                 :             :  * Gets a reference to the thread-default [struct@GLib.MainContext] for this
     929                 :             :  * thread
     930                 :             :  *
     931                 :             :  * This is the same as [func@GLib.MainContext.get_thread_default], but it also
     932                 :             :  * adds a reference to the returned main context with [method@GLib.MainContext.ref].
     933                 :             :  * In addition, unlike
     934                 :             :  * [func@GLib.MainContext.get_thread_default], if the thread-default context
     935                 :             :  * is the global-default context, this will return that
     936                 :             :  * [struct@GLib.MainContext] (with a ref added to it) rather than returning
     937                 :             :  * `NULL`.
     938                 :             :  *
     939                 :             :  * Returns: (transfer full) (not nullable): the thread-default main context
     940                 :             :  * Since: 2.32
     941                 :             :  */
     942                 :             : GMainContext *
     943                 :      425904 : g_main_context_ref_thread_default (void)
     944                 :             : {
     945                 :             :   GMainContext *context;
     946                 :             : 
     947                 :      425904 :   context = g_main_context_get_thread_default ();
     948                 :      425904 :   if (!context)
     949                 :      142329 :     context = g_main_context_default ();
     950                 :      425904 :   return g_main_context_ref (context);
     951                 :             : }
     952                 :             : 
     953                 :             : /* Hooks for adding to the main loop */
     954                 :             : 
     955                 :             : /**
     956                 :             :  * g_source_new:
     957                 :             :  * @source_funcs: structure containing functions that implement
     958                 :             :  *   the source‘s behavior
     959                 :             :  * @struct_size: size of the [struct@GLib.Source] structure to create, in bytes
     960                 :             :  * 
     961                 :             :  * Creates a new [struct@GLib.Source] structure.
     962                 :             :  *
     963                 :             :  * The size is specified to
     964                 :             :  * allow creating structures derived from [struct@GLib.Source] that contain
     965                 :             :  * additional data. The size passed in must be at least
     966                 :             :  * `sizeof (GSource)`.
     967                 :             :  * 
     968                 :             :  * The source will not initially be associated with any [struct@GLib.MainContext]
     969                 :             :  * and must be added to one with [method@GLib.Source.attach] before it will be
     970                 :             :  * executed.
     971                 :             :  * 
     972                 :             :  * Returns: (transfer full): the newly-created source
     973                 :             :  **/
     974                 :             : GSource *
     975                 :      988922 : g_source_new (GSourceFuncs *source_funcs,
     976                 :             :               guint         struct_size)
     977                 :             : {
     978                 :             :   GSource *source;
     979                 :             : 
     980                 :      988922 :   g_return_val_if_fail (source_funcs != NULL, NULL);
     981                 :      988922 :   g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
     982                 :             :   
     983                 :      988922 :   source = (GSource*) g_malloc0 (struct_size);
     984                 :      988922 :   source->priv = g_slice_new0 (GSourcePrivate);
     985                 :      988922 :   source->source_funcs = source_funcs;
     986                 :      988922 :   g_atomic_int_set (&source->ref_count, 1);
     987                 :             :   
     988                 :      988922 :   source->priority = G_PRIORITY_DEFAULT;
     989                 :             : 
     990                 :      988922 :   g_atomic_int_set (&source->flags, G_HOOK_FLAG_ACTIVE);
     991                 :             : 
     992                 :      988922 :   source->priv->ready_time_ns = 0;
     993                 :      988922 :   source->priv->has_ready_time = FALSE;
     994                 :             : 
     995                 :             :   /* NULL/0 initialization for all other fields */
     996                 :             : 
     997                 :      720754 :   TRACE (GLIB_SOURCE_NEW (source, source_funcs->prepare, source_funcs->check,
     998                 :             :                           source_funcs->dispatch, source_funcs->finalize,
     999                 :             :                           struct_size));
    1000                 :             : 
    1001                 :      988922 :   return source;
    1002                 :      268168 : }
    1003                 :             : 
    1004                 :             : /**
    1005                 :             :  * g_source_set_dispose_function:
    1006                 :             :  * @source: a source to set the dispose function on
    1007                 :             :  * @dispose: dispose function to set on the source
    1008                 :             :  *
    1009                 :             :  * Set @dispose as dispose function on @source.
    1010                 :             :  *
    1011                 :             :  * The @dispose function will be called once the reference count of @source
    1012                 :             :  * reaches zero but before any of the state of the source is freed, especially
    1013                 :             :  * before the finalize function (set as part of the [type@GLib.SourceFuncs]) is
    1014                 :             :  * called.
    1015                 :             :  *
    1016                 :             :  * This means that at this point @source is still a valid [struct@GLib.Source]
    1017                 :             :  * and it is allow for the reference count to increase again until @dispose
    1018                 :             :  * returns.
    1019                 :             :  *
    1020                 :             :  * The dispose function can be used to clear any ‘weak’ references to
    1021                 :             :  * the @source in other data structures in a thread-safe way where it is
    1022                 :             :  * possible for another thread to increase the reference count of @source again
    1023                 :             :  * while it is being freed.
    1024                 :             :  *
    1025                 :             :  * The finalize function can not be used for this purpose as at that
    1026                 :             :  * point @source is already partially freed and not valid any more.
    1027                 :             :  *
    1028                 :             :  * This should only ever be called from [struct@GLib.Source] implementations.
    1029                 :             :  *
    1030                 :             :  * Since: 2.64
    1031                 :             :  **/
    1032                 :             : void
    1033                 :      214539 : g_source_set_dispose_function (GSource            *source,
    1034                 :             :                                GSourceDisposeFunc  dispose)
    1035                 :             : {
    1036                 :             :   gboolean was_unset G_GNUC_UNUSED;
    1037                 :             : 
    1038                 :      214539 :   g_return_if_fail (source != NULL);
    1039                 :      214539 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1040                 :             : 
    1041                 :      314752 :   was_unset = g_atomic_pointer_compare_and_exchange (&source->priv->dispose,
    1042                 :      100213 :                                                      NULL, dispose);
    1043                 :      214539 :   g_return_if_fail (was_unset);
    1044                 :      100213 : }
    1045                 :             : 
    1046                 :             : /* Holds context's lock */
    1047                 :             : static void
    1048                 :     2109417 : g_source_iter_init (GSourceIter  *iter,
    1049                 :             :                     GMainContext *context,
    1050                 :             :                     gboolean      may_modify)
    1051                 :             : {
    1052                 :     2109417 :   iter->context = context;
    1053                 :     2109417 :   iter->current_list = NULL;
    1054                 :     2109417 :   iter->source = NULL;
    1055                 :     2109417 :   iter->may_modify = may_modify;
    1056                 :     2109417 : }
    1057                 :             : 
    1058                 :             : /* Holds context's lock */
    1059                 :             : static gboolean
    1060                 :     5667741 : g_source_iter_next (GSourceIter *iter, GSource **source)
    1061                 :             : {
    1062                 :             :   GSource *next_source;
    1063                 :             : 
    1064                 :     5667741 :   if (iter->source)
    1065                 :     3558324 :     next_source = iter->source->next;
    1066                 :             :   else
    1067                 :     2109417 :     next_source = NULL;
    1068                 :             : 
    1069                 :     5667741 :   if (!next_source)
    1070                 :             :     {
    1071                 :     4347016 :       if (iter->current_list)
    1072                 :     2237568 :         iter->current_list = iter->current_list->next;
    1073                 :             :       else
    1074                 :     2109448 :         iter->current_list = iter->context->source_lists.head;
    1075                 :             : 
    1076                 :     4347016 :       if (iter->current_list)
    1077                 :             :         {
    1078                 :     2569205 :           GSourceList *source_list = iter->current_list->data;
    1079                 :             : 
    1080                 :     2569205 :           next_source = source_list->head;
    1081                 :      522314 :         }
    1082                 :     1022781 :     }
    1083                 :             : 
    1084                 :             :   /* Note: unreffing iter->source could potentially cause its
    1085                 :             :    * GSourceList to be removed from source_lists (if iter->source is
    1086                 :             :    * the only source in its list, and it is destroyed), so we have to
    1087                 :             :    * keep it reffed until after we advance iter->current_list, above.
    1088                 :             :    *
    1089                 :             :    * Also we first have to ref the next source before unreffing the
    1090                 :             :    * previous one as unreffing the previous source can potentially
    1091                 :             :    * free the next one.
    1092                 :             :    */
    1093                 :     5667741 :   if (next_source && iter->may_modify)
    1094                 :     3889683 :     g_source_ref (next_source);
    1095                 :             : 
    1096                 :     5667741 :   if (iter->source && iter->may_modify)
    1097                 :     3558052 :     g_source_unref_internal (iter->source, iter->context, TRUE);
    1098                 :     5667741 :   iter->source = next_source;
    1099                 :             : 
    1100                 :     5667741 :   *source = iter->source;
    1101                 :     5667741 :   return *source != NULL;
    1102                 :             : }
    1103                 :             : 
    1104                 :             : /* Holds context's lock. Only necessary to call if you broke out of
    1105                 :             :  * the g_source_iter_next() loop early.
    1106                 :             :  */
    1107                 :             : static void
    1108                 :     2109465 : g_source_iter_clear (GSourceIter *iter)
    1109                 :             : {
    1110                 :     2109465 :   if (iter->source && iter->may_modify)
    1111                 :             :     {
    1112                 :      331634 :       g_source_unref_internal (iter->source, iter->context, TRUE);
    1113                 :      331634 :       iter->source = NULL;
    1114                 :          65 :     }
    1115                 :     2109465 : }
    1116                 :             : 
    1117                 :             : /* Holds context's lock
    1118                 :             :  */
    1119                 :             : static GSourceList *
    1120                 :     1576067 : find_source_list_for_priority (GMainContext *context,
    1121                 :             :                                gint          priority,
    1122                 :             :                                gboolean      create)
    1123                 :             : {
    1124                 :             :   GList *iter;
    1125                 :             :   GSourceList *source_list;
    1126                 :             : 
    1127                 :    11566172 :   for (iter = context->source_lists.head; iter; iter = iter->next)
    1128                 :             :     {
    1129                 :    11357963 :       source_list = iter->data;
    1130                 :             : 
    1131                 :    11357963 :       if (source_list->priority == priority)
    1132                 :     1267402 :         return source_list;
    1133                 :             : 
    1134                 :    10090561 :       if (source_list->priority > priority)
    1135                 :             :         {
    1136                 :      100456 :           if (!create)
    1137                 :           0 :             return NULL;
    1138                 :             : 
    1139                 :      100456 :           source_list = g_slice_new0 (GSourceList);
    1140                 :      100456 :           source_list->link.data = source_list;
    1141                 :      100456 :           source_list->priority = priority;
    1142                 :      100712 :           g_queue_insert_before_link (&context->source_lists,
    1143                 :         256 :                                       iter,
    1144                 :         256 :                                       &source_list->link);
    1145                 :      100456 :           return source_list;
    1146                 :             :         }
    1147                 :     4944130 :     }
    1148                 :             : 
    1149                 :      208209 :   if (!create)
    1150                 :           0 :     return NULL;
    1151                 :             : 
    1152                 :      208209 :   source_list = g_slice_new0 (GSourceList);
    1153                 :      208209 :   source_list->link.data = source_list;
    1154                 :      208209 :   source_list->priority = priority;
    1155                 :      208209 :   g_queue_push_tail_link (&context->source_lists, &source_list->link);
    1156                 :             : 
    1157                 :      208209 :   return source_list;
    1158                 :      336064 : }
    1159                 :             : 
    1160                 :             : /* Holds context's lock
    1161                 :             :  */
    1162                 :             : static void
    1163                 :      788924 : source_add_to_context (GSource      *source,
    1164                 :             :                        GMainContext *context)
    1165                 :             : {
    1166                 :             :   GSourceList *source_list;
    1167                 :             :   GSource *prev, *next;
    1168                 :             : 
    1169                 :      788924 :   source_list = find_source_list_for_priority (context, source->priority, TRUE);
    1170                 :             : 
    1171                 :      788924 :   if (source->priv->parent_source)
    1172                 :             :     {
    1173                 :       18918 :       g_assert (source_list->head != NULL);
    1174                 :             : 
    1175                 :             :       /* Put the source immediately before its parent */
    1176                 :       18918 :       prev = source->priv->parent_source->prev;
    1177                 :       18918 :       next = source->priv->parent_source;
    1178                 :         198 :     }
    1179                 :             :   else
    1180                 :             :     {
    1181                 :      770006 :       prev = source_list->tail;
    1182                 :      770006 :       next = NULL;
    1183                 :             :     }
    1184                 :             : 
    1185                 :      788924 :   source->next = next;
    1186                 :      788924 :   if (next)
    1187                 :       18918 :     next->prev = source;
    1188                 :             :   else
    1189                 :      770006 :     source_list->tail = source;
    1190                 :             :   
    1191                 :      788924 :   source->prev = prev;
    1192                 :      788924 :   if (prev)
    1193                 :      480202 :     prev->next = source;
    1194                 :             :   else
    1195                 :      308722 :     source_list->head = source;
    1196                 :      788924 : }
    1197                 :             : 
    1198                 :             : /* Holds context's lock
    1199                 :             :  */
    1200                 :             : static void
    1201                 :      787144 : source_remove_from_context (GSource      *source,
    1202                 :             :                             GMainContext *context)
    1203                 :             : {
    1204                 :             :   GSourceList *source_list;
    1205                 :             : 
    1206                 :      787144 :   source_list = find_source_list_for_priority (context, source->priority, FALSE);
    1207                 :      787144 :   g_return_if_fail (source_list != NULL);
    1208                 :             : 
    1209                 :      787144 :   if (source->prev)
    1210                 :      354657 :     source->prev->next = source->next;
    1211                 :             :   else
    1212                 :      432487 :     source_list->head = source->next;
    1213                 :             : 
    1214                 :      787144 :   if (source->next)
    1215                 :      462762 :     source->next->prev = source->prev;
    1216                 :             :   else
    1217                 :      324382 :     source_list->tail = source->prev;
    1218                 :             : 
    1219                 :      787144 :   source->prev = NULL;
    1220                 :      787144 :   source->next = NULL;
    1221                 :             : 
    1222                 :      787144 :   if (source_list->head == NULL)
    1223                 :             :     {
    1224                 :      308170 :       g_queue_unlink (&context->source_lists, &source_list->link);
    1225                 :      308170 :       g_slice_free (GSourceList, source_list);
    1226                 :       14154 :     }
    1227                 :      167896 : }
    1228                 :             : 
    1229                 :             : static guint
    1230                 :      788914 : g_source_attach_unlocked (GSource      *source,
    1231                 :             :                           GMainContext *context,
    1232                 :             :                           gboolean      do_wakeup)
    1233                 :             : {
    1234                 :             :   GSList *tmp_list;
    1235                 :             :   guint id;
    1236                 :             : 
    1237                 :             :   /* The counter may have wrapped, so we must ensure that we do not
    1238                 :             :    * reuse the source id of an existing source.
    1239                 :             :    */
    1240                 :      168165 :   do
    1241                 :      957081 :     id = context->next_id++;
    1242                 :      788916 :   while (id == 0 || g_hash_table_contains (context->sources, &id));
    1243                 :             : 
    1244                 :      788914 :   source->context = context;
    1245                 :      788914 :   source->source_id = id;
    1246                 :      788914 :   g_source_ref (source);
    1247                 :             : 
    1248                 :      788914 :   g_hash_table_add (context->sources, &source->source_id);
    1249                 :             : 
    1250                 :      788914 :   source_add_to_context (source, context);
    1251                 :             : 
    1252                 :      788914 :   if (!SOURCE_BLOCKED (source))
    1253                 :             :     {
    1254                 :      788910 :       tmp_list = source->poll_fds;
    1255                 :      791463 :       while (tmp_list)
    1256                 :             :         {
    1257                 :        2553 :           g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
    1258                 :        2553 :           tmp_list = tmp_list->next;
    1259                 :             :         }
    1260                 :             : 
    1261                 :      803357 :       for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    1262                 :       14447 :         g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
    1263                 :      168163 :     }
    1264                 :             : 
    1265                 :      788914 :   tmp_list = source->priv->child_sources;
    1266                 :      807818 :   while (tmp_list)
    1267                 :             :     {
    1268                 :       18904 :       g_source_attach_unlocked (tmp_list->data, context, FALSE);
    1269                 :       18904 :       tmp_list = tmp_list->next;
    1270                 :             :     }
    1271                 :             : 
    1272                 :             :   /* If another thread has acquired the context, wake it up since it
    1273                 :             :    * might be in poll() right now.
    1274                 :             :    */
    1275                 :      791457 :   if (do_wakeup &&
    1276                 :      770010 :       (context->flags & G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING ||
    1277                 :      770006 :        (context->owner && context->owner != G_THREAD_SELF)))
    1278                 :             :     {
    1279                 :       67586 :       g_wakeup_signal (context->wakeup);
    1280                 :         718 :     }
    1281                 :             : 
    1282                 :     1223919 :   g_trace_mark (G_TRACE_CURRENT_TIME, 0,
    1283                 :             :                 "GLib", "g_source_attach",
    1284                 :             :                 "%s to context %p",
    1285                 :     1223919 :                 (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)",
    1286                 :             :                 context);
    1287                 :             : 
    1288                 :      788914 :   return source->source_id;
    1289                 :             : }
    1290                 :             : 
    1291                 :             : /**
    1292                 :             :  * g_source_attach:
    1293                 :             :  * @source: a source
    1294                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    1295                 :             :  *   main context will be used)
    1296                 :             :  * 
    1297                 :             :  * Adds a [struct@GLib.Source] to a @context so that it will be executed within
    1298                 :             :  * that context.
    1299                 :             :  *
    1300                 :             :  * Remove it by calling [method@GLib.Source.destroy].
    1301                 :             :  *
    1302                 :             :  * This function is safe to call from any thread, regardless of which thread
    1303                 :             :  * the @context is running in.
    1304                 :             :  *
    1305                 :             :  * Returns: the ID (greater than 0) for the source within the
    1306                 :             :  *   [struct@GLib.MainContext]
    1307                 :             :  **/
    1308                 :             : guint
    1309                 :      769988 : g_source_attach (GSource      *source,
    1310                 :             :                  GMainContext *context)
    1311                 :             : {
    1312                 :      769988 :   guint result = 0;
    1313                 :             : 
    1314                 :      769988 :   g_return_val_if_fail (source != NULL, 0);
    1315                 :      769988 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0);
    1316                 :      769988 :   g_return_val_if_fail (source->context == NULL, 0);
    1317                 :      769988 :   g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
    1318                 :             :   
    1319                 :      769988 :   if (!context)
    1320                 :        3126 :     context = g_main_context_default ();
    1321                 :             : 
    1322                 :      769988 :   g_rw_lock_writer_lock (&source_destroy_lock);
    1323                 :      769988 :   LOCK_CONTEXT (context);
    1324                 :             : 
    1325                 :      769988 :   result = g_source_attach_unlocked (source, context, TRUE);
    1326                 :             : 
    1327                 :      602031 :   TRACE (GLIB_MAIN_SOURCE_ATTACH (g_source_get_name (source), source, context,
    1328                 :             :                                   result));
    1329                 :             : 
    1330                 :      769988 :   UNLOCK_CONTEXT (context);
    1331                 :      769988 :   g_rw_lock_writer_unlock (&source_destroy_lock);
    1332                 :             : 
    1333                 :      769988 :   return result;
    1334                 :      167957 : }
    1335                 :             : 
    1336                 :             : static void
    1337                 :      806717 : g_source_destroy_internal (GSource      *source,
    1338                 :             :                            GMainContext *context,
    1339                 :             :                            gboolean      have_lock)
    1340                 :             : {
    1341                 :      638481 :   TRACE (GLIB_MAIN_SOURCE_DESTROY (g_source_get_name (source), source,
    1342                 :             :                                    context));
    1343                 :             : 
    1344                 :      806717 :   if (!have_lock)
    1345                 :      450546 :     LOCK_CONTEXT (context);
    1346                 :             :   
    1347                 :      806717 :   if (!SOURCE_DESTROYED (source))
    1348                 :             :     {
    1349                 :             :       GSList *tmp_list;
    1350                 :             :       gpointer old_cb_data;
    1351                 :             :       GSourceCallbackFuncs *old_cb_funcs;
    1352                 :             : 
    1353                 :      787833 :       g_atomic_int_and (&source->flags, ~G_HOOK_FLAG_ACTIVE);
    1354                 :             : 
    1355                 :      787833 :       old_cb_data = source->callback_data;
    1356                 :      787833 :       old_cb_funcs = source->callback_funcs;
    1357                 :             : 
    1358                 :      787833 :       source->callback_data = NULL;
    1359                 :      787833 :       source->callback_funcs = NULL;
    1360                 :             : 
    1361                 :      787833 :       if (old_cb_funcs)
    1362                 :             :         {
    1363                 :      762629 :           UNLOCK_CONTEXT (context);
    1364                 :      762629 :           old_cb_funcs->unref (old_cb_data);
    1365                 :      762629 :           LOCK_CONTEXT (context);
    1366                 :      155772 :         }
    1367                 :             : 
    1368                 :      787833 :       if (!SOURCE_BLOCKED (source))
    1369                 :             :         {
    1370                 :      786540 :           tmp_list = source->poll_fds;
    1371                 :      789033 :           while (tmp_list)
    1372                 :             :             {
    1373                 :        2493 :               g_main_context_remove_poll_unlocked (context, tmp_list->data);
    1374                 :        2493 :               tmp_list = tmp_list->next;
    1375                 :             :             }
    1376                 :             : 
    1377                 :      800530 :           for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    1378                 :       13990 :             g_main_context_remove_poll_unlocked (context, tmp_list->data);
    1379                 :      167911 :         }
    1380                 :             : 
    1381                 :      806655 :       while (source->priv->child_sources)
    1382                 :       18822 :         g_child_source_remove_internal (source->priv->child_sources->data, context);
    1383                 :             : 
    1384                 :      787833 :       if (source->priv->parent_source)
    1385                 :           2 :         g_child_source_remove_internal (source, context);
    1386                 :             :           
    1387                 :      787833 :       g_source_unref_internal (source, context, TRUE);
    1388                 :      168042 :     }
    1389                 :             : 
    1390                 :      806717 :   if (!have_lock)
    1391                 :      450547 :     UNLOCK_CONTEXT (context);
    1392                 :      806717 : }
    1393                 :             : 
    1394                 :             : static GMainContext *
    1395                 :     5955233 : source_dup_main_context (GSource *source)
    1396                 :             : {
    1397                 :     5955233 :   GMainContext *ret = NULL;
    1398                 :             : 
    1399                 :     5955233 :   g_rw_lock_reader_lock (&source_destroy_lock);
    1400                 :             : 
    1401                 :     5955233 :   ret = source->context;
    1402                 :     5955233 :   if (ret)
    1403                 :     1768514 :     g_atomic_int_inc (&ret->ref_count);
    1404                 :             : 
    1405                 :     5955233 :   g_rw_lock_reader_unlock (&source_destroy_lock);
    1406                 :             : 
    1407                 :     5955233 :   return ret;
    1408                 :             : }
    1409                 :             : 
    1410                 :             : /**
    1411                 :             :  * g_source_destroy:
    1412                 :             :  * @source: a source
    1413                 :             :  *
    1414                 :             :  * Removes a source from its [struct@GLib.MainContext], if any, and marks it as
    1415                 :             :  * destroyed.
    1416                 :             :  *
    1417                 :             :  * The source cannot be subsequently added to another
    1418                 :             :  * context. It is safe to call this on sources which have already been
    1419                 :             :  * removed from their context.
    1420                 :             :  *
    1421                 :             :  * This does not unref the [struct@GLib.Source]: if you still hold a reference,
    1422                 :             :  * use [method@GLib.Source.unref] to drop it.
    1423                 :             :  *
    1424                 :             :  * This function is safe to call from any thread, regardless of which thread
    1425                 :             :  * the [struct@GLib.MainContext] is running in.
    1426                 :             :  *
    1427                 :             :  * If the source is currently attached to a [struct@GLib.MainContext],
    1428                 :             :  * destroying it will effectively unset the callback similar to calling
    1429                 :             :  * [method@GLib.Source.set_callback]. This can mean, that the data’s
    1430                 :             :  * [callback@GLib.DestroyNotify] gets called right away.
    1431                 :             :  */
    1432                 :             : void
    1433                 :      450563 : g_source_destroy (GSource *source)
    1434                 :             : {
    1435                 :             :   GMainContext *context;
    1436                 :             :   
    1437                 :      450563 :   g_return_if_fail (source != NULL);
    1438                 :      450563 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1439                 :             :   
    1440                 :      450563 :   context = source_dup_main_context (source);
    1441                 :             : 
    1442                 :      450563 :   if (context)
    1443                 :             :     {
    1444                 :      450547 :       g_source_destroy_internal (source, context, FALSE);
    1445                 :      450547 :       g_main_context_unref (context);
    1446                 :      162583 :     }
    1447                 :             :   else
    1448                 :          16 :     g_atomic_int_and (&source->flags, ~G_HOOK_FLAG_ACTIVE);
    1449                 :      162591 : }
    1450                 :             : 
    1451                 :             : /**
    1452                 :             :  * g_source_get_id:
    1453                 :             :  * @source: a source
    1454                 :             :  *
    1455                 :             :  * Returns the numeric ID for a particular source.
    1456                 :             :  * 
    1457                 :             :  * The ID of a source
    1458                 :             :  * is a positive integer which is unique within a particular main loop 
    1459                 :             :  * context. The reverse mapping from ID to source is done by
    1460                 :             :  * [method@GLib.MainContext.find_source_by_id].
    1461                 :             :  *
    1462                 :             :  * You can only call this function while the source is associated to a
    1463                 :             :  * [struct@GLib.MainContext] instance; calling this function before
    1464                 :             :  * [method@GLib.Source.attach] or after [method@GLib.Source.destroy] yields
    1465                 :             :  * undefined behavior. The ID returned is unique within the
    1466                 :             :  * [struct@GLib.MainContext] instance passed to [method@GLib.Source.attach].
    1467                 :             :  *
    1468                 :             :  * Returns: the ID (greater than 0) for the source
    1469                 :             :  **/
    1470                 :             : guint
    1471                 :      200108 : g_source_get_id (GSource *source)
    1472                 :             : {
    1473                 :             :   guint result;
    1474                 :             :   GMainContext *context;
    1475                 :             :   
    1476                 :      200108 :   g_return_val_if_fail (source != NULL, 0);
    1477                 :      200108 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0);
    1478                 :      200108 :   context = source_dup_main_context (source);
    1479                 :      200108 :   g_return_val_if_fail (context != NULL, 0);
    1480                 :             : 
    1481                 :      200108 :   LOCK_CONTEXT (context);
    1482                 :      200108 :   result = source->source_id;
    1483                 :      200108 :   UNLOCK_CONTEXT (context);
    1484                 :             :   
    1485                 :      200108 :   g_main_context_unref (context);
    1486                 :             : 
    1487                 :      200108 :   return result;
    1488                 :      100054 : }
    1489                 :             : 
    1490                 :             : /**
    1491                 :             :  * g_source_get_context:
    1492                 :             :  * @source: a source
    1493                 :             :  *
    1494                 :             :  * Gets the [struct@GLib.MainContext] with which the source is associated.
    1495                 :             :  *
    1496                 :             :  * You can call this on a source that has been destroyed, provided
    1497                 :             :  * that the [struct@GLib.MainContext] it was attached to still exists (in which
    1498                 :             :  * case it will return that [struct@GLib.MainContext]). In particular, you can
    1499                 :             :  * always call this function on the source returned from
    1500                 :             :  * [func@GLib.main_current_source]. But calling this function on a source
    1501                 :             :  * whose [struct@GLib.MainContext] has been destroyed is an error.
    1502                 :             :  *
    1503                 :             :  * If the associated [struct@GLib.MainContext] could be destroy concurrently from
    1504                 :             :  * a different thread, then this function is not safe to call and
    1505                 :             :  * [method@GLib.Source.dup_context] should be used instead.
    1506                 :             :  *
    1507                 :             :  * Returns: (transfer none) (nullable): the main context with which the
    1508                 :             :  *   source is associated, or `NULL` if the context has not yet been added to a
    1509                 :             :  *   source
    1510                 :             :  **/
    1511                 :             : GMainContext *
    1512                 :      114133 : g_source_get_context (GSource *source)
    1513                 :             : {
    1514                 :      114133 :   g_return_val_if_fail (source != NULL, NULL);
    1515                 :      114133 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL);
    1516                 :      114133 :   g_return_val_if_fail (source->context != NULL || !SOURCE_DESTROYED (source), NULL);
    1517                 :             : 
    1518                 :      114131 :   return source->context;
    1519                 :        2909 : }
    1520                 :             : 
    1521                 :             : /**
    1522                 :             :  * g_source_dup_context:
    1523                 :             :  * @source: a source
    1524                 :             :  *
    1525                 :             :  * Gets a reference to the [struct@GLib.MainContext] with which the source is
    1526                 :             :  * associated.
    1527                 :             :  *
    1528                 :             :  * You can call this on a source that has been destroyed. You can
    1529                 :             :  * always call this function on the source returned from
    1530                 :             :  * [func@GLib.main_current_source].
    1531                 :             :  *
    1532                 :             :  * Returns: (transfer full) (nullable): the [struct@GLib.MainContext] with which
    1533                 :             :  *   the source is associated, or `NULL` if the context has not yet been added
    1534                 :             :  *   to a source
    1535                 :             :  * Since: 2.86
    1536                 :             :  **/
    1537                 :             : GMainContext *
    1538                 :           2 : g_source_dup_context (GSource *source)
    1539                 :             : {
    1540                 :           2 :   g_return_val_if_fail (source != NULL, NULL);
    1541                 :           2 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL);
    1542                 :           2 :   g_return_val_if_fail (source->context != NULL || !SOURCE_DESTROYED (source), NULL);
    1543                 :             : 
    1544                 :           2 :   return source_dup_main_context (source);
    1545                 :           1 : }
    1546                 :             : 
    1547                 :             : /**
    1548                 :             :  * g_source_add_poll:
    1549                 :             :  * @source:a source
    1550                 :             :  * @fd: a [struct@GLib.PollFD] structure holding information about a file
    1551                 :             :  *   descriptor to watch
    1552                 :             :  *
    1553                 :             :  * Adds a file descriptor to the set of file descriptors polled for
    1554                 :             :  * this source.
    1555                 :             :  *
    1556                 :             :  * This is usually combined with [ctor@GLib.Source.new] to add an
    1557                 :             :  * event source. The event source’s check function will typically test
    1558                 :             :  * the @revents field in the [struct@GLib.PollFD] struct and return true if
    1559                 :             :  * events need to be processed.
    1560                 :             :  *
    1561                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    1562                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    1563                 :             :  *
    1564                 :             :  * Using this API forces the linear scanning of event sources on each
    1565                 :             :  * main loop iteration.  Newly-written event sources should try to use
    1566                 :             :  * `g_source_add_unix_fd()` instead of this API.
    1567                 :             :  **/
    1568                 :             : void
    1569                 :        2555 : g_source_add_poll (GSource *source,
    1570                 :             :                    GPollFD *fd)
    1571                 :             : {
    1572                 :             :   GMainContext *context;
    1573                 :             :   
    1574                 :        2555 :   g_return_if_fail (source != NULL);
    1575                 :        2555 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1576                 :        2555 :   g_return_if_fail (fd != NULL);
    1577                 :        2555 :   g_return_if_fail (!SOURCE_DESTROYED (source));
    1578                 :             :   
    1579                 :        2555 :   context = source_dup_main_context (source);
    1580                 :             : 
    1581                 :        2555 :   if (context)
    1582                 :           0 :     LOCK_CONTEXT (context);
    1583                 :             :   
    1584                 :        2555 :   source->poll_fds = g_slist_prepend (source->poll_fds, fd);
    1585                 :             : 
    1586                 :        2555 :   if (context)
    1587                 :             :     {
    1588                 :           0 :       if (!SOURCE_BLOCKED (source))
    1589                 :           0 :         g_main_context_add_poll_unlocked (context, source->priority, fd);
    1590                 :           0 :       UNLOCK_CONTEXT (context);
    1591                 :           0 :       g_main_context_unref (context);
    1592                 :           0 :     }
    1593                 :        1102 : }
    1594                 :             : 
    1595                 :             : /**
    1596                 :             :  * g_source_remove_poll:
    1597                 :             :  * @source:a source
    1598                 :             :  * @fd: a [struct@GLib.PollFD] structure previously passed to
    1599                 :             :  *   [method@GLib.Source.add_poll]
    1600                 :             :  *
    1601                 :             :  * Removes a file descriptor from the set of file descriptors polled for
    1602                 :             :  * this source.
    1603                 :             :  *
    1604                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    1605                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    1606                 :             :  **/
    1607                 :             : void
    1608                 :           1 : g_source_remove_poll (GSource *source,
    1609                 :             :                       GPollFD *fd)
    1610                 :             : {
    1611                 :             :   GMainContext *context;
    1612                 :             :   
    1613                 :           1 :   g_return_if_fail (source != NULL);
    1614                 :           1 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1615                 :           1 :   g_return_if_fail (fd != NULL);
    1616                 :           1 :   g_return_if_fail (!SOURCE_DESTROYED (source));
    1617                 :             :   
    1618                 :           1 :   context = source_dup_main_context (source);
    1619                 :             : 
    1620                 :           1 :   if (context)
    1621                 :           1 :     LOCK_CONTEXT (context);
    1622                 :             :   
    1623                 :           1 :   source->poll_fds = g_slist_remove (source->poll_fds, fd);
    1624                 :             : 
    1625                 :           1 :   if (context)
    1626                 :             :     {
    1627                 :           1 :       if (!SOURCE_BLOCKED (source))
    1628                 :           1 :         g_main_context_remove_poll_unlocked (context, fd);
    1629                 :           1 :       UNLOCK_CONTEXT (context);
    1630                 :           1 :       g_main_context_unref (context);
    1631                 :           1 :     }
    1632                 :           1 : }
    1633                 :             : 
    1634                 :             : /**
    1635                 :             :  * g_source_add_child_source:
    1636                 :             :  * @source:a source
    1637                 :             :  * @child_source: a second source that @source should ‘poll’
    1638                 :             :  *
    1639                 :             :  * Adds @child_source to @source as a ‘polled’ source.
    1640                 :             :  *
    1641                 :             :  * When @source is added to a [struct@GLib.MainContext], @child_source will be
    1642                 :             :  * automatically added with the same priority. When @child_source is triggered,
    1643                 :             :  * it will cause @source to dispatch (in addition to calling its own callback),
    1644                 :             :  * and when @source is destroyed, it will destroy @child_source as well.
    1645                 :             :  *
    1646                 :             :  * The @source will also still be dispatched if its own prepare/check functions
    1647                 :             :  * indicate that it is ready.
    1648                 :             :  *
    1649                 :             :  * If you don’t need @child_source to do anything on its own when it
    1650                 :             :  * triggers, you can call `g_source_set_dummy_callback()` on it to set a
    1651                 :             :  * callback that does nothing (except return true if appropriate).
    1652                 :             :  *
    1653                 :             :  * The @source will hold a reference on @child_source while @child_source
    1654                 :             :  * is attached to it.
    1655                 :             :  *
    1656                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    1657                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    1658                 :             :  *
    1659                 :             :  * Since: 2.28
    1660                 :             :  **/
    1661                 :             : void
    1662                 :       18914 : g_source_add_child_source (GSource *source,
    1663                 :             :                            GSource *child_source)
    1664                 :             : {
    1665                 :             :   GMainContext *context;
    1666                 :             : 
    1667                 :       18914 :   g_return_if_fail (source != NULL);
    1668                 :       18914 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1669                 :       18914 :   g_return_if_fail (child_source != NULL);
    1670                 :       18914 :   g_return_if_fail (g_atomic_int_get (&child_source->ref_count) > 0);
    1671                 :       18914 :   g_return_if_fail (!SOURCE_DESTROYED (source));
    1672                 :       18914 :   g_return_if_fail (!SOURCE_DESTROYED (child_source));
    1673                 :       18914 :   g_return_if_fail (child_source->context == NULL);
    1674                 :       18914 :   g_return_if_fail (child_source->priv->parent_source == NULL);
    1675                 :             : 
    1676                 :       18914 :   context = source_dup_main_context (source);
    1677                 :             : 
    1678                 :       18914 :   if (context)
    1679                 :             :     {
    1680                 :          10 :       g_rw_lock_writer_lock (&source_destroy_lock);
    1681                 :          10 :       LOCK_CONTEXT (context);
    1682                 :           5 :     }
    1683                 :             : 
    1684                 :       18718 :   TRACE (GLIB_SOURCE_ADD_CHILD_SOURCE (source, child_source));
    1685                 :             : 
    1686                 :       19110 :   source->priv->child_sources = g_slist_prepend (source->priv->child_sources,
    1687                 :       18914 :                                                  g_source_ref (child_source));
    1688                 :       18914 :   child_source->priv->parent_source = source;
    1689                 :       18914 :   g_source_set_priority_unlocked (child_source, NULL, source->priority);
    1690                 :       18914 :   if (SOURCE_BLOCKED (source))
    1691                 :           4 :     block_source (child_source, NULL);
    1692                 :             : 
    1693                 :       18914 :   if (context)
    1694                 :             :     {
    1695                 :          10 :       g_source_attach_unlocked (child_source, context, TRUE);
    1696                 :          10 :       UNLOCK_CONTEXT (context);
    1697                 :          10 :       g_rw_lock_writer_unlock (&source_destroy_lock);
    1698                 :          10 :       g_main_context_unref (context);
    1699                 :           5 :     }
    1700                 :         196 : }
    1701                 :             : 
    1702                 :             : static void
    1703                 :       18826 : g_child_source_remove_internal (GSource *child_source,
    1704                 :             :                                 GMainContext *context)
    1705                 :             : {
    1706                 :       18826 :   GSource *parent_source = child_source->priv->parent_source;
    1707                 :             : 
    1708                 :       37466 :   parent_source->priv->child_sources =
    1709                 :       18826 :     g_slist_remove (parent_source->priv->child_sources, child_source);
    1710                 :       18826 :   child_source->priv->parent_source = NULL;
    1711                 :             : 
    1712                 :       18826 :   g_source_destroy_internal (child_source, context, TRUE);
    1713                 :       18826 :   g_source_unref_internal (child_source, context, TRUE);
    1714                 :       18826 : }
    1715                 :             : 
    1716                 :             : /**
    1717                 :             :  * g_source_remove_child_source:
    1718                 :             :  * @source:a source
    1719                 :             :  * @child_source: a source previously passed to
    1720                 :             :  *   [method@GLib.Source.add_child_source]
    1721                 :             :  *
    1722                 :             :  * Detaches @child_source from @source and destroys it.
    1723                 :             :  *
    1724                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    1725                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    1726                 :             :  *
    1727                 :             :  * Since: 2.28
    1728                 :             :  **/
    1729                 :             : void
    1730                 :           2 : g_source_remove_child_source (GSource *source,
    1731                 :             :                               GSource *child_source)
    1732                 :             : {
    1733                 :             :   GMainContext *context;
    1734                 :             : 
    1735                 :           2 :   g_return_if_fail (source != NULL);
    1736                 :           2 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1737                 :           2 :   g_return_if_fail (child_source != NULL);
    1738                 :           2 :   g_return_if_fail (g_atomic_int_get (&child_source->ref_count) > 0);
    1739                 :           2 :   g_return_if_fail (child_source->priv->parent_source == source);
    1740                 :           2 :   g_return_if_fail (!SOURCE_DESTROYED (source));
    1741                 :           2 :   g_return_if_fail (!SOURCE_DESTROYED (child_source));
    1742                 :             : 
    1743                 :           2 :   context = source_dup_main_context (source);
    1744                 :             : 
    1745                 :           2 :   if (context)
    1746                 :           2 :     LOCK_CONTEXT (context);
    1747                 :             : 
    1748                 :           2 :   g_child_source_remove_internal (child_source, context);
    1749                 :             : 
    1750                 :           2 :   if (context)
    1751                 :             :     {
    1752                 :           2 :       UNLOCK_CONTEXT (context);
    1753                 :           2 :       g_main_context_unref (context);
    1754                 :           1 :     }
    1755                 :           1 : }
    1756                 :             : 
    1757                 :             : static void
    1758                 :      852384 : g_source_callback_ref (gpointer cb_data)
    1759                 :             : {
    1760                 :      852384 :   GSourceCallback *callback = cb_data;
    1761                 :             : 
    1762                 :      852384 :   g_atomic_int_inc (&callback->ref_count);
    1763                 :      852384 : }
    1764                 :             : 
    1765                 :             : static void
    1766                 :     1796140 : g_source_callback_unref (gpointer cb_data)
    1767                 :             : {
    1768                 :     1796140 :   GSourceCallback *callback = cb_data;
    1769                 :             : 
    1770                 :     1796140 :   if (g_atomic_int_dec_and_test (&callback->ref_count))
    1771                 :             :     {
    1772                 :      943785 :       if (callback->notify)
    1773                 :      338966 :         callback->notify (callback->data);
    1774                 :      943785 :       g_free (callback);
    1775                 :      255568 :     }
    1776                 :     1796140 : }
    1777                 :             : 
    1778                 :             : static void
    1779                 :      852389 : g_source_callback_get (gpointer     cb_data,
    1780                 :             :                        GSource     *source, 
    1781                 :             :                        GSourceFunc *func,
    1782                 :             :                        gpointer    *data)
    1783                 :             : {
    1784                 :      852389 :   GSourceCallback *callback = cb_data;
    1785                 :             : 
    1786                 :      852389 :   *func = callback->func;
    1787                 :      852389 :   *data = callback->data;
    1788                 :      852389 : }
    1789                 :             : 
    1790                 :             : static GSourceCallbackFuncs g_source_callback_funcs = {
    1791                 :             :   g_source_callback_ref,
    1792                 :             :   g_source_callback_unref,
    1793                 :             :   g_source_callback_get,
    1794                 :             : };
    1795                 :             : 
    1796                 :             : /**
    1797                 :             :  * g_source_set_callback_indirect:
    1798                 :             :  * @source: the source
    1799                 :             :  * @callback_data: pointer to callback data ‘object’
    1800                 :             :  * @callback_funcs: functions for reference counting @callback_data
    1801                 :             :  *   and getting the callback and data
    1802                 :             :  *
    1803                 :             :  * Sets the callback function storing the data as a reference counted callback
    1804                 :             :  * ‘object’.
    1805                 :             :  *
    1806                 :             :  * This is used internally. Note that calling
    1807                 :             :  * [method@GLib.Source.set_callback_indirect] assumes
    1808                 :             :  * an initial reference count on @callback_data, and thus
    1809                 :             :  * `callback_funcs->unref` will eventually be called once more than
    1810                 :             :  * `callback_funcs->ref`.
    1811                 :             :  *
    1812                 :             :  * It is safe to call this function multiple times on a source which has already
    1813                 :             :  * been attached to a context. The changes will take effect for the next time
    1814                 :             :  * the source is dispatched after this call returns.
    1815                 :             :  **/
    1816                 :             : void
    1817                 :      963480 : g_source_set_callback_indirect (GSource              *source,
    1818                 :             :                                 gpointer              callback_data,
    1819                 :             :                                 GSourceCallbackFuncs *callback_funcs)
    1820                 :             : {
    1821                 :             :   GMainContext *context;
    1822                 :             :   gpointer old_cb_data;
    1823                 :             :   GSourceCallbackFuncs *old_cb_funcs;
    1824                 :             :   
    1825                 :      963480 :   g_return_if_fail (source != NULL);
    1826                 :      963480 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1827                 :      963480 :   g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
    1828                 :             : 
    1829                 :      963480 :   context = source_dup_main_context (source);
    1830                 :             : 
    1831                 :      963480 :   if (context)
    1832                 :           2 :     LOCK_CONTEXT (context);
    1833                 :             : 
    1834                 :      963480 :   if (callback_funcs != &g_source_callback_funcs)
    1835                 :             :     {
    1836                 :       18718 :       TRACE (GLIB_SOURCE_SET_CALLBACK_INDIRECT (source, callback_data,
    1837                 :             :                                                 callback_funcs->ref,
    1838                 :             :                                                 callback_funcs->unref,
    1839                 :             :                                                 callback_funcs->get));
    1840                 :         194 :     }
    1841                 :             : 
    1842                 :      963480 :   old_cb_data = source->callback_data;
    1843                 :      963480 :   old_cb_funcs = source->callback_funcs;
    1844                 :             : 
    1845                 :      963480 :   source->callback_data = callback_data;
    1846                 :      963480 :   source->callback_funcs = callback_funcs;
    1847                 :             :   
    1848                 :      963480 :   if (context)
    1849                 :             :     {
    1850                 :           2 :       UNLOCK_CONTEXT (context);
    1851                 :           2 :       g_main_context_unref (context);
    1852                 :           1 :     }
    1853                 :             : 
    1854                 :      963480 :   if (old_cb_funcs)
    1855                 :           0 :     old_cb_funcs->unref (old_cb_data);
    1856                 :      255895 : }
    1857                 :             : 
    1858                 :             : /**
    1859                 :             :  * g_source_set_callback:
    1860                 :             :  * @source: the source
    1861                 :             :  * @func: a callback function
    1862                 :             :  * @data: the data to pass to callback function
    1863                 :             :  * @notify: (nullable): a function to call when @data is no longer in use
    1864                 :             :  * 
    1865                 :             :  * Sets the callback function for a source. The callback for a source is
    1866                 :             :  * called from the source’s dispatch function.
    1867                 :             :  *
    1868                 :             :  * The exact type of @func depends on the type of source; ie. you
    1869                 :             :  * should not count on @func being called with @data as its first
    1870                 :             :  * parameter. Cast @func with [func@GLib.SOURCE_FUNC] to avoid warnings about
    1871                 :             :  * incompatible function types.
    1872                 :             :  *
    1873                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    1874                 :             :  * on how to handle memory management of @data.
    1875                 :             :  * 
    1876                 :             :  * Typically, you won’t use this function. Instead use functions specific
    1877                 :             :  * to the type of source you are using, such as [func@GLib.idle_add] or
    1878                 :             :  * [func@GLib.timeout_add].
    1879                 :             :  *
    1880                 :             :  * It is safe to call this function multiple times on a source which has already
    1881                 :             :  * been attached to a context. The changes will take effect for the next time
    1882                 :             :  * the source is dispatched after this call returns.
    1883                 :             :  *
    1884                 :             :  * Note that [method@GLib.Source.destroy] for a currently attached source has the effect
    1885                 :             :  * of also unsetting the callback.
    1886                 :             :  **/
    1887                 :             : void
    1888                 :      944563 : g_source_set_callback (GSource        *source,
    1889                 :             :                        GSourceFunc     func,
    1890                 :             :                        gpointer        data,
    1891                 :             :                        GDestroyNotify  notify)
    1892                 :             : {
    1893                 :             :   GSourceCallback *new_callback;
    1894                 :             : 
    1895                 :      944563 :   g_return_if_fail (source != NULL);
    1896                 :      944563 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1897                 :             : 
    1898                 :      688867 :   TRACE (GLIB_SOURCE_SET_CALLBACK (source, func, data, notify));
    1899                 :             : 
    1900                 :      944563 :   new_callback = g_new (GSourceCallback, 1);
    1901                 :             : 
    1902                 :      944563 :   new_callback->ref_count = 1;
    1903                 :      944563 :   new_callback->func = func;
    1904                 :      944563 :   new_callback->data = data;
    1905                 :      944563 :   new_callback->notify = notify;
    1906                 :             : 
    1907                 :      944563 :   g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
    1908                 :      255696 : }
    1909                 :             : 
    1910                 :             : 
    1911                 :             : /**
    1912                 :             :  * g_source_set_funcs:
    1913                 :             :  * @source: a source
    1914                 :             :  * @funcs: the new source functions
    1915                 :             :  * 
    1916                 :             :  * Sets the source functions of an unattached source.
    1917                 :             :  *
    1918                 :             :  * These can be used to override the default implementations for the type
    1919                 :             :  * of @source.
    1920                 :             :  * 
    1921                 :             :  * Since: 2.12
    1922                 :             :  */
    1923                 :             : void
    1924                 :           4 : g_source_set_funcs (GSource     *source,
    1925                 :             :                    GSourceFuncs *funcs)
    1926                 :             : {
    1927                 :           4 :   g_return_if_fail (source != NULL);
    1928                 :           4 :   g_return_if_fail (source->context == NULL);
    1929                 :           4 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    1930                 :           4 :   g_return_if_fail (funcs != NULL);
    1931                 :             : 
    1932                 :           4 :   source->source_funcs = funcs;
    1933                 :           2 : }
    1934                 :             : 
    1935                 :             : static void
    1936                 :     1195825 : g_source_set_priority_unlocked (GSource      *source,
    1937                 :             :                                 GMainContext *context,
    1938                 :             :                                 gint          priority)
    1939                 :             : {
    1940                 :             :   GSList *tmp_list;
    1941                 :             :   
    1942                 :     1195825 :   g_return_if_fail (source->priv->parent_source == NULL ||
    1943                 :         297 :                     source->priv->parent_source->priority == priority);
    1944                 :             : 
    1945                 :      987528 :   TRACE (GLIB_SOURCE_SET_PRIORITY (source, context, priority));
    1946                 :             : 
    1947                 :     1195825 :   if (context)
    1948                 :             :     {
    1949                 :             :       /* Remove the source from the context's source and then
    1950                 :             :        * add it back after so it is sorted in the correct place
    1951                 :             :        */
    1952                 :          10 :       source_remove_from_context (source, context);
    1953                 :           4 :     }
    1954                 :             : 
    1955                 :     1195825 :   source->priority = priority;
    1956                 :             : 
    1957                 :     1195825 :   if (context)
    1958                 :             :     {
    1959                 :          10 :       source_add_to_context (source, context);
    1960                 :             : 
    1961                 :          10 :       if (!SOURCE_BLOCKED (source))
    1962                 :             :         {
    1963                 :          10 :           tmp_list = source->poll_fds;
    1964                 :          10 :           while (tmp_list)
    1965                 :             :             {
    1966                 :           0 :               g_main_context_remove_poll_unlocked (context, tmp_list->data);
    1967                 :           0 :               g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
    1968                 :             :               
    1969                 :           0 :               tmp_list = tmp_list->next;
    1970                 :             :             }
    1971                 :             : 
    1972                 :          12 :           for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    1973                 :             :             {
    1974                 :           2 :               g_main_context_remove_poll_unlocked (context, tmp_list->data);
    1975                 :           2 :               g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
    1976                 :           0 :             }
    1977                 :           4 :         }
    1978                 :           4 :     }
    1979                 :             : 
    1980                 :     1195825 :   if (source->priv->child_sources)
    1981                 :             :     {
    1982                 :       14319 :       tmp_list = source->priv->child_sources;
    1983                 :       32934 :       while (tmp_list)
    1984                 :             :         {
    1985                 :       18615 :           g_source_set_priority_unlocked (tmp_list->data, context, priority);
    1986                 :       18615 :           tmp_list = tmp_list->next;
    1987                 :             :         }
    1988                 :         100 :     }
    1989                 :      208297 : }
    1990                 :             : 
    1991                 :             : /**
    1992                 :             :  * g_source_set_priority:
    1993                 :             :  * @source: a source
    1994                 :             :  * @priority: the new priority
    1995                 :             :  *
    1996                 :             :  * Sets the priority of a source.
    1997                 :             :  *
    1998                 :             :  * While the main loop is being run, a
    1999                 :             :  * source will be dispatched if it is ready to be dispatched and no
    2000                 :             :  * sources at a higher (numerically smaller) priority are ready to be
    2001                 :             :  * dispatched.
    2002                 :             :  *
    2003                 :             :  * A child source always has the same priority as its parent.  It is not
    2004                 :             :  * permitted to change the priority of a source once it has been added
    2005                 :             :  * as a child of another source.
    2006                 :             :  **/
    2007                 :             : void
    2008                 :     1158304 : g_source_set_priority (GSource  *source,
    2009                 :             :                        gint      priority)
    2010                 :             : {
    2011                 :             :   GMainContext *context;
    2012                 :             : 
    2013                 :     1158304 :   g_return_if_fail (source != NULL);
    2014                 :     1158304 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    2015                 :     1158304 :   g_return_if_fail (source->priv->parent_source == NULL);
    2016                 :             : 
    2017                 :     1158304 :   context = source_dup_main_context (source);
    2018                 :             : 
    2019                 :     1158304 :   if (context)
    2020                 :           6 :     LOCK_CONTEXT (context);
    2021                 :     1158304 :   g_source_set_priority_unlocked (source, context, priority);
    2022                 :     1158304 :   if (context)
    2023                 :             :     {
    2024                 :           6 :       UNLOCK_CONTEXT (context);
    2025                 :           6 :       g_main_context_unref (context);
    2026                 :           2 :     }
    2027                 :      208008 : }
    2028                 :             : 
    2029                 :             : /**
    2030                 :             :  * g_source_get_priority:
    2031                 :             :  * @source: a source
    2032                 :             :  * 
    2033                 :             :  * Gets the priority of a source.
    2034                 :             :  * 
    2035                 :             :  * Returns: the priority of the source
    2036                 :             :  **/
    2037                 :             : gint
    2038                 :          16 : g_source_get_priority (GSource *source)
    2039                 :             : {
    2040                 :          16 :   g_return_val_if_fail (source != NULL, 0);
    2041                 :          16 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0);
    2042                 :             : 
    2043                 :          16 :   return source->priority;
    2044                 :           8 : }
    2045                 :             : 
    2046                 :             : static void
    2047                 :      344876 : g_source_update_ready_time_internal (GSource *source,
    2048                 :             :                                      gboolean has_ready_time,
    2049                 :             :                                      uint64_t ready_time_ns)
    2050                 :             : {
    2051                 :             :   GMainContext *context;
    2052                 :             : 
    2053                 :      344876 :   context = source_dup_main_context (source);
    2054                 :             : 
    2055                 :      344876 :   if (context)
    2056                 :      106431 :     LOCK_CONTEXT (context);
    2057                 :             : 
    2058                 :      344876 :   if (source->priv->ready_time_ns == ready_time_ns &&
    2059                 :      312215 :       source->priv->has_ready_time == has_ready_time)
    2060                 :             :     {
    2061                 :       29622 :       if (context)
    2062                 :             :         {
    2063                 :       19153 :           UNLOCK_CONTEXT (context);
    2064                 :       19153 :           g_main_context_unref (context);
    2065                 :         820 :         }
    2066                 :       29622 :       return;
    2067                 :             :     }
    2068                 :             : 
    2069                 :      315254 :   source->priv->ready_time_ns = ready_time_ns;
    2070                 :      315254 :   source->priv->has_ready_time = has_ready_time;
    2071                 :             : 
    2072                 :      158802 :   TRACE (GLIB_SOURCE_SET_READY_TIME (source, ready_time_ns));
    2073                 :             : 
    2074                 :      315254 :   if (context)
    2075                 :             :     {
    2076                 :             :       /* Quite likely that we need to change the timeout on the poll */
    2077                 :       87353 :       if (!SOURCE_BLOCKED (source))
    2078                 :       41920 :         g_wakeup_signal (context->wakeup);
    2079                 :       87353 :       UNLOCK_CONTEXT (context);
    2080                 :       87353 :       g_main_context_unref (context);
    2081                 :       43944 :     }
    2082                 :      157529 : }
    2083                 :             : 
    2084                 :             : /**
    2085                 :             :  * g_source_set_ready_time:
    2086                 :             :  * @source: a source
    2087                 :             :  * @ready_time: the monotonic time in microseconds at which the source will
    2088                 :             :  *   be ready; `0` for ‘immediately’, `-1` for ‘never’
    2089                 :             :  *
    2090                 :             :  * Sets a source to be dispatched when the given monotonic time is
    2091                 :             :  * reached (or passed).
    2092                 :             :  *
    2093                 :             :  * If the monotonic time is in the past (as it
    2094                 :             :  * always will be if @ready_time is `0`) then the source will be
    2095                 :             :  * dispatched immediately.
    2096                 :             :  *
    2097                 :             :  * If @ready_time is `-1` then the source is never woken up on the basis
    2098                 :             :  * of the passage of time.
    2099                 :             :  * Since GLib 2.90 [method@GLib.Source.clear_ready_time] should be used
    2100                 :             :  * instead for this purpose.
    2101                 :             :  *
    2102                 :             :  * Dispatching the source does not reset the ready time.  You should do
    2103                 :             :  * so yourself, from the source dispatch function.
    2104                 :             :  *
    2105                 :             :  * Note that if you have a pair of sources where the ready time of one
    2106                 :             :  * suggests that it will be delivered first but the priority for the
    2107                 :             :  * other suggests that it would be delivered first, and the ready time
    2108                 :             :  * for both sources is reached during the same main context iteration,
    2109                 :             :  * then the order of dispatch is undefined.
    2110                 :             :  *
    2111                 :             :  * It is a no-op to call this function on a [struct@GLib.Source] which has
    2112                 :             :  * already been destroyed with [method@GLib.Source.destroy].
    2113                 :             :  *
    2114                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    2115                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    2116                 :             :  *
    2117                 :             :  * Since: 2.36
    2118                 :             :  **/
    2119                 :             : void
    2120                 :      312857 : g_source_set_ready_time (GSource *source,
    2121                 :             :                          gint64   ready_time)
    2122                 :             : {
    2123                 :      312857 :   g_return_if_fail (source != NULL);
    2124                 :      312857 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    2125                 :             : 
    2126                 :      312857 :   if (ready_time == -1)
    2127                 :       67971 :     g_source_update_ready_time_internal (source, FALSE, 0);
    2128                 :             :   else
    2129                 :             :     {
    2130                 :             :       uint64_t ready_time_ns;
    2131                 :             : 
    2132                 :      244886 :       if (ready_time < 0)
    2133                 :           6 :         ready_time_ns = 0; /* backwards compat */
    2134                 :      244880 :       else if ((uint64_t) ready_time > UINT64_MAX / 1000)
    2135                 :           6 :         ready_time_ns = UINT64_MAX;
    2136                 :             :       else
    2137                 :      244874 :         ready_time_ns = (uint64_t) ready_time * 1000;
    2138                 :             : 
    2139                 :      244886 :       g_source_update_ready_time_internal (source, TRUE, ready_time_ns);
    2140                 :             :     }
    2141                 :      144934 : }
    2142                 :             : 
    2143                 :             : /**
    2144                 :             :  * g_source_set_ready_time_ns:
    2145                 :             :  * @source: a source
    2146                 :             :  * @ready_time: the monotonic time in nanoseconds at which the source will
    2147                 :             :  *   be ready; `0` for ‘immediately’
    2148                 :             :  *
    2149                 :             :  * Sets a source to be dispatched when the given monotonic time is
    2150                 :             :  * reached (or passed).
    2151                 :             :  *
    2152                 :             :  * If the monotonic time is in the past (as it
    2153                 :             :  * always will be if @ready_time is `0`) then the source will be
    2154                 :             :  * dispatched immediately.
    2155                 :             :  *
    2156                 :             :  * Dispatching the source does not reset the ready time.  You should do
    2157                 :             :  * so yourself, from the source dispatch function.
    2158                 :             :  *
    2159                 :             :  * To reset the ready time, use [method@GLib.Source.clear_ready_time].
    2160                 :             :  *
    2161                 :             :  * Note that if you have a pair of sources where the ready time of one
    2162                 :             :  * suggests that it will be delivered first but the priority for the
    2163                 :             :  * other suggests that it would be delivered first, and the ready time
    2164                 :             :  * for both sources is reached during the same main context iteration,
    2165                 :             :  * then the order of dispatch is undefined.
    2166                 :             :  *
    2167                 :             :  * It is a no-op to call this function on a [struct@GLib.Source] which has
    2168                 :             :  * already been destroyed with [method@GLib.Source.destroy].
    2169                 :             :  *
    2170                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    2171                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    2172                 :             :  *
    2173                 :             :  * Since: 2.90
    2174                 :             :  */
    2175                 :             : void
    2176                 :       31944 : g_source_set_ready_time_ns (GSource  *source,
    2177                 :             :                             uint64_t  ready_time)
    2178                 :             : {
    2179                 :       31944 :   g_return_if_fail (source != NULL);
    2180                 :       31944 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    2181                 :             : 
    2182                 :       31944 :   g_source_update_ready_time_internal (source, TRUE, ready_time);
    2183                 :       12565 : }
    2184                 :             : 
    2185                 :             : /**
    2186                 :             :  * g_source_clear_ready_time:
    2187                 :             :  * @source: a source
    2188                 :             :  * 
    2189                 :             :  * Unsets any previously set ready time.
    2190                 :             :  *
    2191                 :             :  * If the source does not have a ready time set, this function
    2192                 :             :  * does nothing.
    2193                 :             :  *
    2194                 :             :  * Since: 2.90
    2195                 :             :  */
    2196                 :             : void
    2197                 :          89 : g_source_clear_ready_time (GSource *source)
    2198                 :             : {
    2199                 :          89 :   g_return_if_fail (source != NULL);
    2200                 :             : 
    2201                 :          89 :   g_source_update_ready_time_internal (source, FALSE, 0);
    2202                 :          44 : }
    2203                 :             : 
    2204                 :             : /**
    2205                 :             :  * g_source_get_ready_time:
    2206                 :             :  * @source: a source
    2207                 :             :  *
    2208                 :             :  * Gets the ‘ready time’ of @source, as set by
    2209                 :             :  * [method@GLib.Source.set_ready_time].
    2210                 :             :  *
    2211                 :             :  * Any time before or equal to the current monotonic time (including zero)
    2212                 :             :  * is an indication that the source will fire immediately.
    2213                 :             :  *
    2214                 :             :  * Returns: the monotonic ready time, `-1` for ‘never’
    2215                 :             :  **/
    2216                 :             : gint64
    2217                 :       10588 : g_source_get_ready_time (GSource *source)
    2218                 :             : {
    2219                 :       10588 :   g_return_val_if_fail (source != NULL, -1);
    2220                 :       10588 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, -1);
    2221                 :             : 
    2222                 :       10588 :   if (source->priv->has_ready_time)
    2223                 :         300 :     return (MIN (source->priv->ready_time_ns, UINT64_MAX - 999) + 999) / 1000;
    2224                 :             :   else
    2225                 :       10288 :     return -1;
    2226                 :         419 : }
    2227                 :             : 
    2228                 :             : /**
    2229                 :             :  * g_source_get_ready_time_ns:
    2230                 :             :  * @source: a source
    2231                 :             :  * @ready_time: (optional) (out caller-allocates): Set to the ready time
    2232                 :             :  *   on success
    2233                 :             :  *
    2234                 :             :  * Gets the ‘ready time’ of @source, as set by
    2235                 :             :  * [method@GLib.Source.set_ready_time_ns]. If no ready time has been set
    2236                 :             :  * or it has been cleared via method@GLib.Source.clear_ready_time], this
    2237                 :             :  * function returns false.
    2238                 :             :  *
    2239                 :             :  * Any time before or equal to the current monotonic time (including zero)
    2240                 :             :  * is an indication that the source will fire immediately.
    2241                 :             :  *
    2242                 :             :  * Returns: true if the source has a ready time set.
    2243                 :             :  *
    2244                 :             :  * Since: 2.90
    2245                 :             :  **/
    2246                 :             : gboolean
    2247                 :         532 : g_source_get_ready_time_ns (GSource  *source,
    2248                 :             :                             uint64_t *ready_time)
    2249                 :             : {
    2250                 :         532 :   g_return_val_if_fail (source != NULL, -1);
    2251                 :         532 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, -1);
    2252                 :             : 
    2253                 :         532 :   if (source->priv->has_ready_time)
    2254                 :             :     {
    2255                 :         226 :       if (ready_time)
    2256                 :         226 :         *ready_time = source->priv->ready_time_ns;
    2257                 :         226 :       return TRUE;
    2258                 :             :     }
    2259                 :             :   else
    2260                 :             :     {
    2261                 :         306 :       if (ready_time)
    2262                 :         306 :         *ready_time = UINT64_MAX; /* defensive programming */
    2263                 :         306 :       return FALSE;
    2264                 :             :     }
    2265                 :         270 : }
    2266                 :             : 
    2267                 :             : /**
    2268                 :             :  * g_source_set_can_recurse:
    2269                 :             :  * @source: a source
    2270                 :             :  * @can_recurse: whether recursion is allowed for this source
    2271                 :             :  * 
    2272                 :             :  * Sets whether a source can be called recursively.
    2273                 :             :  *
    2274                 :             :  * If @can_recurse is true, then while the source is being dispatched then this
    2275                 :             :  * source will be processed normally. Otherwise, all processing of this
    2276                 :             :  * source is blocked until the dispatch function returns.
    2277                 :             :  **/
    2278                 :             : void
    2279                 :           2 : g_source_set_can_recurse (GSource  *source,
    2280                 :             :                           gboolean  can_recurse)
    2281                 :             : {
    2282                 :             :   GMainContext *context;
    2283                 :             :   
    2284                 :           2 :   g_return_if_fail (source != NULL);
    2285                 :           2 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    2286                 :             : 
    2287                 :           2 :   context = source_dup_main_context (source);
    2288                 :             : 
    2289                 :           2 :   if (context)
    2290                 :           0 :     LOCK_CONTEXT (context);
    2291                 :             :   
    2292                 :           2 :   if (can_recurse)
    2293                 :           2 :     g_atomic_int_or (&source->flags, G_SOURCE_CAN_RECURSE);
    2294                 :             :   else
    2295                 :           0 :     g_atomic_int_and (&source->flags, ~G_SOURCE_CAN_RECURSE);
    2296                 :             : 
    2297                 :           2 :   if (context)
    2298                 :             :     {
    2299                 :           0 :       UNLOCK_CONTEXT (context);
    2300                 :           0 :       g_main_context_unref (context);
    2301                 :           0 :     }
    2302                 :           1 : }
    2303                 :             : 
    2304                 :             : /**
    2305                 :             :  * g_source_get_can_recurse:
    2306                 :             :  * @source: a source
    2307                 :             :  * 
    2308                 :             :  * Checks whether a source is allowed to be called recursively.
    2309                 :             :  *
    2310                 :             :  * See [method@GLib.Source.set_can_recurse].
    2311                 :             :  * 
    2312                 :             :  * Returns: whether recursion is allowed
    2313                 :             :  **/
    2314                 :             : gboolean
    2315                 :           4 : g_source_get_can_recurse (GSource  *source)
    2316                 :             : {
    2317                 :           4 :   g_return_val_if_fail (source != NULL, FALSE);
    2318                 :           4 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, FALSE);
    2319                 :             : 
    2320                 :           4 :   return (g_atomic_int_get (&source->flags) & G_SOURCE_CAN_RECURSE) != 0;
    2321                 :           2 : }
    2322                 :             : 
    2323                 :             : static void
    2324                 :     1376571 : g_source_set_name_full (GSource    *source,
    2325                 :             :                         const char *name,
    2326                 :             :                         gboolean    is_static)
    2327                 :             : {
    2328                 :             :   GMainContext *context;
    2329                 :             : 
    2330                 :     1376571 :   g_return_if_fail (source != NULL);
    2331                 :     1376571 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    2332                 :             : 
    2333                 :     1376571 :   context = source_dup_main_context (source);
    2334                 :             : 
    2335                 :     1376571 :   if (context)
    2336                 :          12 :     LOCK_CONTEXT (context);
    2337                 :             : 
    2338                 :     1117741 :   TRACE (GLIB_SOURCE_SET_NAME (source, name));
    2339                 :             : 
    2340                 :             :   /* setting back to NULL is allowed, just because it's
    2341                 :             :    * weird if get_name can return NULL but you can't
    2342                 :             :    * set that.
    2343                 :             :    */
    2344                 :             : 
    2345                 :     1376571 :   if (!source->priv->static_name)
    2346                 :      958278 :     g_free (source->name);
    2347                 :             : 
    2348                 :     1376571 :   if (is_static)
    2349                 :     1296285 :     source->name = (char *)name;
    2350                 :             :   else
    2351                 :       80286 :     source->name = g_strdup (name);
    2352                 :             : 
    2353                 :     1376571 :   source->priv->static_name = is_static;
    2354                 :             : 
    2355                 :     1376571 :   if (context)
    2356                 :             :     {
    2357                 :          12 :       UNLOCK_CONTEXT (context);
    2358                 :          12 :       g_main_context_unref (context);
    2359                 :           6 :     }
    2360                 :      258830 : }
    2361                 :             : 
    2362                 :             : /**
    2363                 :             :  * g_source_set_name:
    2364                 :             :  * @source: a source
    2365                 :             :  * @name: debug name for the source
    2366                 :             :  *
    2367                 :             :  * Sets a name for the source, used in debugging and profiling.
    2368                 :             :  *
    2369                 :             :  * The name defaults to `NULL`.
    2370                 :             :  *
    2371                 :             :  * The source name should describe in a human-readable way
    2372                 :             :  * what the source does. For example, ‘X11 event queue’
    2373                 :             :  * or ‘GTK repaint idle handler’.
    2374                 :             :  *
    2375                 :             :  * It is permitted to call this function multiple times, but is not
    2376                 :             :  * recommended due to the potential performance impact.  For example,
    2377                 :             :  * one could change the name in the `check` function of a
    2378                 :             :  * [struct@GLib.SourceFuncs] to include details like the event type in the
    2379                 :             :  * source name.
    2380                 :             :  *
    2381                 :             :  * Use caution if changing the name while another thread may be
    2382                 :             :  * accessing it with [method@GLib.Source.get_name]; that function does not copy
    2383                 :             :  * the value, and changing the value will free it while the other thread
    2384                 :             :  * may be attempting to use it.
    2385                 :             :  *
    2386                 :             :  * Also see [method@GLib.Source.set_static_name].
    2387                 :             :  *
    2388                 :             :  * Since: 2.26
    2389                 :             :  **/
    2390                 :             : void
    2391                 :       80286 : g_source_set_name (GSource    *source,
    2392                 :             :                    const char *name)
    2393                 :             : {
    2394                 :       80286 :   g_source_set_name_full (source, name, FALSE);
    2395                 :       80286 : }
    2396                 :             : 
    2397                 :             : /**
    2398                 :             :  * g_source_set_static_name:
    2399                 :             :  * @source: a source
    2400                 :             :  * @name: debug name for the source
    2401                 :             :  *
    2402                 :             :  * A variant of [method@GLib.Source.set_name] that does not
    2403                 :             :  * duplicate the @name, and can only be used with
    2404                 :             :  * string literals.
    2405                 :             :  *
    2406                 :             :  * Since: 2.70
    2407                 :             :  */
    2408                 :             : void
    2409                 :     1296279 : g_source_set_static_name (GSource    *source,
    2410                 :             :                           const char *name)
    2411                 :             : {
    2412                 :     1296279 :   g_source_set_name_full (source, name, TRUE);
    2413                 :     1296279 : }
    2414                 :             : 
    2415                 :             : /**
    2416                 :             :  * g_source_get_name:
    2417                 :             :  * @source: a source
    2418                 :             :  *
    2419                 :             :  * Gets a name for the source, used in debugging and profiling.
    2420                 :             :  *
    2421                 :             :  * The
    2422                 :             :  * name may be `NULL` if it has never been set with [method@GLib.Source.set_name].
    2423                 :             :  *
    2424                 :             :  * Returns: (nullable): the name of the source
    2425                 :             :  * Since: 2.26
    2426                 :             :  **/
    2427                 :             : const char *
    2428                 :     5885353 : g_source_get_name (GSource *source)
    2429                 :             : {
    2430                 :     5885353 :   g_return_val_if_fail (source != NULL, NULL);
    2431                 :     5885353 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL);
    2432                 :             : 
    2433                 :     5885353 :   return source->name;
    2434                 :        2276 : }
    2435                 :             : 
    2436                 :             : /**
    2437                 :             :  * g_source_set_name_by_id:
    2438                 :             :  * @tag: a source ID
    2439                 :             :  * @name: debug name for the source
    2440                 :             :  *
    2441                 :             :  * Sets the name of a source using its ID.
    2442                 :             :  *
    2443                 :             :  * This is a convenience utility to set source names from the return
    2444                 :             :  * value of [func@GLib.idle_add], [func@GLib.timeout_add], etc.
    2445                 :             :  *
    2446                 :             :  * It is a programmer error to attempt to set the name of a non-existent
    2447                 :             :  * source.
    2448                 :             :  *
    2449                 :             :  * More specifically: source IDs can be reissued after a source has been
    2450                 :             :  * destroyed and therefore it is never valid to use this function with a
    2451                 :             :  * source ID which may have already been removed.  An example is when
    2452                 :             :  * scheduling an idle to run in another thread with [func@GLib.idle_add]: the
    2453                 :             :  * idle may already have run and been removed by the time this function
    2454                 :             :  * is called on its (now invalid) source ID.  This source ID may have
    2455                 :             :  * been reissued, leading to the operation being performed against the
    2456                 :             :  * wrong source.
    2457                 :             :  *
    2458                 :             :  * Since: 2.26
    2459                 :             :  **/
    2460                 :             : void
    2461                 :          12 : g_source_set_name_by_id (guint           tag,
    2462                 :             :                          const char     *name)
    2463                 :             : {
    2464                 :             :   GSource *source;
    2465                 :             : 
    2466                 :          12 :   g_return_if_fail (tag > 0);
    2467                 :             : 
    2468                 :          12 :   source = g_main_context_find_source_by_id (NULL, tag);
    2469                 :          12 :   if (source == NULL)
    2470                 :           0 :     return;
    2471                 :             : 
    2472                 :          12 :   g_source_set_name (source, name);
    2473                 :           6 : }
    2474                 :             : 
    2475                 :             : 
    2476                 :             : /**
    2477                 :             :  * g_source_ref:
    2478                 :             :  * @source: a source
    2479                 :             :  * 
    2480                 :             :  * Increases the reference count on a source by one.
    2481                 :             :  * 
    2482                 :             :  * Returns: @source
    2483                 :             :  **/
    2484                 :             : GSource *
    2485                 :     5795892 : g_source_ref (GSource *source)
    2486                 :             : {
    2487                 :             :   int old_ref G_GNUC_UNUSED;
    2488                 :     5795892 :   g_return_val_if_fail (source != NULL, NULL);
    2489                 :             : 
    2490                 :     5795892 :   old_ref = g_atomic_int_add (&source->ref_count, 1);
    2491                 :             :   /* We allow ref_count == 0 here to allow the dispose function to resurrect
    2492                 :             :    * the GSource if needed */
    2493                 :     5795892 :   g_return_val_if_fail (old_ref >= 0, NULL);
    2494                 :             : 
    2495                 :     5795892 :   return source;
    2496                 :     1071336 : }
    2497                 :             : 
    2498                 :             : /* g_source_unref() but possible to call within context lock
    2499                 :             :  */
    2500                 :             : static void
    2501                 :     6782442 : g_source_unref_internal (GSource      *source,
    2502                 :             :                          GMainContext *context,
    2503                 :             :                          gboolean      have_lock)
    2504                 :             : {
    2505                 :     6782442 :   gpointer old_cb_data = NULL;
    2506                 :     6782442 :   GSourceCallbackFuncs *old_cb_funcs = NULL;
    2507                 :             :   int old_ref;
    2508                 :             : 
    2509                 :    11506568 :   g_return_if_fail (source != NULL);
    2510                 :             : 
    2511                 :     6782442 :   old_ref = g_atomic_int_get (&source->ref_count);
    2512                 :             : 
    2513                 :         135 : retry_beginning:
    2514                 :     6782579 :   if (old_ref > 1)
    2515                 :             :     {
    2516                 :             :       /* We have many references. If we can decrement the ref counter, we are done. */
    2517                 :     6866898 :       if (!g_atomic_int_compare_and_exchange_full ((int *) &source->ref_count,
    2518                 :     2142640 :                                                    old_ref, old_ref - 1,
    2519                 :             :                                                    &old_ref))
    2520                 :         133 :         goto retry_beginning;
    2521                 :             : 
    2522                 :     5795445 :       return;
    2523                 :             :     }
    2524                 :             : 
    2525                 :      987001 :   g_return_if_fail (old_ref > 0);
    2526                 :             : 
    2527                 :      987001 :   if (!have_lock && context)
    2528                 :      230598 :     LOCK_CONTEXT (context);
    2529                 :             : 
    2530                 :             :   /* We are about to drop the last reference, there's not guarantee at this
    2531                 :             :    * point that another thread already changed the value at this point or
    2532                 :             :    * that is also entering the disposal phase, but there is no much we can do
    2533                 :             :    * and dropping the reference too early would be still risky since it could
    2534                 :             :    * lead to a preventive finalization.
    2535                 :             :    * So let's just get all the threads that reached this point to get in, while
    2536                 :             :    * the final check on whether is the case or not to continue with the
    2537                 :             :    * finalization will be done by a final unique atomic dec and test.
    2538                 :             :    */
    2539                 :      987001 :   if (old_ref == 1)
    2540                 :             :     {
    2541                 :             :       /* If there's a dispose function, call this first */
    2542                 :             :       GSourceDisposeFunc dispose_func;
    2543                 :             : 
    2544                 :      987419 :       if ((dispose_func = g_atomic_pointer_get (&source->priv->dispose)))
    2545                 :             :         {
    2546                 :      214414 :           if (context)
    2547                 :       14406 :             UNLOCK_CONTEXT (context);
    2548                 :      214414 :           dispose_func (source);
    2549                 :      214414 :           if (context)
    2550                 :       14406 :             LOCK_CONTEXT (context);
    2551                 :      100191 :         }
    2552                 :             : 
    2553                 :             :       /* At this point the source can have been revived by any of the threads
    2554                 :             :        * acting on it or it's really ready for being finalized.
    2555                 :             :        */
    2556                 :      987419 :       if (!g_atomic_int_compare_and_exchange_full ((int *) &source->ref_count,
    2557                 :             :                                                    1, 0, &old_ref))
    2558                 :             :         {
    2559                 :           4 :           if (!have_lock && context)
    2560                 :           0 :             UNLOCK_CONTEXT (context);
    2561                 :             : 
    2562                 :           4 :           goto retry_beginning;
    2563                 :             :         }
    2564                 :             : 
    2565                 :      719384 :       TRACE (GLIB_SOURCE_BEFORE_FREE (source, context,
    2566                 :             :                                       source->source_funcs->finalize));
    2567                 :             : 
    2568                 :      987415 :       old_cb_data = source->callback_data;
    2569                 :      987415 :       old_cb_funcs = source->callback_funcs;
    2570                 :             : 
    2571                 :      987415 :       source->callback_data = NULL;
    2572                 :      987415 :       source->callback_funcs = NULL;
    2573                 :             : 
    2574                 :      987415 :       if (context)
    2575                 :             :         {
    2576                 :      787132 :           if (!SOURCE_DESTROYED (source))
    2577                 :           0 :             g_warning (G_STRLOC ": ref_count == 0, but source was still attached to a context!");
    2578                 :      787132 :           source_remove_from_context (source, context);
    2579                 :             : 
    2580                 :      787132 :           g_hash_table_remove (context->sources, &source->source_id);
    2581                 :      167890 :         }
    2582                 :             : 
    2583                 :      987415 :       if (source->source_funcs->finalize)
    2584                 :             :         {
    2585                 :             :           gint old_ref_count;
    2586                 :             : 
    2587                 :             :           /* Temporarily increase the ref count again so that GSource methods
    2588                 :             :            * can be called from finalize(). */
    2589                 :       17917 :           g_atomic_int_inc (&source->ref_count);
    2590                 :       17917 :           if (context)
    2591                 :       17878 :             UNLOCK_CONTEXT (context);
    2592                 :       17917 :           source->source_funcs->finalize (source);
    2593                 :       17917 :           if (context)
    2594                 :       17878 :             LOCK_CONTEXT (context);
    2595                 :       17917 :           old_ref_count = g_atomic_int_add (&source->ref_count, -1);
    2596                 :       17917 :           g_warn_if_fail (old_ref_count == 1);
    2597                 :        1246 :         }
    2598                 :             : 
    2599                 :      987415 :       if (old_cb_funcs)
    2600                 :             :         {
    2601                 :             :           gint old_ref_count;
    2602                 :             : 
    2603                 :             :           /* Temporarily increase the ref count again so that GSource methods
    2604                 :             :            * can be called from callback_funcs.unref(). */
    2605                 :      200000 :           g_atomic_int_inc (&source->ref_count);
    2606                 :      200000 :           if (context)
    2607                 :           0 :             UNLOCK_CONTEXT (context);
    2608                 :             : 
    2609                 :      200000 :           old_cb_funcs->unref (old_cb_data);
    2610                 :             : 
    2611                 :      200000 :           if (context)
    2612                 :           0 :             LOCK_CONTEXT (context);
    2613                 :      200000 :           old_ref_count = g_atomic_int_add (&source->ref_count, -1);
    2614                 :      200000 :           g_warn_if_fail (old_ref_count == 1);
    2615                 :      100000 :         }
    2616                 :             : 
    2617                 :      987415 :       if (!source->priv->static_name)
    2618                 :      110441 :         g_free (source->name);
    2619                 :      987415 :       source->name = NULL;
    2620                 :             : 
    2621                 :      987415 :       g_slist_free (source->poll_fds);
    2622                 :      987415 :       source->poll_fds = NULL;
    2623                 :             : 
    2624                 :      987415 :       g_slist_free_full (source->priv->fds, g_free);
    2625                 :             : 
    2626                 :      987415 :       while (source->priv->child_sources)
    2627                 :             :         {
    2628                 :           0 :           GSource *child_source = source->priv->child_sources->data;
    2629                 :             : 
    2630                 :           0 :           source->priv->child_sources =
    2631                 :           0 :             g_slist_remove (source->priv->child_sources, child_source);
    2632                 :           0 :           child_source->priv->parent_source = NULL;
    2633                 :             : 
    2634                 :           0 :           g_source_unref_internal (child_source, context, TRUE);
    2635                 :             :         }
    2636                 :             : 
    2637                 :      987415 :       g_slice_free (GSourcePrivate, source->priv);
    2638                 :      987415 :       source->priv = NULL;
    2639                 :             : 
    2640                 :      987415 :       g_free (source);
    2641                 :      268031 :     }
    2642                 :             : 
    2643                 :      987833 :   if (!have_lock && context)
    2644                 :      230599 :     UNLOCK_CONTEXT (context);
    2645                 :     1339768 : }
    2646                 :             : 
    2647                 :             : /**
    2648                 :             :  * g_source_unref:
    2649                 :             :  * @source: a source
    2650                 :             :  *
    2651                 :             :  * Decreases the reference count of a source by one.
    2652                 :             :  * 
    2653                 :             :  * If the resulting reference count is zero the source and associated
    2654                 :             :  * memory will be destroyed.
    2655                 :             :  **/
    2656                 :             : void
    2657                 :     1186328 : g_source_unref (GSource *source)
    2658                 :             : {
    2659                 :             :   GMainContext *context;
    2660                 :             : 
    2661                 :     1186328 :   g_return_if_fail (source != NULL);
    2662                 :             :   /* refcount is checked inside g_source_unref_internal() */
    2663                 :             : 
    2664                 :     1186328 :   context = source_dup_main_context (source);
    2665                 :             : 
    2666                 :     1186328 :   g_source_unref_internal (source, context, FALSE);
    2667                 :             : 
    2668                 :     1186328 :   if (context)
    2669                 :      771143 :     g_main_context_unref (context);
    2670                 :      368147 : }
    2671                 :             : 
    2672                 :             : /**
    2673                 :             :  * g_main_context_find_source_by_id:
    2674                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    2675                 :             :  *   main context will be used)
    2676                 :             :  * @source_id: the source ID, as returned by [method@GLib.Source.get_id]
    2677                 :             :  *
    2678                 :             :  * Finds a [struct@GLib.Source] given a pair of context and ID.
    2679                 :             :  *
    2680                 :             :  * It is a programmer error to attempt to look up a non-existent source.
    2681                 :             :  *
    2682                 :             :  * More specifically: source IDs can be reissued after a source has been
    2683                 :             :  * destroyed and therefore it is never valid to use this function with a
    2684                 :             :  * source ID which may have already been removed.  An example is when
    2685                 :             :  * scheduling an idle to run in another thread with [func@GLib.idle_add]: the
    2686                 :             :  * idle may already have run and been removed by the time this function
    2687                 :             :  * is called on its (now invalid) source ID.  This source ID may have
    2688                 :             :  * been reissued, leading to the operation being performed against the
    2689                 :             :  * wrong source.
    2690                 :             :  *
    2691                 :             :  * Returns: (transfer none): the source
    2692                 :             :  **/
    2693                 :             : GSource *
    2694                 :      200469 : g_main_context_find_source_by_id (GMainContext *context,
    2695                 :             :                                   guint         source_id)
    2696                 :             : {
    2697                 :      200469 :   GSource *source = NULL;
    2698                 :             :   gconstpointer ptr;
    2699                 :             : 
    2700                 :      200469 :   g_return_val_if_fail (source_id > 0, NULL);
    2701                 :             : 
    2702                 :      200469 :   if (context == NULL)
    2703                 :         253 :     context = g_main_context_default ();
    2704                 :             : 
    2705                 :      200469 :   LOCK_CONTEXT (context);
    2706                 :      200469 :   ptr = g_hash_table_lookup (context->sources, &source_id);
    2707                 :      200469 :   if (ptr)
    2708                 :             :     {
    2709                 :      200459 :       source = G_CONTAINER_OF (ptr, GSource, source_id);
    2710                 :      200459 :       if (SOURCE_DESTROYED (source))
    2711                 :         108 :         source = NULL;
    2712                 :      100139 :     }
    2713                 :      200469 :   UNLOCK_CONTEXT (context);
    2714                 :             : 
    2715                 :      200469 :   return source;
    2716                 :      100144 : }
    2717                 :             : 
    2718                 :             : /**
    2719                 :             :  * g_main_context_find_source_by_funcs_user_data:
    2720                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    2721                 :             :  *   main context will be used).
    2722                 :             :  * @funcs: the @source_funcs passed to [ctor@GLib.Source.new]
    2723                 :             :  * @user_data: the user data from the callback
    2724                 :             :  *
    2725                 :             :  * Finds a source with the given source functions and user data.
    2726                 :             :  * 
    2727                 :             :  * If multiple sources exist with the same source function and user data,
    2728                 :             :  * the first one found will be returned.
    2729                 :             :  * 
    2730                 :             :  * Returns: (transfer none) (nullable): the source, if one was found,
    2731                 :             :  *   otherwise `NULL`
    2732                 :             :  **/
    2733                 :             : GSource *
    2734                 :           6 : g_main_context_find_source_by_funcs_user_data (GMainContext *context,
    2735                 :             :                                                GSourceFuncs *funcs,
    2736                 :             :                                                gpointer      user_data)
    2737                 :             : {
    2738                 :             :   GSourceIter iter;
    2739                 :             :   GSource *source;
    2740                 :             :   
    2741                 :           6 :   g_return_val_if_fail (funcs != NULL, NULL);
    2742                 :             : 
    2743                 :           6 :   if (context == NULL)
    2744                 :           4 :     context = g_main_context_default ();
    2745                 :             :   
    2746                 :           6 :   LOCK_CONTEXT (context);
    2747                 :             : 
    2748                 :           6 :   g_source_iter_init (&iter, context, FALSE);
    2749                 :           6 :   while (g_source_iter_next (&iter, &source))
    2750                 :             :     {
    2751                 :           4 :       if (!SOURCE_DESTROYED (source) &&
    2752                 :           4 :           source->source_funcs == funcs &&
    2753                 :           4 :           source->callback_funcs)
    2754                 :             :         {
    2755                 :             :           GSourceFunc callback;
    2756                 :             :           gpointer callback_data;
    2757                 :             : 
    2758                 :           4 :           source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
    2759                 :             :           
    2760                 :           4 :           if (callback_data == user_data)
    2761                 :           4 :             break;
    2762                 :           0 :         }
    2763                 :             :     }
    2764                 :           6 :   g_source_iter_clear (&iter);
    2765                 :             : 
    2766                 :           6 :   UNLOCK_CONTEXT (context);
    2767                 :             : 
    2768                 :           6 :   return source;
    2769                 :           3 : }
    2770                 :             : 
    2771                 :             : /**
    2772                 :             :  * g_main_context_find_source_by_user_data:
    2773                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    2774                 :             :  *   main context will be used)
    2775                 :             :  * @user_data: the user_data for the callback
    2776                 :             :  * 
    2777                 :             :  * Finds a source with the given user data for the callback.
    2778                 :             :  *
    2779                 :             :  * If multiple sources exist with the same user data, the first
    2780                 :             :  * one found will be returned.
    2781                 :             :  * 
    2782                 :             :  * Returns: (transfer none) (nullable): the source, if one was found,
    2783                 :             :  *   otherwise `NULL`
    2784                 :             :  **/
    2785                 :             : GSource *
    2786                 :           6 : g_main_context_find_source_by_user_data (GMainContext *context,
    2787                 :             :                                          gpointer      user_data)
    2788                 :             : {
    2789                 :             :   GSourceIter iter;
    2790                 :             :   GSource *source;
    2791                 :             :   
    2792                 :           6 :   if (context == NULL)
    2793                 :           4 :     context = g_main_context_default ();
    2794                 :             :   
    2795                 :           6 :   LOCK_CONTEXT (context);
    2796                 :             : 
    2797                 :           6 :   g_source_iter_init (&iter, context, FALSE);
    2798                 :           6 :   while (g_source_iter_next (&iter, &source))
    2799                 :             :     {
    2800                 :           2 :       if (!SOURCE_DESTROYED (source) &&
    2801                 :           2 :           source->callback_funcs)
    2802                 :             :         {
    2803                 :             :           GSourceFunc callback;
    2804                 :           2 :           gpointer callback_data = NULL;
    2805                 :             : 
    2806                 :           2 :           source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
    2807                 :             : 
    2808                 :           2 :           if (callback_data == user_data)
    2809                 :           2 :             break;
    2810                 :           0 :         }
    2811                 :             :     }
    2812                 :           6 :   g_source_iter_clear (&iter);
    2813                 :             : 
    2814                 :           6 :   UNLOCK_CONTEXT (context);
    2815                 :             : 
    2816                 :           6 :   return source;
    2817                 :             : }
    2818                 :             : 
    2819                 :             : /**
    2820                 :             :  * g_source_remove:
    2821                 :             :  * @tag: the ID of the source to remove.
    2822                 :             :  *
    2823                 :             :  * Removes the source with the given ID from the default main context.
    2824                 :             :  *
    2825                 :             :  * You must
    2826                 :             :  * use [method@GLib.Source.destroy] for sources added to a non-default main context.
    2827                 :             :  *
    2828                 :             :  * The ID of a [struct@GLib.Source] is given by [method@GLib.Source.get_id], or will be
    2829                 :             :  * returned by the functions [method@GLib.Source.attach], [func@GLib.idle_add],
    2830                 :             :  * [func@GLib.idle_add_full], [func@GLib.timeout_add],
    2831                 :             :  * [func@GLib.timeout_add_full], [func@GLib.child_watch_add],
    2832                 :             :  * [func@GLib.child_watch_add_full], [func@GLib.io_add_watch], and
    2833                 :             :  * [func@GLib.io_add_watch_full].
    2834                 :             :  *
    2835                 :             :  * It is a programmer error to attempt to remove a non-existent source.
    2836                 :             :  *
    2837                 :             :  * More specifically: source IDs can be reissued after a source has been
    2838                 :             :  * destroyed and therefore it is never valid to use this function with a
    2839                 :             :  * source ID which may have already been removed.  An example is when
    2840                 :             :  * scheduling an idle to run in another thread with [func@GLib.idle_add]: the
    2841                 :             :  * idle may already have run and been removed by the time this function
    2842                 :             :  * is called on its (now invalid) source ID.  This source ID may have
    2843                 :             :  * been reissued, leading to the operation being performed against the
    2844                 :             :  * wrong source.
    2845                 :             :  *
    2846                 :             :  * Returns: true if the source was found and removed, false otherwise
    2847                 :             :  **/
    2848                 :             : gboolean
    2849                 :         235 : g_source_remove (guint tag)
    2850                 :             : {
    2851                 :             :   GSource *source;
    2852                 :             : 
    2853                 :         235 :   g_return_val_if_fail (tag > 0, FALSE);
    2854                 :             : 
    2855                 :         235 :   source = g_main_context_find_source_by_id (NULL, tag);
    2856                 :         235 :   if (source)
    2857                 :         225 :     g_source_destroy (source);
    2858                 :             :   else
    2859                 :          10 :     g_critical ("Source ID %u was not found when attempting to remove it", tag);
    2860                 :             : 
    2861                 :         235 :   return source != NULL;
    2862                 :          28 : }
    2863                 :             : 
    2864                 :             : /**
    2865                 :             :  * g_source_remove_by_user_data:
    2866                 :             :  * @user_data: the user_data for the callback
    2867                 :             :  * 
    2868                 :             :  * Removes a source from the default main loop context given the user
    2869                 :             :  * data for the callback.
    2870                 :             :  * 
    2871                 :             :  * If multiple sources exist with the same user data, only one will be destroyed.
    2872                 :             :  *
    2873                 :             :  * Returns: true if a source was found and removed, false otherwise
    2874                 :             :  **/
    2875                 :             : gboolean
    2876                 :           4 : g_source_remove_by_user_data (gpointer user_data)
    2877                 :             : {
    2878                 :             :   GSource *source;
    2879                 :             :   
    2880                 :           4 :   source = g_main_context_find_source_by_user_data (NULL, user_data);
    2881                 :           4 :   if (source)
    2882                 :             :     {
    2883                 :           2 :       g_source_destroy (source);
    2884                 :           2 :       return TRUE;
    2885                 :             :     }
    2886                 :             :   else
    2887                 :           2 :     return FALSE;
    2888                 :           2 : }
    2889                 :             : 
    2890                 :             : /**
    2891                 :             :  * g_source_remove_by_funcs_user_data:
    2892                 :             :  * @funcs: the @source_funcs passed to [ctor@GLib.Source.new]
    2893                 :             :  * @user_data: the user data for the callback
    2894                 :             :  * 
    2895                 :             :  * Removes a source from the default main loop context given the
    2896                 :             :  * source functions and user data.
    2897                 :             :  * 
    2898                 :             :  * If multiple sources exist with the same source functions and user data, only
    2899                 :             :  * one will be destroyed.
    2900                 :             :  *
    2901                 :             :  * Returns: true if a source was found and removed, false otherwise
    2902                 :             :  **/
    2903                 :             : gboolean
    2904                 :           4 : g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
    2905                 :             :                                     gpointer      user_data)
    2906                 :             : {
    2907                 :             :   GSource *source;
    2908                 :             : 
    2909                 :           4 :   g_return_val_if_fail (funcs != NULL, FALSE);
    2910                 :             : 
    2911                 :           4 :   source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
    2912                 :           4 :   if (source)
    2913                 :             :     {
    2914                 :           4 :       g_source_destroy (source);
    2915                 :           4 :       return TRUE;
    2916                 :             :     }
    2917                 :             :   else
    2918                 :           0 :     return FALSE;
    2919                 :           2 : }
    2920                 :             : 
    2921                 :             : /**
    2922                 :             :  * g_clear_handle_id: (skip)
    2923                 :             :  * @tag_ptr: (not nullable): a pointer to the handler ID
    2924                 :             :  * @clear_func: (not nullable): the function to call to clear the handler
    2925                 :             :  *
    2926                 :             :  * Clears a numeric handler, such as a [struct@GLib.Source] ID.
    2927                 :             :  *
    2928                 :             :  * The @tag_ptr must be a valid pointer to the variable holding the handler.
    2929                 :             :  *
    2930                 :             :  * If the ID is zero then this function does nothing.
    2931                 :             :  * Otherwise, @clear_func is called with the ID as a parameter, and the tag is
    2932                 :             :  * set to zero.
    2933                 :             :  *
    2934                 :             :  * A macro is also included that allows this function to be used without
    2935                 :             :  * pointer casts.
    2936                 :             :  *
    2937                 :             :  * Since: 2.56
    2938                 :             :  */
    2939                 :             : #undef g_clear_handle_id
    2940                 :             : void
    2941                 :           0 : g_clear_handle_id (guint            *tag_ptr,
    2942                 :             :                    GClearHandleFunc  clear_func)
    2943                 :             : {
    2944                 :             :   guint _handle_id;
    2945                 :             : 
    2946                 :           0 :   _handle_id = *tag_ptr;
    2947                 :           0 :   if (_handle_id > 0)
    2948                 :             :     {
    2949                 :           0 :       *tag_ptr = 0;
    2950                 :           0 :       clear_func (_handle_id);
    2951                 :           0 :     }
    2952                 :           0 : }
    2953                 :             : 
    2954                 :             : #ifdef G_OS_UNIX
    2955                 :             : /**
    2956                 :             :  * g_source_add_unix_fd:
    2957                 :             :  * @source: a source
    2958                 :             :  * @fd: the file descriptor to monitor
    2959                 :             :  * @events: an event mask
    2960                 :             :  *
    2961                 :             :  * Monitors @fd for the IO events in @events.
    2962                 :             :  *
    2963                 :             :  * The tag returned by this function can be used to remove or modify the
    2964                 :             :  * monitoring of the @fd using [method@GLib.Source.remove_unix_fd] or
    2965                 :             :  * [method@GLib.Source.modify_unix_fd].
    2966                 :             :  *
    2967                 :             :  * It is not necessary to remove the file descriptor before destroying the
    2968                 :             :  * source; it will be cleaned up automatically.
    2969                 :             :  *
    2970                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    2971                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    2972                 :             :  *
    2973                 :             :  * As the name suggests, this function is not available on Windows.
    2974                 :             :  *
    2975                 :             :  * Returns: (not nullable): an opaque tag
    2976                 :             :  * Since: 2.36
    2977                 :             :  **/
    2978                 :             : gpointer
    2979                 :       14449 : g_source_add_unix_fd (GSource      *source,
    2980                 :             :                       gint          fd,
    2981                 :             :                       GIOCondition  events)
    2982                 :             : {
    2983                 :             :   GMainContext *context;
    2984                 :             :   GPollFD *poll_fd;
    2985                 :             : 
    2986                 :       14449 :   g_return_val_if_fail (source != NULL, NULL);
    2987                 :       14449 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, NULL);
    2988                 :       14449 :   g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
    2989                 :             : 
    2990                 :       14449 :   poll_fd = g_new (GPollFD, 1);
    2991                 :       14449 :   poll_fd->fd = fd;
    2992                 :       14449 :   poll_fd->events = events;
    2993                 :       14449 :   poll_fd->revents = 0;
    2994                 :             : 
    2995                 :       14449 :   context = source_dup_main_context (source);
    2996                 :             : 
    2997                 :       14449 :   if (context)
    2998                 :           2 :     LOCK_CONTEXT (context);
    2999                 :             : 
    3000                 :       14449 :   source->priv->fds = g_slist_prepend (source->priv->fds, poll_fd);
    3001                 :             : 
    3002                 :       14449 :   if (context)
    3003                 :             :     {
    3004                 :           2 :       if (!SOURCE_BLOCKED (source))
    3005                 :           2 :         g_main_context_add_poll_unlocked (context, source->priority, poll_fd);
    3006                 :           2 :       UNLOCK_CONTEXT (context);
    3007                 :           2 :       g_main_context_unref (context);
    3008                 :             :     }
    3009                 :             : 
    3010                 :       14449 :   return poll_fd;
    3011                 :             : }
    3012                 :             : 
    3013                 :             : /**
    3014                 :             :  * g_source_modify_unix_fd:
    3015                 :             :  * @source: a source
    3016                 :             :  * @tag: (not nullable): the tag from [method@GLib.Source.add_unix_fd]
    3017                 :             :  * @new_events: the new event mask to watch
    3018                 :             :  *
    3019                 :             :  * Updates the event mask to watch for the file descriptor identified by @tag.
    3020                 :             :  *
    3021                 :             :  * The @tag is the tag returned from [method@GLib.Source.add_unix_fd].
    3022                 :             :  *
    3023                 :             :  * If you want to remove a file descriptor, don’t set its event mask to zero.
    3024                 :             :  * Instead, call [method@GLib.Source.remove_unix_fd].
    3025                 :             :  *
    3026                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    3027                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    3028                 :             :  *
    3029                 :             :  * As the name suggests, this function is not available on Windows.
    3030                 :             :  *
    3031                 :             :  * Since: 2.36
    3032                 :             :  **/
    3033                 :             : void
    3034                 :          18 : g_source_modify_unix_fd (GSource      *source,
    3035                 :             :                          gpointer      tag,
    3036                 :             :                          GIOCondition  new_events)
    3037                 :             : {
    3038                 :             :   GMainContext *context;
    3039                 :             :   GPollFD *poll_fd;
    3040                 :             : 
    3041                 :          18 :   g_return_if_fail (source != NULL);
    3042                 :          18 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    3043                 :          18 :   g_return_if_fail (g_slist_find (source->priv->fds, tag));
    3044                 :             : 
    3045                 :          18 :   context = source_dup_main_context (source);
    3046                 :          18 :   poll_fd = tag;
    3047                 :             : 
    3048                 :          18 :   poll_fd->events = new_events;
    3049                 :             : 
    3050                 :          18 :   if (context)
    3051                 :             :     {
    3052                 :          18 :       g_main_context_wakeup (context);
    3053                 :          18 :       g_main_context_unref (context);
    3054                 :             :     }
    3055                 :             : }
    3056                 :             : 
    3057                 :             : /**
    3058                 :             :  * g_source_remove_unix_fd:
    3059                 :             :  * @source: a source
    3060                 :             :  * @tag: (not nullable): the tag from [method@GLib.Source.add_unix_fd]
    3061                 :             :  *
    3062                 :             :  * Reverses the effect of a previous call to [method@GLib.Source.add_unix_fd].
    3063                 :             :  *
    3064                 :             :  * You only need to call this if you want to remove a file descriptor from being
    3065                 :             :  * watched while keeping the same source around.  In the normal case you
    3066                 :             :  * will just want to destroy the source.
    3067                 :             :  *
    3068                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    3069                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    3070                 :             :  *
    3071                 :             :  * As the name suggests, this function is not available on Windows.
    3072                 :             :  *
    3073                 :             :  * Since: 2.36
    3074                 :             :  **/
    3075                 :             : void
    3076                 :          34 : g_source_remove_unix_fd (GSource  *source,
    3077                 :             :                          gpointer  tag)
    3078                 :             : {
    3079                 :             :   GMainContext *context;
    3080                 :             :   GPollFD *poll_fd;
    3081                 :             : 
    3082                 :          34 :   g_return_if_fail (source != NULL);
    3083                 :          34 :   g_return_if_fail (g_atomic_int_get (&source->ref_count) > 0);
    3084                 :          34 :   g_return_if_fail (g_slist_find (source->priv->fds, tag));
    3085                 :             : 
    3086                 :          34 :   context = source_dup_main_context (source);
    3087                 :          34 :   poll_fd = tag;
    3088                 :             : 
    3089                 :          34 :   if (context)
    3090                 :          34 :     LOCK_CONTEXT (context);
    3091                 :             : 
    3092                 :          34 :   source->priv->fds = g_slist_remove (source->priv->fds, poll_fd);
    3093                 :             : 
    3094                 :          34 :   if (context)
    3095                 :             :     {
    3096                 :          34 :       if (!SOURCE_BLOCKED (source))
    3097                 :           2 :         g_main_context_remove_poll_unlocked (context, poll_fd);
    3098                 :             : 
    3099                 :          34 :       UNLOCK_CONTEXT (context);
    3100                 :          34 :       g_main_context_unref (context);
    3101                 :             :     }
    3102                 :             : 
    3103                 :          34 :   g_free (poll_fd);
    3104                 :             : }
    3105                 :             : 
    3106                 :             : /**
    3107                 :             :  * g_source_query_unix_fd:
    3108                 :             :  * @source: a source
    3109                 :             :  * @tag: (not nullable): the tag from [method@GLib.Source.add_unix_fd]
    3110                 :             :  *
    3111                 :             :  * Queries the events reported for the file descriptor corresponding to @tag
    3112                 :             :  * on @source during the last poll.
    3113                 :             :  *
    3114                 :             :  * The return value of this function is only defined when the function
    3115                 :             :  * is called from the check or dispatch functions for @source.
    3116                 :             :  *
    3117                 :             :  * This API is only intended to be used by implementations of [struct@GLib.Source].
    3118                 :             :  * Do not call this API on a [struct@GLib.Source] that you did not create.
    3119                 :             :  *
    3120                 :             :  * As the name suggests, this function is not available on Windows.
    3121                 :             :  *
    3122                 :             :  * Returns: the conditions reported on the file descriptor
    3123                 :             :  * Since: 2.36
    3124                 :             :  **/
    3125                 :             : GIOCondition
    3126                 :      276774 : g_source_query_unix_fd (GSource  *source,
    3127                 :             :                         gpointer  tag)
    3128                 :             : {
    3129                 :             :   GPollFD *poll_fd;
    3130                 :             : 
    3131                 :      276774 :   g_return_val_if_fail (source != NULL, 0);
    3132                 :      276774 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0);
    3133                 :      276774 :   g_return_val_if_fail (g_slist_find (source->priv->fds, tag), 0);
    3134                 :             : 
    3135                 :      276774 :   poll_fd = tag;
    3136                 :             : 
    3137                 :      276774 :   return poll_fd->revents;
    3138                 :             : }
    3139                 :             : #endif /* G_OS_UNIX */
    3140                 :             : 
    3141                 :             : /**
    3142                 :             :  * g_get_current_time:
    3143                 :             :  * @result: [struct@GLib.TimeVal] structure in which to store current time
    3144                 :             :  *
    3145                 :             :  * Queries the system wall-clock time.
    3146                 :             :  *
    3147                 :             :  * This is equivalent to the UNIX [`gettimeofday()`](man:gettimeofday(2))
    3148                 :             :  * function, but portable.
    3149                 :             :  *
    3150                 :             :  * You may find [func@GLib.get_real_time] to be more convenient.
    3151                 :             :  *
    3152                 :             :  * Deprecated: 2.62: [struct@GLib.TimeVal] is not year-2038-safe. Use
    3153                 :             :  *    [func@GLib.get_real_time] instead.
    3154                 :             :  **/
    3155                 :             : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
    3156                 :             : void
    3157                 :          17 : g_get_current_time (GTimeVal *result)
    3158                 :             : {
    3159                 :             :   gint64 tv;
    3160                 :             : 
    3161                 :          17 :   g_return_if_fail (result != NULL);
    3162                 :             : 
    3163                 :          17 :   tv = g_get_real_time ();
    3164                 :             : 
    3165                 :          17 :   result->tv_sec = tv / 1000000;
    3166                 :          17 :   result->tv_usec = tv % 1000000;
    3167                 :           8 : }
    3168                 :             : G_GNUC_END_IGNORE_DEPRECATIONS
    3169                 :             : 
    3170                 :             : /**
    3171                 :             :  * g_get_real_time:
    3172                 :             :  *
    3173                 :             :  * Queries the system wall-clock time.
    3174                 :             :  *
    3175                 :             :  * This is equivalent to the UNIX [`gettimeofday()`](man:gettimeofday(2))
    3176                 :             :  * function, but portable.
    3177                 :             :  *
    3178                 :             :  * You should only use this call if you are actually interested in the real
    3179                 :             :  * wall-clock time. [func@GLib.get_monotonic_time] is probably more useful for
    3180                 :             :  * measuring intervals.
    3181                 :             :  *
    3182                 :             :  * Returns: the number of microseconds since
    3183                 :             :  *   [January 1, 1970 UTC](https://en.wikipedia.org/wiki/Unix_time)
    3184                 :             :  * Since: 2.28
    3185                 :             :  **/
    3186                 :             : gint64
    3187                 :        3483 : g_get_real_time (void)
    3188                 :             : {
    3189                 :             : #ifndef G_OS_WIN32
    3190                 :             :   struct timeval r;
    3191                 :             : 
    3192                 :             :   /* this is required on alpha, there the timeval structs are ints
    3193                 :             :    * not longs and a cast only would fail horribly */
    3194                 :        2358 :   gettimeofday (&r, NULL);
    3195                 :             : 
    3196                 :        2358 :   return (((gint64) r.tv_sec) * 1000000) + r.tv_usec;
    3197                 :             : #else
    3198                 :             :   FILETIME ft;
    3199                 :             :   guint64 time64;
    3200                 :             : 
    3201                 :        1125 :   GetSystemTimeAsFileTime (&ft);
    3202                 :        1125 :   memmove (&time64, &ft, sizeof (FILETIME));
    3203                 :             : 
    3204                 :             :   /* Convert from 100s of nanoseconds since 1601-01-01
    3205                 :             :    * to Unix epoch. This is Y2038 safe.
    3206                 :             :    */
    3207                 :        1125 :   time64 -= G_GINT64_CONSTANT (116444736000000000);
    3208                 :        1125 :   time64 /= 10;
    3209                 :             : 
    3210                 :        1125 :   return time64;
    3211                 :             : #endif
    3212                 :             : }
    3213                 :             : 
    3214                 :             : /**
    3215                 :             :  * g_get_monotonic_time:
    3216                 :             :  *
    3217                 :             :  * Queries the system monotonic time in microseconds.
    3218                 :             :  *
    3219                 :             :  * The monotonic clock will always increase and doesn’t suffer
    3220                 :             :  * discontinuities when the user (or NTP) changes the system time.  It
    3221                 :             :  * may or may not continue to tick during times where the machine is
    3222                 :             :  * suspended.
    3223                 :             :  *
    3224                 :             :  * We try to use the clock that corresponds as closely as possible to
    3225                 :             :  * the passage of time as measured by system calls such as
    3226                 :             :  * [`poll()`](man:poll(2)) but it
    3227                 :             :  * may not always be possible to do this.
    3228                 :             :  *
    3229                 :             :  * A more accurate version of this function exists.
    3230                 :             :  * [func@GLib.get_monotonic_time_ns] returns the time in nanoseconds.
    3231                 :             :  *
    3232                 :             :  * Returns: the monotonic time, in microseconds
    3233                 :             :  * Since: 2.28
    3234                 :             :  **/
    3235                 :             : /**
    3236                 :             :  * g_get_monotonic_time_ns:
    3237                 :             :  *
    3238                 :             :  * Queries the system monotonic time in nanoseconds.
    3239                 :             :  *
    3240                 :             :  * The monotonic clock will always increase and doesn’t suffer
    3241                 :             :  * discontinuities when the user (or NTP) changes the system time.  It
    3242                 :             :  * may or may not continue to tick during times where the machine is
    3243                 :             :  * suspended.
    3244                 :             :  *
    3245                 :             :  * We try to use the clock that corresponds as closely as possible to
    3246                 :             :  * the passage of time as measured by system calls such as
    3247                 :             :  * [`poll()`](man:poll(2)) but it
    3248                 :             :  * may not always be possible to do this.
    3249                 :             :  *
    3250                 :             :  * Another version of this function exists.
    3251                 :             :  * [func@GLib.get_monotonic_time] returns the time in microseconds.
    3252                 :             :  * If you want to support older GLib versions, it is an alternative.
    3253                 :             :  *
    3254                 :             :  * Returns: the monotonic time, in nanoseconds
    3255                 :             :  * Since: 2.88
    3256                 :             :  **/
    3257                 :             : #if defined (G_OS_WIN32)
    3258                 :             : /* NOTE:
    3259                 :             :  * time_usec = ticks_since_boot * nsec_per_sec / ticks_per_sec
    3260                 :             :  *
    3261                 :             :  * Doing (ticks_since_boot * nsec_per_sec) before the division can overflow 64 bits
    3262                 :             :  * (ticks_since_boot  / ticks_per_sec) and then multiply would not be accurate enough.
    3263                 :             :  * So for now we calculate (nsec_per_sec / ticks_per_sec) and use floating point
    3264                 :             :  */
    3265                 :             : static double g_monotonic_nsec_per_tick = 0;
    3266                 :             : 
    3267                 :             : void
    3268                 :        1223 : g_clock_win32_init (void)
    3269                 :             : {
    3270                 :             :   LARGE_INTEGER freq;
    3271                 :             : 
    3272                 :        1223 :   if (!QueryPerformanceFrequency (&freq) || freq.QuadPart == 0)
    3273                 :             :     {
    3274                 :             :       /* The documentation says that this should never happen */
    3275                 :             :       g_assert_not_reached ();
    3276                 :           0 :       return;
    3277                 :             :     }
    3278                 :             : 
    3279                 :        1223 :   g_monotonic_nsec_per_tick = (double) G_NSEC_PER_SEC / freq.QuadPart;
    3280                 :        1223 : }
    3281                 :             : 
    3282                 :             : uint64_t
    3283                 :      275525 : g_get_monotonic_time_ns (void)
    3284                 :             : {
    3285                 :      275525 :   if (G_LIKELY (g_monotonic_nsec_per_tick != 0))
    3286                 :             :     {
    3287                 :             :       LARGE_INTEGER ticks;
    3288                 :             : 
    3289                 :      275525 :       if (QueryPerformanceCounter (&ticks))
    3290                 :      275525 :         return (uint64_t) (ticks.QuadPart * g_monotonic_nsec_per_tick);
    3291                 :             : 
    3292                 :           0 :       g_warning ("QueryPerformanceCounter Failed (%lu)", GetLastError ());
    3293                 :           0 :       g_monotonic_nsec_per_tick = 0;
    3294                 :           0 :     }
    3295                 :             : 
    3296                 :           0 :   return 0;
    3297                 :      275525 : }
    3298                 :             : #elif defined(HAVE_MACH_MACH_TIME_H) /* Mac OS */
    3299                 :             : uint64_t
    3300                 :             : g_get_monotonic_time_ns (void)
    3301                 :             : {
    3302                 :             :   mach_timebase_info_data_t timebase_info;
    3303                 :             :   uint64_t val;
    3304                 :             : 
    3305                 :             :   /* we get nanoseconds from mach_absolute_time() using timebase_info */
    3306                 :             :   mach_timebase_info (&timebase_info);
    3307                 :             :   val = mach_absolute_time ();
    3308                 :             : 
    3309                 :             :   if (timebase_info.numer != timebase_info.denom)
    3310                 :             :     {
    3311                 :             : #ifdef HAVE_UINT128_T
    3312                 :             :       val = ((__uint128_t) val * (__uint128_t) timebase_info.numer) / timebase_info.denom;
    3313                 :             : #else
    3314                 :             :       uint64_t t_high, t_low;
    3315                 :             :       uint64_t result_high, result_low;
    3316                 :             : 
    3317                 :             :       /* 64 bit x 32 bit / 32 bit with 96-bit intermediate 
    3318                 :             :        * algorithm lifted from qemu */
    3319                 :             :       t_low = (val & 0xffffffffLL) * (uint64_t) timebase_info.numer;
    3320                 :             :       t_high = (val >> 32) * (uint64_t) timebase_info.numer;
    3321                 :             :       t_high += (t_low >> 32);
    3322                 :             :       result_high = t_high / (uint64_t) timebase_info.denom;
    3323                 :             :       result_low = (((t_high % (uint64_t) timebase_info.denom) << 32) +
    3324                 :             :                     (t_low & 0xffffffff)) /
    3325                 :             :                    (uint64_t) timebase_info.denom;
    3326                 :             :       val = ((result_high << 32) | result_low);
    3327                 :             : #endif
    3328                 :             :     }
    3329                 :             : 
    3330                 :             :   return val;
    3331                 :             : }
    3332                 :             : #else
    3333                 :             : uint64_t
    3334                 :    17097995 : g_get_monotonic_time_ns (void)
    3335                 :             : {
    3336                 :             :   struct timespec ts;
    3337                 :             :   int result;
    3338                 :             : 
    3339                 :    17097995 :   result = clock_gettime (CLOCK_MONOTONIC, &ts);
    3340                 :             : 
    3341                 :    17097995 :   if G_UNLIKELY (result != 0)
    3342                 :           0 :     g_error ("GLib requires working CLOCK_MONOTONIC");
    3343                 :             : 
    3344                 :    17097995 :   return (((uint64_t) ts.tv_sec) * G_NSEC_PER_SEC) + ts.tv_nsec;
    3345                 :             : }
    3346                 :             : #endif
    3347                 :             : 
    3348                 :             : gint64
    3349                 :     9253704 : g_get_monotonic_time (void)
    3350                 :             : {
    3351                 :     9253704 :   return g_get_monotonic_time_ns () / 1000;
    3352                 :             : }
    3353                 :             : 
    3354                 :             : static void
    3355                 :        1217 : g_main_dispatch_free (gpointer dispatch)
    3356                 :             : {
    3357                 :        1217 :   g_free (dispatch);
    3358                 :        1217 : }
    3359                 :             : 
    3360                 :             : /* Running the main loop */
    3361                 :             : 
    3362                 :             : static GMainDispatch *
    3363                 :     1090093 : get_dispatch (void)
    3364                 :             : {
    3365                 :             :   static GPrivate depth_private = G_PRIVATE_INIT (g_main_dispatch_free);
    3366                 :             :   GMainDispatch *dispatch;
    3367                 :             : 
    3368                 :     1090093 :   dispatch = g_private_get (&depth_private);
    3369                 :             : 
    3370                 :     1090093 :   if (!dispatch)
    3371                 :        1892 :     dispatch = g_private_set_alloc0 (&depth_private, sizeof (GMainDispatch));
    3372                 :             : 
    3373                 :     1090093 :   return dispatch;
    3374                 :             : }
    3375                 :             : 
    3376                 :             : /**
    3377                 :             :  * g_main_depth:
    3378                 :             :  *
    3379                 :             :  * Returns the depth of the stack of calls to
    3380                 :             :  * [method@GLib.MainContext.dispatch] on any #GMainContext in the current thread.
    3381                 :             :  *
    3382                 :             :  * That is, when called from the top level, it gives `0`. When
    3383                 :             :  * called from within a callback from [method@GLib.MainContext.iteration]
    3384                 :             :  * (or [method@GLib.MainLoop.run], etc.) it returns `1`. When called from within
    3385                 :             :  * a callback to a recursive call to [method@GLib.MainContext.iteration],
    3386                 :             :  * it returns `2`. And so forth.
    3387                 :             :  *
    3388                 :             :  * This function is useful in a situation like the following:
    3389                 :             :  * Imagine an extremely simple ‘garbage collected’ system.
    3390                 :             :  *
    3391                 :             :  * ```c
    3392                 :             :  * static GList *free_list;
    3393                 :             :  * 
    3394                 :             :  * gpointer
    3395                 :             :  * allocate_memory (gsize size)
    3396                 :             :  * { 
    3397                 :             :  *   gpointer result = g_malloc (size);
    3398                 :             :  *   free_list = g_list_prepend (free_list, result);
    3399                 :             :  *   return result;
    3400                 :             :  * }
    3401                 :             :  * 
    3402                 :             :  * void
    3403                 :             :  * free_allocated_memory (void)
    3404                 :             :  * {
    3405                 :             :  *   GList *l;
    3406                 :             :  *   for (l = free_list; l; l = l->next);
    3407                 :             :  *     g_free (l->data);
    3408                 :             :  *   g_list_free (free_list);
    3409                 :             :  *   free_list = NULL;
    3410                 :             :  *  }
    3411                 :             :  * 
    3412                 :             :  * [...]
    3413                 :             :  * 
    3414                 :             :  * while (TRUE); 
    3415                 :             :  *  {
    3416                 :             :  *    g_main_context_iteration (NULL, TRUE);
    3417                 :             :  *    free_allocated_memory();
    3418                 :             :  *   }
    3419                 :             :  * ```
    3420                 :             :  *
    3421                 :             :  * This works from an application, however, if you want to do the same
    3422                 :             :  * thing from a library, it gets more difficult, since you no longer
    3423                 :             :  * control the main loop. You might think you can simply use an idle
    3424                 :             :  * function to make the call to `free_allocated_memory()`, but that
    3425                 :             :  * doesn’t work, since the idle function could be called from a
    3426                 :             :  * recursive callback. This can be fixed by using [func@GLib.main_depth]
    3427                 :             :  *
    3428                 :             :  * ```c
    3429                 :             :  * gpointer
    3430                 :             :  * allocate_memory (gsize size)
    3431                 :             :  * { 
    3432                 :             :  *   FreeListBlock *block = g_new (FreeListBlock, 1);
    3433                 :             :  *   block->mem = g_malloc (size);
    3434                 :             :  *   block->depth = g_main_depth ();   
    3435                 :             :  *   free_list = g_list_prepend (free_list, block);
    3436                 :             :  *   return block->mem;
    3437                 :             :  * }
    3438                 :             :  * 
    3439                 :             :  * void
    3440                 :             :  * free_allocated_memory (void)
    3441                 :             :  * {
    3442                 :             :  *   GList *l;
    3443                 :             :  *   
    3444                 :             :  *   int depth = g_main_depth ();
    3445                 :             :  *   for (l = free_list; l; );
    3446                 :             :  *     {
    3447                 :             :  *       GList *next = l->next;
    3448                 :             :  *       FreeListBlock *block = l->data;
    3449                 :             :  *       if (block->depth > depth)
    3450                 :             :  *         {
    3451                 :             :  *           g_free (block->mem);
    3452                 :             :  *           g_free (block);
    3453                 :             :  *           free_list = g_list_delete_link (free_list, l);
    3454                 :             :  *         }
    3455                 :             :  *               
    3456                 :             :  *       l = next;
    3457                 :             :  *     }
    3458                 :             :  *   }
    3459                 :             :  * ```
    3460                 :             :  *
    3461                 :             :  * There is a temptation to use [func@GLib.main_depth] to solve
    3462                 :             :  * problems with reentrancy. For instance, while waiting for data
    3463                 :             :  * to be received from the network in response to a menu item,
    3464                 :             :  * the menu item might be selected again. It might seem that
    3465                 :             :  * one could make the menu item’s callback return immediately
    3466                 :             :  * and do nothing if [func@GLib.main_depth] returns a value greater than 1.
    3467                 :             :  * However, this should be avoided since the user then sees selecting
    3468                 :             :  * the menu item do nothing. Furthermore, you’ll find yourself adding
    3469                 :             :  * these checks all over your code, since there are doubtless many,
    3470                 :             :  * many things that the user could do. Instead, you can use the
    3471                 :             :  * following techniques:
    3472                 :             :  *
    3473                 :             :  * 1. Use `gtk_widget_set_sensitive()` or modal dialogs to prevent
    3474                 :             :  *    the user from interacting with elements while the main
    3475                 :             :  *    loop is recursing.
    3476                 :             :  * 
    3477                 :             :  * 2. Avoid main loop recursion in situations where you can’t handle
    3478                 :             :  *    arbitrary  callbacks. Instead, structure your code so that you
    3479                 :             :  *    simply return to the main loop and then get called again when
    3480                 :             :  *    there is more work to do.
    3481                 :             :  * 
    3482                 :             :  * Returns: the main loop recursion level in the current thread
    3483                 :             :  */
    3484                 :             : int
    3485                 :           2 : g_main_depth (void)
    3486                 :             : {
    3487                 :           2 :   GMainDispatch *dispatch = get_dispatch ();
    3488                 :           2 :   return dispatch->depth;
    3489                 :             : }
    3490                 :             : 
    3491                 :             : /**
    3492                 :             :  * g_main_current_source:
    3493                 :             :  *
    3494                 :             :  * Returns the currently firing source for this thread.
    3495                 :             :  * 
    3496                 :             :  * Returns: (transfer none) (nullable): the currently firing source, or `NULL`
    3497                 :             :  *   if none is firing
    3498                 :             :  * Since: 2.12
    3499                 :             :  */
    3500                 :             : GSource *
    3501                 :      241425 : g_main_current_source (void)
    3502                 :             : {
    3503                 :      241425 :   GMainDispatch *dispatch = get_dispatch ();
    3504                 :      241425 :   return dispatch->source;
    3505                 :             : }
    3506                 :             : 
    3507                 :             : /**
    3508                 :             :  * g_source_is_destroyed:
    3509                 :             :  * @source: a source
    3510                 :             :  *
    3511                 :             :  * Returns whether @source has been destroyed.
    3512                 :             :  *
    3513                 :             :  * This is important when you operate upon your objects 
    3514                 :             :  * from within idle handlers, but may have freed the object 
    3515                 :             :  * before the dispatch of your idle handler.
    3516                 :             :  *
    3517                 :             :  * ```c
    3518                 :             :  * static gboolean 
    3519                 :             :  * idle_callback (gpointer data)
    3520                 :             :  * {
    3521                 :             :  *   SomeWidget *self = data;
    3522                 :             :  *    
    3523                 :             :  *   g_mutex_lock (&self->idle_id_mutex);
    3524                 :             :  *   // do stuff with self
    3525                 :             :  *   self->idle_id = 0;
    3526                 :             :  *   g_mutex_unlock (&self->idle_id_mutex);
    3527                 :             :  *    
    3528                 :             :  *   return G_SOURCE_REMOVE;
    3529                 :             :  * }
    3530                 :             :  *  
    3531                 :             :  * static void 
    3532                 :             :  * some_widget_do_stuff_later (SomeWidget *self)
    3533                 :             :  * {
    3534                 :             :  *   g_mutex_lock (&self->idle_id_mutex);
    3535                 :             :  *   self->idle_id = g_idle_add (idle_callback, self);
    3536                 :             :  *   g_mutex_unlock (&self->idle_id_mutex);
    3537                 :             :  * }
    3538                 :             :  *  
    3539                 :             :  * static void
    3540                 :             :  * some_widget_init (SomeWidget *self)
    3541                 :             :  * {
    3542                 :             :  *   g_mutex_init (&self->idle_id_mutex);
    3543                 :             :  *
    3544                 :             :  *   // ...
    3545                 :             :  * }
    3546                 :             :  *
    3547                 :             :  * static void 
    3548                 :             :  * some_widget_finalize (GObject *object)
    3549                 :             :  * {
    3550                 :             :  *   SomeWidget *self = SOME_WIDGET (object);
    3551                 :             :  *    
    3552                 :             :  *   if (self->idle_id)
    3553                 :             :  *     g_source_remove (self->idle_id);
    3554                 :             :  *    
    3555                 :             :  *   g_mutex_clear (&self->idle_id_mutex);
    3556                 :             :  *
    3557                 :             :  *   G_OBJECT_CLASS (parent_class)->finalize (object);
    3558                 :             :  * }
    3559                 :             :  * ```
    3560                 :             :  *
    3561                 :             :  * This will fail in a multi-threaded application if the 
    3562                 :             :  * widget is destroyed before the idle handler fires due 
    3563                 :             :  * to the use after free in the callback. A solution, to 
    3564                 :             :  * this particular problem, is to check to if the source
    3565                 :             :  * has already been destroy within the callback.
    3566                 :             :  *
    3567                 :             :  * ```c
    3568                 :             :  * static gboolean 
    3569                 :             :  * idle_callback (gpointer data)
    3570                 :             :  * {
    3571                 :             :  *   SomeWidget *self = data;
    3572                 :             :  *   
    3573                 :             :  *   g_mutex_lock (&self->idle_id_mutex);
    3574                 :             :  *   if (!g_source_is_destroyed (g_main_current_source ()))
    3575                 :             :  *     {
    3576                 :             :  *       // do stuff with self
    3577                 :             :  *     }
    3578                 :             :  *   g_mutex_unlock (&self->idle_id_mutex);
    3579                 :             :  *   
    3580                 :             :  *   return FALSE;
    3581                 :             :  * }
    3582                 :             :  * ```
    3583                 :             :  *
    3584                 :             :  * Calls to this function from a thread other than the one acquired by the
    3585                 :             :  * [struct@GLib.MainContext] the [struct@GLib.Source] is attached to are typically
    3586                 :             :  * redundant, as the source could be destroyed immediately after this function
    3587                 :             :  * returns. However, once a source is destroyed it cannot be un-destroyed, so
    3588                 :             :  * this function can be used for opportunistic checks from any thread.
    3589                 :             :  *
    3590                 :             :  * Returns: true if the source has been destroyed, false otherwise
    3591                 :             :  * Since: 2.12
    3592                 :             :  */
    3593                 :             : gboolean
    3594                 :        2371 : g_source_is_destroyed (GSource *source)
    3595                 :             : {
    3596                 :        2371 :   g_return_val_if_fail (source != NULL, TRUE);
    3597                 :        2371 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, TRUE);
    3598                 :        2371 :   return SOURCE_DESTROYED (source);
    3599                 :          76 : }
    3600                 :             : 
    3601                 :             : /* Temporarily remove all this source's file descriptors from the
    3602                 :             :  * poll(), so that if data comes available for one of the file descriptors
    3603                 :             :  * we don't continually spin in the poll()
    3604                 :             :  */
    3605                 :             : /* HOLDS: source->context's lock */
    3606                 :             : static void
    3607                 :      918770 : block_source (GSource      *source,
    3608                 :             :               GMainContext *context)
    3609                 :             : {
    3610                 :             :   GSList *tmp_list;
    3611                 :             : 
    3612                 :      918770 :   g_return_if_fail (!SOURCE_BLOCKED (source));
    3613                 :             : 
    3614                 :      918770 :   g_atomic_int_or (&source->flags, G_SOURCE_BLOCKED);
    3615                 :             : 
    3616                 :      918770 :   if (context)
    3617                 :             :     {
    3618                 :      918776 :       tmp_list = source->poll_fds;
    3619                 :      922802 :       while (tmp_list)
    3620                 :             :         {
    3621                 :        4026 :           g_main_context_remove_poll_unlocked (context, tmp_list->data);
    3622                 :        4026 :           tmp_list = tmp_list->next;
    3623                 :             :         }
    3624                 :             : 
    3625                 :     1200027 :       for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    3626                 :      281251 :         g_main_context_remove_poll_unlocked (context, tmp_list->data);
    3627                 :      241405 :     }
    3628                 :             : 
    3629                 :      918786 :   if (source->priv && source->priv->child_sources)
    3630                 :             :     {
    3631                 :       14633 :       tmp_list = source->priv->child_sources;
    3632                 :       33572 :       while (tmp_list)
    3633                 :             :         {
    3634                 :       18939 :           block_source (tmp_list->data, context);
    3635                 :       18939 :           tmp_list = tmp_list->next;
    3636                 :             :         }
    3637                 :         173 :     }
    3638                 :      241413 : }
    3639                 :             : 
    3640                 :             : /* HOLDS: source->context's lock */
    3641                 :             : static void
    3642                 :      917477 : unblock_source (GSource      *source,
    3643                 :             :                 GMainContext *context)
    3644                 :             : {
    3645                 :             :   GSList *tmp_list;
    3646                 :             : 
    3647                 :      917477 :   g_return_if_fail (SOURCE_BLOCKED (source)); /* Source already unblocked */
    3648                 :      917477 :   g_return_if_fail (!SOURCE_DESTROYED (source));
    3649                 :             : 
    3650                 :      917477 :   g_atomic_int_and (&source->flags, ~G_SOURCE_BLOCKED);
    3651                 :             : 
    3652                 :      917477 :   tmp_list = source->poll_fds;
    3653                 :      921459 :   while (tmp_list)
    3654                 :             :     {
    3655                 :        3982 :       g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
    3656                 :        3982 :       tmp_list = tmp_list->next;
    3657                 :             :     }
    3658                 :             : 
    3659                 :     1198437 :   for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    3660                 :      280960 :     g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
    3661                 :             : 
    3662                 :      917477 :   if (source->priv && source->priv->child_sources)
    3663                 :             :     {
    3664                 :       14344 :       tmp_list = source->priv->child_sources;
    3665                 :       32994 :       while (tmp_list)
    3666                 :             :         {
    3667                 :       18650 :           unblock_source (tmp_list->data, context);
    3668                 :       18650 :           tmp_list = tmp_list->next;
    3669                 :             :         }
    3670                 :         138 :     }
    3671                 :      241272 : }
    3672                 :             : 
    3673                 :             : /* HOLDS: context's lock */
    3674                 :             : static void
    3675                 :      848664 : g_main_dispatch (GMainContext *context)
    3676                 :             : {
    3677                 :      848664 :   GMainDispatch *current = get_dispatch ();
    3678                 :             :   guint i;
    3679                 :             : 
    3680                 :     1748520 :   for (i = 0; i < context->pending_dispatches->len; i++)
    3681                 :             :     {
    3682                 :      899866 :       GSource *source = context->pending_dispatches->pdata[i];
    3683                 :             : 
    3684                 :      899866 :       context->pending_dispatches->pdata[i] = NULL;
    3685                 :      899866 :       g_assert (source);
    3686                 :             : 
    3687                 :      899866 :       g_atomic_int_and (&source->flags, ~G_SOURCE_READY);
    3688                 :             : 
    3689                 :      899866 :       if (!SOURCE_DESTROYED (source))
    3690                 :             :         {
    3691                 :             :           gboolean was_in_call;
    3692                 :      899835 :           gpointer user_data = NULL;
    3693                 :      899835 :           GSourceFunc callback = NULL;
    3694                 :             :           GSourceCallbackFuncs *cb_funcs;
    3695                 :             :           gpointer cb_data;
    3696                 :             :           gboolean need_destroy;
    3697                 :             : 
    3698                 :             :           gboolean (*dispatch) (GSource *,
    3699                 :             :                                 GSourceFunc,
    3700                 :             :                                 gpointer);
    3701                 :             :           GSource *prev_source;
    3702                 :             :           gint64 begin_time_nsec G_GNUC_UNUSED;
    3703                 :             : 
    3704                 :      899835 :           dispatch = source->source_funcs->dispatch;
    3705                 :      899835 :           cb_funcs = source->callback_funcs;
    3706                 :      899835 :           cb_data = source->callback_data;
    3707                 :             : 
    3708                 :      899835 :           if (cb_funcs)
    3709                 :      858565 :             cb_funcs->ref (cb_data);
    3710                 :             :           
    3711                 :      899835 :           if ((g_atomic_int_get (&source->flags) & G_SOURCE_CAN_RECURSE) == 0)
    3712                 :      899825 :             block_source (source, context);
    3713                 :             :           
    3714                 :     1141059 :           was_in_call = g_atomic_int_or (&source->flags,
    3715                 :      899835 :                                          (GSourceFlags) G_HOOK_FLAG_IN_CALL) &
    3716                 :             :                                          G_HOOK_FLAG_IN_CALL;
    3717                 :             : 
    3718                 :      899835 :           if (cb_funcs)
    3719                 :      858564 :             cb_funcs->get (cb_data, source, &callback, &user_data);
    3720                 :             : 
    3721                 :      899835 :           UNLOCK_CONTEXT (context);
    3722                 :             : 
    3723                 :             :           /* These operations are safe because 'current' is thread-local
    3724                 :             :            * and not modified from anywhere but this function.
    3725                 :             :            */
    3726                 :      899835 :           prev_source = current->source;
    3727                 :      899835 :           current->source = source;
    3728                 :      899835 :           current->depth++;
    3729                 :             : 
    3730                 :      899835 :           begin_time_nsec = G_TRACE_CURRENT_TIME;
    3731                 :             : 
    3732                 :      658611 :           TRACE (GLIB_MAIN_BEFORE_DISPATCH (g_source_get_name (source), source,
    3733                 :             :                                             dispatch, callback, user_data));
    3734                 :      899835 :           need_destroy = !(* dispatch) (source, callback, user_data);
    3735                 :      658605 :           TRACE (GLIB_MAIN_AFTER_DISPATCH (g_source_get_name (source), source,
    3736                 :             :                                            dispatch, need_destroy));
    3737                 :             : 
    3738                 :     1708164 :           g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec,
    3739                 :             :                         "GLib", "GSource.dispatch",
    3740                 :             :                         "%s ⇒ %s",
    3741                 :     1049559 :                         (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)",
    3742                 :             :                         need_destroy ? "destroy" : "keep");
    3743                 :             : 
    3744                 :      899829 :           current->source = prev_source;
    3745                 :      899829 :           current->depth--;
    3746                 :             : 
    3747                 :      899829 :           if (cb_funcs)
    3748                 :      858555 :             cb_funcs->unref (cb_data);
    3749                 :             : 
    3750                 :      899829 :           LOCK_CONTEXT (context);
    3751                 :             :           
    3752                 :      899829 :           if (!was_in_call)
    3753                 :      899826 :             g_atomic_int_and (&source->flags, ~G_HOOK_FLAG_IN_CALL);
    3754                 :             : 
    3755                 :      899829 :           if (SOURCE_BLOCKED (source) && !SOURCE_DESTROYED (source))
    3756                 :      898828 :             unblock_source (source, context);
    3757                 :             :           
    3758                 :             :           /* Note: this depends on the fact that we can't switch
    3759                 :             :            * sources from one main context to another
    3760                 :             :            */
    3761                 :      899829 :           if (need_destroy && !SOURCE_DESTROYED (source))
    3762                 :             :             {
    3763                 :      337089 :               g_assert (source->context == context);
    3764                 :      337089 :               g_source_destroy_internal (source, context, TRUE);
    3765                 :        5349 :             }
    3766                 :      241220 :         }
    3767                 :             :       
    3768                 :      899856 :       g_source_unref_internal (source, context, TRUE);
    3769                 :      241231 :     }
    3770                 :             : 
    3771                 :      848654 :   g_ptr_array_set_size (context->pending_dispatches, 0);
    3772                 :      848654 : }
    3773                 :             : 
    3774                 :             : /**
    3775                 :             :  * g_main_context_acquire:
    3776                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    3777                 :             :  *   main context will be used)
    3778                 :             :  * 
    3779                 :             :  * Tries to become the owner of the specified context.
    3780                 :             :  *
    3781                 :             :  * If some other thread is the owner of the context,
    3782                 :             :  * returns false immediately. Ownership is properly
    3783                 :             :  * recursive: the owner can require ownership again
    3784                 :             :  * and will release ownership when [method@GLib.MainContext.release]
    3785                 :             :  * is called as many times as [method@GLib.MainContext.acquire].
    3786                 :             :  *
    3787                 :             :  * You must be the owner of a context before you
    3788                 :             :  * can call [method@GLib.MainContext.prepare], [method@GLib.MainContext.query],
    3789                 :             :  * [method@GLib.MainContext.check], [method@GLib.MainContext.dispatch],
    3790                 :             :  * [method@GLib.MainContext.release].
    3791                 :             :  *
    3792                 :             :  * Since 2.76 @context can be `NULL` to use the global-default
    3793                 :             :  * main context.
    3794                 :             :  * 
    3795                 :             :  * Returns: true if this thread is now the owner of @context, false otherwise
    3796                 :             :  **/
    3797                 :             : gboolean 
    3798                 :      125801 : g_main_context_acquire (GMainContext *context)
    3799                 :             : {
    3800                 :      125801 :   gboolean result = FALSE;
    3801                 :             : 
    3802                 :      125801 :   if (context == NULL)
    3803                 :           4 :     context = g_main_context_default ();
    3804                 :             :   
    3805                 :      125801 :   LOCK_CONTEXT (context);
    3806                 :             : 
    3807                 :      125801 :   result = g_main_context_acquire_unlocked (context);
    3808                 :             : 
    3809                 :      125801 :   UNLOCK_CONTEXT (context); 
    3810                 :             : 
    3811                 :      125801 :   return result;
    3812                 :             : }
    3813                 :             : 
    3814                 :             : static gboolean
    3815                 :     1206978 : g_main_context_acquire_unlocked (GMainContext *context)
    3816                 :             : {
    3817                 :     1206978 :   GThread *self = G_THREAD_SELF;
    3818                 :             : 
    3819                 :     1206978 :   if (!context->owner)
    3820                 :             :     {
    3821                 :      427267 :       context->owner = self;
    3822                 :      427267 :       g_assert (context->owner_count == 0);
    3823                 :      425072 :       TRACE (GLIB_MAIN_CONTEXT_ACQUIRE (context, TRUE  /* success */));
    3824                 :        2195 :     }
    3825                 :             : 
    3826                 :     1206978 :   if (context->owner == self)
    3827                 :             :     {
    3828                 :     1174579 :       context->owner_count++;
    3829                 :     1174579 :       return TRUE;
    3830                 :             :     }
    3831                 :             :   else
    3832                 :             :     {
    3833                 :       32387 :       TRACE (GLIB_MAIN_CONTEXT_ACQUIRE (context, FALSE  /* failure */));
    3834                 :       32399 :       return FALSE;
    3835                 :             :     }
    3836                 :      250487 : }
    3837                 :             : 
    3838                 :             : /**
    3839                 :             :  * g_main_context_release:
    3840                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    3841                 :             :  *   main context will be used)
    3842                 :             :  * 
    3843                 :             :  * Releases ownership of a context previously acquired by this thread
    3844                 :             :  * with [method@GLib.MainContext.acquire].
    3845                 :             :  *
    3846                 :             :  * If the context was acquired multiple
    3847                 :             :  * times, the ownership will be released only when [method@GLib.MainContext.release]
    3848                 :             :  * is called as many times as it was acquired.
    3849                 :             :  *
    3850                 :             :  * You must have successfully acquired the context with
    3851                 :             :  * [method@GLib.MainContext.acquire] before you may call this function.
    3852                 :             :  **/
    3853                 :             : void
    3854                 :      125658 : g_main_context_release (GMainContext *context)
    3855                 :             : {
    3856                 :      125658 :   if (context == NULL)
    3857                 :       37137 :     context = g_main_context_default ();
    3858                 :             : 
    3859                 :      125658 :   LOCK_CONTEXT (context);
    3860                 :      125658 :   g_main_context_release_unlocked (context);
    3861                 :      125658 :   UNLOCK_CONTEXT (context);
    3862                 :      125658 : }
    3863                 :             : 
    3864                 :             : static void
    3865                 :     1173988 : g_main_context_release_unlocked (GMainContext *context)
    3866                 :             : {
    3867                 :             :   /* NOTE: We should also have the following assert here:
    3868                 :             :    * g_return_if_fail (context->owner == G_THREAD_SELF);
    3869                 :             :    * However, this breaks NetworkManager, which has been (non-compliantly but
    3870                 :             :    * apparently safely) releasing a #GMainContext from a thread which didn’t
    3871                 :             :    * acquire it.
    3872                 :             :    * Breaking that would be quite disruptive, so we won’t do that now. However,
    3873                 :             :    * GLib reserves the right to add that assertion in future, if doing so would
    3874                 :             :    * allow for optimisations or refactorings. By that point, NetworkManager will
    3875                 :             :    * have to have reworked its use of #GMainContext.
    3876                 :             :    *
    3877                 :             :    * See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3513
    3878                 :             :    */
    3879                 :     1173988 :   g_return_if_fail (context->owner_count > 0);
    3880                 :             : 
    3881                 :     1173988 :   context->owner_count--;
    3882                 :     1173988 :   if (context->owner_count == 0)
    3883                 :             :     {
    3884                 :      424729 :       TRACE (GLIB_MAIN_CONTEXT_RELEASE (context));
    3885                 :             : 
    3886                 :      426889 :       context->owner = NULL;
    3887                 :             : 
    3888                 :      426889 :       if (context->waiters)
    3889                 :             :         {
    3890                 :           1 :           GMainWaiter *waiter = context->waiters->data;
    3891                 :           1 :           gboolean loop_internal_waiter = (waiter->mutex == &context->mutex);
    3892                 :           1 :           context->waiters = g_slist_delete_link (context->waiters,
    3893                 :           0 :                                                   context->waiters);
    3894                 :           1 :           if (!loop_internal_waiter)
    3895                 :           0 :             g_mutex_lock (waiter->mutex);
    3896                 :             :           
    3897                 :           1 :           g_cond_signal (waiter->cond);
    3898                 :             :           
    3899                 :           1 :           if (!loop_internal_waiter)
    3900                 :           0 :             g_mutex_unlock (waiter->mutex);
    3901                 :           0 :         }
    3902                 :        2160 :     }
    3903                 :      250433 : }
    3904                 :             : 
    3905                 :             : static gboolean
    3906                 :           1 : g_main_context_wait_internal (GMainContext *context,
    3907                 :             :                               GCond        *cond,
    3908                 :             :                               GMutex       *mutex)
    3909                 :             : {
    3910                 :           1 :   gboolean result = FALSE;
    3911                 :           1 :   GThread *self = G_THREAD_SELF;
    3912                 :             :   gboolean loop_internal_waiter;
    3913                 :             :   
    3914                 :           1 :   loop_internal_waiter = (mutex == &context->mutex);
    3915                 :             :   
    3916                 :           1 :   if (!loop_internal_waiter)
    3917                 :           0 :     LOCK_CONTEXT (context);
    3918                 :             : 
    3919                 :           1 :   if (context->owner && context->owner != self)
    3920                 :             :     {
    3921                 :             :       GMainWaiter waiter;
    3922                 :             : 
    3923                 :           1 :       waiter.cond = cond;
    3924                 :           1 :       waiter.mutex = mutex;
    3925                 :             : 
    3926                 :           1 :       context->waiters = g_slist_append (context->waiters, &waiter);
    3927                 :             :       
    3928                 :           1 :       if (!loop_internal_waiter)
    3929                 :           0 :         UNLOCK_CONTEXT (context);
    3930                 :           1 :       g_cond_wait (cond, mutex);
    3931                 :           1 :       if (!loop_internal_waiter)
    3932                 :           0 :         LOCK_CONTEXT (context);
    3933                 :             : 
    3934                 :           1 :       context->waiters = g_slist_remove (context->waiters, &waiter);
    3935                 :           0 :     }
    3936                 :             : 
    3937                 :           1 :   if (!context->owner)
    3938                 :             :     {
    3939                 :           1 :       context->owner = self;
    3940                 :           1 :       g_assert (context->owner_count == 0);
    3941                 :           0 :     }
    3942                 :             : 
    3943                 :           1 :   if (context->owner == self)
    3944                 :             :     {
    3945                 :           1 :       context->owner_count++;
    3946                 :           1 :       result = TRUE;
    3947                 :           0 :     }
    3948                 :             : 
    3949                 :           1 :   if (!loop_internal_waiter)
    3950                 :           0 :     UNLOCK_CONTEXT (context); 
    3951                 :             :   
    3952                 :           1 :   return result;
    3953                 :             : }
    3954                 :             : 
    3955                 :             : /**
    3956                 :             :  * g_main_context_wait:
    3957                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    3958                 :             :  *   main context will be used)
    3959                 :             :  * @cond: a condition variable
    3960                 :             :  * @mutex: a mutex, currently held
    3961                 :             :  *
    3962                 :             :  * Tries to become the owner of the specified context, and waits on @cond if
    3963                 :             :  * another thread is the owner.
    3964                 :             :  *
    3965                 :             :  * This is the same as [method@GLib.MainContext.acquire], but if another thread
    3966                 :             :  * is the owner, atomically drop @mutex and wait on @cond until
    3967                 :             :  * that owner releases ownership or until @cond is signaled, then
    3968                 :             :  * try again (once) to become the owner.
    3969                 :             :  *
    3970                 :             :  * Returns: true if this thread is now the owner of @context, false otherwise
    3971                 :             :  * Deprecated: 2.58: Use [method@GLib.MainContext.is_owner] and separate
    3972                 :             :  *    locking instead.
    3973                 :             :  */
    3974                 :             : gboolean
    3975                 :           0 : g_main_context_wait (GMainContext *context,
    3976                 :             :                      GCond        *cond,
    3977                 :             :                      GMutex       *mutex)
    3978                 :             : {
    3979                 :           0 :   if (context == NULL)
    3980                 :           0 :     context = g_main_context_default ();
    3981                 :             : 
    3982                 :           0 :   if (G_UNLIKELY (cond != &context->cond || mutex != &context->mutex))
    3983                 :             :     {
    3984                 :             :       static gboolean warned;
    3985                 :             : 
    3986                 :           0 :       if (!warned)
    3987                 :             :         {
    3988                 :           0 :           g_critical ("WARNING!! g_main_context_wait() will be removed in a future release.  "
    3989                 :             :                       "If you see this message, please file a bug immediately.");
    3990                 :           0 :           warned = TRUE;
    3991                 :           0 :         }
    3992                 :           0 :     }
    3993                 :             : 
    3994                 :           0 :   return g_main_context_wait_internal (context, cond, mutex);
    3995                 :             : }
    3996                 :             : 
    3997                 :             : /**
    3998                 :             :  * g_main_context_prepare:
    3999                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4000                 :             :  *   main context will be used)
    4001                 :             :  * @priority: (out) (optional): location to store priority of highest priority
    4002                 :             :  *   source already ready
    4003                 :             :  *
    4004                 :             :  * Prepares to poll sources within a main loop.
    4005                 :             :  *
    4006                 :             :  * The resulting information
    4007                 :             :  * for polling is determined by calling [method@GLib.MainContext.query].
    4008                 :             :  *
    4009                 :             :  * You must have successfully acquired the context with
    4010                 :             :  * [method@GLib.MainContext.acquire] before you may call this function.
    4011                 :             :  *
    4012                 :             :  * Returns: true if some source is ready to be dispatched prior to polling,
    4013                 :             :  *   false otherwise
    4014                 :             :  **/
    4015                 :             : gboolean
    4016                 :          26 : g_main_context_prepare (GMainContext *context,
    4017                 :             :                         gint         *priority)
    4018                 :             : {
    4019                 :             :   gboolean ready;
    4020                 :             : 
    4021                 :          26 :   if (context == NULL)
    4022                 :           2 :     context = g_main_context_default ();
    4023                 :             :   
    4024                 :          26 :   LOCK_CONTEXT (context);
    4025                 :             : 
    4026                 :          26 :   ready = g_main_context_prepare_unlocked (context, priority);
    4027                 :             : 
    4028                 :          26 :   UNLOCK_CONTEXT (context);
    4029                 :             :   
    4030                 :          26 :   return ready;
    4031                 :             : }
    4032                 :             : 
    4033                 :             : static inline int
    4034                 :      244147 : round_timeout_to_msec (gboolean has_timeout,
    4035                 :             :                        uint64_t timeout_ns)
    4036                 :             : {
    4037                 :             :   /* We need to round to milliseconds from our internal nanoseconds for
    4038                 :             :    * various external API and GPollFunc which requires milliseconds.
    4039                 :             :    *
    4040                 :             :    * However, we want to ensure a few invariants for this.
    4041                 :             :    *
    4042                 :             :    *   Return == -1 if we have no timeout specified
    4043                 :             :    *   Return ==  0 if we don't want to block at all
    4044                 :             :    *   Return  >  0 if we have any timeout to avoid spinning the CPU
    4045                 :             :    *
    4046                 :             :    * This does cause jitter if the microsecond timeout is < 1000 usec
    4047                 :             :    * because that is beyond our precision. However, using ppoll() instead
    4048                 :             :    * of poll() (when available) avoids this jitter.
    4049                 :             :    */
    4050                 :             : 
    4051                 :      244147 :   if (!has_timeout)
    4052                 :       24760 :     return -1;
    4053                 :             : 
    4054                 :      219387 :   if (timeout_ns == 0)
    4055                 :      215666 :     return 0;
    4056                 :             : 
    4057                 :        3721 :   timeout_ns = MIN (timeout_ns, ((uint64_t) G_MAXINT) * 1000000);
    4058                 :             : 
    4059                 :        3721 :   return (int) ((timeout_ns + 999999) / 1000000);
    4060                 :      244130 : }
    4061                 :             : 
    4062                 :             : static gboolean
    4063                 :     1040120 : g_main_context_prepare_unlocked (GMainContext *context,
    4064                 :             :                                  gint         *priority)
    4065                 :             : {
    4066                 :             :   guint i;
    4067                 :     1040120 :   gint n_ready = 0;
    4068                 :     1040120 :   gint current_priority = G_MAXINT;
    4069                 :             :   GSource *source;
    4070                 :             :   GSourceIter iter;
    4071                 :             : 
    4072                 :     1040120 :   context->time_is_fresh = FALSE;
    4073                 :             : 
    4074                 :     1040120 :   if (context->in_check_or_prepare)
    4075                 :             :     {
    4076                 :           0 :       g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
    4077                 :             :                  "prepare() member.");
    4078                 :           0 :       return FALSE;
    4079                 :             :     }
    4080                 :             : 
    4081                 :      795981 :   TRACE (GLIB_MAIN_CONTEXT_BEFORE_PREPARE (context));
    4082                 :             : 
    4083                 :             : #if 0
    4084                 :             :   /* If recursing, finish up current dispatch, before starting over */
    4085                 :             :   if (context->pending_dispatches)
    4086                 :             :     {
    4087                 :             :       if (dispatch)
    4088                 :             :         g_main_dispatch (context, &current_time);
    4089                 :             :       
    4090                 :             :       return TRUE;
    4091                 :             :     }
    4092                 :             : #endif
    4093                 :             : 
    4094                 :             :   /* If recursing, clear list of pending dispatches */
    4095                 :             : 
    4096                 :     1040154 :   for (i = 0; i < context->pending_dispatches->len; i++)
    4097                 :             :     {
    4098                 :          34 :       if (context->pending_dispatches->pdata[i])
    4099                 :           8 :         g_source_unref_internal ((GSource *)context->pending_dispatches->pdata[i], context, TRUE);
    4100                 :          16 :     }
    4101                 :     1040120 :   g_ptr_array_set_size (context->pending_dispatches, 0);
    4102                 :             :   
    4103                 :             :   /* Prepare all sources */
    4104                 :             : 
    4105                 :     1040120 :   context->has_timeout = FALSE;
    4106                 :     1040120 :   context->timeout_ns = 0;
    4107                 :             :   
    4108                 :     1040120 :   g_source_iter_init (&iter, context, TRUE);
    4109                 :     2871107 :   while (g_source_iter_next (&iter, &source))
    4110                 :             :     {
    4111                 :     1931171 :       gboolean has_source_timeout = FALSE;
    4112                 :             :       uint64_t source_timeout_ns;
    4113                 :             : 
    4114                 :     1931171 :       if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source))
    4115                 :      471207 :         continue;
    4116                 :     1459964 :       if ((n_ready > 0) && (source->priority > current_priority))
    4117                 :      100184 :         break;
    4118                 :             : 
    4119                 :     1359780 :       if (!(g_atomic_int_get (&source->flags) & G_SOURCE_READY))
    4120                 :             :         {
    4121                 :             :           gboolean result;
    4122                 :             :           gboolean (* prepare) (GSource  *source,
    4123                 :             :                                 gint     *timeout);
    4124                 :             : 
    4125                 :     1359453 :           prepare = source->source_funcs->prepare;
    4126                 :             : 
    4127                 :     1359453 :           if (prepare)
    4128                 :             :             {
    4129                 :             :               gint64 begin_time_nsec G_GNUC_UNUSED;
    4130                 :      672678 :               int source_timeout_msec = -1;
    4131                 :             : 
    4132                 :      672678 :               context->in_check_or_prepare++;
    4133                 :      672678 :               UNLOCK_CONTEXT (context);
    4134                 :             : 
    4135                 :      672678 :               begin_time_nsec = G_TRACE_CURRENT_TIME;
    4136                 :             : 
    4137                 :      672678 :               result = (*prepare) (source, &source_timeout_msec);
    4138                 :      438136 :               TRACE (GLIB_MAIN_AFTER_PREPARE (source, prepare, source_timeout_msec));
    4139                 :             : 
    4140                 :      672678 :               if (source_timeout_msec >= 0)
    4141                 :             :                 {
    4142                 :      558575 :                   has_source_timeout = TRUE;
    4143                 :      558575 :                   source_timeout_ns = ((uint64_t) source_timeout_msec) * 1000 * 1000;
    4144                 :      228114 :                 }
    4145                 :             : 
    4146                 :     1303102 :               g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec,
    4147                 :             :                             "GLib", "GSource.prepare",
    4148                 :             :                             "%s ⇒ %s",
    4149                 :      864966 :                             (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)",
    4150                 :             :                             result ? "ready" : "unready");
    4151                 :             : 
    4152                 :      672678 :               LOCK_CONTEXT (context);
    4153                 :      672678 :               context->in_check_or_prepare--;
    4154                 :      234542 :             }
    4155                 :             :           else
    4156                 :      686775 :             result = FALSE;
    4157                 :             : 
    4158                 :     1359453 :           if (result == FALSE && source->priv->has_ready_time)
    4159                 :             :             {
    4160                 :       97535 :               if (!context->time_is_fresh)
    4161                 :             :                 {
    4162                 :       90180 :                   context->time_ns = g_get_monotonic_time_ns ();
    4163                 :       90180 :                   context->time_is_fresh = TRUE;
    4164                 :       18068 :                 }
    4165                 :             : 
    4166                 :       97535 :               if (source->priv->ready_time_ns <= context->time_ns)
    4167                 :             :                 {
    4168                 :        3095 :                   source_timeout_ns = 0;
    4169                 :        3095 :                   result = TRUE;
    4170                 :         128 :                 }
    4171                 :             :               else
    4172                 :             :                 {
    4173                 :       94440 :                   uint64_t ready_timeout_ns = source->priv->ready_time_ns - context->time_ns;
    4174                 :             : 
    4175                 :       94440 :                   if (!has_source_timeout)
    4176                 :       94440 :                     source_timeout_ns = ready_timeout_ns;
    4177                 :             :                   else
    4178                 :           0 :                     source_timeout_ns = MIN (source_timeout_ns, ready_timeout_ns);
    4179                 :             :                 }
    4180                 :             : 
    4181                 :       97535 :               has_source_timeout = TRUE;
    4182                 :       20334 :             }
    4183                 :             : 
    4184                 :     1359453 :           if (result)
    4185                 :             :             {
    4186                 :      539266 :               GSource *ready_source = source;
    4187                 :             : 
    4188                 :     1078847 :               while (ready_source)
    4189                 :             :                 {
    4190                 :      539581 :                   g_atomic_int_or (&ready_source->flags, G_SOURCE_READY);
    4191                 :      539581 :                   ready_source = ready_source->priv->parent_source;
    4192                 :             :                 }
    4193                 :      217105 :             }
    4194                 :      278537 :         }
    4195                 :             : 
    4196                 :     1359780 :       if (g_atomic_int_get (&source->flags) & G_SOURCE_READY)
    4197                 :             :         {
    4198                 :      539592 :           n_ready++;
    4199                 :      539592 :           current_priority = source->priority;
    4200                 :      539592 :           context->has_timeout = TRUE;
    4201                 :      539592 :           context->timeout_ns = 0;
    4202                 :      217142 :         }
    4203                 :             : 
    4204                 :     1359780 :       if (has_source_timeout)
    4205                 :             :         {
    4206                 :      656110 :           if (!context->has_timeout)
    4207                 :             :             {
    4208                 :       93907 :               context->timeout_ns = source_timeout_ns;
    4209                 :       93907 :               context->has_timeout = TRUE;
    4210                 :       21142 :             }
    4211                 :             :           else
    4212                 :             :             {
    4213                 :      562203 :               context->timeout_ns = MIN (context->timeout_ns, source_timeout_ns);
    4214                 :             :             }
    4215                 :      248448 :         }
    4216                 :             :     }
    4217                 :     1040120 :   g_source_iter_clear (&iter);
    4218                 :             : 
    4219                 :      795981 :   TRACE (GLIB_MAIN_CONTEXT_AFTER_PREPARE (context, current_priority, n_ready));
    4220                 :             :   
    4221                 :     1040120 :   if (priority)
    4222                 :     1040116 :     *priority = current_priority;
    4223                 :             :   
    4224                 :     1040120 :   return (n_ready > 0);
    4225                 :      244139 : }
    4226                 :             : 
    4227                 :             : /**
    4228                 :             :  * g_main_context_query:
    4229                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4230                 :             :  *   main context will be used)
    4231                 :             :  * @max_priority: maximum priority source to check
    4232                 :             :  * @timeout_: (out): location to store timeout to be used in polling
    4233                 :             :  * @fds: (out caller-allocates) (array length=n_fds): location to
    4234                 :             :  *   store [struct@GLib.PollFD] records that need to be polled
    4235                 :             :  * @n_fds: (in): length of @fds
    4236                 :             :  *
    4237                 :             :  * Determines information necessary to poll this main loop.
    4238                 :             :  *
    4239                 :             :  * You should
    4240                 :             :  * be careful to pass the resulting @fds array and its length @n_fds
    4241                 :             :  * as-is when calling [method@GLib.MainContext.check], as this function relies
    4242                 :             :  * on assumptions made when the array is filled.
    4243                 :             :  *
    4244                 :             :  * You must have successfully acquired the context with
    4245                 :             :  * [method@GLib.MainContext.acquire] before you may call this function.
    4246                 :             :  *
    4247                 :             :  * Returns: the number of records actually stored in @fds,
    4248                 :             :  *   or, if more than @n_fds records need to be stored, the number
    4249                 :             :  *   of records that need to be stored
    4250                 :             :  **/
    4251                 :             : gint
    4252                 :          34 : g_main_context_query (GMainContext *context,
    4253                 :             :                       gint          max_priority,
    4254                 :             :                       gint         *timeout_msec,
    4255                 :             :                       GPollFD      *fds,
    4256                 :             :                       gint          n_fds)
    4257                 :             : {
    4258                 :             :   gboolean has_timeout;
    4259                 :             :   uint64_t timeout_ns;
    4260                 :             :   gint n_poll;
    4261                 :             : 
    4262                 :          34 :   if (context == NULL)
    4263                 :           2 :     context = g_main_context_default ();
    4264                 :             : 
    4265                 :          34 :   LOCK_CONTEXT (context);
    4266                 :             : 
    4267                 :          34 :   n_poll = g_main_context_query_unlocked (context, max_priority, &has_timeout, &timeout_ns, fds, n_fds);
    4268                 :             : 
    4269                 :          34 :   UNLOCK_CONTEXT (context);
    4270                 :             : 
    4271                 :          34 :   if (timeout_msec != NULL)
    4272                 :          20 :     *timeout_msec = round_timeout_to_msec (has_timeout, timeout_ns);
    4273                 :             : 
    4274                 :          34 :   return n_poll;
    4275                 :             : }
    4276                 :             : 
    4277                 :             : static gint
    4278                 :     1040333 : g_main_context_query_unlocked (GMainContext *context,
    4279                 :             :                                gint          max_priority,
    4280                 :             :                                gboolean     *has_timeout,
    4281                 :             :                                uint64_t     *timeout_ns,
    4282                 :             :                                GPollFD      *fds,
    4283                 :             :                                gint          n_fds)
    4284                 :             : {
    4285                 :             :   gint n_poll;
    4286                 :             :   GPollRec *pollrec, *lastpollrec;
    4287                 :             :   gushort events;
    4288                 :             :   
    4289                 :      796173 :   TRACE (GLIB_MAIN_CONTEXT_BEFORE_QUERY (context, max_priority));
    4290                 :             : 
    4291                 :             :   /* fds is filled sequentially from poll_records. Since poll_records
    4292                 :             :    * are incrementally sorted by file descriptor identifier, fds will
    4293                 :             :    * also be incrementally sorted.
    4294                 :             :    */
    4295                 :     1040333 :   n_poll = 0;
    4296                 :     1040333 :   lastpollrec = NULL;
    4297                 :     2676869 :   for (pollrec = context->poll_records; pollrec; pollrec = pollrec->next)
    4298                 :             :     {
    4299                 :     1636536 :       if (pollrec->priority > max_priority)
    4300                 :          15 :         continue;
    4301                 :             : 
    4302                 :             :       /* In direct contradiction to the Unix98 spec, IRIX runs into
    4303                 :             :        * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
    4304                 :             :        * flags in the events field of the pollfd while it should
    4305                 :             :        * just ignoring them. So we mask them out here.
    4306                 :             :        */
    4307                 :     1636521 :       events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
    4308                 :             : 
    4309                 :             :       /* This optimization --using the same GPollFD to poll for more
    4310                 :             :        * than one poll record-- relies on the poll records being
    4311                 :             :        * incrementally sorted.
    4312                 :             :        */
    4313                 :     1636521 :       if (lastpollrec && pollrec->fd->fd == lastpollrec->fd->fd)
    4314                 :             :         {
    4315                 :         246 :           if (n_poll - 1 < n_fds)
    4316                 :         246 :             fds[n_poll - 1].events |= events;
    4317                 :          35 :         }
    4318                 :             :       else
    4319                 :             :         {
    4320                 :     1636275 :           if (n_poll < n_fds)
    4321                 :             :             {
    4322                 :     1636043 :               fds[n_poll].fd = pollrec->fd->fd;
    4323                 :     1636043 :               fds[n_poll].events = events;
    4324                 :     1636043 :               fds[n_poll].revents = 0;
    4325                 :      249879 :             }
    4326                 :             : 
    4327                 :     1636275 :           n_poll++;
    4328                 :             :         }
    4329                 :             : 
    4330                 :     1636521 :       lastpollrec = pollrec;
    4331                 :      249940 :     }
    4332                 :             : 
    4333                 :     1040333 :   context->poll_changed = FALSE;
    4334                 :             : 
    4335                 :     1040333 :   *has_timeout = context->has_timeout;
    4336                 :     1040333 :   *timeout_ns = context->timeout_ns;
    4337                 :             : 
    4338                 :     1040333 :   if (!context->has_timeout || context->timeout_ns != 0)
    4339                 :      533586 :     context->time_is_fresh = FALSE;
    4340                 :             : 
    4341                 :      796173 :   TRACE (GLIB_MAIN_CONTEXT_AFTER_QUERY (context, context->has_timeout, context->timeout_ns,
    4342                 :             :                                         fds, n_poll));
    4343                 :             : 
    4344                 :     1040333 :   return n_poll;
    4345                 :             : }
    4346                 :             : 
    4347                 :             : /**
    4348                 :             :  * g_main_context_check:
    4349                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4350                 :             :  *   main context will be used)
    4351                 :             :  * @max_priority: the maximum numerical priority of sources to check
    4352                 :             :  * @fds: (array length=n_fds): array of [struct@GLib.PollFD]s that was passed to
    4353                 :             :  *   the last call to [method@GLib.MainContext.query]
    4354                 :             :  * @n_fds: return value of [method@GLib.MainContext.query]
    4355                 :             :  *
    4356                 :             :  * Passes the results of polling back to the main loop.
    4357                 :             :  *
    4358                 :             :  * You should be
    4359                 :             :  * careful to pass @fds and its length @n_fds as received from
    4360                 :             :  * [method@GLib.MainContext.query], as this functions relies on assumptions
    4361                 :             :  * on how @fds is filled.
    4362                 :             :  *
    4363                 :             :  * You must have successfully acquired the context with
    4364                 :             :  * [method@GLib.MainContext.acquire] before you may call this function.
    4365                 :             :  *
    4366                 :             :  * Since 2.76 @context can be `NULL` to use the global-default
    4367                 :             :  * main context.
    4368                 :             :  *
    4369                 :             :  * Returns: true if some sources are ready to be dispatched, false otherwise
    4370                 :             :  **/
    4371                 :             : gboolean
    4372                 :          18 : g_main_context_check (GMainContext *context,
    4373                 :             :                       gint          max_priority,
    4374                 :             :                       GPollFD      *fds,
    4375                 :             :                       gint          n_fds)
    4376                 :             : {
    4377                 :             :   gboolean ready;
    4378                 :             : 
    4379                 :          18 :   if (context == NULL)
    4380                 :           2 :     context = g_main_context_default ();
    4381                 :             : 
    4382                 :          18 :   LOCK_CONTEXT (context);
    4383                 :             : 
    4384                 :          18 :   ready = g_main_context_check_unlocked (context, max_priority, fds, n_fds);
    4385                 :             : 
    4386                 :          18 :   UNLOCK_CONTEXT (context);
    4387                 :             : 
    4388                 :          18 :   return ready;
    4389                 :             : }
    4390                 :             : 
    4391                 :             : static gboolean
    4392                 :     1039755 : g_main_context_check_unlocked (GMainContext *context,
    4393                 :             :                                gint          max_priority,
    4394                 :             :                                GPollFD      *fds,
    4395                 :             :                                gint          n_fds)
    4396                 :             : {
    4397                 :             :   GSource *source;
    4398                 :             :   GSourceIter iter;
    4399                 :             :   GPollRec *pollrec;
    4400                 :     1039755 :   gint n_ready = 0;
    4401                 :             :   gint i;
    4402                 :             : 
    4403                 :     1039755 :   if (context->in_check_or_prepare)
    4404                 :             :     {
    4405                 :           0 :       g_warning ("g_main_context_check() called recursively from within a source's check() or "
    4406                 :             :                  "prepare() member.");
    4407                 :           0 :       return FALSE;
    4408                 :             :     }
    4409                 :             : 
    4410                 :      795640 :   TRACE (GLIB_MAIN_CONTEXT_BEFORE_CHECK (context, max_priority, fds, n_fds));
    4411                 :             : 
    4412                 :     1081511 :   for (i = 0; i < n_fds; i++)
    4413                 :             :     {
    4414                 :     1081480 :       if (fds[i].fd == context->wake_up_rec.fd)
    4415                 :             :         {
    4416                 :     1039724 :           if (fds[i].revents)
    4417                 :             :             {
    4418                 :      348279 :               TRACE (GLIB_MAIN_CONTEXT_WAKEUP_ACKNOWLEDGE (context));
    4419                 :      373252 :               g_wakeup_acknowledge (context->wakeup);
    4420                 :       24973 :             }
    4421                 :     1039724 :           break;
    4422                 :             :         }
    4423                 :        2089 :     }
    4424                 :             : 
    4425                 :             :   /* If the set of poll file descriptors changed, bail out
    4426                 :             :    * and let the main loop rerun
    4427                 :             :    */
    4428                 :     1039755 :   if (context->poll_changed)
    4429                 :             :     {
    4430                 :          93 :       TRACE (GLIB_MAIN_CONTEXT_AFTER_CHECK (context, 0));
    4431                 :             : 
    4432                 :         160 :       return FALSE;
    4433                 :             :     }
    4434                 :             : 
    4435                 :             :   /* The linear iteration below relies on the assumption that both
    4436                 :             :    * poll records and the fds array are incrementally sorted by file
    4437                 :             :    * descriptor identifier.
    4438                 :             :    */
    4439                 :     1039595 :   pollrec = context->poll_records;
    4440                 :     1039595 :   i = 0;
    4441                 :     2674576 :   while (pollrec && i < n_fds)
    4442                 :             :     {
    4443                 :             :       /* Make sure that fds is sorted by file descriptor identifier. */
    4444                 :     1634991 :       g_assert (i <= 0 || fds[i - 1].fd < fds[i].fd);
    4445                 :             : 
    4446                 :             :       /* Skip until finding the first GPollRec matching the current GPollFD. */
    4447                 :     1634992 :       while (pollrec && pollrec->fd->fd != fds[i].fd)
    4448                 :           1 :         pollrec = pollrec->next;
    4449                 :             : 
    4450                 :             :       /* Update all consecutive GPollRecs that match. */
    4451                 :     3270221 :       while (pollrec && pollrec->fd->fd == fds[i].fd)
    4452                 :             :         {
    4453                 :     1635230 :           if (pollrec->priority <= max_priority)
    4454                 :             :             {
    4455                 :     1635225 :               pollrec->fd->revents =
    4456                 :     1635225 :                 fds[i].revents & (pollrec->fd->events | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
    4457                 :      249722 :             }
    4458                 :     1635230 :           pollrec = pollrec->next;
    4459                 :             :         }
    4460                 :             : 
    4461                 :             :       /* Iterate to next GPollFD. */
    4462                 :     1634981 :       i++;
    4463                 :             :     }
    4464                 :             : 
    4465                 :     1039585 :   g_source_iter_init (&iter, context, TRUE);
    4466                 :     2766666 :   while (g_source_iter_next (&iter, &source))
    4467                 :             :     {
    4468                 :     1958531 :       if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source))
    4469                 :      470783 :         continue;
    4470                 :     1487748 :       if ((n_ready > 0) && (source->priority > max_priority))
    4471                 :      231450 :         break;
    4472                 :             : 
    4473                 :     1256298 :       if (!(g_atomic_int_get (&source->flags) & G_SOURCE_READY))
    4474                 :             :         {
    4475                 :             :           gboolean result;
    4476                 :             :           gboolean (* check) (GSource *source);
    4477                 :             : 
    4478                 :      710815 :           check = source->source_funcs->check;
    4479                 :             : 
    4480                 :      710815 :           if (check)
    4481                 :             :             {
    4482                 :             :               gint64 begin_time_nsec G_GNUC_UNUSED;
    4483                 :             : 
    4484                 :             :               /* If the check function is set, call it. */
    4485                 :       50607 :               context->in_check_or_prepare++;
    4486                 :       50607 :               UNLOCK_CONTEXT (context);
    4487                 :             : 
    4488                 :       50607 :               begin_time_nsec = G_TRACE_CURRENT_TIME;
    4489                 :             : 
    4490                 :       50607 :               result = (* check) (source);
    4491                 :             : 
    4492                 :       44570 :               TRACE (GLIB_MAIN_AFTER_CHECK (source, check, result));
    4493                 :             : 
    4494                 :      133697 :               g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec,
    4495                 :             :                             "GLib", "GSource.check",
    4496                 :             :                             "%s ⇒ %s",
    4497                 :       89127 :                             (g_source_get_name (source) != NULL) ? g_source_get_name (source) : "(unnamed)",
    4498                 :             :                             result ? "dispatch" : "ignore");
    4499                 :             : 
    4500                 :       50607 :               LOCK_CONTEXT (context);
    4501                 :       50607 :               context->in_check_or_prepare--;
    4502                 :        6037 :             }
    4503                 :             :           else
    4504                 :      660208 :             result = FALSE;
    4505                 :             : 
    4506                 :      710815 :           if (result == FALSE)
    4507                 :             :             {
    4508                 :             :               GSList *tmp_list;
    4509                 :             : 
    4510                 :             :               /* If not already explicitly flagged ready by ->check()
    4511                 :             :                * (or if we have no check) then we can still be ready if
    4512                 :             :                * any of our fds poll as ready.
    4513                 :             :                */
    4514                 :      843945 :               for (tmp_list = source->priv->fds; tmp_list; tmp_list = tmp_list->next)
    4515                 :             :                 {
    4516                 :      445811 :                   GPollFD *pollfd = tmp_list->data;
    4517                 :             : 
    4518                 :      445811 :                   if (pollfd->revents)
    4519                 :             :                     {
    4520                 :      275092 :                       result = TRUE;
    4521                 :      275092 :                       break;
    4522                 :             :                     }
    4523                 :           0 :                 }
    4524                 :       59930 :             }
    4525                 :             : 
    4526                 :      710815 :           if (result == FALSE && source->priv->has_ready_time)
    4527                 :             :             {
    4528                 :      128894 :               if (!context->time_is_fresh)
    4529                 :             :                 {
    4530                 :       55482 :                   context->time_ns = g_get_monotonic_time_ns ();
    4531                 :       55482 :                   context->time_is_fresh = TRUE;
    4532                 :       22408 :                 }
    4533                 :             : 
    4534                 :      128894 :               if (source->priv->ready_time_ns <= context->time_ns)
    4535                 :       41716 :                 result = TRUE;
    4536                 :       42039 :             }
    4537                 :             : 
    4538                 :      710815 :           if (result)
    4539                 :             :             {
    4540                 :      354397 :               GSource *ready_source = source;
    4541                 :             : 
    4542                 :      714688 :               while (ready_source)
    4543                 :             :                 {
    4544                 :      360291 :                   g_atomic_int_or (&ready_source->flags, G_SOURCE_READY);
    4545                 :      360291 :                   ready_source = ready_source->priv->parent_source;
    4546                 :             :                 }
    4547                 :       24088 :             }
    4548                 :       61800 :         }
    4549                 :             : 
    4550                 :     1256298 :       if (g_atomic_int_get (&source->flags) & G_SOURCE_READY)
    4551                 :             :         {
    4552                 :      899878 :           g_source_ref (source);
    4553                 :      899878 :           g_ptr_array_add (context->pending_dispatches, source);
    4554                 :             : 
    4555                 :      899878 :           n_ready++;
    4556                 :             : 
    4557                 :             :           /* never dispatch sources with less priority than the first
    4558                 :             :            * one we choose to dispatch
    4559                 :             :            */
    4560                 :      899878 :           max_priority = source->priority;
    4561                 :      241242 :         }
    4562                 :             :     }
    4563                 :     1039585 :   g_source_iter_clear (&iter);
    4564                 :             : 
    4565                 :      795547 :   TRACE (GLIB_MAIN_CONTEXT_AFTER_CHECK (context, n_ready));
    4566                 :             : 
    4567                 :     1039585 :   return n_ready > 0;
    4568                 :      244105 : }
    4569                 :             : 
    4570                 :             : /**
    4571                 :             :  * g_main_context_dispatch:
    4572                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4573                 :             :  *   main context will be used)
    4574                 :             :  *
    4575                 :             :  * Dispatches all pending sources.
    4576                 :             :  *
    4577                 :             :  * You must have successfully acquired the context with
    4578                 :             :  * [method@GLib.MainContext.acquire] before you may call this function.
    4579                 :             :  *
    4580                 :             :  * Since 2.76 @context can be `NULL` to use the global-default
    4581                 :             :  * main context.
    4582                 :             :  **/
    4583                 :             : void
    4584                 :           9 : g_main_context_dispatch (GMainContext *context)
    4585                 :             : {
    4586                 :           9 :   if (context == NULL)
    4587                 :           2 :     context = g_main_context_default ();
    4588                 :             : 
    4589                 :           9 :   LOCK_CONTEXT (context);
    4590                 :             : 
    4591                 :           9 :   g_main_context_dispatch_unlocked (context);
    4592                 :             : 
    4593                 :           9 :   UNLOCK_CONTEXT (context);
    4594                 :           9 : }
    4595                 :             : 
    4596                 :             : static void
    4597                 :     1039708 : g_main_context_dispatch_unlocked (GMainContext *context)
    4598                 :             : {
    4599                 :      795617 :   TRACE (GLIB_MAIN_CONTEXT_BEFORE_DISPATCH (context));
    4600                 :             : 
    4601                 :     1039708 :   if (context->pending_dispatches->len > 0)
    4602                 :             :     {
    4603                 :      848666 :       g_main_dispatch (context);
    4604                 :      239477 :     }
    4605                 :             : 
    4606                 :      795611 :   TRACE (GLIB_MAIN_CONTEXT_AFTER_DISPATCH (context));
    4607                 :     1039702 : }
    4608                 :             : 
    4609                 :             : /* HOLDS context lock */
    4610                 :             : static gboolean
    4611                 :     1072418 : g_main_context_iterate_unlocked (GMainContext *context,
    4612                 :             :                                  gboolean      block,
    4613                 :             :                                  gboolean      dispatch,
    4614                 :             :                                  GThread      *self)
    4615                 :             : {
    4616                 :     1072418 :   gint max_priority = 0;
    4617                 :             :   gboolean has_timeout;
    4618                 :             :   uint64_t timeout_ns;
    4619                 :             :   gboolean some_ready;
    4620                 :             :   gint nfds, allocated_nfds;
    4621                 :     1072418 :   GPollFD *fds = NULL;
    4622                 :             :   gint64 begin_time_nsec G_GNUC_UNUSED;
    4623                 :             : 
    4624                 :     1072418 :   begin_time_nsec = G_TRACE_CURRENT_TIME;
    4625                 :             : 
    4626                 :     1072418 :   if (!g_main_context_acquire_unlocked (context))
    4627                 :             :     {
    4628                 :             :       gboolean got_ownership;
    4629                 :             : 
    4630                 :       32361 :       if (!block)
    4631                 :       32361 :         return FALSE;
    4632                 :             : 
    4633                 :           0 :       got_ownership = g_main_context_wait_internal (context,
    4634                 :           0 :                                                     &context->cond,
    4635                 :           0 :                                                     &context->mutex);
    4636                 :             : 
    4637                 :           0 :       if (!got_ownership)
    4638                 :           0 :         return FALSE;
    4639                 :           0 :     }
    4640                 :             :   
    4641                 :     1040057 :   if (!context->cached_poll_array)
    4642                 :             :     {
    4643                 :        6305 :       context->cached_poll_array_size = context->n_poll_records;
    4644                 :        6305 :       context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
    4645                 :         462 :     }
    4646                 :             : 
    4647                 :     1040057 :   allocated_nfds = context->cached_poll_array_size;
    4648                 :     1040057 :   fds = context->cached_poll_array;
    4649                 :             :   
    4650                 :     1040057 :   g_main_context_prepare_unlocked (context, &max_priority);
    4651                 :             : 
    4652                 :     1040263 :   while ((nfds = g_main_context_query_unlocked (
    4653                 :      244114 :               context, max_priority, &has_timeout, &timeout_ns,
    4654                 :     1284377 :               fds, allocated_nfds)) > allocated_nfds)
    4655                 :             :     {
    4656                 :         206 :       g_free (fds);
    4657                 :         206 :       context->cached_poll_array_size = allocated_nfds = nfds;
    4658                 :         206 :       context->cached_poll_array = fds = g_new (GPollFD, nfds);
    4659                 :             :     }
    4660                 :             : 
    4661                 :     1040057 :   if (!block)
    4662                 :             :     {
    4663                 :      348920 :       has_timeout = TRUE;
    4664                 :      348920 :       timeout_ns = 0;
    4665                 :          73 :     }
    4666                 :             : 
    4667                 :     1040057 :   g_main_context_poll_unlocked (context, has_timeout, timeout_ns, max_priority, fds, nfds);
    4668                 :             : 
    4669                 :     1039720 :   some_ready = g_main_context_check_unlocked (context, max_priority, fds, nfds);
    4670                 :             :   
    4671                 :     1039720 :   if (dispatch)
    4672                 :     1039700 :     g_main_context_dispatch_unlocked (context);
    4673                 :             :   
    4674                 :     1039714 :   g_main_context_release_unlocked (context);
    4675                 :             : 
    4676                 :      795618 :   g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec,
    4677                 :             :                 "GLib", "g_main_context_iterate",
    4678                 :             :                 "Context %p, %s ⇒ %s", context, block ? "blocking" : "non-blocking", some_ready ? "dispatched" : "nothing");
    4679                 :             : 
    4680                 :     1039714 :   return some_ready;
    4681                 :      244096 : }
    4682                 :             : 
    4683                 :             : /**
    4684                 :             :  * g_main_context_pending:
    4685                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4686                 :             :  *   main context will be used)
    4687                 :             :  *
    4688                 :             :  * Checks if any sources have pending events for the given context.
    4689                 :             :  * 
    4690                 :             :  * Returns: true if events are pending, false otherwise
    4691                 :             :  **/
    4692                 :             : gboolean 
    4693                 :          25 : g_main_context_pending (GMainContext *context)
    4694                 :             : {
    4695                 :             :   gboolean retval;
    4696                 :             : 
    4697                 :          25 :   if (!context)
    4698                 :          15 :     context = g_main_context_default();
    4699                 :             : 
    4700                 :          25 :   LOCK_CONTEXT (context);
    4701                 :          25 :   retval = g_main_context_iterate_unlocked (context, FALSE, FALSE, G_THREAD_SELF);
    4702                 :          25 :   UNLOCK_CONTEXT (context);
    4703                 :             :   
    4704                 :          25 :   return retval;
    4705                 :             : }
    4706                 :             : 
    4707                 :             : /**
    4708                 :             :  * g_main_context_iteration:
    4709                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    4710                 :             :  *   main context will be used)
    4711                 :             :  * @may_block: whether the call may block
    4712                 :             :  *
    4713                 :             :  * Runs a single iteration for the given main loop.
    4714                 :             :  *
    4715                 :             :  * This involves
    4716                 :             :  * checking to see if any event sources are ready to be processed,
    4717                 :             :  * then if no events sources are ready and @may_block is true, waiting
    4718                 :             :  * for a source to become ready, then dispatching the highest priority
    4719                 :             :  * events sources that are ready. Otherwise, if @may_block is false,
    4720                 :             :  * this function does not wait for sources to become ready, and only the highest
    4721                 :             :  * priority sources which are already ready (if any) will be dispatched.
    4722                 :             :  *
    4723                 :             :  * Note that even when @may_block is true, it is still possible for
    4724                 :             :  * [method@GLib.MainContext.iteration] to return false, since the wait may
    4725                 :             :  * be interrupted for other reasons than an event source becoming ready.
    4726                 :             :  *
    4727                 :             :  * Returns: true if events were dispatched, false otherwise
    4728                 :             :  **/
    4729                 :             : gboolean
    4730                 :      691565 : g_main_context_iteration (GMainContext *context, gboolean may_block)
    4731                 :             : {
    4732                 :             :   gboolean retval;
    4733                 :             : 
    4734                 :      691565 :   if (!context)
    4735                 :      447579 :     context = g_main_context_default();
    4736                 :             :   
    4737                 :      691565 :   LOCK_CONTEXT (context);
    4738                 :      691565 :   retval = g_main_context_iterate_unlocked (context, may_block, TRUE, G_THREAD_SELF);
    4739                 :      691328 :   UNLOCK_CONTEXT (context);
    4740                 :             :   
    4741                 :      691328 :   return retval;
    4742                 :             : }
    4743                 :             : 
    4744                 :             : /**
    4745                 :             :  * g_main_loop_new:
    4746                 :             :  * @context: (nullable): a main context  (if `NULL`, the global-default
    4747                 :             :  *   main context will be used).
    4748                 :             :  * @is_running: set to true to indicate that the loop is running. This
    4749                 :             :  *   is not very important since calling [method@GLib.MainLoop.run] will set this
    4750                 :             :  *   to true anyway.
    4751                 :             :  * 
    4752                 :             :  * Creates a new [struct@GLib.MainLoop] structure.
    4753                 :             :  * 
    4754                 :             :  * Returns: (transfer full): a new main loop
    4755                 :             :  **/
    4756                 :             : GMainLoop *
    4757                 :        6313 : g_main_loop_new (GMainContext *context,
    4758                 :             :                  gboolean      is_running)
    4759                 :             : {
    4760                 :             :   GMainLoop *loop;
    4761                 :             : 
    4762                 :        6313 :   if (!context)
    4763                 :         520 :     context = g_main_context_default();
    4764                 :             :   
    4765                 :        6313 :   g_main_context_ref (context);
    4766                 :             : 
    4767                 :        6313 :   loop = g_new0 (GMainLoop, 1);
    4768                 :        6313 :   loop->context = context;
    4769                 :        6313 :   loop->is_running = is_running != FALSE;
    4770                 :        6313 :   loop->ref_count = 1;
    4771                 :             : 
    4772                 :        5780 :   TRACE (GLIB_MAIN_LOOP_NEW (loop, context));
    4773                 :             : 
    4774                 :        6313 :   return loop;
    4775                 :             : }
    4776                 :             : 
    4777                 :             : /**
    4778                 :             :  * g_main_loop_ref:
    4779                 :             :  * @loop: a main loop
    4780                 :             :  *
    4781                 :             :  * Increases the reference count on a [struct@GLib.MainLoop] object by one.
    4782                 :             :  *
    4783                 :             :  * Returns: @loop
    4784                 :             :  **/
    4785                 :             : GMainLoop *
    4786                 :           4 : g_main_loop_ref (GMainLoop *loop)
    4787                 :             : {
    4788                 :           4 :   g_return_val_if_fail (loop != NULL, NULL);
    4789                 :           4 :   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
    4790                 :             : 
    4791                 :           4 :   g_atomic_int_inc (&loop->ref_count);
    4792                 :             : 
    4793                 :           4 :   return loop;
    4794                 :           2 : }
    4795                 :             : 
    4796                 :             : /**
    4797                 :             :  * g_main_loop_unref:
    4798                 :             :  * @loop: a main loop
    4799                 :             :  *
    4800                 :             :  * Decreases the reference count on a [struct@GLib.MainLoop] object by one.
    4801                 :             :  *
    4802                 :             :  * If the result is zero, the loop and all associated memory are freed.
    4803                 :             :  **/
    4804                 :             : void
    4805                 :       14810 : g_main_loop_unref (GMainLoop *loop)
    4806                 :             : {
    4807                 :       14810 :   g_return_if_fail (loop != NULL);
    4808                 :       14810 :   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
    4809                 :             : 
    4810                 :       14810 :   if (!g_atomic_int_dec_and_test (&loop->ref_count))
    4811                 :        8620 :     return;
    4812                 :             : 
    4813                 :        6190 :   g_main_context_unref (loop->context);
    4814                 :        6190 :   g_free (loop);
    4815                 :        1781 : }
    4816                 :             : 
    4817                 :             : /**
    4818                 :             :  * g_main_loop_run:
    4819                 :             :  * @loop: a main loop
    4820                 :             :  * 
    4821                 :             :  * Runs a main loop until [method@GLib.MainLoop.quit] is called on the loop.
    4822                 :             :  *
    4823                 :             :  * If this is called from the thread of the loop’s [struct@GLib.MainContext],
    4824                 :             :  * it will process events from the loop, otherwise it will
    4825                 :             :  * simply wait.
    4826                 :             :  **/
    4827                 :             : void 
    4828                 :        8726 : g_main_loop_run (GMainLoop *loop)
    4829                 :             : {
    4830                 :        8726 :   GThread *self = G_THREAD_SELF;
    4831                 :             : 
    4832                 :        8726 :   g_return_if_fail (loop != NULL);
    4833                 :        8726 :   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
    4834                 :             : 
    4835                 :             :   /* Hold a reference in case the loop is unreffed from a callback function */
    4836                 :        8726 :   g_atomic_int_inc (&loop->ref_count);
    4837                 :             : 
    4838                 :        8726 :   LOCK_CONTEXT (loop->context);
    4839                 :             : 
    4840                 :        8726 :   if (!g_main_context_acquire_unlocked (loop->context))
    4841                 :             :     {
    4842                 :           1 :       gboolean got_ownership = FALSE;
    4843                 :             :       
    4844                 :             :       /* Another thread owns this context */
    4845                 :           1 :       g_atomic_int_set (&loop->is_running, TRUE);
    4846                 :             : 
    4847                 :           2 :       while (g_atomic_int_get (&loop->is_running) && !got_ownership)
    4848                 :           1 :         got_ownership = g_main_context_wait_internal (loop->context,
    4849                 :           1 :                                                       &loop->context->cond,
    4850                 :           1 :                                                       &loop->context->mutex);
    4851                 :             :       
    4852                 :           1 :       if (!g_atomic_int_get (&loop->is_running))
    4853                 :             :         {
    4854                 :           1 :           if (got_ownership)
    4855                 :           1 :             g_main_context_release_unlocked (loop->context);
    4856                 :             : 
    4857                 :           1 :           UNLOCK_CONTEXT (loop->context);
    4858                 :           1 :           g_main_loop_unref (loop);
    4859                 :           1 :           return;
    4860                 :             :         }
    4861                 :             : 
    4862                 :           0 :       g_assert (got_ownership);
    4863                 :           0 :     }
    4864                 :             : 
    4865                 :        8725 :   if G_UNLIKELY (loop->context->in_check_or_prepare)
    4866                 :             :     {
    4867                 :           4 :       g_warning ("g_main_loop_run(): called recursively from within a source's "
    4868                 :             :                  "check() or prepare() member, iteration not possible.");
    4869                 :           4 :       g_main_context_release_unlocked (loop->context);
    4870                 :           4 :       UNLOCK_CONTEXT (loop->context);
    4871                 :           4 :       g_main_loop_unref (loop);
    4872                 :           4 :       return;
    4873                 :             :     }
    4874                 :             : 
    4875                 :        8721 :   g_atomic_int_set (&loop->is_running, TRUE);
    4876                 :      389478 :   while (g_atomic_int_get (&loop->is_running))
    4877                 :      380863 :     g_main_context_iterate_unlocked (loop->context, TRUE, TRUE, self);
    4878                 :             : 
    4879                 :        8615 :   g_main_context_release_unlocked (loop->context);
    4880                 :             : 
    4881                 :        8615 :   UNLOCK_CONTEXT (loop->context);
    4882                 :             :   
    4883                 :        8615 :   g_main_loop_unref (loop);
    4884                 :        1260 : }
    4885                 :             : 
    4886                 :             : /**
    4887                 :             :  * g_main_loop_quit:
    4888                 :             :  * @loop: a main loop
    4889                 :             :  *
    4890                 :             :  * Stops a [struct@GLib.MainLoop] from running. Any calls to
    4891                 :             :  * [method@GLib.MainLoop.run] for the loop will return.
    4892                 :             :  *
    4893                 :             :  * Note that sources that have already been dispatched when
    4894                 :             :  * [method@GLib.MainLoop.quit] is called will still be executed.
    4895                 :             :  **/
    4896                 :             : void 
    4897                 :        8708 : g_main_loop_quit (GMainLoop *loop)
    4898                 :             : {
    4899                 :        8708 :   g_return_if_fail (loop != NULL);
    4900                 :        8708 :   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
    4901                 :             : 
    4902                 :        8708 :   LOCK_CONTEXT (loop->context);
    4903                 :        8708 :   g_atomic_int_set (&loop->is_running, FALSE);
    4904                 :        8708 :   g_wakeup_signal (loop->context->wakeup);
    4905                 :             : 
    4906                 :        8708 :   g_cond_broadcast (&loop->context->cond);
    4907                 :             : 
    4908                 :        8708 :   UNLOCK_CONTEXT (loop->context);
    4909                 :             : 
    4910                 :        7426 :   TRACE (GLIB_MAIN_LOOP_QUIT (loop));
    4911                 :        1282 : }
    4912                 :             : 
    4913                 :             : /**
    4914                 :             :  * g_main_loop_is_running:
    4915                 :             :  * @loop: a main loop
    4916                 :             :  *
    4917                 :             :  * Checks to see if the main loop is currently being run via
    4918                 :             :  * [method@GLib.MainLoop.run].
    4919                 :             :  *
    4920                 :             :  * Returns: true if the main loop is currently being run, false otherwise
    4921                 :             :  **/
    4922                 :             : gboolean
    4923                 :         144 : g_main_loop_is_running (GMainLoop *loop)
    4924                 :             : {
    4925                 :         144 :   g_return_val_if_fail (loop != NULL, FALSE);
    4926                 :         144 :   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
    4927                 :             : 
    4928                 :         144 :   return g_atomic_int_get (&loop->is_running);
    4929                 :          70 : }
    4930                 :             : 
    4931                 :             : /**
    4932                 :             :  * g_main_loop_get_context:
    4933                 :             :  * @loop: a main loop
    4934                 :             :  * 
    4935                 :             :  * Returns the [struct@GLib.MainContext] of @loop.
    4936                 :             :  * 
    4937                 :             :  * Returns: (transfer none): the [struct@GLib.MainContext] of @loop
    4938                 :             :  **/
    4939                 :             : GMainContext *
    4940                 :           8 : g_main_loop_get_context (GMainLoop *loop)
    4941                 :             : {
    4942                 :           8 :   g_return_val_if_fail (loop != NULL, NULL);
    4943                 :           8 :   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
    4944                 :             :  
    4945                 :           8 :   return loop->context;
    4946                 :           3 : }
    4947                 :             : 
    4948                 :             : /* HOLDS: context's lock */
    4949                 :             : static void
    4950                 :     1040093 : g_main_context_poll_unlocked (GMainContext *context,
    4951                 :             :                               gboolean      has_timeout,
    4952                 :             :                               uint64_t      timeout_ns,
    4953                 :             :                               int           priority,
    4954                 :             :                               GPollFD      *fds,
    4955                 :             :                               int           n_fds)
    4956                 :             : {
    4957                 :             : #ifdef  G_MAIN_POLL_DEBUG
    4958                 :             :   GTimer *poll_timer;
    4959                 :             :   GPollRec *pollrec;
    4960                 :             :   gint i;
    4961                 :             : #endif
    4962                 :             : 
    4963                 :             :   GPollFunc poll_func;
    4964                 :             : 
    4965                 :     1040093 :   if (n_fds || !has_timeout || timeout_ns != 0)
    4966                 :             :     {
    4967                 :             :       int ret, errsv;
    4968                 :             : 
    4969                 :             : #ifdef  G_MAIN_POLL_DEBUG
    4970                 :      244157 :       poll_timer = NULL;
    4971                 :      244157 :       if (_g_main_poll_debug)
    4972                 :             :         {
    4973                 :           0 :           g_print ("polling context=%p n=%d has_timeout=%s timeout_ns=%"PRIu64"\n",
    4974                 :           0 :                    context, n_fds, has_timeout ? "true" : "false", timeout_ns);
    4975                 :           0 :           poll_timer = g_timer_new ();
    4976                 :           0 :         }
    4977                 :             : #endif
    4978                 :     1040051 :       poll_func = context->poll_func;
    4979                 :             : 
    4980                 :             : #if defined(HAVE_PPOLL) && defined(HAVE_POLL)
    4981                 :      795954 :       if (poll_func == g_poll)
    4982                 :             :         {
    4983                 :             :           struct timespec spec;
    4984                 :      795954 :           struct timespec *spec_p = NULL;
    4985                 :             : 
    4986                 :      795954 :           if (has_timeout)
    4987                 :             :             {
    4988                 :      485651 :               spec.tv_sec = timeout_ns / G_NSEC_PER_SEC;
    4989                 :      485651 :               spec.tv_nsec = timeout_ns % G_NSEC_PER_SEC;
    4990                 :      485651 :               spec_p = &spec;
    4991                 :             :             }
    4992                 :             : 
    4993                 :      795954 :           UNLOCK_CONTEXT (context);
    4994                 :      795954 :           ret = ppoll ((struct pollfd *) fds, n_fds, spec_p, NULL);
    4995                 :      795617 :           LOCK_CONTEXT (context);
    4996                 :             :         }
    4997                 :             :       else
    4998                 :             : #endif
    4999                 :             :         {
    5000                 :      244097 :           int timeout_msec = round_timeout_to_msec (has_timeout, timeout_ns);
    5001                 :             : 
    5002                 :      244097 :           UNLOCK_CONTEXT (context);
    5003                 :      244097 :           ret = (*poll_func) (fds, n_fds, timeout_msec);
    5004                 :      244097 :           LOCK_CONTEXT (context);
    5005                 :             :         }
    5006                 :             : 
    5007                 :     1039714 :       errsv = errno;
    5008                 :     1039714 :       if (ret < 0 && errsv != EINTR)
    5009                 :             :         {
    5010                 :             : #ifndef G_OS_WIN32
    5011                 :           0 :           g_warning ("poll(2) failed due to: %s.",
    5012                 :             :                      g_strerror (errsv));
    5013                 :             : #else
    5014                 :             :           /* If g_poll () returns -1, it has already called g_warning() */
    5015                 :             : #endif
    5016                 :           0 :         }
    5017                 :             :       
    5018                 :             : #ifdef  G_MAIN_POLL_DEBUG
    5019                 :      244097 :       if (_g_main_poll_debug)
    5020                 :             :         {
    5021                 :           0 :           g_print ("g_main_poll(%d) has_timeout: %s timeout_ns: %"PRIu64" - elapsed %12.10f seconds",
    5022                 :           0 :                    n_fds,
    5023                 :           0 :                    has_timeout ? "true" : "false",
    5024                 :           0 :                    timeout_ns,
    5025                 :           0 :                    g_timer_elapsed (poll_timer, NULL));
    5026                 :           0 :           g_timer_destroy (poll_timer);
    5027                 :           0 :           pollrec = context->poll_records;
    5028                 :             : 
    5029                 :           0 :           while (pollrec != NULL)
    5030                 :             :             {
    5031                 :           0 :               i = 0;
    5032                 :           0 :               while (i < n_fds)
    5033                 :             :                 {
    5034                 :           0 :                   if (fds[i].fd == pollrec->fd->fd &&
    5035                 :           0 :                       pollrec->fd->events &&
    5036                 :           0 :                       fds[i].revents)
    5037                 :             :                     {
    5038                 :           0 :                       g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
    5039                 :           0 :                       if (fds[i].revents & G_IO_IN)
    5040                 :           0 :                         g_print ("i");
    5041                 :           0 :                       if (fds[i].revents & G_IO_OUT)
    5042                 :           0 :                         g_print ("o");
    5043                 :           0 :                       if (fds[i].revents & G_IO_PRI)
    5044                 :           0 :                         g_print ("p");
    5045                 :           0 :                       if (fds[i].revents & G_IO_ERR)
    5046                 :           0 :                         g_print ("e");
    5047                 :           0 :                       if (fds[i].revents & G_IO_HUP)
    5048                 :           0 :                         g_print ("h");
    5049                 :           0 :                       if (fds[i].revents & G_IO_NVAL)
    5050                 :           0 :                         g_print ("n");
    5051                 :           0 :                       g_print ("]");
    5052                 :           0 :                     }
    5053                 :           0 :                   i++;
    5054                 :             :                 }
    5055                 :           0 :               pollrec = pollrec->next;
    5056                 :             :             }
    5057                 :           0 :           g_print ("\n");
    5058                 :           0 :         }
    5059                 :             : #endif
    5060                 :      244097 :     } /* if (n_fds || !has_timeout || timeout_ns != 0) */
    5061                 :     1039746 : }
    5062                 :             : 
    5063                 :             : /**
    5064                 :             :  * g_main_context_add_poll:
    5065                 :             :  * @context: (nullable): a main context (or `NULL` for the global-default
    5066                 :             :  *   main context)
    5067                 :             :  * @fd: a [struct@GLib.PollFD] structure holding information about a file
    5068                 :             :  *   descriptor to watch.
    5069                 :             :  * @priority: the priority for this file descriptor which should be
    5070                 :             :  *   the same as the priority used for [method@GLib.Source.attach] to ensure
    5071                 :             :  *   that the file descriptor is polled whenever the results may be needed.
    5072                 :             :  *
    5073                 :             :  * Adds a file descriptor to the set of file descriptors polled for
    5074                 :             :  * this context.
    5075                 :             :  *
    5076                 :             :  * This will very seldom be used directly. Instead
    5077                 :             :  * a typical event source will use `g_source_add_unix_fd()` instead.
    5078                 :             :  **/
    5079                 :             : void
    5080                 :           0 : g_main_context_add_poll (GMainContext *context,
    5081                 :             :                          GPollFD      *fd,
    5082                 :             :                          gint          priority)
    5083                 :             : {
    5084                 :           0 :   if (!context)
    5085                 :           0 :     context = g_main_context_default ();
    5086                 :             :   
    5087                 :           0 :   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
    5088                 :           0 :   g_return_if_fail (fd);
    5089                 :             : 
    5090                 :           0 :   LOCK_CONTEXT (context);
    5091                 :           0 :   g_main_context_add_poll_unlocked (context, priority, fd);
    5092                 :           0 :   UNLOCK_CONTEXT (context);
    5093                 :           0 : }
    5094                 :             : 
    5095                 :             : /* HOLDS: main_loop_lock */
    5096                 :             : static void 
    5097                 :      332442 : g_main_context_add_poll_unlocked (GMainContext *context,
    5098                 :             :                                   gint          priority,
    5099                 :             :                                   GPollFD      *fd)
    5100                 :             : {
    5101                 :             :   GPollRec *prevrec, *nextrec;
    5102                 :      332442 :   GPollRec *newrec = g_slice_new (GPollRec);
    5103                 :             : 
    5104                 :             :   /* This file descriptor may be checked before we ever poll */
    5105                 :      332442 :   fd->revents = 0;
    5106                 :      332442 :   newrec->fd = fd;
    5107                 :      332442 :   newrec->priority = priority;
    5108                 :             : 
    5109                 :             :   /* Poll records are incrementally sorted by file descriptor identifier. */
    5110                 :      332442 :   prevrec = NULL;
    5111                 :      332442 :   nextrec = context->poll_records;
    5112                 :      742242 :   while (nextrec)
    5113                 :             :     {
    5114                 :      574052 :       if (nextrec->fd->fd > fd->fd)
    5115                 :      164252 :         break;
    5116                 :      409800 :       prevrec = nextrec;
    5117                 :      409800 :       nextrec = nextrec->next;
    5118                 :             :     }
    5119                 :             : 
    5120                 :      332442 :   if (prevrec)
    5121                 :      273035 :     prevrec->next = newrec;
    5122                 :             :   else
    5123                 :       59407 :     context->poll_records = newrec;
    5124                 :             : 
    5125                 :      332442 :   newrec->prev = prevrec;
    5126                 :      332442 :   newrec->next = nextrec;
    5127                 :             : 
    5128                 :      332442 :   if (nextrec)
    5129                 :      164252 :     nextrec->prev = newrec;
    5130                 :             : 
    5131                 :      332442 :   context->n_poll_records++;
    5132                 :             : 
    5133                 :      332442 :   context->poll_changed = TRUE;
    5134                 :             : 
    5135                 :             :   /* Now wake up the main loop if it is waiting in the poll() */
    5136                 :      332442 :   if (fd != &context->wake_up_rec)
    5137                 :      301946 :     g_wakeup_signal (context->wakeup);
    5138                 :      332442 : }
    5139                 :             : 
    5140                 :             : /**
    5141                 :             :  * g_main_context_remove_poll:
    5142                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    5143                 :             :  *   main context will be used)
    5144                 :             :  * @fd: a [struct@GLib.PollFD] descriptor previously added with
    5145                 :             :  *   [method@GLib.MainContext.add_poll]
    5146                 :             :  *
    5147                 :             :  * Removes file descriptor from the set of file descriptors to be
    5148                 :             :  * polled for a particular context.
    5149                 :             :  **/
    5150                 :             : void
    5151                 :           0 : g_main_context_remove_poll (GMainContext *context,
    5152                 :             :                             GPollFD      *fd)
    5153                 :             : {
    5154                 :           0 :   if (!context)
    5155                 :           0 :     context = g_main_context_default ();
    5156                 :             :   
    5157                 :           0 :   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
    5158                 :           0 :   g_return_if_fail (fd);
    5159                 :             : 
    5160                 :           0 :   LOCK_CONTEXT (context);
    5161                 :           0 :   g_main_context_remove_poll_unlocked (context, fd);
    5162                 :           0 :   UNLOCK_CONTEXT (context);
    5163                 :           0 : }
    5164                 :             : 
    5165                 :             : static void
    5166                 :      301765 : g_main_context_remove_poll_unlocked (GMainContext *context,
    5167                 :             :                                      GPollFD      *fd)
    5168                 :             : {
    5169                 :             :   GPollRec *pollrec, *prevrec, *nextrec;
    5170                 :             : 
    5171                 :      301765 :   prevrec = NULL;
    5172                 :      301765 :   pollrec = context->poll_records;
    5173                 :             : 
    5174                 :      710584 :   while (pollrec)
    5175                 :             :     {
    5176                 :      710584 :       nextrec = pollrec->next;
    5177                 :      710584 :       if (pollrec->fd == fd)
    5178                 :             :         {
    5179                 :      301765 :           if (prevrec != NULL)
    5180                 :      272906 :             prevrec->next = nextrec;
    5181                 :             :           else
    5182                 :       28859 :             context->poll_records = nextrec;
    5183                 :             : 
    5184                 :      301765 :           if (nextrec != NULL)
    5185                 :      164121 :             nextrec->prev = prevrec;
    5186                 :             : 
    5187                 :      301765 :           g_slice_free (GPollRec, pollrec);
    5188                 :             : 
    5189                 :      301765 :           context->n_poll_records--;
    5190                 :      301765 :           break;
    5191                 :             :         }
    5192                 :      408819 :       prevrec = pollrec;
    5193                 :      408819 :       pollrec = nextrec;
    5194                 :             :     }
    5195                 :             : 
    5196                 :      301765 :   context->poll_changed = TRUE;
    5197                 :             : 
    5198                 :             :   /* Now wake up the main loop if it is waiting in the poll() */
    5199                 :      301765 :   g_wakeup_signal (context->wakeup);
    5200                 :      301765 : }
    5201                 :             : 
    5202                 :             : /**
    5203                 :             :  * g_source_get_current_time:
    5204                 :             :  * @source:  a source
    5205                 :             :  * @timeval: [struct@GLib.TimeVal] structure in which to store current time
    5206                 :             :  *
    5207                 :             :  * This function ignores @source and is otherwise the same as
    5208                 :             :  * [func@GLib.get_current_time].
    5209                 :             :  *
    5210                 :             :  * Deprecated: 2.28: use [method@GLib.Source.get_time] instead
    5211                 :             :  **/
    5212                 :             : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
    5213                 :             : void
    5214                 :           4 : g_source_get_current_time (GSource  *source,
    5215                 :             :                            GTimeVal *timeval)
    5216                 :             : {
    5217                 :           4 :   g_get_current_time (timeval);
    5218                 :           4 : }
    5219                 :             : G_GNUC_END_IGNORE_DEPRECATIONS
    5220                 :             : 
    5221                 :             : /**
    5222                 :             :  * g_source_get_time_ns:
    5223                 :             :  * @source: a source
    5224                 :             :  *
    5225                 :             :  * Gets the time to be used when checking this source.
    5226                 :             :  *
    5227                 :             :  * The advantage of calling this function over calling
    5228                 :             :  * [func@GLib.get_monotonic_time_ns] directly is
    5229                 :             :  * that when checking multiple sources, GLib can cache a single value
    5230                 :             :  * instead of having to repeatedly get the system monotonic time.
    5231                 :             :  *
    5232                 :             :  * The time here is the system monotonic time, if available, or some
    5233                 :             :  * other reasonable alternative otherwise.  See [func@GLib.get_monotonic_time_ns].
    5234                 :             :  *
    5235                 :             :  * Returns: the monotonic time in nanoseconds
    5236                 :             :  * Since: 2.90
    5237                 :             :  **/
    5238                 :             : uint64_t
    5239                 :      240084 : g_source_get_time_ns (GSource *source)
    5240                 :             : {
    5241                 :             :   GMainContext *context;
    5242                 :             :   gint64 result;
    5243                 :             : 
    5244                 :      240084 :   g_return_val_if_fail (source != NULL, 0);
    5245                 :      240084 :   g_return_val_if_fail (g_atomic_int_get (&source->ref_count) > 0, 0);
    5246                 :      240084 :   context = source_dup_main_context (source);
    5247                 :      240084 :   g_return_val_if_fail (context != NULL, 0);
    5248                 :             : 
    5249                 :      240084 :   LOCK_CONTEXT (context);
    5250                 :             : 
    5251                 :      240084 :   if (!context->time_is_fresh)
    5252                 :             :     {
    5253                 :       76808 :       context->time_ns = g_get_monotonic_time_ns ();
    5254                 :       76808 :       context->time_is_fresh = TRUE;
    5255                 :        4846 :     }
    5256                 :             : 
    5257                 :      240084 :   result = context->time_ns;
    5258                 :             : 
    5259                 :      240084 :   UNLOCK_CONTEXT (context);
    5260                 :      240084 :   g_main_context_unref (context);
    5261                 :             : 
    5262                 :      240084 :   return result;
    5263                 :       17111 : }
    5264                 :             : 
    5265                 :             : /**
    5266                 :             :  * g_source_get_time:
    5267                 :             :  * @source: a source
    5268                 :             :  *
    5269                 :             :  * Gets the time to be used when checking this source.
    5270                 :             :  *
    5271                 :             :  * The advantage of
    5272                 :             :  * calling this function over calling [func@GLib.get_monotonic_time] directly is
    5273                 :             :  * that when checking multiple sources, GLib can cache a single value
    5274                 :             :  * instead of having to repeatedly get the system monotonic time.
    5275                 :             :  *
    5276                 :             :  * The time here is the system monotonic time, if available, or some
    5277                 :             :  * other reasonable alternative otherwise.  See [func@GLib.get_monotonic_time].
    5278                 :             :  *
    5279                 :             :  * Returns: the monotonic time in microseconds
    5280                 :             :  * Since: 2.28
    5281                 :             :  **/
    5282                 :             : gint64
    5283                 :      239461 : g_source_get_time (GSource *source)
    5284                 :             : {
    5285                 :      239461 :   g_return_val_if_fail (source != NULL, 0);
    5286                 :             : 
    5287                 :      239461 :   return g_source_get_time_ns (source) / 1000;
    5288                 :       16931 : }
    5289                 :             : 
    5290                 :             : /**
    5291                 :             :  * g_main_context_set_poll_func:
    5292                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    5293                 :             :  *   main context will be used)
    5294                 :             :  * @func: the function to call to poll all file descriptors
    5295                 :             :  * 
    5296                 :             :  * Sets the function to use to handle polling of file descriptors.
    5297                 :             :  *
    5298                 :             :  * It will be used instead of the [`poll()`](man:poll(2)) system call
    5299                 :             :  * (or GLib’s replacement function, which is used where
    5300                 :             :  * `poll()` isn’t available).
    5301                 :             :  *
    5302                 :             :  * This function could possibly be used to integrate the GLib event
    5303                 :             :  * loop with an external event loop.
    5304                 :             :  **/
    5305                 :             : void
    5306                 :           0 : g_main_context_set_poll_func (GMainContext *context,
    5307                 :             :                               GPollFunc     func)
    5308                 :             : {
    5309                 :           0 :   if (!context)
    5310                 :           0 :     context = g_main_context_default ();
    5311                 :             :   
    5312                 :           0 :   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
    5313                 :             : 
    5314                 :           0 :   LOCK_CONTEXT (context);
    5315                 :             :   
    5316                 :           0 :   if (func)
    5317                 :           0 :     context->poll_func = func;
    5318                 :             :   else
    5319                 :           0 :     context->poll_func = g_poll;
    5320                 :             : 
    5321                 :           0 :   UNLOCK_CONTEXT (context);
    5322                 :           0 : }
    5323                 :             : 
    5324                 :             : /**
    5325                 :             :  * g_main_context_get_poll_func:
    5326                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    5327                 :             :  *   main context will be used)
    5328                 :             :  * 
    5329                 :             :  * Gets the poll function set by [method@GLib.MainContext.set_poll_func].
    5330                 :             :  * 
    5331                 :             :  * Returns: the poll function
    5332                 :             :  **/
    5333                 :             : GPollFunc
    5334                 :           0 : g_main_context_get_poll_func (GMainContext *context)
    5335                 :             : {
    5336                 :             :   GPollFunc result;
    5337                 :             :   
    5338                 :           0 :   if (!context)
    5339                 :           0 :     context = g_main_context_default ();
    5340                 :             :   
    5341                 :           0 :   g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
    5342                 :             : 
    5343                 :           0 :   LOCK_CONTEXT (context);
    5344                 :           0 :   result = context->poll_func;
    5345                 :           0 :   UNLOCK_CONTEXT (context);
    5346                 :             : 
    5347                 :           0 :   return result;
    5348                 :           0 : }
    5349                 :             : 
    5350                 :             : /**
    5351                 :             :  * g_main_context_wakeup:
    5352                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    5353                 :             :  *   main context will be used)
    5354                 :             :  * 
    5355                 :             :  * Wake up @context if it’s currently blocking in
    5356                 :             :  * [method@GLib.MainContext.iteration], causing it to stop blocking.
    5357                 :             :  *
    5358                 :             :  * The @context could be blocking waiting for a source to become ready.
    5359                 :             :  * Otherwise, if @context is not currently blocking, this function causes the
    5360                 :             :  * next invocation of [method@GLib.MainContext.iteration] to return without
    5361                 :             :  * blocking.
    5362                 :             :  *
    5363                 :             :  * This API is useful for low-level control over [struct@GLib.MainContext]; for
    5364                 :             :  * example, integrating it with main loop implementations such as
    5365                 :             :  * [struct@GLib.MainLoop].
    5366                 :             :  *
    5367                 :             :  * Another related use for this function is when implementing a main
    5368                 :             :  * loop with a termination condition, computed from multiple threads:
    5369                 :             :  *
    5370                 :             :  * ```c
    5371                 :             :  *   #define NUM_TASKS 10
    5372                 :             :  *   static gint tasks_remaining = NUM_TASKS;  // (atomic)
    5373                 :             :  *   ...
    5374                 :             :  *  
    5375                 :             :  *   while (g_atomic_int_get (&tasks_remaining) != 0)
    5376                 :             :  *     g_main_context_iteration (NULL, TRUE);
    5377                 :             :  * ```
    5378                 :             :  *  
    5379                 :             :  * Then in a thread:
    5380                 :             :  * ```c
    5381                 :             :  *   perform_work ();
    5382                 :             :  *
    5383                 :             :  *   if (g_atomic_int_dec_and_test (&tasks_remaining))
    5384                 :             :  *     g_main_context_wakeup (NULL);
    5385                 :             :  * ```
    5386                 :             :  **/
    5387                 :             : void
    5388                 :        1239 : g_main_context_wakeup (GMainContext *context)
    5389                 :             : {
    5390                 :        1239 :   if (!context)
    5391                 :        1008 :     context = g_main_context_default ();
    5392                 :             : 
    5393                 :        1239 :   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
    5394                 :             : 
    5395                 :        1088 :   TRACE (GLIB_MAIN_CONTEXT_WAKEUP (context));
    5396                 :             : 
    5397                 :        1239 :   g_wakeup_signal (context->wakeup);
    5398                 :         151 : }
    5399                 :             : 
    5400                 :             : /**
    5401                 :             :  * g_main_context_is_owner:
    5402                 :             :  * @context: (nullable): a main context (if `NULL`, the global-default
    5403                 :             :  *   main context will be used)
    5404                 :             :  * 
    5405                 :             :  * Determines whether this thread holds the (recursive)
    5406                 :             :  * ownership of this [struct@GLib.MainContext].
    5407                 :             :  *
    5408                 :             :  * This is useful to
    5409                 :             :  * know before waiting on another thread that may be
    5410                 :             :  * blocking to get ownership of @context.
    5411                 :             :  *
    5412                 :             :  * Returns: true if current thread is owner of @context, false otherwise
    5413                 :             :  * Since: 2.10
    5414                 :             :  **/
    5415                 :             : gboolean
    5416                 :         310 : g_main_context_is_owner (GMainContext *context)
    5417                 :             : {
    5418                 :             :   gboolean is_owner;
    5419                 :             : 
    5420                 :         310 :   if (!context)
    5421                 :           0 :     context = g_main_context_default ();
    5422                 :             : 
    5423                 :         310 :   LOCK_CONTEXT (context);
    5424                 :         310 :   is_owner = context->owner == G_THREAD_SELF;
    5425                 :         310 :   UNLOCK_CONTEXT (context);
    5426                 :             : 
    5427                 :         310 :   return is_owner;
    5428                 :             : }
    5429                 :             : 
    5430                 :             : /* Timeouts */
    5431                 :             : 
    5432                 :             : static void
    5433                 :       31727 : g_timeout_set_expiration (GTimeoutSource *timeout_source,
    5434                 :             :                           uint64_t        current_time_ns)
    5435                 :             : {
    5436                 :             :   uint64_t expiration_ns;
    5437                 :             : 
    5438                 :       31727 :   if (timeout_source->seconds)
    5439                 :             :     {
    5440                 :             :       static gsize timer_perturb;
    5441                 :             :       gsize perturb;
    5442                 :             :       uint64_t remainder_ns;
    5443                 :             : 
    5444                 :       24162 :       if (g_once_init_enter (&timer_perturb))
    5445                 :             :         {
    5446                 :             :           /*
    5447                 :             :            * we want a per machine/session unique 'random' value; try the dbus
    5448                 :             :            * address first, that has a UUID in it. If there is no dbus, use the
    5449                 :             :            * hostname for hashing.
    5450                 :             :            */
    5451                 :          36 :           const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
    5452                 :          36 :           if (!session_bus_address)
    5453                 :          20 :             session_bus_address = g_getenv ("HOSTNAME");
    5454                 :          36 :           if (session_bus_address)
    5455                 :          36 :             perturb = ABS ((gint) g_str_hash (session_bus_address)) % G_NSEC_PER_SEC;
    5456                 :             :           else
    5457                 :           0 :             perturb = 0;
    5458                 :             : 
    5459                 :             :           /* g_once_init_leave() treats 0 as "not initialised", so add 1. */
    5460                 :          36 :           g_once_init_leave (&timer_perturb, perturb + 1);
    5461                 :           6 :         }
    5462                 :             : 
    5463                 :             :       /* Remove the extra value added during initialization. */
    5464                 :       24162 :       perturb = timer_perturb - 1;
    5465                 :             : 
    5466                 :       24162 :       expiration_ns = current_time_ns + timeout_source->interval * G_NSEC_PER_SEC;
    5467                 :             : 
    5468                 :             :       /* We want the microseconds part of the timeout to land on the
    5469                 :             :        * 'timer_perturb' mark, but we need to make sure we don't try to
    5470                 :             :        * set the timeout in the past.  We do this by ensuring that we
    5471                 :             :        * always only *increase* the expiration time by adding a full
    5472                 :             :        * second in the case that the microsecond portion decreases.
    5473                 :             :        */
    5474                 :       24162 :       expiration_ns -= perturb;
    5475                 :             : 
    5476                 :       24162 :       remainder_ns = expiration_ns % G_NSEC_PER_SEC;
    5477                 :       24162 :       if (remainder_ns >= G_NSEC_PER_SEC / 4)
    5478                 :       18763 :         expiration_ns += G_NSEC_PER_SEC;
    5479                 :             : 
    5480                 :       24162 :       expiration_ns -= remainder_ns;
    5481                 :       24162 :       expiration_ns += perturb;
    5482                 :       12017 :     }
    5483                 :             :   else
    5484                 :             :     {
    5485                 :        7565 :       if (G_UNLIKELY (!g_uint64_checked_add (&expiration_ns, current_time_ns, timeout_source->interval)))
    5486                 :           0 :         expiration_ns = UINT64_MAX;
    5487                 :             :     }
    5488                 :             : 
    5489                 :       31727 :   g_source_set_ready_time_ns ((GSource *) timeout_source, expiration_ns);
    5490                 :       31727 : }
    5491                 :             : 
    5492                 :             : static gboolean
    5493                 :        1532 : g_timeout_dispatch (GSource     *source,
    5494                 :             :                     GSourceFunc  callback,
    5495                 :             :                     gpointer     user_data)
    5496                 :             : {
    5497                 :        1532 :   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
    5498                 :             :   gboolean again;
    5499                 :             : 
    5500                 :        1532 :   if (!callback)
    5501                 :             :     {
    5502                 :           0 :       g_warning ("Timeout source dispatched without callback. "
    5503                 :             :                  "You must call g_source_set_callback().");
    5504                 :           0 :       return FALSE;
    5505                 :             :     }
    5506                 :             : 
    5507                 :        1532 :   if (timeout_source->one_shot)
    5508                 :             :     {
    5509                 :          17 :       GSourceOnceFunc once_callback = (GSourceOnceFunc) callback;
    5510                 :          17 :       once_callback (user_data);
    5511                 :          17 :       again = G_SOURCE_REMOVE;
    5512                 :           8 :     }
    5513                 :             :   else
    5514                 :             :     {
    5515                 :        1515 :       again = callback (user_data);
    5516                 :             :     }
    5517                 :             : 
    5518                 :        1158 :   TRACE (GLIB_TIMEOUT_DISPATCH (source, source->context, callback, user_data, again));
    5519                 :             : 
    5520                 :        1532 :   if (again)
    5521                 :         389 :     g_timeout_set_expiration (timeout_source, g_source_get_time_ns (source));
    5522                 :             : 
    5523                 :        1532 :   return again;
    5524                 :         374 : }
    5525                 :             : 
    5526                 :             : static GSource *
    5527                 :       31339 : timeout_source_new (uint64_t interval,
    5528                 :             :                     gboolean seconds,
    5529                 :             :                     gboolean one_shot)
    5530                 :             : {
    5531                 :       31339 :   GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
    5532                 :       31339 :   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
    5533                 :             : 
    5534                 :       31339 :   timeout_source->interval = interval;
    5535                 :       31339 :   timeout_source->seconds = seconds;
    5536                 :       31339 :   timeout_source->one_shot = one_shot;
    5537                 :             : 
    5538                 :       31339 :   g_timeout_set_expiration (timeout_source, g_get_monotonic_time_ns ());
    5539                 :             : 
    5540                 :       31339 :   return source;
    5541                 :             : }
    5542                 :             : 
    5543                 :             : /**
    5544                 :             :  * g_timeout_source_new:
    5545                 :             :  * @interval: the timeout interval in milliseconds
    5546                 :             :  * 
    5547                 :             :  * Creates a new timeout source.
    5548                 :             :  *
    5549                 :             :  * The source will not initially be associated with any [struct@GLib.MainContext]
    5550                 :             :  * and must be added to one with [method@GLib.Source.attach] before it will be
    5551                 :             :  * executed.
    5552                 :             :  *
    5553                 :             :  * The interval given is in terms of monotonic time, not wall clock
    5554                 :             :  * time.  See [func@GLib.get_monotonic_time].
    5555                 :             :  * 
    5556                 :             :  * Returns: (transfer full): the newly-created timeout source
    5557                 :             :  **/
    5558                 :             : GSource *
    5559                 :        6462 : g_timeout_source_new (guint interval)
    5560                 :             : {
    5561                 :        6462 :   return timeout_source_new ((uint64_t) interval * (G_NSEC_PER_SEC / 1000), FALSE, FALSE);
    5562                 :             : }
    5563                 :             : 
    5564                 :             : /**
    5565                 :             :  * g_timeout_source_new_ns:
    5566                 :             :  * @interval: the timeout interval in nanoseconds
    5567                 :             :  * 
    5568                 :             :  * Creates a new timeout source.
    5569                 :             :  *
    5570                 :             :  * The source will not initially be associated with any [struct@GLib.MainContext]
    5571                 :             :  * and must be added to one with [method@GLib.Source.attach] before it will be
    5572                 :             :  * executed.
    5573                 :             :  *
    5574                 :             :  * The interval given is in terms of monotonic time, not wall clock
    5575                 :             :  * time.  See [func@GLib.get_monotonic_time_ns].
    5576                 :             :  *
    5577                 :             :  * Returns: (transfer full): the newly-created timeout source
    5578                 :             :  * Since: 2.90
    5579                 :             :  **/
    5580                 :             : GSource *
    5581                 :           2 : g_timeout_source_new_ns (uint64_t interval)
    5582                 :             : {
    5583                 :           2 :   return timeout_source_new (interval, FALSE, FALSE);
    5584                 :             : }
    5585                 :             : 
    5586                 :             : /**
    5587                 :             :  * g_timeout_source_new_seconds:
    5588                 :             :  * @interval: the timeout interval in seconds
    5589                 :             :  *
    5590                 :             :  * Creates a new timeout source.
    5591                 :             :  *
    5592                 :             :  * The source will not initially be associated with any
    5593                 :             :  * [struct@GLib.MainContext] and must be added to one with
    5594                 :             :  * [method@GLib.Source.attach] before it will be executed.
    5595                 :             :  *
    5596                 :             :  * The scheduling granularity/accuracy of this timeout source will be
    5597                 :             :  * in seconds.
    5598                 :             :  *
    5599                 :             :  * The interval given is in terms of monotonic time, not wall clock time.
    5600                 :             :  * See [func@GLib.get_monotonic_time].
    5601                 :             :  *
    5602                 :             :  * Returns: (transfer full): the newly-created timeout source
    5603                 :             :  * Since: 2.14
    5604                 :             :  **/
    5605                 :             : GSource *
    5606                 :       24031 : g_timeout_source_new_seconds (guint interval)
    5607                 :             : {
    5608                 :       24031 :   return timeout_source_new (interval, TRUE, FALSE);
    5609                 :             : }
    5610                 :             : 
    5611                 :             : static guint
    5612                 :         844 : timeout_add_full (gint           priority,
    5613                 :             :                   uint64_t       interval,
    5614                 :             :                   gboolean       seconds,
    5615                 :             :                   gboolean       one_shot,
    5616                 :             :                   GSourceFunc    function,
    5617                 :             :                   gpointer       data,
    5618                 :             :                   GDestroyNotify notify)
    5619                 :             : {
    5620                 :             :   GSource *source;
    5621                 :             :   guint id;
    5622                 :             : 
    5623                 :         844 :   g_return_val_if_fail (function != NULL, 0);
    5624                 :             : 
    5625                 :         844 :   source = timeout_source_new (interval, seconds, one_shot);
    5626                 :             : 
    5627                 :         844 :   if (priority != G_PRIORITY_DEFAULT)
    5628                 :           2 :     g_source_set_priority (source, priority);
    5629                 :             : 
    5630                 :         844 :   g_source_set_callback (source, function, data, notify);
    5631                 :         844 :   id = g_source_attach (source, NULL);
    5632                 :             : 
    5633                 :         558 :   TRACE (GLIB_TIMEOUT_ADD (source, g_main_context_default (), id, priority, interval, function, data));
    5634                 :             : 
    5635                 :         844 :   g_source_unref (source);
    5636                 :             : 
    5637                 :         844 :   return id;
    5638                 :         286 : }
    5639                 :             : 
    5640                 :             : /**
    5641                 :             :  * g_timeout_add_full: (rename-to g_timeout_add)
    5642                 :             :  * @priority: the priority of the timeout source; typically this will be in
    5643                 :             :  *   the range between [const@GLib.PRIORITY_DEFAULT] and
    5644                 :             :  *   [const@GLib.PRIORITY_HIGH]
    5645                 :             :  * @interval: the time between calls to the function, in milliseconds
    5646                 :             :  * @function: function to call
    5647                 :             :  * @data: data to pass to @function
    5648                 :             :  * @notify: (nullable): function to call when the timeout is removed
    5649                 :             :  * 
    5650                 :             :  * Sets a function to be called at regular intervals, with the given
    5651                 :             :  * priority.
    5652                 :             :  *
    5653                 :             :  * The function is called repeatedly until it returns
    5654                 :             :  * [const@GLib.SOURCE_REMOVE], at which point the timeout is automatically
    5655                 :             :  * destroyed and
    5656                 :             :  * the function will not be called again.  The @notify function is
    5657                 :             :  * called when the timeout is destroyed.  The first call to the
    5658                 :             :  * function will be at the end of the first @interval.
    5659                 :             :  *
    5660                 :             :  * Note that timeout functions may be delayed, due to the processing of other
    5661                 :             :  * event sources. Thus they should not be relied on for precise timing.
    5662                 :             :  * After each call to the timeout function, the time of the next
    5663                 :             :  * timeout is recalculated based on the current time and the given interval
    5664                 :             :  * (it does not try to ‘catch up’ time lost in delays).
    5665                 :             :  *
    5666                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    5667                 :             :  * on how to handle the return value and memory management of @data.
    5668                 :             :  *
    5669                 :             :  * This internally creates a main loop source using
    5670                 :             :  * [func@GLib.timeout_source_new] and attaches it to the global
    5671                 :             :  * [struct@GLib.MainContext] using [method@GLib.Source.attach], so the callback
    5672                 :             :  * will be invoked in whichever thread is running that main context. You can do
    5673                 :             :  * these steps manually if you need greater control or to use a custom main
    5674                 :             :  * context.
    5675                 :             :  *
    5676                 :             :  * The interval given is in terms of monotonic time, not wall clock time.
    5677                 :             :  * See [func@GLib.get_monotonic_time].
    5678                 :             :  * 
    5679                 :             :  * Returns: the ID (greater than 0) of the event source
    5680                 :             :  **/
    5681                 :             : guint
    5682                 :         704 : g_timeout_add_full (gint           priority,
    5683                 :             :                     guint          interval,
    5684                 :             :                     GSourceFunc    function,
    5685                 :             :                     gpointer       data,
    5686                 :             :                     GDestroyNotify notify)
    5687                 :             : {
    5688                 :         704 :   return timeout_add_full (priority, (uint64_t) interval * (G_NSEC_PER_SEC / 1000), FALSE, FALSE, function, data, notify);
    5689                 :             : }
    5690                 :             : 
    5691                 :             : /**
    5692                 :             :  * g_timeout_add:
    5693                 :             :  * @interval: the time between calls to the function, in milliseconds
    5694                 :             :  * @function: function to call
    5695                 :             :  * @data: data to pass to @function
    5696                 :             :  *
    5697                 :             :  * Sets a function to be called at regular intervals, with the default
    5698                 :             :  * priority, [const@GLib.PRIORITY_DEFAULT].
    5699                 :             :  *
    5700                 :             :  * The given @function is called repeatedly until it returns
    5701                 :             :  * [const@GLib.SOURCE_REMOVE], at which point the timeout is
    5702                 :             :  * automatically destroyed and the function will not be called again. The first
    5703                 :             :  * call to the function will be at the end of the first @interval.
    5704                 :             :  *
    5705                 :             :  * Note that timeout functions may be delayed, due to the processing of other
    5706                 :             :  * event sources. Thus they should not be relied on for precise timing.
    5707                 :             :  * After each call to the timeout function, the time of the next
    5708                 :             :  * timeout is recalculated based on the current time and the given interval
    5709                 :             :  * (it does not try to ‘catch up’ time lost in delays).
    5710                 :             :  *
    5711                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    5712                 :             :  * on how to handle the return value and memory management of @data.
    5713                 :             :  *
    5714                 :             :  * If you want to have a timer in the ‘seconds’ range and do not care
    5715                 :             :  * about the exact time of the first call of the timer, use the
    5716                 :             :  * [func@GLib.timeout_add_seconds] function; this function allows for more
    5717                 :             :  * optimizations and more efficient system power usage.
    5718                 :             :  *
    5719                 :             :  * This internally creates a main loop source using
    5720                 :             :  * [func@GLib.timeout_source_new] and attaches it to the global
    5721                 :             :  * [struct@GLib.MainContext] using [method@GLib.Source.attach], so the callback
    5722                 :             :  * will be invoked in whichever thread is running that main context. You can do
    5723                 :             :  * these steps manually if you need greater control or to use a custom main
    5724                 :             :  * context.
    5725                 :             :  *
    5726                 :             :  * It is safe to call this function from any thread.
    5727                 :             :  *
    5728                 :             :  * The interval given is in terms of monotonic time, not wall clock
    5729                 :             :  * time. See [func@GLib.get_monotonic_time].
    5730                 :             :  *
    5731                 :             :  * Returns: the ID (greater than 0) of the event source
    5732                 :             :  **/
    5733                 :             : guint
    5734                 :         312 : g_timeout_add (guint32        interval,
    5735                 :             :                GSourceFunc    function,
    5736                 :             :                gpointer       data)
    5737                 :             : {
    5738                 :         312 :   return g_timeout_add_full (G_PRIORITY_DEFAULT, 
    5739                 :          69 :                              interval, function, data, NULL);
    5740                 :             : }
    5741                 :             : 
    5742                 :             : /**
    5743                 :             :  * g_timeout_add_once:
    5744                 :             :  * @interval: the time after which the function will be called, in milliseconds
    5745                 :             :  * @function: function to call
    5746                 :             :  * @data: data to pass to @function
    5747                 :             :  *
    5748                 :             :  * Sets a function to be called after @interval milliseconds have elapsed,
    5749                 :             :  * with the default priority, [const@GLib.PRIORITY_DEFAULT].
    5750                 :             :  *
    5751                 :             :  * The given @function is called once and then the source will be automatically
    5752                 :             :  * removed from the main context.
    5753                 :             :  *
    5754                 :             :  * This function otherwise behaves like [func@GLib.timeout_add].
    5755                 :             :  *
    5756                 :             :  * Returns: the ID (greater than 0) of the event source
    5757                 :             :  * Since: 2.74
    5758                 :             :  */
    5759                 :             : guint
    5760                 :          28 : g_timeout_add_once (guint32         interval,
    5761                 :             :                     GSourceOnceFunc function,
    5762                 :             :                     gpointer        data)
    5763                 :             : {
    5764                 :          28 :   return timeout_add_full (G_PRIORITY_DEFAULT, (uint64_t) interval * (G_NSEC_PER_SEC / 1000), FALSE, TRUE, (GSourceFunc) function, data, NULL);
    5765                 :             : }
    5766                 :             : 
    5767                 :             : /**
    5768                 :             :  * g_timeout_add_seconds_full: (rename-to g_timeout_add_seconds)
    5769                 :             :  * @priority: the priority of the timeout source; typically this will be in
    5770                 :             :  *   the range between [const@GLib.PRIORITY_DEFAULT] and
    5771                 :             :  *   [const@GLib.PRIORITY_HIGH]
    5772                 :             :  * @interval: the time between calls to the function, in seconds
    5773                 :             :  * @function: function to call
    5774                 :             :  * @data: data to pass to @function
    5775                 :             :  * @notify: (nullable): function to call when the timeout is removed
    5776                 :             :  *
    5777                 :             :  * Sets a function to be called at regular intervals, with @priority.
    5778                 :             :  *
    5779                 :             :  * The function is called repeatedly until it returns [const@GLib.SOURCE_REMOVE],
    5780                 :             :  * at which point the timeout is automatically destroyed and
    5781                 :             :  * the function will not be called again.
    5782                 :             :  *
    5783                 :             :  * Unlike [func@GLib.timeout_add], this function operates at whole second
    5784                 :             :  * granularity. The initial starting point of the timer is determined by the
    5785                 :             :  * implementation and the implementation is expected to group multiple timers
    5786                 :             :  * together so that they fire all at the same time. To allow this grouping,
    5787                 :             :  * the @interval to the first timer is rounded and can deviate up to one second
    5788                 :             :  * from the specified interval. Subsequent timer iterations will generally run
    5789                 :             :  * at the specified interval.
    5790                 :             :  *
    5791                 :             :  * Note that timeout functions may be delayed, due to the processing of other
    5792                 :             :  * event sources. Thus they should not be relied on for precise timing.
    5793                 :             :  * After each call to the timeout function, the time of the next
    5794                 :             :  * timeout is recalculated based on the current time and the given @interval
    5795                 :             :  *
    5796                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    5797                 :             :  * on how to handle the return value and memory management of @data.
    5798                 :             :  *
    5799                 :             :  * If you want timing more precise than whole seconds, use
    5800                 :             :  * [func@GLib.timeout_add] instead.
    5801                 :             :  *
    5802                 :             :  * The grouping of timers to fire at the same time results in a more power
    5803                 :             :  * and CPU efficient behavior so if your timer is in multiples of seconds
    5804                 :             :  * and you don’t require the first timer exactly one second from now, the
    5805                 :             :  * use of [func@GLib.timeout_add_seconds] is preferred over
    5806                 :             :  * [func@GLib.timeout_add].
    5807                 :             :  *
    5808                 :             :  * This internally creates a main loop source using
    5809                 :             :  * [func@GLib.timeout_source_new_seconds] and attaches it to the main loop
    5810                 :             :  * context using [method@GLib.Source.attach]. You can do these steps manually
    5811                 :             :  * if you need greater control.
    5812                 :             :  *
    5813                 :             :  * It is safe to call this function from any thread.
    5814                 :             :  *
    5815                 :             :  * The interval given is in terms of monotonic time, not wall clock
    5816                 :             :  * time. See [func@GLib.get_monotonic_time].
    5817                 :             :  *
    5818                 :             :  * Returns: the ID (greater than 0) of the event source
    5819                 :             :  * Since: 2.14
    5820                 :             :  **/
    5821                 :             : guint
    5822                 :          79 : g_timeout_add_seconds_full (gint           priority,
    5823                 :             :                             guint32        interval,
    5824                 :             :                             GSourceFunc    function,
    5825                 :             :                             gpointer       data,
    5826                 :             :                             GDestroyNotify notify)
    5827                 :             : {
    5828                 :          79 :   return timeout_add_full (priority, interval, TRUE, FALSE, function, data, notify);
    5829                 :             : }
    5830                 :             : 
    5831                 :             : /**
    5832                 :             :  * g_timeout_add_seconds:
    5833                 :             :  * @interval: the time between calls to the function, in seconds
    5834                 :             :  * @function: function to call
    5835                 :             :  * @data: data to pass to @function
    5836                 :             :  *
    5837                 :             :  * Sets a function to be called at regular intervals with the default
    5838                 :             :  * priority, [const@GLib.PRIORITY_DEFAULT].
    5839                 :             :  *
    5840                 :             :  * The function is called repeatedly until it returns [const@GLib.SOURCE_REMOVE],
    5841                 :             :  * at which point the timeout is automatically destroyed
    5842                 :             :  * and the function will not be called again.
    5843                 :             :  *
    5844                 :             :  * This internally creates a main loop source using
    5845                 :             :  * [func@GLib.timeout_source_new_seconds] and attaches it to the main loop context
    5846                 :             :  * using [method@GLib.Source.attach]. You can do these steps manually if you need
    5847                 :             :  * greater control. Also see [func@GLib.timeout_add_seconds_full].
    5848                 :             :  *
    5849                 :             :  * It is safe to call this function from any thread.
    5850                 :             :  *
    5851                 :             :  * Note that the first call of the timer may not be precise for timeouts
    5852                 :             :  * of one second. If you need finer precision and have such a timeout,
    5853                 :             :  * you may want to use [func@GLib.timeout_add] instead.
    5854                 :             :  *
    5855                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    5856                 :             :  * on how to handle the return value and memory management of @data.
    5857                 :             :  *
    5858                 :             :  * The interval given is in terms of monotonic time, not wall clock
    5859                 :             :  * time. See [func@GLib.get_monotonic_time].
    5860                 :             :  * 
    5861                 :             :  * Returns: the ID (greater than 0) of the event source
    5862                 :             :  * Since: 2.14
    5863                 :             :  **/
    5864                 :             : guint
    5865                 :          79 : g_timeout_add_seconds (guint       interval,
    5866                 :             :                        GSourceFunc function,
    5867                 :             :                        gpointer    data)
    5868                 :             : {
    5869                 :          79 :   g_return_val_if_fail (function != NULL, 0);
    5870                 :             : 
    5871                 :          79 :   return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
    5872                 :           5 : }
    5873                 :             : 
    5874                 :             : /**
    5875                 :             :  * g_timeout_add_seconds_once:
    5876                 :             :  * @interval: the time after which the function will be called, in seconds
    5877                 :             :  * @function: function to call
    5878                 :             :  * @data: data to pass to @function
    5879                 :             :  *
    5880                 :             :  * This function behaves like [func@GLib.timeout_add_once] but with a range in
    5881                 :             :  * seconds.
    5882                 :             :  *
    5883                 :             :  * Returns: the ID (greater than 0) of the event source
    5884                 :             :  * Since: 2.78
    5885                 :             :  */
    5886                 :             : guint
    5887                 :          33 : g_timeout_add_seconds_once (guint           interval,
    5888                 :             :                             GSourceOnceFunc function,
    5889                 :             :                             gpointer        data)
    5890                 :             : {
    5891                 :          33 :   return timeout_add_full (G_PRIORITY_DEFAULT, interval, TRUE, TRUE, (GSourceFunc) function, data, NULL);
    5892                 :             : }
    5893                 :             : 
    5894                 :             : /* Child watch functions */
    5895                 :             : 
    5896                 :             : #ifdef HAVE_PIDFD
    5897                 :             : static int
    5898                 :         585 : siginfo_t_to_wait_status (const siginfo_t *info)
    5899                 :             : {
    5900                 :             :   /* Each of these returns is essentially the inverse of WIFEXITED(),
    5901                 :             :    * WIFSIGNALED(), etc. */
    5902                 :         585 :   switch (info->si_code)
    5903                 :             :     {
    5904                 :         578 :     case CLD_EXITED:
    5905                 :         578 :       return W_EXITCODE (info->si_status, 0);
    5906                 :           7 :     case CLD_KILLED:
    5907                 :           7 :       return W_EXITCODE (0, info->si_status);
    5908                 :           0 :     case CLD_DUMPED:
    5909                 :           0 :       return W_EXITCODE (0, info->si_status | WCOREFLAG);
    5910                 :           0 :     case CLD_CONTINUED:
    5911                 :           0 :       return __W_CONTINUED;
    5912                 :           0 :     case CLD_STOPPED:
    5913                 :             :     case CLD_TRAPPED:
    5914                 :             :     default:
    5915                 :           0 :       return W_STOPCODE (info->si_status);
    5916                 :             :     }
    5917                 :             : }
    5918                 :             : #endif /* HAVE_PIDFD */
    5919                 :             : 
    5920                 :             : static gboolean
    5921                 :        6147 : g_child_watch_prepare (GSource *source,
    5922                 :             :                        gint    *timeout)
    5923                 :             : {
    5924                 :             : #ifdef G_OS_WIN32
    5925                 :        2120 :   return FALSE;
    5926                 :             : #else  /* G_OS_WIN32 */
    5927                 :             :   {
    5928                 :             :     GChildWatchSource *child_watch_source;
    5929                 :             : 
    5930                 :        4027 :     child_watch_source = (GChildWatchSource *) source;
    5931                 :             : 
    5932                 :        4027 :     if (child_watch_source->poll.fd >= 0)
    5933                 :        4027 :       return FALSE;
    5934                 :             : 
    5935                 :           0 :     return g_atomic_int_get (&child_watch_source->child_maybe_exited);
    5936                 :             :   }
    5937                 :             : #endif /* G_OS_WIN32 */
    5938                 :             : }
    5939                 :             : 
    5940                 :             : static gboolean
    5941                 :        6107 : g_child_watch_check (GSource *source)
    5942                 :             : {
    5943                 :             :   GChildWatchSource *child_watch_source;
    5944                 :             :   gboolean child_exited;
    5945                 :             : 
    5946                 :        6107 :   child_watch_source = (GChildWatchSource *) source;
    5947                 :             : 
    5948                 :             : #ifdef G_OS_WIN32
    5949                 :        2092 :   child_exited = !!(child_watch_source->poll.revents & G_IO_IN);
    5950                 :             : #else /* G_OS_WIN32 */
    5951                 :             : #ifdef HAVE_PIDFD
    5952                 :        4015 :   if (child_watch_source->poll.fd >= 0)
    5953                 :             :     {
    5954                 :        4015 :       child_exited = !!(child_watch_source->poll.revents & G_IO_IN);
    5955                 :        4015 :       return child_exited;
    5956                 :             :     }
    5957                 :             : #endif /* HAVE_PIDFD */
    5958                 :           0 :   child_exited = g_atomic_int_get (&child_watch_source->child_maybe_exited);
    5959                 :             : #endif /* G_OS_WIN32 */
    5960                 :             : 
    5961                 :        2092 :   return child_exited;
    5962                 :             : }
    5963                 :             : 
    5964                 :             : static void
    5965                 :         935 : g_child_watch_finalize (GSource *source)
    5966                 :             : {
    5967                 :             : #ifndef G_OS_WIN32
    5968                 :         587 :   GChildWatchSource *child_watch_source = (GChildWatchSource *) source;
    5969                 :             : 
    5970                 :         587 :   if (child_watch_source->poll.fd >= 0)
    5971                 :             :     {
    5972                 :         587 :       close (child_watch_source->poll.fd);
    5973                 :         587 :       return;
    5974                 :             :     }
    5975                 :             : 
    5976                 :           0 :   G_LOCK (unix_signal_lock);
    5977                 :           0 :   unix_child_watches = g_slist_remove (unix_child_watches, source);
    5978                 :           0 :   unref_unix_signal_handler_unlocked (SIGCHLD);
    5979                 :           0 :   G_UNLOCK (unix_signal_lock);
    5980                 :             : #endif /* G_OS_WIN32 */
    5981                 :         348 : }
    5982                 :             : 
    5983                 :             : #ifndef G_OS_WIN32
    5984                 :             : 
    5985                 :             : static void
    5986                 :          32 : wake_source (GSource *source)
    5987                 :             : {
    5988                 :             :   GMainContext *context;
    5989                 :             : 
    5990                 :             :   /* This should be thread-safe:
    5991                 :             :    *
    5992                 :             :    *  - if the source is currently being added to a context, that
    5993                 :             :    *    context will be woken up anyway
    5994                 :             :    *
    5995                 :             :    *  - if the source is currently being destroyed, we simply need not
    5996                 :             :    *    to crash:
    5997                 :             :    *
    5998                 :             :    *    - the memory for the source will remain valid until after the
    5999                 :             :    *      source finalize function was called (which would remove the
    6000                 :             :    *      source from the global list which we are currently holding the
    6001                 :             :    *      lock for)
    6002                 :             :    *
    6003                 :             :    *    - the GMainContext will either be NULL or point to a live
    6004                 :             :    *      GMainContext
    6005                 :             :    *
    6006                 :             :    *    - the GMainContext will remain valid since source_dup_main_context()
    6007                 :             :    *      gave us a ref or NULL
    6008                 :             :    *
    6009                 :             :    *  Since we are holding a lot of locks here, don't try to enter any
    6010                 :             :    *  more GMainContext functions for fear of dealock -- just hit the
    6011                 :             :    *  GWakeup and run.  Even if that's safe now, it could easily become
    6012                 :             :    *  unsafe with some very minor changes in the future, and signal
    6013                 :             :    *  handling is not the most well-tested codepath.
    6014                 :             :    */
    6015                 :          32 :   context = source_dup_main_context (source);
    6016                 :          32 :   if (context)
    6017                 :          32 :     g_wakeup_signal (context->wakeup);
    6018                 :             : 
    6019                 :          32 :   if (context)
    6020                 :          32 :     g_main_context_unref (context);
    6021                 :          32 : }
    6022                 :             : 
    6023                 :             : static void
    6024                 :          54 : dispatch_unix_signals_unlocked (void)
    6025                 :             : {
    6026                 :             :   gboolean pending[NSIG];
    6027                 :             :   GSList *node;
    6028                 :             :   gint i;
    6029                 :             : 
    6030                 :             :   /* clear this first in case another one arrives while we're processing */
    6031                 :          54 :   g_atomic_int_set (&any_unix_signal_pending, 0);
    6032                 :             : 
    6033                 :             :   /* We atomically test/clear the bit from the global array in case
    6034                 :             :    * other signals arrive while we are dispatching.
    6035                 :             :    *
    6036                 :             :    * We then can safely use our own array below without worrying about
    6037                 :             :    * races.
    6038                 :             :    */
    6039                 :        3564 :   for (i = 0; i < NSIG; i++)
    6040                 :             :     {
    6041                 :             :       /* Be very careful with (the volatile) unix_signal_pending.
    6042                 :             :        *
    6043                 :             :        * We must ensure that it's not possible that we clear it without
    6044                 :             :        * handling the signal.  We therefore must ensure that our pending
    6045                 :             :        * array has a field set (ie: we will do something about the
    6046                 :             :        * signal) before we clear the item in unix_signal_pending.
    6047                 :             :        *
    6048                 :             :        * Note specifically: we must check _our_ array.
    6049                 :             :        */
    6050                 :        3510 :       pending[i] = g_atomic_int_compare_and_exchange (&unix_signal_pending[i], 1, 0);
    6051                 :             :     }
    6052                 :             : 
    6053                 :             :   /* handle GChildWatchSource instances */
    6054                 :          54 :   if (pending[SIGCHLD])
    6055                 :             :     {
    6056                 :             :       /* The only way we can do this is to scan all of the children.
    6057                 :             :        *
    6058                 :             :        * The docs promise that we will not reap children that we are not
    6059                 :             :        * explicitly watching, so that ties our hands from calling
    6060                 :             :        * waitpid(-1).  We also can't use siginfo's si_pid field since if
    6061                 :             :        * multiple SIGCHLD arrive at the same time, one of them can be
    6062                 :             :        * dropped (since a given UNIX signal can only be pending once).
    6063                 :             :        */
    6064                 :           0 :       for (node = unix_child_watches; node; node = node->next)
    6065                 :             :         {
    6066                 :           0 :           GChildWatchSource *source = node->data;
    6067                 :             : 
    6068                 :           0 :           if (g_atomic_int_compare_and_exchange (&source->child_maybe_exited, FALSE, TRUE))
    6069                 :           0 :             wake_source ((GSource *) source);
    6070                 :             :         }
    6071                 :             :     }
    6072                 :             : 
    6073                 :             :   /* handle GUnixSignalWatchSource instances */
    6074                 :         128 :   for (node = unix_signal_watches; node; node = node->next)
    6075                 :             :     {
    6076                 :          74 :       GUnixSignalWatchSource *source = node->data;
    6077                 :             : 
    6078                 :          74 :       if (pending[source->signum] &&
    6079                 :          32 :           g_atomic_int_compare_and_exchange (&source->pending, FALSE, TRUE))
    6080                 :             :         {
    6081                 :          32 :           wake_source ((GSource *) source);
    6082                 :             :         }
    6083                 :             :     }
    6084                 :             : 
    6085                 :          54 : }
    6086                 :             : 
    6087                 :             : static void
    6088                 :          22 : dispatch_unix_signals (void)
    6089                 :             : {
    6090                 :          22 :   G_LOCK(unix_signal_lock);
    6091                 :          22 :   dispatch_unix_signals_unlocked ();
    6092                 :          22 :   G_UNLOCK(unix_signal_lock);
    6093                 :          22 : }
    6094                 :             : 
    6095                 :             : static gboolean
    6096                 :          53 : g_unix_signal_watch_prepare (GSource *source,
    6097                 :             :                              gint    *timeout)
    6098                 :             : {
    6099                 :             :   GUnixSignalWatchSource *unix_signal_source;
    6100                 :             : 
    6101                 :          53 :   unix_signal_source = (GUnixSignalWatchSource *) source;
    6102                 :             : 
    6103                 :          53 :   return g_atomic_int_get (&unix_signal_source->pending);
    6104                 :             : }
    6105                 :             : 
    6106                 :             : static gboolean
    6107                 :          40 : g_unix_signal_watch_check (GSource  *source)
    6108                 :             : {
    6109                 :             :   GUnixSignalWatchSource *unix_signal_source;
    6110                 :             : 
    6111                 :          40 :   unix_signal_source = (GUnixSignalWatchSource *) source;
    6112                 :             : 
    6113                 :          40 :   return g_atomic_int_get (&unix_signal_source->pending);
    6114                 :             : }
    6115                 :             : 
    6116                 :             : static gboolean
    6117                 :          32 : g_unix_signal_watch_dispatch (GSource    *source, 
    6118                 :             :                               GSourceFunc callback,
    6119                 :             :                               gpointer    user_data)
    6120                 :             : {
    6121                 :             :   GUnixSignalWatchSource *unix_signal_source;
    6122                 :             :   gboolean again;
    6123                 :             : 
    6124                 :          32 :   unix_signal_source = (GUnixSignalWatchSource *) source;
    6125                 :             : 
    6126                 :          32 :   if (!callback)
    6127                 :             :     {
    6128                 :           0 :       g_warning ("Unix signal source dispatched without callback. "
    6129                 :             :                  "You must call g_source_set_callback().");
    6130                 :           0 :       return FALSE;
    6131                 :             :     }
    6132                 :             : 
    6133                 :          32 :   g_atomic_int_set (&unix_signal_source->pending, FALSE);
    6134                 :             : 
    6135                 :          32 :   again = (callback) (user_data);
    6136                 :             : 
    6137                 :          32 :   return again;
    6138                 :             : }
    6139                 :             : 
    6140                 :             : static void
    6141                 :          32 : ref_unix_signal_handler_unlocked (int signum)
    6142                 :             : {
    6143                 :             :   /* Ensure we have the worker context */
    6144                 :          32 :   g_get_worker_context ();
    6145                 :          32 :   unix_signal_refcount[signum]++;
    6146                 :          32 :   if (unix_signal_refcount[signum] == 1)
    6147                 :             :     {
    6148                 :             :       struct sigaction action;
    6149                 :          22 :       action.sa_handler = g_unix_signal_handler;
    6150                 :          22 :       sigemptyset (&action.sa_mask);
    6151                 :             : #ifdef SA_RESTART
    6152                 :          22 :       action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
    6153                 :             : #else
    6154                 :             :       action.sa_flags = SA_NOCLDSTOP;
    6155                 :             : #endif
    6156                 :             : #ifdef SA_ONSTACK
    6157                 :          22 :       action.sa_flags |= SA_ONSTACK;
    6158                 :             : #endif
    6159                 :          22 :       sigaction (signum, &action, NULL);
    6160                 :             :     }
    6161                 :          32 : }
    6162                 :             : 
    6163                 :             : static void
    6164                 :          32 : unref_unix_signal_handler_unlocked (int signum)
    6165                 :             : {
    6166                 :          32 :   unix_signal_refcount[signum]--;
    6167                 :          32 :   if (unix_signal_refcount[signum] == 0)
    6168                 :             :     {
    6169                 :             :       struct sigaction action;
    6170                 :          22 :       memset (&action, 0, sizeof (action));
    6171                 :          22 :       action.sa_handler = SIG_DFL;
    6172                 :          22 :       sigemptyset (&action.sa_mask);
    6173                 :          22 :       sigaction (signum, &action, NULL);
    6174                 :             :     }
    6175                 :          32 : }
    6176                 :             : 
    6177                 :             : /* Return a const string to avoid allocations. We lose precision in the case the
    6178                 :             :  * @signum is unrecognised, but that’ll do. */
    6179                 :             : static const gchar *
    6180                 :          32 : signum_to_string (int signum)
    6181                 :             : {
    6182                 :             :   /* See `man 0P signal.h` */
    6183                 :             : #define SIGNAL(s) \
    6184                 :             :     case (s): \
    6185                 :             :       return ("GUnixSignalSource: " #s);
    6186                 :          32 :   switch (signum)
    6187                 :             :     {
    6188                 :             :     /* These signals are guaranteed to exist by POSIX. */
    6189                 :           0 :     SIGNAL (SIGABRT)
    6190                 :           0 :     SIGNAL (SIGFPE)
    6191                 :           0 :     SIGNAL (SIGILL)
    6192                 :           0 :     SIGNAL (SIGINT)
    6193                 :           0 :     SIGNAL (SIGSEGV)
    6194                 :           9 :     SIGNAL (SIGTERM)
    6195                 :             :     /* Frustratingly, these are not, and hence for brevity the list is
    6196                 :             :      * incomplete. */
    6197                 :             : #ifdef SIGALRM
    6198                 :           0 :     SIGNAL (SIGALRM)
    6199                 :             : #endif
    6200                 :             : #ifdef SIGCHLD
    6201                 :           0 :     SIGNAL (SIGCHLD)
    6202                 :             : #endif
    6203                 :             : #ifdef SIGHUP
    6204                 :          21 :     SIGNAL (SIGHUP)
    6205                 :             : #endif
    6206                 :             : #ifdef SIGKILL
    6207                 :           0 :     SIGNAL (SIGKILL)
    6208                 :             : #endif
    6209                 :             : #ifdef SIGPIPE
    6210                 :           0 :     SIGNAL (SIGPIPE)
    6211                 :             : #endif
    6212                 :             : #ifdef SIGQUIT
    6213                 :           0 :     SIGNAL (SIGQUIT)
    6214                 :             : #endif
    6215                 :             : #ifdef SIGSTOP
    6216                 :           0 :     SIGNAL (SIGSTOP)
    6217                 :             : #endif
    6218                 :             : #ifdef SIGUSR1
    6219                 :           1 :     SIGNAL (SIGUSR1)
    6220                 :             : #endif
    6221                 :             : #ifdef SIGUSR2
    6222                 :           0 :     SIGNAL (SIGUSR2)
    6223                 :             : #endif
    6224                 :             : #ifdef SIGPOLL
    6225                 :           0 :     SIGNAL (SIGPOLL)
    6226                 :             : #endif
    6227                 :             : #ifdef SIGPROF
    6228                 :           0 :     SIGNAL (SIGPROF)
    6229                 :             : #endif
    6230                 :             : #ifdef SIGTRAP
    6231                 :           0 :     SIGNAL (SIGTRAP)
    6232                 :             : #endif
    6233                 :           1 :     default:
    6234                 :           1 :       return "GUnixSignalSource: Unrecognized signal";
    6235                 :             :     }
    6236                 :             : #undef SIGNAL
    6237                 :             : }
    6238                 :             : 
    6239                 :             : GSource *
    6240                 :          32 : _g_main_create_unix_signal_watch (int signum)
    6241                 :             : {
    6242                 :             :   GSource *source;
    6243                 :             :   GUnixSignalWatchSource *unix_signal_source;
    6244                 :             : 
    6245                 :          32 :   source = g_source_new (&g_unix_signal_funcs, sizeof (GUnixSignalWatchSource));
    6246                 :          32 :   unix_signal_source = (GUnixSignalWatchSource *) source;
    6247                 :             : 
    6248                 :          32 :   unix_signal_source->signum = signum;
    6249                 :          32 :   unix_signal_source->pending = FALSE;
    6250                 :             : 
    6251                 :             :   /* Set a default name on the source, just in case the caller does not. */
    6252                 :          32 :   g_source_set_static_name (source, signum_to_string (signum));
    6253                 :             : 
    6254                 :          32 :   G_LOCK (unix_signal_lock);
    6255                 :          32 :   ref_unix_signal_handler_unlocked (signum);
    6256                 :          32 :   unix_signal_watches = g_slist_prepend (unix_signal_watches, unix_signal_source);
    6257                 :          32 :   dispatch_unix_signals_unlocked ();
    6258                 :          32 :   G_UNLOCK (unix_signal_lock);
    6259                 :             : 
    6260                 :          32 :   return source;
    6261                 :             : }
    6262                 :             : 
    6263                 :             : static void
    6264                 :          32 : g_unix_signal_watch_finalize (GSource    *source)
    6265                 :             : {
    6266                 :             :   GUnixSignalWatchSource *unix_signal_source;
    6267                 :             : 
    6268                 :          32 :   unix_signal_source = (GUnixSignalWatchSource *) source;
    6269                 :             : 
    6270                 :          32 :   G_LOCK (unix_signal_lock);
    6271                 :          32 :   unref_unix_signal_handler_unlocked (unix_signal_source->signum);
    6272                 :          32 :   unix_signal_watches = g_slist_remove (unix_signal_watches, source);
    6273                 :          32 :   G_UNLOCK (unix_signal_lock);
    6274                 :          32 : }
    6275                 :             : 
    6276                 :             : #endif /* G_OS_WIN32 */
    6277                 :             : 
    6278                 :             : static gboolean
    6279                 :         934 : g_child_watch_dispatch (GSource    *source, 
    6280                 :             :                         GSourceFunc callback,
    6281                 :             :                         gpointer    user_data)
    6282                 :             : {
    6283                 :             :   GChildWatchSource *child_watch_source;
    6284                 :         934 :   GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
    6285                 :             :   int wait_status;
    6286                 :             : 
    6287                 :         934 :   child_watch_source = (GChildWatchSource *) source;
    6288                 :             : 
    6289                 :             :   /* We only (try to) reap the child process right before dispatching the callback.
    6290                 :             :    * That way, the caller can rely that the process is there until the callback
    6291                 :             :    * is invoked; or, if the caller calls g_source_destroy() without the callback
    6292                 :             :    * being dispatched, the process is still not reaped. */
    6293                 :             : 
    6294                 :             : #ifdef G_OS_WIN32
    6295                 :             :   {
    6296                 :             :     DWORD child_status;
    6297                 :             : 
    6298                 :             :     /*
    6299                 :             :      * Note: We do _not_ check for the special value of STILL_ACTIVE
    6300                 :             :      * since we know that the process has exited and doing so runs into
    6301                 :             :      * problems if the child process "happens to return STILL_ACTIVE(259)"
    6302                 :             :      * as Microsoft's Platform SDK puts it.
    6303                 :             :      */
    6304                 :         348 :     if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
    6305                 :             :       {
    6306                 :           0 :         gchar *emsg = g_win32_error_message (GetLastError ());
    6307                 :           0 :         g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
    6308                 :           0 :         g_free (emsg);
    6309                 :             : 
    6310                 :             :         /* Unknown error. We got signaled that the process might be exited,
    6311                 :             :          * but now we failed to reap it? Assume the process is gone and proceed. */
    6312                 :           0 :         wait_status = -1;
    6313                 :           0 :       }
    6314                 :             :     else
    6315                 :         348 :       wait_status = child_status;
    6316                 :             :   }
    6317                 :             : #else /* G_OS_WIN32 */
    6318                 :             :   {
    6319                 :         586 :     gboolean child_exited = FALSE;
    6320                 :             : 
    6321                 :         586 :     wait_status = -1;
    6322                 :             : 
    6323                 :             : #ifdef HAVE_PIDFD
    6324                 :         586 :     if (child_watch_source->poll.fd >= 0)
    6325                 :             :       {
    6326                 :         586 :         siginfo_t child_info = {
    6327                 :             :           0,
    6328                 :             :         };
    6329                 :             : 
    6330                 :             :         /* Get the exit status */
    6331                 :         586 :         if (waitid (P_PIDFD, child_watch_source->poll.fd, &child_info, WEXITED | WNOHANG) >= 0)
    6332                 :             :           {
    6333                 :         585 :             if (child_info.si_pid != 0)
    6334                 :             :               {
    6335                 :             :                 /* waitid() helpfully provides the wait status in a decomposed
    6336                 :             :                  * form which is quite useful. Unfortunately we have to report it
    6337                 :             :                  * to the #GChildWatchFunc as a waitpid()-style platform-specific
    6338                 :             :                  * wait status, so that the user code in #GChildWatchFunc can then
    6339                 :             :                  * call WIFEXITED() (etc.) on it. That means re-composing the
    6340                 :             :                  * status information. */
    6341                 :         585 :                 wait_status = siginfo_t_to_wait_status (&child_info);
    6342                 :         585 :                 child_exited = TRUE;
    6343                 :             :               }
    6344                 :             :             else
    6345                 :             :               {
    6346                 :           0 :                 g_debug (G_STRLOC ": pidfd signaled but pid %" G_PID_FORMAT " didn't exit",
    6347                 :             :                          child_watch_source->pid);
    6348                 :           0 :                 return TRUE;
    6349                 :             :               }
    6350                 :             :           }
    6351                 :           1 :         else if (errno == EINVAL)
    6352                 :             :           {
    6353                 :             :             /* Linux 5.3, and some Android kernels which have pidfd_open()
    6354                 :             :              * backported, can poll a pidfd but do not support
    6355                 :             :              * waitid (P_PIDFD, ...). Reap the child with waitpid() below. */
    6356                 :             :           }
    6357                 :             :         else
    6358                 :             :           {
    6359                 :           0 :             int errsv = errno;
    6360                 :             : 
    6361                 :           0 :             g_warning (G_STRLOC ": waitid(pid:%" G_PID_FORMAT ", pidfd=%d) failed: %s (%d). %s",
    6362                 :             :                        child_watch_source->pid, child_watch_source->poll.fd, g_strerror (errsv), errsv,
    6363                 :             :                        "See documentation of g_child_watch_source_new() for possible causes.");
    6364                 :             : 
    6365                 :             :             /* Assume the process is gone and proceed. */
    6366                 :           0 :             child_exited = TRUE;
    6367                 :             :           }
    6368                 :             :       }
    6369                 :             : #endif /* HAVE_PIDFD*/
    6370                 :             : 
    6371                 :         586 :     if (!child_exited)
    6372                 :             :       {
    6373                 :             :         pid_t pid;
    6374                 :             :         int wstatus;
    6375                 :             : 
    6376                 :           1 :       waitpid_again:
    6377                 :             : 
    6378                 :             :         /* We must reset the flag before waitpid(). Otherwise, there would be a
    6379                 :             :          * race. */
    6380                 :           1 :         g_atomic_int_set (&child_watch_source->child_maybe_exited, FALSE);
    6381                 :             : 
    6382                 :           1 :         pid = waitpid (child_watch_source->pid, &wstatus, WNOHANG);
    6383                 :             : 
    6384                 :           1 :         if (G_UNLIKELY (pid < 0 && errno == EINTR))
    6385                 :           0 :           goto waitpid_again;
    6386                 :             : 
    6387                 :           1 :         if (pid == 0)
    6388                 :             :           {
    6389                 :             :             /* Not exited yet. Wait longer. */
    6390                 :           0 :             return TRUE;
    6391                 :             :           }
    6392                 :             : 
    6393                 :           1 :         if (pid > 0)
    6394                 :           1 :           wait_status = wstatus;
    6395                 :             :         else
    6396                 :             :           {
    6397                 :           0 :             int errsv = errno;
    6398                 :             : 
    6399                 :           0 :             g_warning (G_STRLOC ": waitpid(pid:%" G_PID_FORMAT ") failed: %s (%d). %s",
    6400                 :             :                        child_watch_source->pid, g_strerror (errsv), errsv,
    6401                 :             :                        "See documentation of g_child_watch_source_new() for possible causes.");
    6402                 :             : 
    6403                 :             :             /* Assume the process is gone and proceed. */
    6404                 :             :           }
    6405                 :             :       }
    6406                 :             :   }
    6407                 :             : #endif /* G_OS_WIN32 */
    6408                 :             : 
    6409                 :         934 :   if (!callback)
    6410                 :             :     {
    6411                 :           0 :       g_warning ("Child watch source dispatched without callback. "
    6412                 :             :                  "You must call g_source_set_callback().");
    6413                 :           0 :       return FALSE;
    6414                 :             :     }
    6415                 :             : 
    6416                 :         934 :   (child_watch_callback) (child_watch_source->pid, wait_status, user_data);
    6417                 :             : 
    6418                 :             :   /* We never keep a child watch source around as the child is gone */
    6419                 :         934 :   return FALSE;
    6420                 :         348 : }
    6421                 :             : 
    6422                 :             : #ifndef G_OS_WIN32
    6423                 :             : 
    6424                 :             : static void
    6425                 :          22 : g_unix_signal_handler (int signum)
    6426                 :             : {
    6427                 :          22 :   gint saved_errno = errno;
    6428                 :             : 
    6429                 :             : #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
    6430                 :          22 :   g_atomic_int_set (&unix_signal_pending[signum], 1);
    6431                 :          22 :   g_atomic_int_set (&any_unix_signal_pending, 1);
    6432                 :             : #else
    6433                 :             : #warning "Can't use atomics in g_unix_signal_handler(): Unix signal handling will be racy"
    6434                 :             :   unix_signal_pending[signum] = 1;
    6435                 :             :   any_unix_signal_pending = 1;
    6436                 :             : #endif
    6437                 :             : 
    6438                 :          22 :   g_wakeup_signal (glib_worker_context->wakeup);
    6439                 :             : 
    6440                 :          22 :   errno = saved_errno;
    6441                 :          22 : }
    6442                 :             : 
    6443                 :             : #endif /* !G_OS_WIN32 */
    6444                 :             : 
    6445                 :             : /**
    6446                 :             :  * g_child_watch_source_new:
    6447                 :             :  * @pid: process to watch — on POSIX systems, this is the positive PID of a
    6448                 :             :  *   child process; on Windows it is a handle for a process (which doesn’t have
    6449                 :             :  *   to be a child)
    6450                 :             :  * 
    6451                 :             :  * Creates a new child watch source.
    6452                 :             :  *
    6453                 :             :  * The source will not initially be associated with any
    6454                 :             :  * [struct@GLib.MainContext] and must be added to one with
    6455                 :             :  * [method@GLib.Source.attach] before it will be executed.
    6456                 :             :  *
    6457                 :             :  * Note that child watch sources can only be used in conjunction with
    6458                 :             :  * `g_spawn...` when the [flags@GLib.SpawnFlags.DO_NOT_REAP_CHILD] flag is used.
    6459                 :             :  *
    6460                 :             :  * Note that on platforms where [type@GLib.Pid] must be explicitly closed
    6461                 :             :  * (see [func@GLib.spawn_close_pid]) @pid must not be closed while the
    6462                 :             :  * source is still active. Typically, you will want to call
    6463                 :             :  * [func@GLib.spawn_close_pid] in the callback function for the source.
    6464                 :             :  *
    6465                 :             :  * On POSIX platforms, the following restrictions apply to this API
    6466                 :             :  * due to limitations in POSIX process interfaces:
    6467                 :             :  *
    6468                 :             :  * * @pid must be a child of this process.
    6469                 :             :  * * @pid must be positive.
    6470                 :             :  * * The application must not call [`waitpid()`](man:waitpid(1)) with a
    6471                 :             :  *   non-positive first argument, for instance in another thread.
    6472                 :             :  * * The application must not wait for @pid to exit by any other
    6473                 :             :  *   mechanism, including `waitpid(pid, ...)` or a second child-watch
    6474                 :             :  *   source for the same @pid.
    6475                 :             :  * * The application must not ignore `SIGCHLD`.
    6476                 :             :  * * Before 2.78, the application could not send a signal ([`kill()`](man:kill(2))) to the
    6477                 :             :  *   watched @pid in a race free manner. Since 2.78, you can do that while the
    6478                 :             :  *   associated [struct@GLib.MainContext] is acquired.
    6479                 :             :  * * Before 2.78, even after destroying the [struct@GLib.Source], you could not
    6480                 :             :  *   be sure that @pid wasn’t already reaped. Hence, it was also not
    6481                 :             :  *   safe to `kill()` or `waitpid()` on the process ID after the child watch
    6482                 :             :  *   source was gone. Destroying the source before it fired made it
    6483                 :             :  *   impossible to reliably reap the process.
    6484                 :             :  *
    6485                 :             :  * If any of those conditions are not met, this and related APIs will
    6486                 :             :  * not work correctly. This can often be diagnosed via a GLib warning
    6487                 :             :  * stating that `ECHILD` was received by `waitpid()`.
    6488                 :             :  *
    6489                 :             :  * Calling [`waitpid()`](man:waitpid(2)) for specific processes other than @pid
    6490                 :             :  * remains a valid thing to do.
    6491                 :             :  *
    6492                 :             :  * Returns: (transfer full): the newly-created child watch source
    6493                 :             :  * Since: 2.4
    6494                 :             :  **/
    6495                 :             : GSource *
    6496                 :         936 : g_child_watch_source_new (GPid pid)
    6497                 :             : {
    6498                 :             :   GSource *source;
    6499                 :             :   GChildWatchSource *child_watch_source;
    6500                 :             : #ifdef HAVE_PIDFD
    6501                 :             :   int errsv;
    6502                 :             : #endif
    6503                 :             : 
    6504                 :             : #ifndef G_OS_WIN32
    6505                 :         587 :   g_return_val_if_fail (pid > 0, NULL);
    6506                 :             : #endif
    6507                 :             : 
    6508                 :         936 :   source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
    6509                 :         936 :   child_watch_source = (GChildWatchSource *)source;
    6510                 :             : 
    6511                 :             :   /* Set a default name on the source, just in case the caller does not. */
    6512                 :         936 :   g_source_set_static_name (source, "GChildWatchSource");
    6513                 :             : 
    6514                 :         936 :   child_watch_source->pid = pid;
    6515                 :             : 
    6516                 :             : #ifdef G_OS_WIN32
    6517                 :         349 :   child_watch_source->poll.fd = (gintptr) pid;
    6518                 :         349 :   child_watch_source->poll.events = G_IO_IN;
    6519                 :             : 
    6520                 :         349 :   g_source_add_poll (source, &child_watch_source->poll);
    6521                 :             : #else /* !G_OS_WIN32 */
    6522                 :             : 
    6523                 :             : #ifdef HAVE_PIDFD
    6524                 :             :   /* Use a pidfd, if possible, to avoid having to install a global SIGCHLD
    6525                 :             :    * handler and potentially competing with any other library/code which wants
    6526                 :             :    * to install one.
    6527                 :             :    *
    6528                 :             :    * Unfortunately this use of pidfd isn’t race-free (the PID could be recycled
    6529                 :             :    * between the caller calling g_child_watch_source_new() and here), but it’s
    6530                 :             :    * better than SIGCHLD.
    6531                 :             :    */
    6532                 :         587 :   child_watch_source->poll.fd = (int) syscall (SYS_pidfd_open, pid, 0);
    6533                 :             : 
    6534                 :         587 :   if (child_watch_source->poll.fd >= 0)
    6535                 :             :     {
    6536                 :         587 :       child_watch_source->poll.events = G_IO_IN;
    6537                 :         587 :       g_source_add_poll (source, &child_watch_source->poll);
    6538                 :         587 :       return source;
    6539                 :             :     }
    6540                 :             : 
    6541                 :           0 :   errsv = errno;
    6542                 :           0 :   g_debug ("pidfd_open(%" G_PID_FORMAT ") failed with error: %s",
    6543                 :             :            pid, g_strerror (errsv));
    6544                 :             :   /* Fall through; likely the kernel isn’t new enough to support pidfd_open() */
    6545                 :             : #endif /* HAVE_PIDFD */
    6546                 :             : 
    6547                 :             :   /* We can do that without atomic, as the source is not yet added in
    6548                 :             :    * unix_child_watches (which we do next under a lock). */
    6549                 :           0 :   child_watch_source->child_maybe_exited = TRUE;
    6550                 :           0 :   child_watch_source->poll.fd = -1;
    6551                 :             : 
    6552                 :           0 :   G_LOCK (unix_signal_lock);
    6553                 :           0 :   ref_unix_signal_handler_unlocked (SIGCHLD);
    6554                 :           0 :   unix_child_watches = g_slist_prepend (unix_child_watches, child_watch_source);
    6555                 :           0 :   G_UNLOCK (unix_signal_lock);
    6556                 :             : #endif /* !G_OS_WIN32 */
    6557                 :             : 
    6558                 :         349 :   return source;
    6559                 :             : }
    6560                 :             : 
    6561                 :             : /**
    6562                 :             :  * g_child_watch_add_full: (rename-to g_child_watch_add)
    6563                 :             :  * @priority: the priority of the idle source; typically this will be in the
    6564                 :             :  *   range between [const@GLib.PRIORITY_DEFAULT_IDLE] and
    6565                 :             :  *   [const@GLib.PRIORITY_HIGH_IDLE]
    6566                 :             :  * @pid: process to watch — on POSIX systems, this is the positive PID of a
    6567                 :             :  *   child process; on Windows it is a handle for a process (which doesn’t have
    6568                 :             :  *   to be a child)
    6569                 :             :  * @function: function to call
    6570                 :             :  * @data: data to pass to @function
    6571                 :             :  * @notify: (nullable): function to call when the idle is removed
    6572                 :             :  * 
    6573                 :             :  * Sets a function to be called when the child indicated by @pid 
    6574                 :             :  * exits, at the priority @priority.
    6575                 :             :  *
    6576                 :             :  * If you obtain @pid from [func@GLib.spawn_async] or
    6577                 :             :  * [func@GLib.spawn_async_with_pipes] you will need to pass
    6578                 :             :  * [flags@GLib.SpawnFlags.DO_NOT_REAP_CHILD] as a flag to the spawn function for
    6579                 :             :  * the child watching to work.
    6580                 :             :  *
    6581                 :             :  * In many programs, you will want to call [func@GLib.spawn_check_wait_status]
    6582                 :             :  * in the callback to determine whether or not the child exited
    6583                 :             :  * successfully.
    6584                 :             :  *
    6585                 :             :  * Also, note that on platforms where [type@GLib.Pid] must be explicitly closed
    6586                 :             :  * (see [func@GLib.spawn_close_pid]) @pid must not be closed while the source
    6587                 :             :  * is still active.  Typically, you should invoke [func@GLib.spawn_close_pid]
    6588                 :             :  * in the callback function for the source.
    6589                 :             :  * 
    6590                 :             :  * GLib supports only a single callback per process ID.
    6591                 :             :  * On POSIX platforms, the same restrictions mentioned for
    6592                 :             :  * [func@GLib.child_watch_source_new] apply to this function.
    6593                 :             :  *
    6594                 :             :  * This internally creates a main loop source using 
    6595                 :             :  * [func@GLib.child_watch_source_new] and attaches it to the main loop context
    6596                 :             :  * using [method@GLib.Source.attach]. You can do these steps manually if you
    6597                 :             :  * need greater control.
    6598                 :             :  *
    6599                 :             :  * Returns: the ID (greater than 0) of the event source
    6600                 :             :  * Since: 2.4
    6601                 :             :  **/
    6602                 :             : guint
    6603                 :          43 : g_child_watch_add_full (gint            priority,
    6604                 :             :                         GPid            pid,
    6605                 :             :                         GChildWatchFunc function,
    6606                 :             :                         gpointer        data,
    6607                 :             :                         GDestroyNotify  notify)
    6608                 :             : {
    6609                 :             :   GSource *source;
    6610                 :             :   guint id;
    6611                 :             :   
    6612                 :          43 :   g_return_val_if_fail (function != NULL, 0);
    6613                 :             : #ifndef G_OS_WIN32
    6614                 :          37 :   g_return_val_if_fail (pid > 0, 0);
    6615                 :             : #endif
    6616                 :             : 
    6617                 :          43 :   source = g_child_watch_source_new (pid);
    6618                 :             : 
    6619                 :          43 :   if (priority != G_PRIORITY_DEFAULT)
    6620                 :           1 :     g_source_set_priority (source, priority);
    6621                 :             : 
    6622                 :          43 :   g_source_set_callback (source, (GSourceFunc) function, data, notify);
    6623                 :          43 :   id = g_source_attach (source, NULL);
    6624                 :          43 :   g_source_unref (source);
    6625                 :             : 
    6626                 :          43 :   return id;
    6627                 :           6 : }
    6628                 :             : 
    6629                 :             : /**
    6630                 :             :  * g_child_watch_add:
    6631                 :             :  * @pid: process to watch — on POSIX systems, this is the positive PID of a
    6632                 :             :  *   child process; on Windows it is a handle for a process (which doesn’t have
    6633                 :             :  *   to be a child)
    6634                 :             :  * @function: function to call
    6635                 :             :  * @data: data to pass to @function
    6636                 :             :  *
    6637                 :             :  * Sets a function to be called when the child indicated by @pid 
    6638                 :             :  * exits, at a default priority, [const@GLib.PRIORITY_DEFAULT].
    6639                 :             :  *
    6640                 :             :  * If you obtain @pid from [func@GLib.spawn_async] or
    6641                 :             :  * [func@GLib.spawn_async_with_pipes] you will need to pass
    6642                 :             :  * [flags@GLib.SpawnFlags.DO_NOT_REAP_CHILD] as a flag to the spawn function for
    6643                 :             :  * the child watching to work.
    6644                 :             :  *
    6645                 :             :  * Note that on platforms where [type@GLib.Pid] must be explicitly closed
    6646                 :             :  * (see [func@GLib.spawn_close_pid]) @pid must not be closed while the
    6647                 :             :  * source is still active. Typically, you will want to call
    6648                 :             :  * [func@GLib.spawn_close_pid] in the callback function for the source.
    6649                 :             :  *
    6650                 :             :  * GLib supports only a single callback per process ID.
    6651                 :             :  * On POSIX platforms, the same restrictions mentioned for
    6652                 :             :  * [func@GLib.child_watch_source_new] apply to this function.
    6653                 :             :  *
    6654                 :             :  * This internally creates a main loop source using 
    6655                 :             :  * [func@GLib.child_watch_source_new] and attaches it to the main loop context
    6656                 :             :  * using [method@GLib.Source.attach]. You can do these steps manually if you
    6657                 :             :  * need greater control.
    6658                 :             :  *
    6659                 :             :  * Returns: the ID (greater than 0) of the event source
    6660                 :             :  * Since: 2.4
    6661                 :             :  **/
    6662                 :             : guint 
    6663                 :          42 : g_child_watch_add (GPid            pid,
    6664                 :             :                    GChildWatchFunc function,
    6665                 :             :                    gpointer        data)
    6666                 :             : {
    6667                 :          42 :   return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
    6668                 :             : }
    6669                 :             : 
    6670                 :             : 
    6671                 :             : /* Idle functions */
    6672                 :             : 
    6673                 :             : static gboolean 
    6674                 :      535963 : g_idle_prepare  (GSource  *source,
    6675                 :             :                  gint     *timeout)
    6676                 :             : {
    6677                 :      535963 :   *timeout = 0;
    6678                 :             : 
    6679                 :      535963 :   return TRUE;
    6680                 :             : }
    6681                 :             : 
    6682                 :             : static gboolean 
    6683                 :       33689 : g_idle_check    (GSource  *source)
    6684                 :             : {
    6685                 :       33689 :   return TRUE;
    6686                 :             : }
    6687                 :             : 
    6688                 :             : static gboolean
    6689                 :      569650 : g_idle_dispatch (GSource    *source, 
    6690                 :             :                  GSourceFunc callback,
    6691                 :             :                  gpointer    user_data)
    6692                 :             : {
    6693                 :      569650 :   GIdleSource *idle_source = (GIdleSource *)source;
    6694                 :             :   gboolean again;
    6695                 :             : 
    6696                 :      569650 :   if (!callback)
    6697                 :             :     {
    6698                 :           0 :       g_warning ("Idle source dispatched without callback. "
    6699                 :             :                  "You must call g_source_set_callback().");
    6700                 :           0 :       return FALSE;
    6701                 :             :     }
    6702                 :             : 
    6703                 :      569650 :   if (idle_source->one_shot)
    6704                 :             :     {
    6705                 :         730 :       GSourceOnceFunc once_callback = (GSourceOnceFunc) callback;
    6706                 :         730 :       once_callback (user_data);
    6707                 :         730 :       again = G_SOURCE_REMOVE;
    6708                 :           2 :     }
    6709                 :             :   else
    6710                 :             :     {
    6711                 :      568920 :       again = callback (user_data);
    6712                 :             :     }
    6713                 :             : 
    6714                 :      352328 :   TRACE (GLIB_IDLE_DISPATCH (source, source->context, callback, user_data, again));
    6715                 :             : 
    6716                 :      569644 :   return again;
    6717                 :      217316 : }
    6718                 :             : 
    6719                 :             : static GSource *
    6720                 :      719889 : idle_source_new (gboolean one_shot)
    6721                 :             : {
    6722                 :             :   GSource *source;
    6723                 :             :   GIdleSource *idle_source;
    6724                 :             : 
    6725                 :      719889 :   source = g_source_new (&g_idle_funcs, sizeof (GIdleSource));
    6726                 :      719889 :   idle_source = (GIdleSource *) source;
    6727                 :             : 
    6728                 :      719889 :   idle_source->one_shot = one_shot;
    6729                 :             : 
    6730                 :      719889 :   g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
    6731                 :             : 
    6732                 :             :   /* Set a default name on the source, just in case the caller does not. */
    6733                 :      719889 :   g_source_set_static_name (source, "GIdleSource");
    6734                 :             : 
    6735                 :      719889 :   return source;
    6736                 :             : }
    6737                 :             : 
    6738                 :             : /**
    6739                 :             :  * g_idle_source_new:
    6740                 :             :  * 
    6741                 :             :  * Creates a new idle source.
    6742                 :             :  *
    6743                 :             :  * The source will not initially be associated with any
    6744                 :             :  * [struct@GLib.MainContext] and must be added to one with
    6745                 :             :  * [method@GLib.Source.attach] before it will be executed. Note that the
    6746                 :             :  * default priority for idle sources is [const@GLib.PRIORITY_DEFAULT_IDLE], as
    6747                 :             :  * compared to other sources which have a default priority of
    6748                 :             :  * [const@GLib.PRIORITY_DEFAULT].
    6749                 :             :  *
    6750                 :             :  * Returns: (transfer full): the newly-created idle source
    6751                 :             :  **/
    6752                 :             : GSource *
    6753                 :      718468 : g_idle_source_new (void)
    6754                 :             : {
    6755                 :      718468 :   return idle_source_new (FALSE);
    6756                 :             : }
    6757                 :             : 
    6758                 :             : static guint
    6759                 :        1421 : idle_add_full (gint           priority,
    6760                 :             :                gboolean       one_shot,
    6761                 :             :                GSourceFunc    function,
    6762                 :             :                gpointer       data,
    6763                 :             :                GDestroyNotify notify)
    6764                 :             : {
    6765                 :             :   GSource *source;
    6766                 :             :   guint id;
    6767                 :             : 
    6768                 :        1421 :   g_return_val_if_fail (function != NULL, 0);
    6769                 :             : 
    6770                 :        1421 :   source = idle_source_new (one_shot);
    6771                 :             : 
    6772                 :        1421 :   if (priority != G_PRIORITY_DEFAULT_IDLE)
    6773                 :          74 :     g_source_set_priority (source, priority);
    6774                 :             : 
    6775                 :        1421 :   g_source_set_callback (source, function, data, notify);
    6776                 :        1421 :   id = g_source_attach (source, NULL);
    6777                 :             : 
    6778                 :        1104 :   TRACE (GLIB_IDLE_ADD (source, g_main_context_default (), id, priority, function, data));
    6779                 :             : 
    6780                 :        1421 :   g_source_unref (source);
    6781                 :             : 
    6782                 :        1421 :   return id;
    6783                 :         317 : }
    6784                 :             : 
    6785                 :             : /**
    6786                 :             :  * g_idle_add_full: (rename-to g_idle_add)
    6787                 :             :  * @priority: the priority of the idle source; typically this will be in the
    6788                 :             :  *   range between [const@GLib.PRIORITY_DEFAULT_IDLE] and
    6789                 :             :  *   [const@GLib.PRIORITY_HIGH_IDLE]
    6790                 :             :  * @function: function to call
    6791                 :             :  * @data: data to pass to @function
    6792                 :             :  * @notify: (nullable): function to call when the idle is removed
    6793                 :             :  * 
    6794                 :             :  * Adds a function to be called whenever there are no higher priority
    6795                 :             :  * events pending.
    6796                 :             :  *
    6797                 :             :  * If the function returns [const@GLib.SOURCE_REMOVE] it is automatically
    6798                 :             :  * removed from the list of event sources and will not be called again.
    6799                 :             :  *
    6800                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    6801                 :             :  * on how to handle the return value and memory management of @data.
    6802                 :             :  *
    6803                 :             :  * This internally creates a main loop source using [func@GLib.idle_source_new]
    6804                 :             :  * and attaches it to the global [struct@GLib.MainContext] using
    6805                 :             :  * [method@GLib.Source.attach], so the callback will be invoked in whichever
    6806                 :             :  * thread is running that main context. You can do these steps manually if you
    6807                 :             :  * need greater control or to use a custom main context.
    6808                 :             :  *
    6809                 :             :  * Returns: the ID (greater than 0) of the event source
    6810                 :             :  **/
    6811                 :             : guint 
    6812                 :         679 : g_idle_add_full (gint           priority,
    6813                 :             :                  GSourceFunc    function,
    6814                 :             :                  gpointer       data,
    6815                 :             :                  GDestroyNotify notify)
    6816                 :             : {
    6817                 :         679 :   return idle_add_full (priority, FALSE, function, data, notify);
    6818                 :             : }
    6819                 :             : 
    6820                 :             : /**
    6821                 :             :  * g_idle_add:
    6822                 :             :  * @function: function to call
    6823                 :             :  * @data: data to pass to @function
    6824                 :             :  * 
    6825                 :             :  * Adds a function to be called whenever there are no higher priority
    6826                 :             :  * events pending to the default main loop.
    6827                 :             :  *
    6828                 :             :  * The function is given the
    6829                 :             :  * default idle priority, [const@GLib.PRIORITY_DEFAULT_IDLE].  If the function
    6830                 :             :  * returns [const@GLib.SOURCE_REMOVE] it is automatically removed from the list
    6831                 :             :  * of event sources and will not be called again.
    6832                 :             :  *
    6833                 :             :  * See [main loop memory management](main-loop.html#memory-management-of-sources) for details
    6834                 :             :  * on how to handle the return value and memory management of @data.
    6835                 :             :  *
    6836                 :             :  * This internally creates a main loop source using [func@GLib.idle_source_new]
    6837                 :             :  * and attaches it to the global [struct@GLib.MainContext] using
    6838                 :             :  * [method@GLib.Source.attach], so the callback will be invoked in whichever
    6839                 :             :  * thread is running that main context. You can do these steps manually if you
    6840                 :             :  * need greater control or to use a custom main context.
    6841                 :             :  *
    6842                 :             :  * Returns: the ID (greater than 0) of the event source
    6843                 :             :  **/
    6844                 :             : guint 
    6845                 :         605 : g_idle_add (GSourceFunc    function,
    6846                 :             :             gpointer       data)
    6847                 :             : {
    6848                 :         605 :   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
    6849                 :             : }
    6850                 :             : 
    6851                 :             : /**
    6852                 :             :  * g_idle_add_once:
    6853                 :             :  * @function: function to call
    6854                 :             :  * @data: data to pass to @function
    6855                 :             :  *
    6856                 :             :  * Adds a function to be called whenever there are no higher priority
    6857                 :             :  * events pending to the default main loop.
    6858                 :             :  *
    6859                 :             :  * The function is given the
    6860                 :             :  * default idle priority, [const@GLib.PRIORITY_DEFAULT_IDLE].
    6861                 :             :  *
    6862                 :             :  * The function will only be called once and then the source will be
    6863                 :             :  * automatically removed from the main context.
    6864                 :             :  *
    6865                 :             :  * This function otherwise behaves like [func@GLib.idle_add].
    6866                 :             :  *
    6867                 :             :  * Returns: the ID (greater than 0) of the event source
    6868                 :             :  * Since: 2.74
    6869                 :             :  */
    6870                 :             : guint
    6871                 :         742 : g_idle_add_once (GSourceOnceFunc function,
    6872                 :             :                  gpointer        data)
    6873                 :             : {
    6874                 :         742 :   return idle_add_full (G_PRIORITY_DEFAULT_IDLE, TRUE, (GSourceFunc) function, data, NULL);
    6875                 :             : }
    6876                 :             : 
    6877                 :             : /**
    6878                 :             :  * g_idle_remove_by_data:
    6879                 :             :  * @data: the data for the idle source’s callback.
    6880                 :             :  * 
    6881                 :             :  * Removes the idle function with the given data.
    6882                 :             :  * 
    6883                 :             :  * Returns: true if an idle source was found and removed, false otherwise
    6884                 :             :  **/
    6885                 :             : gboolean
    6886                 :           2 : g_idle_remove_by_data (gpointer data)
    6887                 :             : {
    6888                 :           2 :   return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
    6889                 :             : }
    6890                 :             : 
    6891                 :             : /**
    6892                 :             :  * g_main_context_invoke:
    6893                 :             :  * @context: (nullable): a main context, or `NULL` for the global-default
    6894                 :             :  *   main context
    6895                 :             :  * @function: function to call
    6896                 :             :  * @data: data to pass to @function
    6897                 :             :  *
    6898                 :             :  * Invokes a function in such a way that @context is owned during the
    6899                 :             :  * invocation of @function.
    6900                 :             :  *
    6901                 :             :  * If @context is `NULL` then the global-default main context — as
    6902                 :             :  * returned by [func@GLib.MainContext.default] — is used.
    6903                 :             :  *
    6904                 :             :  * If @context is owned by the current thread, @function is called
    6905                 :             :  * directly.  Otherwise, if @context is the thread-default main context
    6906                 :             :  * of the current thread and [method@GLib.MainContext.acquire] succeeds,
    6907                 :             :  * then @function is called and [method@GLib.MainContext.release] is called
    6908                 :             :  * afterwards.
    6909                 :             :  *
    6910                 :             :  * In any other case, an idle source is created to call @function and
    6911                 :             :  * that source is attached to @context (presumably to be run in another
    6912                 :             :  * thread).  The idle source is attached with [const@GLib.PRIORITY_DEFAULT]
    6913                 :             :  * priority.  If you want a different priority, use
    6914                 :             :  * [method@GLib.MainContext.invoke_full].
    6915                 :             :  *
    6916                 :             :  * Note that, as with normal idle functions, @function should probably return
    6917                 :             :  * [const@GLib.SOURCE_REMOVE].  If it returns [const@GLib.SOURCE_CONTINUE], it
    6918                 :             :  * will be continuously run in a loop (and may prevent this call from returning).
    6919                 :             :  *
    6920                 :             :  * Since: 2.28
    6921                 :             :  **/
    6922                 :             : void
    6923                 :         274 : g_main_context_invoke (GMainContext *context,
    6924                 :             :                        GSourceFunc   function,
    6925                 :             :                        gpointer      data)
    6926                 :             : {
    6927                 :         306 :   g_main_context_invoke_full (context,
    6928                 :             :                               G_PRIORITY_DEFAULT,
    6929                 :          32 :                               function, data, NULL);
    6930                 :         274 : }
    6931                 :             : 
    6932                 :             : /**
    6933                 :             :  * g_main_context_invoke_full:
    6934                 :             :  * @context: (nullable): a main context, or `NULL` for the global-default
    6935                 :             :  *   main context
    6936                 :             :  * @priority: the priority at which to run @function
    6937                 :             :  * @function: function to call
    6938                 :             :  * @data: data to pass to @function
    6939                 :             :  * @notify: (nullable): a function to call when @data is no longer in use
    6940                 :             :  *
    6941                 :             :  * Invokes a function in such a way that @context is owned during the
    6942                 :             :  * invocation of @function.
    6943                 :             :  *
    6944                 :             :  * This function is the same as [method@GLib.MainContext.invoke] except that it
    6945                 :             :  * lets you specify the priority in case @function ends up being
    6946                 :             :  * scheduled as an idle and also lets you give a [callback@GLib.DestroyNotify]
    6947                 :             :  * for @data.
    6948                 :             :  *
    6949                 :             :  * The @notify function should not assume that it is called from any particular
    6950                 :             :  * thread or with any particular context acquired.
    6951                 :             :  *
    6952                 :             :  * Since: 2.28
    6953                 :             :  **/
    6954                 :             : void
    6955                 :         291 : g_main_context_invoke_full (GMainContext   *context,
    6956                 :             :                             gint            priority,
    6957                 :             :                             GSourceFunc     function,
    6958                 :             :                             gpointer        data,
    6959                 :             :                             GDestroyNotify  notify)
    6960                 :             : {
    6961                 :         291 :   g_return_if_fail (function != NULL);
    6962                 :             : 
    6963                 :         291 :   if (!context)
    6964                 :          10 :     context = g_main_context_default ();
    6965                 :             : 
    6966                 :         291 :   if (g_main_context_is_owner (context))
    6967                 :             :     {
    6968                 :          20 :       while (function (data));
    6969                 :          20 :       if (notify != NULL)
    6970                 :           0 :         notify (data);
    6971                 :           8 :     }
    6972                 :             : 
    6973                 :             :   else
    6974                 :             :     {
    6975                 :             :       GMainContext *thread_default;
    6976                 :             : 
    6977                 :         271 :       thread_default = g_main_context_get_thread_default ();
    6978                 :             : 
    6979                 :         271 :       if (!thread_default)
    6980                 :         263 :         thread_default = g_main_context_default ();
    6981                 :             : 
    6982                 :         271 :       if (thread_default == context && g_main_context_acquire (context))
    6983                 :             :         {
    6984                 :         224 :           while (function (data));
    6985                 :             : 
    6986                 :         224 :           g_main_context_release (context);
    6987                 :             : 
    6988                 :         224 :           if (notify != NULL)
    6989                 :           2 :             notify (data);
    6990                 :           9 :         }
    6991                 :             :       else
    6992                 :             :         {
    6993                 :             :           GSource *source;
    6994                 :             : 
    6995                 :          47 :           source = g_idle_source_new ();
    6996                 :          47 :           g_source_set_priority (source, priority);
    6997                 :          47 :           g_source_set_callback (source, function, data, notify);
    6998                 :          47 :           g_source_attach (source, context);
    6999                 :          47 :           g_source_unref (source);
    7000                 :             :         }
    7001                 :             :     }
    7002                 :          32 : }
    7003                 :             : 
    7004                 :             : static gpointer
    7005                 :         268 : glib_worker_main (gpointer data)
    7006                 :             : {
    7007                 :          31 :   while (TRUE)
    7008                 :             :     {
    7009                 :        2370 :       g_main_context_iteration (glib_worker_context, TRUE);
    7010                 :             : 
    7011                 :             : #ifdef G_OS_UNIX
    7012                 :        2133 :       if (g_atomic_int_get (&any_unix_signal_pending))
    7013                 :          22 :         dispatch_unix_signals ();
    7014                 :             : #endif
    7015                 :             :     }
    7016                 :             : 
    7017                 :             :   return NULL; /* worst GCC warning message ever... */
    7018                 :             : }
    7019                 :             : 
    7020                 :             : GMainContext *
    7021                 :        1396 : g_get_worker_context (void)
    7022                 :             : {
    7023                 :             :   static gsize initialised;
    7024                 :             : 
    7025                 :        1396 :   if (g_once_init_enter (&initialised))
    7026                 :             :     {
    7027                 :             :       /* mask all signals in the worker thread */
    7028                 :             : #ifdef G_OS_UNIX
    7029                 :             :       sigset_t prev_mask;
    7030                 :             :       sigset_t all;
    7031                 :             : 
    7032                 :         237 :       sigfillset (&all);
    7033                 :         237 :       pthread_sigmask (SIG_SETMASK, &all, &prev_mask);
    7034                 :             : #endif
    7035                 :         285 :       glib_worker_context = g_main_context_new ();
    7036                 :         285 :       g_thread_new ("gmain", glib_worker_main, NULL);
    7037                 :             : #ifdef G_OS_UNIX
    7038                 :         237 :       pthread_sigmask (SIG_SETMASK, &prev_mask, NULL);
    7039                 :             : #endif
    7040                 :         285 :       g_once_init_leave (&initialised, TRUE);
    7041                 :          48 :     }
    7042                 :             : 
    7043                 :        1396 :   return glib_worker_context;
    7044                 :             : }
    7045                 :             : 
        

Generated by: LCOV version 2.0-1