Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-06 08:41:19

0001 /* GLIB - Library of useful routines for C programming
0002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful, but
0012  * WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 /*
0021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
0022  * file for a list of people on the GLib Team.  See the ChangeLog
0023  * files for a list of changes.  These files are distributed with
0024  * GLib at ftp://ftp.gtk.org/pub/gtk/.
0025  */
0026 
0027 #ifndef __G_THREAD_H__
0028 #define __G_THREAD_H__
0029 
0030 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
0031 #error "Only <glib.h> can be included directly."
0032 #endif
0033 
0034 #include <glib/gatomic.h>
0035 #include <glib/gerror.h>
0036 #include <glib/gutils.h>
0037 
0038 G_BEGIN_DECLS
0039 
0040 #define G_THREAD_ERROR g_thread_error_quark ()
0041 GLIB_AVAILABLE_IN_ALL
0042 GQuark g_thread_error_quark (void);
0043 
0044 typedef enum
0045 {
0046   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
0047 } GThreadError;
0048 
0049 typedef gpointer (*GThreadFunc) (gpointer data);
0050 
0051 typedef struct _GThread         GThread;
0052 
0053 typedef union  _GMutex          GMutex;
0054 typedef struct _GRecMutex       GRecMutex;
0055 typedef struct _GRWLock         GRWLock;
0056 typedef struct _GCond           GCond;
0057 typedef struct _GPrivate        GPrivate;
0058 typedef struct _GOnce           GOnce;
0059 
0060 union _GMutex
0061 {
0062   /*< private >*/
0063   gpointer p;
0064   guint i[2];
0065 };
0066 
0067 struct _GRWLock
0068 {
0069   /*< private >*/
0070   gpointer p;
0071   guint i[2];
0072 };
0073 
0074 struct _GCond
0075 {
0076   /*< private >*/
0077   gpointer p;
0078   guint i[2];
0079 };
0080 
0081 struct _GRecMutex
0082 {
0083   /*< private >*/
0084   gpointer p;
0085   guint i[2];
0086 };
0087 
0088 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
0089 struct _GPrivate
0090 {
0091   /*< private >*/
0092   gpointer       p;
0093   GDestroyNotify notify;
0094   gpointer future[2];
0095 };
0096 
0097 typedef enum
0098 {
0099   G_ONCE_STATUS_NOTCALLED,
0100   G_ONCE_STATUS_PROGRESS,
0101   G_ONCE_STATUS_READY
0102 } GOnceStatus;
0103 
0104 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
0105 struct _GOnce
0106 {
0107   volatile GOnceStatus status;
0108   volatile gpointer retval;
0109 };
0110 
0111 #define G_LOCK_NAME(name)             g__ ## name ## _lock
0112 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
0113 #define G_LOCK_DEFINE(name)           GMutex G_LOCK_NAME (name)
0114 #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
0115 
0116 #ifdef G_DEBUG_LOCKS
0117 #  define G_LOCK(name)                G_STMT_START{             \
0118       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
0119              "file %s: line %d (%s): locking: %s ",             \
0120              __FILE__,        __LINE__, G_STRFUNC,              \
0121              #name);                                            \
0122       g_mutex_lock (&G_LOCK_NAME (name));                       \
0123    }G_STMT_END
0124 #  define G_UNLOCK(name)              G_STMT_START{             \
0125       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
0126              "file %s: line %d (%s): unlocking: %s ",           \
0127              __FILE__,        __LINE__, G_STRFUNC,              \
0128              #name);                                            \
0129      g_mutex_unlock (&G_LOCK_NAME (name));                      \
0130    }G_STMT_END
0131 #  define G_TRYLOCK(name)                                       \
0132       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
0133              "file %s: line %d (%s): try locking: %s ",         \
0134              __FILE__,        __LINE__, G_STRFUNC,              \
0135              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
0136 #else  /* !G_DEBUG_LOCKS */
0137 #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
0138 #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
0139 #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
0140 #endif /* !G_DEBUG_LOCKS */
0141 
0142 #ifdef g_autoptr
0143 #define G_AUTO_LOCK(name) G_MUTEX_AUTO_LOCK (&G_LOCK_NAME (name), g__##name##_locker)
0144 #endif /* g_autoptr */
0145 
0146 GLIB_AVAILABLE_IN_2_32
0147 GThread *       g_thread_ref                    (GThread        *thread);
0148 GLIB_AVAILABLE_IN_2_32
0149 void            g_thread_unref                  (GThread        *thread);
0150 GLIB_AVAILABLE_IN_2_32
0151 GThread *       g_thread_new                    (const gchar    *name,
0152                                                  GThreadFunc     func,
0153                                                  gpointer        data);
0154 GLIB_AVAILABLE_IN_2_32
0155 GThread *       g_thread_try_new                (const gchar    *name,
0156                                                  GThreadFunc     func,
0157                                                  gpointer        data,
0158                                                  GError        **error);
0159 GLIB_AVAILABLE_IN_ALL
0160 GThread *       g_thread_self                   (void);
0161 G_NORETURN GLIB_AVAILABLE_IN_ALL
0162 void            g_thread_exit                   (gpointer        retval);
0163 GLIB_AVAILABLE_IN_ALL
0164 gpointer        g_thread_join                   (GThread        *thread);
0165 GLIB_AVAILABLE_IN_ALL
0166 void            g_thread_yield                  (void);
0167 
0168 GLIB_AVAILABLE_IN_2_84
0169 const char *    g_thread_get_name               (GThread        *thread);
0170 
0171 GLIB_AVAILABLE_IN_2_32
0172 void            g_mutex_init                    (GMutex         *mutex);
0173 GLIB_AVAILABLE_IN_2_32
0174 void            g_mutex_clear                   (GMutex         *mutex);
0175 GLIB_AVAILABLE_IN_ALL
0176 void            g_mutex_lock                    (GMutex         *mutex);
0177 GLIB_AVAILABLE_IN_ALL
0178 gboolean        g_mutex_trylock                 (GMutex         *mutex);
0179 GLIB_AVAILABLE_IN_ALL
0180 void            g_mutex_unlock                  (GMutex         *mutex);
0181 
0182 GLIB_AVAILABLE_IN_2_32
0183 void            g_rw_lock_init                  (GRWLock        *rw_lock);
0184 GLIB_AVAILABLE_IN_2_32
0185 void            g_rw_lock_clear                 (GRWLock        *rw_lock);
0186 GLIB_AVAILABLE_IN_2_32
0187 void            g_rw_lock_writer_lock           (GRWLock        *rw_lock);
0188 GLIB_AVAILABLE_IN_2_32
0189 gboolean        g_rw_lock_writer_trylock        (GRWLock        *rw_lock);
0190 GLIB_AVAILABLE_IN_2_32
0191 void            g_rw_lock_writer_unlock         (GRWLock        *rw_lock);
0192 GLIB_AVAILABLE_IN_2_32
0193 void            g_rw_lock_reader_lock           (GRWLock        *rw_lock);
0194 GLIB_AVAILABLE_IN_2_32
0195 gboolean        g_rw_lock_reader_trylock        (GRWLock        *rw_lock);
0196 GLIB_AVAILABLE_IN_2_32
0197 void            g_rw_lock_reader_unlock         (GRWLock        *rw_lock);
0198 
0199 GLIB_AVAILABLE_IN_2_32
0200 void            g_rec_mutex_init                (GRecMutex      *rec_mutex);
0201 GLIB_AVAILABLE_IN_2_32
0202 void            g_rec_mutex_clear               (GRecMutex      *rec_mutex);
0203 GLIB_AVAILABLE_IN_2_32
0204 void            g_rec_mutex_lock                (GRecMutex      *rec_mutex);
0205 GLIB_AVAILABLE_IN_2_32
0206 gboolean        g_rec_mutex_trylock             (GRecMutex      *rec_mutex);
0207 GLIB_AVAILABLE_IN_2_32
0208 void            g_rec_mutex_unlock              (GRecMutex      *rec_mutex);
0209 
0210 GLIB_AVAILABLE_IN_2_32
0211 void            g_cond_init                     (GCond          *cond);
0212 GLIB_AVAILABLE_IN_2_32
0213 void            g_cond_clear                    (GCond          *cond);
0214 GLIB_AVAILABLE_IN_ALL
0215 void            g_cond_wait                     (GCond          *cond,
0216                                                  GMutex         *mutex);
0217 GLIB_AVAILABLE_IN_ALL
0218 void            g_cond_signal                   (GCond          *cond);
0219 GLIB_AVAILABLE_IN_ALL
0220 void            g_cond_broadcast                (GCond          *cond);
0221 GLIB_AVAILABLE_IN_2_32
0222 gboolean        g_cond_wait_until               (GCond          *cond,
0223                                                  GMutex         *mutex,
0224                                                  gint64          end_time);
0225 
0226 GLIB_AVAILABLE_IN_ALL
0227 gpointer        g_private_get                   (GPrivate       *key);
0228 GLIB_AVAILABLE_IN_ALL
0229 void            g_private_set                   (GPrivate       *key,
0230                                                  gpointer        value);
0231 GLIB_AVAILABLE_IN_2_32
0232 void            g_private_replace               (GPrivate       *key,
0233                                                  gpointer        value);
0234 
0235 GLIB_AVAILABLE_IN_ALL
0236 gpointer        g_once_impl                     (GOnce          *once,
0237                                                  GThreadFunc     func,
0238                                                  gpointer        arg);
0239 GLIB_AVAILABLE_IN_ALL
0240 gboolean        g_once_init_enter               (volatile void  *location);
0241 GLIB_AVAILABLE_IN_ALL
0242 void            g_once_init_leave               (volatile void  *location,
0243                                                  gsize           result);
0244 
0245 GLIB_AVAILABLE_IN_2_80
0246 gboolean g_once_init_enter_pointer              (void *location);
0247 GLIB_AVAILABLE_IN_2_80
0248 void g_once_init_leave_pointer                  (void *location,
0249                                                  gpointer result);
0250 
0251 /* Use C11-style atomic extensions to check the fast path for status=ready. If
0252  * they are not available, fall back to using a mutex and condition variable in
0253  * g_once_impl().
0254  *
0255  * On the C11-style codepath, only the load of once->status needs to be atomic,
0256  * as the writes to it and once->retval in g_once_impl() are related by a
0257  * happens-before relation. Release-acquire semantics are defined such that any
0258  * atomic/non-atomic write which happens-before a store/release is guaranteed to
0259  * be seen by the load/acquire of the same atomic variable. */
0260 #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && defined(__ATOMIC_SEQ_CST)
0261 # define g_once(once, func, arg) \
0262   ((__atomic_load_n (&(once)->status, __ATOMIC_ACQUIRE) == G_ONCE_STATUS_READY) ? \
0263    (once)->retval : \
0264    g_once_impl ((once), (func), (arg)))
0265 #else
0266 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
0267 #endif
0268 
0269 #ifdef __GNUC__
0270 # define g_once_init_enter(location) \
0271   (G_GNUC_EXTENSION ({                                               \
0272     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
0273     (void) (0 ? (gpointer) *(location) : NULL);                      \
0274     (!g_atomic_pointer_get (location) &&                             \
0275      g_once_init_enter (location));                                  \
0276   }))
0277 # define g_once_init_leave(location, result) \
0278   (G_GNUC_EXTENSION ({                                               \
0279     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
0280     0 ? (void) (*(location) = (result)) : (void) 0;                  \
0281     g_once_init_leave ((location), (gsize) (result));                \
0282   }))
0283 # define g_once_init_enter_pointer(location)                   \
0284   (G_GNUC_EXTENSION ({                                         \
0285     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
0286     (void) (0 ? (gpointer) * (location) : NULL);               \
0287     (!g_atomic_pointer_get (location) &&                       \
0288      g_once_init_enter_pointer (location));                    \
0289   })) GLIB_AVAILABLE_MACRO_IN_2_80
0290 # define g_once_init_leave_pointer(location, result)                        \
0291   (G_GNUC_EXTENSION ({                                                      \
0292     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));              \
0293     0 ? (void) (*(location) = (result)) : (void) 0;                         \
0294     g_once_init_leave_pointer ((location), (gpointer) (guintptr) (result)); \
0295   })) GLIB_AVAILABLE_MACRO_IN_2_80
0296 #else
0297 # define g_once_init_enter(location) \
0298   (g_once_init_enter((location)))
0299 # define g_once_init_leave(location, result) \
0300   (g_once_init_leave((location), (gsize) (result)))
0301 # define g_once_init_enter_pointer(location) \
0302   (g_once_init_enter_pointer((location))) \
0303   GLIB_AVAILABLE_MACRO_IN_2_80
0304 # define g_once_init_leave_pointer(location, result) \
0305   (g_once_init_leave_pointer((location), (gpointer) (guintptr) (result))) \
0306   GLIB_AVAILABLE_MACRO_IN_2_80
0307 #endif
0308 
0309 GLIB_AVAILABLE_IN_2_36
0310 guint          g_get_num_processors (void);
0311 
0312 /**
0313  * GMutexLocker:
0314  *
0315  * Opaque type. See g_mutex_locker_new() for details.
0316  * Since: 2.44
0317  */
0318 typedef void GMutexLocker;
0319 
0320 /**
0321  * g_mutex_locker_new:
0322  * @mutex: a mutex to lock
0323  *
0324  * Lock @mutex and return a new #GMutexLocker. Unlock with
0325  * g_mutex_locker_free(). Using g_mutex_unlock() on @mutex
0326  * while a #GMutexLocker exists can lead to undefined behaviour.
0327  *
0328  * No allocation is performed, it is equivalent to a g_mutex_lock() call.
0329  *
0330  * This is intended to be used with g_autoptr().  Note that g_autoptr()
0331  * is only available when using GCC or clang, so the following example
0332  * will only work with those compilers:
0333  * |[
0334  * typedef struct
0335  * {
0336  *   ...
0337  *   GMutex mutex;
0338  *   ...
0339  * } MyObject;
0340  *
0341  * static void
0342  * my_object_do_stuff (MyObject *self)
0343  * {
0344  *   g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->mutex);
0345  *
0346  *   // Code with mutex locked here
0347  *
0348  *   if (condition)
0349  *     // No need to unlock
0350  *     return;
0351  *
0352  *   // Optionally early unlock
0353  *   g_clear_pointer (&locker, g_mutex_locker_free);
0354  *
0355  *   // Code with mutex unlocked here
0356  * }
0357  * ]|
0358  *
0359  * Note that it is common for the declared variable to not be used in the scope,
0360  * which causes some compilers to warn. That can be avoided by using
0361  * `G_GNUC_UNUSED` or, since 2.80, [func@GLib.MUTEX_AUTO_LOCK].
0362  *
0363  * Returns: a #GMutexLocker
0364  * Since: 2.44
0365  */
0366 GLIB_AVAILABLE_STATIC_INLINE_IN_2_44
0367 static inline GMutexLocker *
0368 g_mutex_locker_new (GMutex *mutex)
0369 {
0370   g_mutex_lock (mutex);
0371   return (GMutexLocker *) mutex;
0372 }
0373 
0374 /**
0375  * g_mutex_locker_free:
0376  * @locker: a GMutexLocker
0377  *
0378  * Unlock @locker's mutex. See g_mutex_locker_new() for details.
0379  *
0380  * No memory is freed, it is equivalent to a g_mutex_unlock() call.
0381  *
0382  * Since: 2.44
0383  */
0384 GLIB_AVAILABLE_STATIC_INLINE_IN_2_44
0385 static inline void
0386 g_mutex_locker_free (GMutexLocker *locker)
0387 {
0388   g_mutex_unlock ((GMutex *) locker);
0389 }
0390 
0391 /**
0392  * G_MUTEX_AUTO_LOCK:
0393  * @mutex: a [type@GLib.Mutex]
0394  * @var: a variable name to be declared
0395  *
0396  * Declare a [type@GLib.MutexLocker] variable with `g_autoptr()` and lock the
0397  * mutex. The mutex will be unlocked automatically when leaving the scope. The
0398  * variable is declared with `G_GNUC_UNUSED` to avoid compiler warning if it is
0399  * not used in the scope.
0400  *
0401  * This feature is only supported on GCC and clang. This macro is not defined on
0402  * other compilers and should not be used in programs that are intended to be
0403  * portable to those compilers.
0404  *
0405  * Note that this should be used in a place where it is allowed to declare a
0406  * variable, which could be before any statement in the case
0407  * `-Wdeclaration-after-statement` is used, or C standard prior to C99.
0408  *
0409  * ```c
0410  * {
0411  *   G_MUTEX_AUTO_LOCK (&obj->mutex, locker);
0412  *
0413  *   obj->stuff_with_lock ();
0414  *   if (condition)
0415  *     {
0416  *       // No need to unlock
0417  *       return;
0418  *     }
0419  *
0420  *   // Unlock before end of scope
0421  *   g_clear_pointer (&locker, g_mutex_locker_free);
0422  *   obj->stuff_without_lock ();
0423  * }
0424  * ```
0425  *
0426  * Since: 2.80.0
0427  */
0428 #ifdef g_autoptr
0429 #define G_MUTEX_AUTO_LOCK(mutex, var)                   \
0430   GLIB_AVAILABLE_MACRO_IN_2_80 g_autoptr (GMutexLocker) \
0431   G_GNUC_UNUSED var = g_mutex_locker_new (mutex)
0432 #endif /* g_autoptr */
0433 
0434 /**
0435  * GRecMutexLocker:
0436  *
0437  * Opaque type. See g_rec_mutex_locker_new() for details.
0438  * Since: 2.60
0439  */
0440 typedef void GRecMutexLocker;
0441 
0442 /**
0443  * g_rec_mutex_locker_new:
0444  * @rec_mutex: a recursive mutex to lock
0445  *
0446  * Lock @rec_mutex and return a new #GRecMutexLocker. Unlock with
0447  * g_rec_mutex_locker_free(). Using g_rec_mutex_unlock() on @rec_mutex
0448  * while a #GRecMutexLocker exists can lead to undefined behaviour.
0449  *
0450  * No allocation is performed, it is equivalent to a g_rec_mutex_lock() call.
0451  *
0452  * This is intended to be used with g_autoptr().  Note that g_autoptr()
0453  * is only available when using GCC or clang, so the following example
0454  * will only work with those compilers:
0455  * |[
0456  * typedef struct
0457  * {
0458  *   ...
0459  *   GRecMutex rec_mutex;
0460  *   ...
0461  * } MyObject;
0462  *
0463  * static void
0464  * my_object_do_stuff (MyObject *self)
0465  * {
0466  *   g_autoptr(GRecMutexLocker) locker = g_rec_mutex_locker_new (&self->rec_mutex);
0467  *
0468  *   // Code with rec_mutex locked here
0469  *
0470  *   if (condition)
0471  *     // No need to unlock
0472  *     return;
0473  *
0474  *   // Optionally early unlock
0475  *   g_clear_pointer (&locker, g_rec_mutex_locker_free);
0476  *
0477  *   // Code with rec_mutex unlocked here
0478  * }
0479  * ]|
0480  *
0481  * Note that it is common for the declared variable to not be used in the scope,
0482  * which causes some compilers to warn. That can be avoided by using
0483  * `G_GNUC_UNUSED` or, since 2.80, [func@GLib.REC_MUTEX_AUTO_LOCK].
0484  *
0485  * Returns: a #GRecMutexLocker
0486  * Since: 2.60
0487  */
0488 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0489 GLIB_AVAILABLE_STATIC_INLINE_IN_2_60
0490 static inline GRecMutexLocker *
0491 g_rec_mutex_locker_new (GRecMutex *rec_mutex)
0492 {
0493   g_rec_mutex_lock (rec_mutex);
0494   return (GRecMutexLocker *) rec_mutex;
0495 }
0496 G_GNUC_END_IGNORE_DEPRECATIONS
0497 
0498 /**
0499  * g_rec_mutex_locker_free:
0500  * @locker: a GRecMutexLocker
0501  *
0502  * Unlock @locker's recursive mutex. See g_rec_mutex_locker_new() for details.
0503  *
0504  * No memory is freed, it is equivalent to a g_rec_mutex_unlock() call.
0505  *
0506  * Since: 2.60
0507  */
0508 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0509 GLIB_AVAILABLE_STATIC_INLINE_IN_2_60
0510 static inline void
0511 g_rec_mutex_locker_free (GRecMutexLocker *locker)
0512 {
0513   g_rec_mutex_unlock ((GRecMutex *) locker);
0514 }
0515 G_GNUC_END_IGNORE_DEPRECATIONS
0516 
0517 /**
0518  * G_REC_MUTEX_AUTO_LOCK:
0519  * @mutex: a [type@GLib.RecMutex]
0520  * @var: a variable name to be declared
0521  *
0522  * Declare a [type@GLib.RecMutexLocker] variable with `g_autoptr()` and lock the
0523  * mutex. The mutex will be unlocked automatically when leaving the scope. The
0524  * variable is declared with `G_GNUC_UNUSED` to avoid compiler warning if it is
0525  * not used in the scope.
0526  *
0527  * This feature is only supported on GCC and clang. This macro is not defined on
0528  * other compilers and should not be used in programs that are intended to be
0529  * portable to those compilers.
0530  *
0531  * Note that this should be used in a place where it is allowed to declare a
0532  * variable, which could be before any statement in the case
0533  * `-Wdeclaration-after-statement` is used, or C standard prior to C99.
0534  *
0535  * ```c
0536  * {
0537  *   G_REC_MUTEX_AUTO_LOCK (&obj->rec_mutex, locker);
0538  *
0539  *   obj->stuff_with_lock ();
0540  *   if (condition)
0541  *     {
0542  *       // No need to unlock
0543  *       return;
0544  *     }
0545  *
0546  *   // Unlock before end of scope
0547  *   g_clear_pointer (&locker, g_rec_mutex_locker_free);
0548  *   obj->stuff_without_lock ();
0549  * }
0550  * ```
0551  *
0552  * Since: 2.80.0
0553  */
0554 #ifdef g_autoptr
0555 #define G_REC_MUTEX_AUTO_LOCK(mutex, var)                  \
0556   GLIB_AVAILABLE_MACRO_IN_2_80 g_autoptr (GRecMutexLocker) \
0557   G_GNUC_UNUSED var = g_rec_mutex_locker_new (mutex)
0558 #endif /* g_autoptr */
0559 
0560 /**
0561  * GRWLockWriterLocker:
0562  *
0563  * Opaque type. See g_rw_lock_writer_locker_new() for details.
0564  * Since: 2.62
0565  */
0566 typedef void GRWLockWriterLocker;
0567 
0568 /**
0569  * g_rw_lock_writer_locker_new:
0570  * @rw_lock: a #GRWLock
0571  *
0572  * Obtain a write lock on @rw_lock and return a new #GRWLockWriterLocker.
0573  * Unlock with g_rw_lock_writer_locker_free(). Using g_rw_lock_writer_unlock()
0574  * on @rw_lock while a #GRWLockWriterLocker exists can lead to undefined
0575  * behaviour.
0576  *
0577  * No allocation is performed, it is equivalent to a g_rw_lock_writer_lock() call.
0578  *
0579  * This is intended to be used with g_autoptr().  Note that g_autoptr()
0580  * is only available when using GCC or clang, so the following example
0581  * will only work with those compilers:
0582  * |[
0583  * typedef struct
0584  * {
0585  *   ...
0586  *   GRWLock rw_lock;
0587  *   GPtrArray *array;
0588  *   ...
0589  * } MyObject;
0590  *
0591  * static gchar *
0592  * my_object_get_data (MyObject *self, guint index)
0593  * {
0594  *   g_autoptr(GRWLockReaderLocker) locker = g_rw_lock_reader_locker_new (&self->rw_lock);
0595  *
0596  *   // Code with a read lock obtained on rw_lock here
0597  *
0598  *   if (self->array == NULL)
0599  *     // No need to unlock
0600  *     return NULL;
0601  *
0602  *   if (index < self->array->len)
0603  *     // No need to unlock
0604  *     return g_ptr_array_index (self->array, index);
0605  *
0606  *   // Optionally early unlock
0607  *   g_clear_pointer (&locker, g_rw_lock_reader_locker_free);
0608  *
0609  *   // Code with rw_lock unlocked here
0610  *   return NULL;
0611  * }
0612  *
0613  * static void
0614  * my_object_set_data (MyObject *self, guint index, gpointer data)
0615  * {
0616  *   g_autoptr(GRWLockWriterLocker) locker = g_rw_lock_writer_locker_new (&self->rw_lock);
0617  *
0618  *   // Code with a write lock obtained on rw_lock here
0619  *
0620  *   if (self->array == NULL)
0621  *     self->array = g_ptr_array_new ();
0622  *
0623  *   if (condition)
0624  *     // No need to unlock
0625  *     return;
0626  *
0627  *   if (index >= self->array->len)
0628  *     g_ptr_array_set_size (self->array, index+1);
0629  *   g_ptr_array_index (self->array, index) = data;
0630  *
0631  *   // Optionally early unlock
0632  *   g_clear_pointer (&locker, g_rw_lock_writer_locker_free);
0633  *
0634  *   // Code with rw_lock unlocked here
0635  * }
0636  * ]|
0637  *
0638  * Note that it is common for the declared variable to not be used in the scope,
0639  * which causes some compilers to warn. That can be avoided by using
0640  * `G_GNUC_UNUSED` or, since 2.80, [func@GLib.RW_LOCK_WRITER_AUTO_LOCK].
0641  *
0642  * Returns: a #GRWLockWriterLocker
0643  * Since: 2.62
0644  */
0645 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0646 GLIB_AVAILABLE_STATIC_INLINE_IN_2_62
0647 static inline GRWLockWriterLocker *
0648 g_rw_lock_writer_locker_new (GRWLock *rw_lock)
0649 {
0650   g_rw_lock_writer_lock (rw_lock);
0651   return (GRWLockWriterLocker *) rw_lock;
0652 }
0653 G_GNUC_END_IGNORE_DEPRECATIONS
0654 
0655 /**
0656  * g_rw_lock_writer_locker_free:
0657  * @locker: a GRWLockWriterLocker
0658  *
0659  * Release a write lock on @locker's read-write lock. See
0660  * g_rw_lock_writer_locker_new() for details.
0661  *
0662  * No memory is freed, it is equivalent to a g_rw_lock_writer_unlock() call.
0663  *
0664  * Since: 2.62
0665  */
0666 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0667 GLIB_AVAILABLE_STATIC_INLINE_IN_2_62
0668 static inline void
0669 g_rw_lock_writer_locker_free (GRWLockWriterLocker *locker)
0670 {
0671   g_rw_lock_writer_unlock ((GRWLock *) locker);
0672 }
0673 G_GNUC_END_IGNORE_DEPRECATIONS
0674 
0675 /**
0676  * G_RW_LOCK_WRITER_AUTO_LOCK:
0677  * @mutex: a [type@GLib.RWLock]
0678  * @var: a variable name to be declared
0679  *
0680  * Declare a [type@GLib.RWLockWriterLocker] variable with `g_autoptr()` and lock
0681  * for writing. The mutex will be unlocked automatically when leaving the scope.
0682  * The variable is declared with `G_GNUC_UNUSED` to avoid compiler warning if it
0683  * is not used in the scope.
0684  *
0685  * This feature is only supported on GCC and clang. This macro is not defined on
0686  * other compilers and should not be used in programs that are intended to be
0687  * portable to those compilers.
0688  *
0689  * Note that this should be used in a place where it is allowed to declare a
0690  * variable, which could be before any statement in the case
0691  * `-Wdeclaration-after-statement` is used, or C standard prior to C99.
0692  *
0693  * ```c
0694  * {
0695  *   G_RW_LOCK_WRITER_AUTO_LOCK (&obj->rw_lock, locker);
0696  *
0697  *   obj->stuff_with_lock ();
0698  *   if (condition)
0699  *     {
0700  *       // No need to unlock
0701  *       return;
0702  *     }
0703  *
0704  *   // Unlock before end of scope
0705  *   g_clear_pointer (&locker, g_rw_lock_writer_locker_free);
0706  *   obj->stuff_without_lock ();
0707  * }
0708  * ```
0709  *
0710  * Since: 2.80.0
0711  */
0712 #ifdef g_autoptr
0713 #define G_RW_LOCK_WRITER_AUTO_LOCK(mutex, var)                 \
0714   GLIB_AVAILABLE_MACRO_IN_2_80 g_autoptr (GRWLockWriterLocker) \
0715   G_GNUC_UNUSED var = g_rw_lock_writer_locker_new (mutex)
0716 #endif /* g_autoptr */
0717 
0718 /**
0719  * GRWLockReaderLocker:
0720  *
0721  * Opaque type. See g_rw_lock_reader_locker_new() for details.
0722  * Since: 2.62
0723  */
0724 typedef void GRWLockReaderLocker;
0725 
0726 /**
0727  * g_rw_lock_reader_locker_new:
0728  * @rw_lock: a #GRWLock
0729  *
0730  * Obtain a read lock on @rw_lock and return a new #GRWLockReaderLocker.
0731  * Unlock with g_rw_lock_reader_locker_free(). Using g_rw_lock_reader_unlock()
0732  * on @rw_lock while a #GRWLockReaderLocker exists can lead to undefined
0733  * behaviour.
0734  *
0735  * No allocation is performed, it is equivalent to a g_rw_lock_reader_lock() call.
0736  *
0737  * This is intended to be used with g_autoptr(). For a code sample, see
0738  * g_rw_lock_writer_locker_new().
0739  *
0740  * Returns: a #GRWLockReaderLocker
0741  * Since: 2.62
0742  */
0743 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0744 GLIB_AVAILABLE_STATIC_INLINE_IN_2_62
0745 static inline GRWLockReaderLocker *
0746 g_rw_lock_reader_locker_new (GRWLock *rw_lock)
0747 {
0748   g_rw_lock_reader_lock (rw_lock);
0749   return (GRWLockReaderLocker *) rw_lock;
0750 }
0751 G_GNUC_END_IGNORE_DEPRECATIONS
0752 
0753 /**
0754  * g_rw_lock_reader_locker_free:
0755  * @locker: a GRWLockReaderLocker
0756  *
0757  * Release a read lock on @locker's read-write lock. See
0758  * g_rw_lock_reader_locker_new() for details.
0759  *
0760  * No memory is freed, it is equivalent to a g_rw_lock_reader_unlock() call.
0761  *
0762  * Since: 2.62
0763  */
0764 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0765 GLIB_AVAILABLE_STATIC_INLINE_IN_2_62
0766 static inline void
0767 g_rw_lock_reader_locker_free (GRWLockReaderLocker *locker)
0768 {
0769   g_rw_lock_reader_unlock ((GRWLock *) locker);
0770 }
0771 G_GNUC_END_IGNORE_DEPRECATIONS
0772 
0773 /**
0774  * G_RW_LOCK_READER_AUTO_LOCK:
0775  * @mutex: a [type@GLib.RWLock]
0776  * @var: a variable name to be declared
0777  *
0778  * Declare a [type@GLib.RWLockReaderLocker] variable with `g_autoptr()` and lock
0779  * for reading. The mutex will be unlocked automatically when leaving the scope.
0780  * The variable is declared with `G_GNUC_UNUSED` to avoid compiler warning if it
0781  * is not used in the scope.
0782  *
0783  * This feature is only supported on GCC and clang. This macro is not defined on
0784  * other compilers and should not be used in programs that are intended to be
0785  * portable to those compilers.
0786  *
0787  * Note that this should be used in a place where it is allowed to declare a
0788  * variable, which could be before any statement in the case
0789  * `-Wdeclaration-after-statement` is used, or C standard prior to C99.
0790  *
0791  * ```c
0792  * {
0793  *   G_RW_LOCK_READER_AUTO_LOCK (&obj->rw_lock, locker);
0794  *
0795  *   obj->stuff_with_lock ();
0796  *   if (condition)
0797  *     {
0798  *       // No need to unlock
0799  *       return;
0800  *     }
0801  *
0802  *   // Unlock before end of scope
0803  *   g_clear_pointer (&locker, g_rw_lock_reader_locker_free);
0804  *   obj->stuff_without_lock ();
0805  * }
0806  * ```
0807  *
0808  * Since: 2.80.0
0809  */
0810 #ifdef g_autoptr
0811 #define G_RW_LOCK_READER_AUTO_LOCK(mutex, var)                 \
0812   GLIB_AVAILABLE_MACRO_IN_2_80 g_autoptr (GRWLockReaderLocker) \
0813   G_GNUC_UNUSED var = g_rw_lock_reader_locker_new (mutex)
0814 #endif /* g_autoptr */
0815 
0816 G_END_DECLS
0817 
0818 #endif /* __G_THREAD_H__ */