Blame glib/gthread.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * gthread.c: MT safety related functions
Packit ae235b
 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
Packit ae235b
 *                Owen Taylor
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* Prelude {{{1 ----------------------------------------------------------- */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* implement gthread.h's inline functions */
Packit ae235b
#define G_IMPLEMENT_INLINES 1
Packit ae235b
#define __G_THREAD_C__
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "gthreadprivate.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef G_OS_WIN32
Packit ae235b
#include <sys/time.h>
Packit ae235b
#include <time.h>
Packit ae235b
#else
Packit ae235b
#include <windows.h>
Packit ae235b
#endif /* G_OS_WIN32 */
Packit ae235b
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "glib_trace.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:threads
Packit ae235b
 * @title: Threads
Packit ae235b
 * @short_description: portable support for threads, mutexes, locks,
Packit ae235b
 *     conditions and thread private data
Packit ae235b
 * @see_also: #GThreadPool, #GAsyncQueue
Packit ae235b
 *
Packit ae235b
 * Threads act almost like processes, but unlike processes all threads
Packit ae235b
 * of one process share the same memory. This is good, as it provides
Packit ae235b
 * easy communication between the involved threads via this shared
Packit ae235b
 * memory, and it is bad, because strange things (so called
Packit ae235b
 * "Heisenbugs") might happen if the program is not carefully designed.
Packit ae235b
 * In particular, due to the concurrent nature of threads, no
Packit ae235b
 * assumptions on the order of execution of code running in different
Packit ae235b
 * threads can be made, unless order is explicitly forced by the
Packit ae235b
 * programmer through synchronization primitives.
Packit ae235b
 *
Packit ae235b
 * The aim of the thread-related functions in GLib is to provide a
Packit ae235b
 * portable means for writing multi-threaded software. There are
Packit ae235b
 * primitives for mutexes to protect the access to portions of memory
Packit ae235b
 * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
Packit ae235b
 * individual bits for locks (g_bit_lock()). There are primitives
Packit ae235b
 * for condition variables to allow synchronization of threads (#GCond).
Packit ae235b
 * There are primitives for thread-private data - data that every
Packit ae235b
 * thread has a private instance of (#GPrivate). There are facilities
Packit ae235b
 * for one-time initialization (#GOnce, g_once_init_enter()). Finally,
Packit ae235b
 * there are primitives to create and manage threads (#GThread).
Packit ae235b
 *
Packit ae235b
 * The GLib threading system used to be initialized with g_thread_init().
Packit ae235b
 * This is no longer necessary. Since version 2.32, the GLib threading
Packit ae235b
 * system is automatically initialized at the start of your program,
Packit ae235b
 * and all thread-creation functions and synchronization primitives
Packit ae235b
 * are available right away.
Packit ae235b
 *
Packit ae235b
 * Note that it is not safe to assume that your program has no threads
Packit ae235b
 * even if you don't call g_thread_new() yourself. GLib and GIO can
Packit ae235b
 * and will create threads for their own purposes in some cases, such
Packit ae235b
 * as when using g_unix_signal_source_new() or when using GDBus.
Packit ae235b
 *
Packit ae235b
 * Originally, UNIX did not have threads, and therefore some traditional
Packit ae235b
 * UNIX APIs are problematic in threaded programs. Some notable examples
Packit ae235b
 * are
Packit ae235b
 * 
Packit ae235b
 * - C library functions that return data in statically allocated
Packit ae235b
 *   buffers, such as strtok() or strerror(). For many of these,
Packit ae235b
 *   there are thread-safe variants with a _r suffix, or you can
Packit ae235b
 *   look at corresponding GLib APIs (like g_strsplit() or g_strerror()).
Packit ae235b
 *
Packit ae235b
 * - The functions setenv() and unsetenv() manipulate the process
Packit ae235b
 *   environment in a not thread-safe way, and may interfere with getenv()
Packit ae235b
 *   calls in other threads. Note that getenv() calls may be hidden behind
Packit ae235b
 *   other APIs. For example, GNU gettext() calls getenv() under the
Packit ae235b
 *   covers. In general, it is best to treat the environment as readonly.
Packit ae235b
 *   If you absolutely have to modify the environment, do it early in
Packit ae235b
 *   main(), when no other threads are around yet.
Packit ae235b
 *
Packit ae235b
 * - The setlocale() function changes the locale for the entire process,
Packit ae235b
 *   affecting all threads. Temporary changes to the locale are often made
Packit ae235b
 *   to change the behavior of string scanning or formatting functions
Packit ae235b
 *   like scanf() or printf(). GLib offers a number of string APIs
Packit ae235b
 *   (like g_ascii_formatd() or g_ascii_strtod()) that can often be
Packit ae235b
 *   used as an alternative. Or you can use the uselocale() function
Packit ae235b
 *   to change the locale only for the current thread.
Packit ae235b
 *
Packit ae235b
 * - The fork() function only takes the calling thread into the child's
Packit ae235b
 *   copy of the process image. If other threads were executing in critical
Packit ae235b
 *   sections they could have left mutexes locked which could easily
Packit ae235b
 *   cause deadlocks in the new child. For this reason, you should
Packit ae235b
 *   call exit() or exec() as soon as possible in the child and only
Packit ae235b
 *   make signal-safe library calls before that.
Packit ae235b
 *
Packit ae235b
 * - The daemon() function uses fork() in a way contrary to what is
Packit ae235b
 *   described above. It should not be used with GLib programs.
Packit ae235b
 *
Packit ae235b
 * GLib itself is internally completely thread-safe (all global data is
Packit ae235b
 * automatically locked), but individual data structure instances are
Packit ae235b
 * not automatically locked for performance reasons. For example,
Packit ae235b
 * you must coordinate accesses to the same #GHashTable from multiple
Packit ae235b
 * threads. The two notable exceptions from this rule are #GMainLoop
Packit ae235b
 * and #GAsyncQueue, which are thread-safe and need no further
Packit ae235b
 * application-level locking to be accessed from multiple threads.
Packit ae235b
 * Most refcounting functions such as g_object_ref() are also thread-safe.
Packit ae235b
 *
Packit ae235b
 * A common use for #GThreads is to move a long-running blocking operation out
Packit ae235b
 * of the main thread and into a worker thread. For GLib functions, such as
Packit ae235b
 * single GIO operations, this is not necessary, and complicates the code.
Packit ae235b
 * Instead, the `…_async()` version of the function should be used from the main
Packit ae235b
 * thread, eliminating the need for locking and synchronisation between multiple
Packit ae235b
 * threads. If an operation does need to be moved to a worker thread, consider
Packit ae235b
 * using g_task_run_in_thread(), or a #GThreadPool. #GThreadPool is often a
Packit ae235b
 * better choice than #GThread, as it handles thread reuse and task queueing;
Packit ae235b
 * #GTask uses this internally.
Packit ae235b
 *
Packit ae235b
 * However, if multiple blocking operations need to be performed in sequence,
Packit ae235b
 * and it is not possible to use #GTask for them, moving them to a worker thread
Packit ae235b
 * can clarify the code.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* G_LOCK Documentation {{{1 ---------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOCK_DEFINE:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * The #G_LOCK_ macros provide a convenient interface to #GMutex.
Packit ae235b
 * #G_LOCK_DEFINE defines a lock. It can appear in any place where
Packit ae235b
 * variable definitions may appear in programs, i.e. in the first block
Packit ae235b
 * of a function or outside of functions. The @name parameter will be
Packit ae235b
 * mangled to get the name of the #GMutex. This means that you
Packit ae235b
 * can use names of existing variables as the parameter - e.g. the name
Packit ae235b
 * of the variable you intend to protect with the lock. Look at our
Packit ae235b
 * give_me_next_number() example using the #G_LOCK macros:
Packit ae235b
 *
Packit ae235b
 * Here is an example for using the #G_LOCK convenience macros:
Packit ae235b
 * |[ 
Packit ae235b
 *   G_LOCK_DEFINE (current_number);
Packit ae235b
 *
Packit ae235b
 *   int
Packit ae235b
 *   give_me_next_number (void)
Packit ae235b
 *   {
Packit ae235b
 *     static int current_number = 0;
Packit ae235b
 *     int ret_val;
Packit ae235b
 *
Packit ae235b
 *     G_LOCK (current_number);
Packit ae235b
 *     ret_val = current_number = calc_next_number (current_number);
Packit ae235b
 *     G_UNLOCK (current_number);
Packit ae235b
 *
Packit ae235b
 *     return ret_val;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOCK_DEFINE_STATIC:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * This works like #G_LOCK_DEFINE, but it creates a static object.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOCK_EXTERN:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * This declares a lock, that is defined with #G_LOCK_DEFINE in another
Packit ae235b
 * module.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_LOCK:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_lock(), but for a lock defined with
Packit ae235b
 * #G_LOCK_DEFINE.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_TRYLOCK:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_trylock(), but for a lock defined with
Packit ae235b
 * #G_LOCK_DEFINE.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if the lock could be locked.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_UNLOCK:
Packit ae235b
 * @name: the name of the lock
Packit ae235b
 *
Packit ae235b
 * Works like g_mutex_unlock(), but for a lock defined with
Packit ae235b
 * #G_LOCK_DEFINE.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GMutex Documentation {{{1 ------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GMutex:
Packit ae235b
 *
Packit ae235b
 * The #GMutex struct is an opaque data structure to represent a mutex
Packit ae235b
 * (mutual exclusion). It can be used to protect data against shared
Packit ae235b
 * access.
Packit ae235b
 *
Packit ae235b
 * Take for example the following function:
Packit ae235b
 * |[ 
Packit ae235b
 *   int
Packit ae235b
 *   give_me_next_number (void)
Packit ae235b
 *   {
Packit ae235b
 *     static int current_number = 0;
Packit ae235b
 *
Packit ae235b
 *     // now do a very complicated calculation to calculate the new
Packit ae235b
 *     // number, this might for example be a random number generator
Packit ae235b
 *     current_number = calc_next_number (current_number);
Packit ae235b
 *
Packit ae235b
 *     return current_number;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 * It is easy to see that this won't work in a multi-threaded
Packit ae235b
 * application. There current_number must be protected against shared
Packit ae235b
 * access. A #GMutex can be used as a solution to this problem:
Packit ae235b
 * |[ 
Packit ae235b
 *   int
Packit ae235b
 *   give_me_next_number (void)
Packit ae235b
 *   {
Packit ae235b
 *     static GMutex mutex;
Packit ae235b
 *     static int current_number = 0;
Packit ae235b
 *     int ret_val;
Packit ae235b
 *
Packit ae235b
 *     g_mutex_lock (&mutex);
Packit ae235b
 *     ret_val = current_number = calc_next_number (current_number);
Packit ae235b
 *     g_mutex_unlock (&mutex);
Packit ae235b
 *
Packit ae235b
 *     return ret_val;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 * Notice that the #GMutex is not initialised to any particular value.
Packit ae235b
 * Its placement in static storage ensures that it will be initialised
Packit ae235b
 * to all-zeros, which is appropriate.
Packit ae235b
 *
Packit ae235b
 * If a #GMutex is placed in other contexts (eg: embedded in a struct)
Packit ae235b
 * then it must be explicitly initialised using g_mutex_init().
Packit ae235b
 *
Packit ae235b
 * A #GMutex should only be accessed via g_mutex_ functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GRecMutex Documentation {{{1 -------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GRecMutex:
Packit ae235b
 *
Packit ae235b
 * The GRecMutex struct is an opaque data structure to represent a
Packit ae235b
 * recursive mutex. It is similar to a #GMutex with the difference
Packit ae235b
 * that it is possible to lock a GRecMutex multiple times in the same
Packit ae235b
 * thread without deadlock. When doing so, care has to be taken to
Packit ae235b
 * unlock the recursive mutex as often as it has been locked.
Packit ae235b
 *
Packit ae235b
 * If a #GRecMutex is allocated in static storage then it can be used
Packit ae235b
 * without initialisation.  Otherwise, you should call
Packit ae235b
 * g_rec_mutex_init() on it and g_rec_mutex_clear() when done.
Packit ae235b
 *
Packit ae235b
 * A GRecMutex should only be accessed with the
Packit ae235b
 * g_rec_mutex_ functions.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GRWLock Documentation {{{1 ---------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GRWLock:
Packit ae235b
 *
Packit ae235b
 * The GRWLock struct is an opaque data structure to represent a
Packit ae235b
 * reader-writer lock. It is similar to a #GMutex in that it allows
Packit ae235b
 * multiple threads to coordinate access to a shared resource.
Packit ae235b
 *
Packit ae235b
 * The difference to a mutex is that a reader-writer lock discriminates
Packit ae235b
 * between read-only ('reader') and full ('writer') access. While only
Packit ae235b
 * one thread at a time is allowed write access (by holding the 'writer'
Packit ae235b
 * lock via g_rw_lock_writer_lock()), multiple threads can gain
Packit ae235b
 * simultaneous read-only access (by holding the 'reader' lock via
Packit ae235b
 * g_rw_lock_reader_lock()).
Packit ae235b
 *
Packit ae235b
 * Here is an example for an array with access functions:
Packit ae235b
 * |[ 
Packit ae235b
 *   GRWLock lock;
Packit ae235b
 *   GPtrArray *array;
Packit ae235b
 *
Packit ae235b
 *   gpointer
Packit ae235b
 *   my_array_get (guint index)
Packit ae235b
 *   {
Packit ae235b
 *     gpointer retval = NULL;
Packit ae235b
 *
Packit ae235b
 *     if (!array)
Packit ae235b
 *       return NULL;
Packit ae235b
 *
Packit ae235b
 *     g_rw_lock_reader_lock (&lock);
Packit ae235b
 *     if (index < array->len)
Packit ae235b
 *       retval = g_ptr_array_index (array, index);
Packit ae235b
 *     g_rw_lock_reader_unlock (&lock);
Packit ae235b
 *
Packit ae235b
 *     return retval;
Packit ae235b
 *   }
Packit ae235b
 *
Packit ae235b
 *   void
Packit ae235b
 *   my_array_set (guint index, gpointer data)
Packit ae235b
 *   {
Packit ae235b
 *     g_rw_lock_writer_lock (&lock);
Packit ae235b
 *
Packit ae235b
 *     if (!array)
Packit ae235b
 *       array = g_ptr_array_new ();
Packit ae235b
 *
Packit ae235b
 *     if (index >= array->len)
Packit ae235b
 *       g_ptr_array_set_size (array, index+1);
Packit ae235b
 *     g_ptr_array_index (array, index) = data;
Packit ae235b
 *
Packit ae235b
 *     g_rw_lock_writer_unlock (&lock);
Packit ae235b
 *   }
Packit ae235b
 *  ]|
Packit ae235b
 * This example shows an array which can be accessed by many readers
Packit ae235b
 * (the my_array_get() function) simultaneously, whereas the writers
Packit ae235b
 * (the my_array_set() function) will only be allowed one at a time
Packit ae235b
 * and only if no readers currently access the array. This is because
Packit ae235b
 * of the potentially dangerous resizing of the array. Using these
Packit ae235b
 * functions is fully multi-thread safe now.
Packit ae235b
 *
Packit ae235b
 * If a #GRWLock is allocated in static storage then it can be used
Packit ae235b
 * without initialisation.  Otherwise, you should call
Packit ae235b
 * g_rw_lock_init() on it and g_rw_lock_clear() when done.
Packit ae235b
 *
Packit ae235b
 * A GRWLock should only be accessed with the g_rw_lock_ functions.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GCond Documentation {{{1 ------------------------------------------ */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCond:
Packit ae235b
 *
Packit ae235b
 * The #GCond struct is an opaque data structure that represents a
Packit ae235b
 * condition. Threads can block on a #GCond if they find a certain
Packit ae235b
 * condition to be false. If other threads change the state of this
Packit ae235b
 * condition they signal the #GCond, and that causes the waiting
Packit ae235b
 * threads to be woken up.
Packit ae235b
 *
Packit ae235b
 * Consider the following example of a shared variable.  One or more
Packit ae235b
 * threads can wait for data to be published to the variable and when
Packit ae235b
 * another thread publishes the data, it can signal one of the waiting
Packit ae235b
 * threads to wake up to collect the data.
Packit ae235b
 *
Packit ae235b
 * Here is an example for using GCond to block a thread until a condition
Packit ae235b
 * is satisfied:
Packit ae235b
 * |[ 
Packit ae235b
 *   gpointer current_data = NULL;
Packit ae235b
 *   GMutex data_mutex;
Packit ae235b
 *   GCond data_cond;
Packit ae235b
 *
Packit ae235b
 *   void
Packit ae235b
 *   push_data (gpointer data)
Packit ae235b
 *   {
Packit ae235b
 *     g_mutex_lock (&data_mutex);
Packit ae235b
 *     current_data = data;
Packit ae235b
 *     g_cond_signal (&data_cond);
Packit ae235b
 *     g_mutex_unlock (&data_mutex);
Packit ae235b
 *   }
Packit ae235b
 *
Packit ae235b
 *   gpointer
Packit ae235b
 *   pop_data (void)
Packit ae235b
 *   {
Packit ae235b
 *     gpointer data;
Packit ae235b
 *
Packit ae235b
 *     g_mutex_lock (&data_mutex);
Packit ae235b
 *     while (!current_data)
Packit ae235b
 *       g_cond_wait (&data_cond, &data_mutex);
Packit ae235b
 *     data = current_data;
Packit ae235b
 *     current_data = NULL;
Packit ae235b
 *     g_mutex_unlock (&data_mutex);
Packit ae235b
 *
Packit ae235b
 *     return data;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 * Whenever a thread calls pop_data() now, it will wait until
Packit ae235b
 * current_data is non-%NULL, i.e. until some other thread
Packit ae235b
 * has called push_data().
Packit ae235b
 *
Packit ae235b
 * The example shows that use of a condition variable must always be
Packit ae235b
 * paired with a mutex.  Without the use of a mutex, there would be a
Packit ae235b
 * race between the check of @current_data by the while loop in
Packit ae235b
 * pop_data() and waiting. Specifically, another thread could set
Packit ae235b
 * @current_data after the check, and signal the cond (with nobody
Packit ae235b
 * waiting on it) before the first thread goes to sleep. #GCond is
Packit ae235b
 * specifically useful for its ability to release the mutex and go
Packit ae235b
 * to sleep atomically.
Packit ae235b
 *
Packit ae235b
 * It is also important to use the g_cond_wait() and g_cond_wait_until()
Packit ae235b
 * functions only inside a loop which checks for the condition to be
Packit ae235b
 * true.  See g_cond_wait() for an explanation of why the condition may
Packit ae235b
 * not be true even after it returns.
Packit ae235b
 *
Packit ae235b
 * If a #GCond is allocated in static storage then it can be used
Packit ae235b
 * without initialisation.  Otherwise, you should call g_cond_init()
Packit ae235b
 * on it and g_cond_clear() when done.
Packit ae235b
 *
Packit ae235b
 * A #GCond should only be accessed via the g_cond_ functions.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GThread Documentation {{{1 ---------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GThread:
Packit ae235b
 *
Packit ae235b
 * The #GThread struct represents a running thread. This struct
Packit ae235b
 * is returned by g_thread_new() or g_thread_try_new(). You can
Packit ae235b
 * obtain the #GThread struct representing the current thread by
Packit ae235b
 * calling g_thread_self().
Packit ae235b
 *
Packit ae235b
 * GThread is refcounted, see g_thread_ref() and g_thread_unref().
Packit ae235b
 * The thread represented by it holds a reference while it is running,
Packit ae235b
 * and g_thread_join() consumes the reference that it is given, so
Packit ae235b
 * it is normally not necessary to manage GThread references
Packit ae235b
 * explicitly.
Packit ae235b
 *
Packit ae235b
 * The structure is opaque -- none of its fields may be directly
Packit ae235b
 * accessed.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GThreadFunc:
Packit ae235b
 * @data: data passed to the thread
Packit ae235b
 *
Packit ae235b
 * Specifies the type of the @func functions passed to g_thread_new()
Packit ae235b
 * or g_thread_try_new().
Packit ae235b
 *
Packit ae235b
 * Returns: the return value of the thread
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_supported:
Packit ae235b
 *
Packit ae235b
 * This macro returns %TRUE if the thread system is initialized,
Packit ae235b
 * and %FALSE if it is not.
Packit ae235b
 *
Packit ae235b
 * For language bindings, g_thread_get_initialized() provides
Packit ae235b
 * the same functionality as a function.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE, if the thread system is initialized
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* GThreadError {{{1 ------------------------------------------------------- */
Packit ae235b
/**
Packit ae235b
 * GThreadError:
Packit ae235b
 * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
Packit ae235b
 *                        shortage. Try again later.
Packit ae235b
 *
Packit ae235b
 * Possible errors of thread related functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_THREAD_ERROR:
Packit ae235b
 *
Packit ae235b
 * The error domain of the GLib thread subsystem.
Packit ae235b
 **/
Packit ae235b
G_DEFINE_QUARK (g_thread_error, g_thread_error)
Packit ae235b
Packit ae235b
/* Local Data {{{1 -------------------------------------------------------- */
Packit ae235b
Packit ae235b
static GMutex    g_once_mutex;
Packit ae235b
static GCond     g_once_cond;
Packit ae235b
static GSList   *g_once_init_list = NULL;
Packit ae235b
Packit ae235b
static void g_thread_cleanup (gpointer data);
Packit ae235b
static GPrivate     g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (g_thread_new);
Packit ae235b
Packit ae235b
/* GOnce {{{1 ------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GOnce:
Packit ae235b
 * @status: the status of the #GOnce
Packit ae235b
 * @retval: the value returned by the call to the function, if @status
Packit ae235b
 *          is %G_ONCE_STATUS_READY
Packit ae235b
 *
Packit ae235b
 * A #GOnce struct controls a one-time initialization function. Any
Packit ae235b
 * one-time initialization function must have its own unique #GOnce
Packit ae235b
 * struct.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * G_ONCE_INIT:
Packit ae235b
 *
Packit ae235b
 * A #GOnce must be initialized with this macro before it can be used.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   GOnce my_once = G_ONCE_INIT;
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GOnceStatus:
Packit ae235b
 * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
Packit ae235b
 * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
Packit ae235b
 * @G_ONCE_STATUS_READY: the function has been called.
Packit ae235b
 *
Packit ae235b
 * The possible statuses of a one-time initialization function
Packit ae235b
 * controlled by a #GOnce struct.
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_once:
Packit ae235b
 * @once: a #GOnce structure
Packit ae235b
 * @func: the #GThreadFunc function associated to @once. This function
Packit ae235b
 *        is called only once, regardless of the number of times it and
Packit ae235b
 *        its associated #GOnce struct are passed to g_once().
Packit ae235b
 * @arg: data to be passed to @func
Packit ae235b
 *
Packit ae235b
 * The first call to this routine by a process with a given #GOnce
Packit ae235b
 * struct calls @func with the given argument. Thereafter, subsequent
Packit ae235b
 * calls to g_once()  with the same #GOnce struct do not call @func
Packit ae235b
 * again, but return the stored result of the first call. On return
Packit ae235b
 * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
Packit ae235b
 *
Packit ae235b
 * For example, a mutex or a thread-specific data key must be created
Packit ae235b
 * exactly once. In a threaded environment, calling g_once() ensures
Packit ae235b
 * that the initialization is serialized across multiple threads.
Packit ae235b
 *
Packit ae235b
 * Calling g_once() recursively on the same #GOnce struct in
Packit ae235b
 * @func will lead to a deadlock.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   gpointer
Packit ae235b
 *   get_debug_flags (void)
Packit ae235b
 *   {
Packit ae235b
 *     static GOnce my_once = G_ONCE_INIT;
Packit ae235b
 *
Packit ae235b
 *     g_once (&my_once, parse_debug_flags, NULL);
Packit ae235b
 *
Packit ae235b
 *     return my_once.retval;
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.4
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_once_impl (GOnce       *once,
Packit ae235b
	     GThreadFunc  func,
Packit ae235b
	     gpointer     arg)
Packit ae235b
{
Packit ae235b
  g_mutex_lock (&g_once_mutex);
Packit ae235b
Packit ae235b
  while (once->status == G_ONCE_STATUS_PROGRESS)
Packit ae235b
    g_cond_wait (&g_once_cond, &g_once_mutex);
Packit ae235b
Packit ae235b
  if (once->status != G_ONCE_STATUS_READY)
Packit ae235b
    {
Packit ae235b
      once->status = G_ONCE_STATUS_PROGRESS;
Packit ae235b
      g_mutex_unlock (&g_once_mutex);
Packit ae235b
Packit ae235b
      once->retval = func (arg);
Packit ae235b
Packit ae235b
      g_mutex_lock (&g_once_mutex);
Packit ae235b
      once->status = G_ONCE_STATUS_READY;
Packit ae235b
      g_cond_broadcast (&g_once_cond);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_mutex_unlock (&g_once_mutex);
Packit ae235b
Packit ae235b
  return once->retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_once_init_enter:
Packit ae235b
 * @location: (not nullable): location of a static initializable variable
Packit ae235b
 *    containing 0
Packit ae235b
 *
Packit ae235b
 * Function to be called when starting a critical initialization
Packit ae235b
 * section. The argument @location must point to a static
Packit ae235b
 * 0-initialized variable that will be set to a value other than 0 at
Packit ae235b
 * the end of the initialization section. In combination with
Packit ae235b
 * g_once_init_leave() and the unique address @value_location, it can
Packit ae235b
 * be ensured that an initialization section will be executed only once
Packit ae235b
 * during a program's life time, and that concurrent threads are
Packit ae235b
 * blocked until initialization completed. To be used in constructs
Packit ae235b
 * like this:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   static gsize initialization_value = 0;
Packit ae235b
 *
Packit ae235b
 *   if (g_once_init_enter (&initialization_value))
Packit ae235b
 *     {
Packit ae235b
 *       gsize setup_value = 42; // initialization code here
Packit ae235b
 *
Packit ae235b
 *       g_once_init_leave (&initialization_value, setup_value);
Packit ae235b
 *     }
Packit ae235b
 *
Packit ae235b
 *   // use initialization_value here
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the initialization section should be entered,
Packit ae235b
 *     %FALSE and blocks otherwise
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
(g_once_init_enter) (volatile void *location)
Packit ae235b
{
Packit ae235b
  volatile gsize *value_location = location;
Packit ae235b
  gboolean need_init = FALSE;
Packit ae235b
  g_mutex_lock (&g_once_mutex);
Packit ae235b
  if (g_atomic_pointer_get (value_location) == NULL)
Packit ae235b
    {
Packit ae235b
      if (!g_slist_find (g_once_init_list, (void*) value_location))
Packit ae235b
        {
Packit ae235b
          need_init = TRUE;
Packit ae235b
          g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        do
Packit ae235b
          g_cond_wait (&g_once_cond, &g_once_mutex);
Packit ae235b
        while (g_slist_find (g_once_init_list, (void*) value_location));
Packit ae235b
    }
Packit ae235b
  g_mutex_unlock (&g_once_mutex);
Packit ae235b
  return need_init;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_once_init_leave:
Packit ae235b
 * @location: (not nullable): location of a static initializable variable
Packit ae235b
 *    containing 0
Packit ae235b
 * @result: new non-0 value for *@value_location
Packit ae235b
 *
Packit ae235b
 * Counterpart to g_once_init_enter(). Expects a location of a static
Packit ae235b
 * 0-initialized initialization variable, and an initialization value
Packit ae235b
 * other than 0. Sets the variable to the initialization value, and
Packit ae235b
 * releases concurrent threads blocking in g_once_init_enter() on this
Packit ae235b
 * initialization variable.
Packit ae235b
 *
Packit ae235b
 * Since: 2.14
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
(g_once_init_leave) (volatile void *location,
Packit ae235b
                     gsize          result)
Packit ae235b
{
Packit ae235b
  volatile gsize *value_location = location;
Packit ae235b
Packit ae235b
  g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
Packit ae235b
  g_return_if_fail (result != 0);
Packit ae235b
  g_return_if_fail (g_once_init_list != NULL);
Packit ae235b
Packit ae235b
  g_atomic_pointer_set (value_location, result);
Packit ae235b
  g_mutex_lock (&g_once_mutex);
Packit ae235b
  g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
Packit ae235b
  g_cond_broadcast (&g_once_cond);
Packit ae235b
  g_mutex_unlock (&g_once_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* GThread {{{1 -------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_ref:
Packit ae235b
 * @thread: a #GThread
Packit ae235b
 *
Packit ae235b
 * Increase the reference count on @thread.
Packit ae235b
 *
Packit ae235b
 * Returns: a new reference to @thread
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
GThread *
Packit ae235b
g_thread_ref (GThread *thread)
Packit ae235b
{
Packit ae235b
  GRealThread *real = (GRealThread *) thread;
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&real->ref_count);
Packit ae235b
Packit ae235b
  return thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_unref:
Packit ae235b
 * @thread: a #GThread
Packit ae235b
 *
Packit ae235b
 * Decrease the reference count on @thread, possibly freeing all
Packit ae235b
 * resources associated with it.
Packit ae235b
 *
Packit ae235b
 * Note that each thread holds a reference to its #GThread while
Packit ae235b
 * it is running, so it is safe to drop your own reference to it
Packit ae235b
 * if you don't need it anymore.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_unref (GThread *thread)
Packit ae235b
{
Packit ae235b
  GRealThread *real = (GRealThread *) thread;
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&real->ref_count))
Packit ae235b
    {
Packit ae235b
      if (real->ours)
Packit ae235b
        g_system_thread_free (real);
Packit ae235b
      else
Packit ae235b
        g_slice_free (GRealThread, real);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_cleanup (gpointer data)
Packit ae235b
{
Packit ae235b
  g_thread_unref (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
g_thread_proxy (gpointer data)
Packit ae235b
{
Packit ae235b
  GRealThread* thread = data;
Packit ae235b
Packit ae235b
  g_assert (data);
Packit ae235b
Packit ae235b
  /* This has to happen before G_LOCK, as that might call g_thread_self */
Packit ae235b
  g_private_set (&g_thread_specific_private, data);
Packit ae235b
Packit ae235b
  /* The lock makes sure that g_thread_new_internal() has a chance to
Packit ae235b
   * setup 'func' and 'data' before we make the call.
Packit ae235b
   */
Packit ae235b
  G_LOCK (g_thread_new);
Packit ae235b
  G_UNLOCK (g_thread_new);
Packit ae235b
Packit ae235b
  TRACE (GLIB_THREAD_SPAWNED (thread->thread.func, thread->thread.data,
Packit ae235b
                              thread->name));
Packit ae235b
Packit ae235b
  if (thread->name)
Packit ae235b
    {
Packit ae235b
      g_system_thread_set_name (thread->name);
Packit ae235b
      g_free (thread->name);
Packit ae235b
      thread->name = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  thread->retval = thread->thread.func (thread->thread.data);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_new:
Packit ae235b
 * @name: (nullable): an (optional) name for the new thread
Packit ae235b
 * @func: a function to execute in the new thread
Packit ae235b
 * @data: an argument to supply to the new thread
Packit ae235b
 *
Packit ae235b
 * This function creates a new thread. The new thread starts by invoking
Packit ae235b
 * @func with the argument data. The thread will run until @func returns
Packit ae235b
 * or until g_thread_exit() is called from the new thread. The return value
Packit ae235b
 * of @func becomes the return value of the thread, which can be obtained
Packit ae235b
 * with g_thread_join().
Packit ae235b
 *
Packit ae235b
 * The @name can be useful for discriminating threads in a debugger.
Packit ae235b
 * It is not used for other purposes and does not have to be unique.
Packit ae235b
 * Some systems restrict the length of @name to 16 bytes.
Packit ae235b
 *
Packit ae235b
 * If the thread can not be created the program aborts. See
Packit ae235b
 * g_thread_try_new() if you want to attempt to deal with failures.
Packit ae235b
 *
Packit ae235b
 * If you are using threads to offload (potentially many) short-lived tasks,
Packit ae235b
 * #GThreadPool may be more appropriate than manually spawning and tracking
Packit ae235b
 * multiple #GThreads.
Packit ae235b
 *
Packit ae235b
 * To free the struct returned by this function, use g_thread_unref().
Packit ae235b
 * Note that g_thread_join() implicitly unrefs the #GThread as well.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GThread
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
GThread *
Packit ae235b
g_thread_new (const gchar *name,
Packit ae235b
              GThreadFunc  func,
Packit ae235b
              gpointer     data)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GThread *thread;
Packit ae235b
Packit ae235b
  thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (thread == NULL)
Packit ae235b
    g_error ("creating thread '%s': %s", name ? name : "", error->message);
Packit ae235b
Packit ae235b
  return thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_try_new:
Packit ae235b
 * @name: (nullable): an (optional) name for the new thread
Packit ae235b
 * @func: a function to execute in the new thread
Packit ae235b
 * @data: an argument to supply to the new thread
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * This function is the same as g_thread_new() except that
Packit ae235b
 * it allows for the possibility of failure.
Packit ae235b
 *
Packit ae235b
 * If a thread can not be created (due to resource limits),
Packit ae235b
 * @error is set and %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GThread, or %NULL if an error occurred
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
GThread *
Packit ae235b
g_thread_try_new (const gchar  *name,
Packit ae235b
                  GThreadFunc   func,
Packit ae235b
                  gpointer      data,
Packit ae235b
                  GError      **error)
Packit ae235b
{
Packit ae235b
  return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
GThread *
Packit ae235b
g_thread_new_internal (const gchar   *name,
Packit ae235b
                       GThreadFunc    proxy,
Packit ae235b
                       GThreadFunc    func,
Packit ae235b
                       gpointer       data,
Packit ae235b
                       gsize          stack_size,
Packit ae235b
                       GError       **error)
Packit ae235b
{
Packit ae235b
  GRealThread *thread;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (func != NULL, NULL);
Packit ae235b
Packit ae235b
  G_LOCK (g_thread_new);
Packit ae235b
  thread = g_system_thread_new (proxy, stack_size, error);
Packit ae235b
  if (thread)
Packit ae235b
    {
Packit ae235b
      thread->ref_count = 2;
Packit ae235b
      thread->ours = TRUE;
Packit ae235b
      thread->thread.joinable = TRUE;
Packit ae235b
      thread->thread.func = func;
Packit ae235b
      thread->thread.data = data;
Packit ae235b
      thread->name = g_strdup (name);
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (g_thread_new);
Packit ae235b
Packit ae235b
  return (GThread*) thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_exit:
Packit ae235b
 * @retval: the return value of this thread
Packit ae235b
 *
Packit ae235b
 * Terminates the current thread.
Packit ae235b
 *
Packit ae235b
 * If another thread is waiting for us using g_thread_join() then the
Packit ae235b
 * waiting thread will be woken up and get @retval as the return value
Packit ae235b
 * of g_thread_join().
Packit ae235b
 *
Packit ae235b
 * Calling g_thread_exit() with a parameter @retval is equivalent to
Packit ae235b
 * returning @retval from the function @func, as given to g_thread_new().
Packit ae235b
 *
Packit ae235b
 * You must only call g_thread_exit() from a thread that you created
Packit ae235b
 * yourself with g_thread_new() or related APIs. You must not call
Packit ae235b
 * this function from a thread created with another threading library
Packit ae235b
 * or or from within a #GThreadPool.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_exit (gpointer retval)
Packit ae235b
{
Packit ae235b
  GRealThread* real = (GRealThread*) g_thread_self ();
Packit ae235b
Packit ae235b
  if G_UNLIKELY (!real->ours)
Packit ae235b
    g_error ("attempt to g_thread_exit() a thread not created by GLib");
Packit ae235b
Packit ae235b
  real->retval = retval;
Packit ae235b
Packit ae235b
  g_system_thread_exit ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_join:
Packit ae235b
 * @thread: a #GThread
Packit ae235b
 *
Packit ae235b
 * Waits until @thread finishes, i.e. the function @func, as
Packit ae235b
 * given to g_thread_new(), returns or g_thread_exit() is called.
Packit ae235b
 * If @thread has already terminated, then g_thread_join()
Packit ae235b
 * returns immediately.
Packit ae235b
 *
Packit ae235b
 * Any thread can wait for any other thread by calling g_thread_join(),
Packit ae235b
 * not just its 'creator'. Calling g_thread_join() from multiple threads
Packit ae235b
 * for the same @thread leads to undefined behaviour.
Packit ae235b
 *
Packit ae235b
 * The value returned by @func or given to g_thread_exit() is
Packit ae235b
 * returned by this function.
Packit ae235b
 *
Packit ae235b
 * g_thread_join() consumes the reference to the passed-in @thread.
Packit ae235b
 * This will usually cause the #GThread struct and associated resources
Packit ae235b
 * to be freed. Use g_thread_ref() to obtain an extra reference if you
Packit ae235b
 * want to keep the GThread alive beyond the g_thread_join() call.
Packit ae235b
 *
Packit ae235b
 * Returns: the return value of the thread
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_thread_join (GThread *thread)
Packit ae235b
{
Packit ae235b
  GRealThread *real = (GRealThread*) thread;
Packit ae235b
  gpointer retval;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (thread, NULL);
Packit ae235b
  g_return_val_if_fail (real->ours, NULL);
Packit ae235b
Packit ae235b
  g_system_thread_wait (real);
Packit ae235b
Packit ae235b
  retval = real->retval;
Packit ae235b
Packit ae235b
  /* Just to make sure, this isn't used any more */
Packit ae235b
  thread->joinable = 0;
Packit ae235b
Packit ae235b
  g_thread_unref (thread);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_self:
Packit ae235b
 *
Packit ae235b
 * This function returns the #GThread corresponding to the
Packit ae235b
 * current thread. Note that this function does not increase
Packit ae235b
 * the reference count of the returned struct.
Packit ae235b
 *
Packit ae235b
 * This function will return a #GThread even for threads that
Packit ae235b
 * were not created by GLib (i.e. those created by other threading
Packit ae235b
 * APIs). This may be useful for thread identification purposes
Packit ae235b
 * (i.e. comparisons) but you must not use GLib functions (such
Packit ae235b
 * as g_thread_join()) on these threads.
Packit ae235b
 *
Packit ae235b
 * Returns: the #GThread representing the current thread
Packit ae235b
 */
Packit ae235b
GThread*
Packit ae235b
g_thread_self (void)
Packit ae235b
{
Packit ae235b
  GRealThread* thread = g_private_get (&g_thread_specific_private);
Packit ae235b
Packit ae235b
  if (!thread)
Packit ae235b
    {
Packit ae235b
      /* If no thread data is available, provide and set one.
Packit ae235b
       * This can happen for the main thread and for threads
Packit ae235b
       * that are not created by GLib.
Packit ae235b
       */
Packit ae235b
      thread = g_slice_new0 (GRealThread);
Packit ae235b
      thread->ref_count = 1;
Packit ae235b
Packit ae235b
      g_private_set (&g_thread_specific_private, thread);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (GThread*) thread;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_get_num_processors:
Packit ae235b
 *
Packit ae235b
 * Determine the approximate number of threads that the system will
Packit ae235b
 * schedule simultaneously for this process.  This is intended to be
Packit ae235b
 * used as a parameter to g_thread_pool_new() for CPU bound tasks and
Packit ae235b
 * similar cases.
Packit ae235b
 *
Packit ae235b
 * Returns: Number of schedulable threads, always greater than 0
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_get_num_processors (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  unsigned int count;
Packit ae235b
  SYSTEM_INFO sysinfo;
Packit ae235b
  DWORD_PTR process_cpus;
Packit ae235b
  DWORD_PTR system_cpus;
Packit ae235b
Packit ae235b
  /* This *never* fails, use it as fallback */
Packit ae235b
  GetNativeSystemInfo (&sysinfo);
Packit ae235b
  count = (int) sysinfo.dwNumberOfProcessors;
Packit ae235b
Packit ae235b
  if (GetProcessAffinityMask (GetCurrentProcess (),
Packit ae235b
                              &process_cpus, &system_cpus))
Packit ae235b
    {
Packit ae235b
      unsigned int af_count;
Packit ae235b
Packit ae235b
      for (af_count = 0; process_cpus != 0; process_cpus >>= 1)
Packit ae235b
        if (process_cpus & 1)
Packit ae235b
          af_count++;
Packit ae235b
Packit ae235b
      /* Prefer affinity-based result, if available */
Packit ae235b
      if (af_count > 0)
Packit ae235b
        count = af_count;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (count > 0)
Packit ae235b
    return count;
Packit ae235b
#elif defined(_SC_NPROCESSORS_ONLN)
Packit ae235b
  {
Packit ae235b
    int count;
Packit ae235b
Packit ae235b
    count = sysconf (_SC_NPROCESSORS_ONLN);
Packit ae235b
    if (count > 0)
Packit ae235b
      return count;
Packit ae235b
  }
Packit ae235b
#elif defined HW_NCPU
Packit ae235b
  {
Packit ae235b
    int mib[2], count = 0;
Packit ae235b
    size_t len;
Packit ae235b
Packit ae235b
    mib[0] = CTL_HW;
Packit ae235b
    mib[1] = HW_NCPU;
Packit ae235b
    len = sizeof(count);
Packit ae235b
Packit ae235b
    if (sysctl (mib, 2, &count, &len, NULL, 0) == 0 && count > 0)
Packit ae235b
      return count;
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return 1; /* Fallback */
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Epilogue {{{1 */
Packit ae235b
/* vim: set foldmethod=marker: */