1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: MT safety related functions
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 /* Prelude {{{1 ----------------------------------------------------------- */
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 /* implement gthread.h's inline functions */
36 #define G_IMPLEMENT_INLINES 1
37 #define __G_THREAD_C__
42 #include "gthreadprivate.h"
55 #endif /* G_OS_WIN32 */
58 #include "gstrfuncs.h"
59 #include "gtestutils.h"
64 * @short_description: portable support for threads, mutexes, locks,
65 * conditions and thread private data
66 * @see_also: #GThreadPool, #GAsyncQueue
68 * Threads act almost like processes, but unlike processes all threads
69 * of one process share the same memory. This is good, as it provides
70 * easy communication between the involved threads via this shared
71 * memory, and it is bad, because strange things (so called
72 * "Heisenbugs") might happen if the program is not carefully designed.
73 * In particular, due to the concurrent nature of threads, no
74 * assumptions on the order of execution of code running in different
75 * threads can be made, unless order is explicitly forced by the
76 * programmer through synchronization primitives.
78 * The aim of the thread-related functions in GLib is to provide a
79 * portable means for writing multi-threaded software. There are
80 * primitives for mutexes to protect the access to portions of memory
81 * (#GMutex, #GRecMutex and #GRWLock). There is a facility to use
82 * individual bits for locks (g_bit_lock()). There are primitives
83 * for condition variables to allow synchronization of threads (#GCond).
84 * There are primitives for thread-private data - data that every
85 * thread has a private instance of (#GPrivate). There are facilities
86 * for one-time initialization (#GOnce, g_once_init_enter()). Finally,
87 * there are primitives to create and manage threads (#GThread).
89 * The GLib threading system used to be initialized with g_thread_init().
90 * This is no longer necessary. Since version 2.32, the GLib threading
91 * system is automatically initialized at the start of your program,
92 * and all thread-creation functions and synchronization primitives
93 * are available right away.
95 * Note that it is not safe to assume that your program has no threads
96 * even if you don't call g_thread_new() yourself. GLib and GIO can
97 * and will create threads for their own purposes in some cases, such
98 * as when using g_unix_signal_source_new() or when using GDBus.
100 * Originally, UNIX did not have threads, and therefore some traditional
101 * UNIX APIs are problematic in threaded programs. Some notable examples
104 * - C library functions that return data in statically allocated
105 * buffers, such as strtok() or strerror(). For many of these,
106 * there are thread-safe variants with a _r suffix, or you can
107 * look at corresponding GLib APIs (like g_strsplit() or g_strerror()).
109 * - setenv() and unsetenv() manipulate the process environment in
110 * a not thread-safe way, and may interfere with getenv() calls
111 * in other threads. Note that getenv() calls may be
112 * <quote>hidden</quote> behind other APIs. For example, GNU gettext()
113 * calls getenv() under the covers. In general, it is best to treat
114 * the environment as readonly. If you absolutely have to modify the
115 * environment, do it early in main(), when no other threads are around yet.
117 * - setlocale() changes the locale for the entire process, affecting
118 * all threads. Temporary changes to the locale are often made to
119 * change the behavior of string scanning or formatting functions
120 * like scanf() or printf(). GLib offers a number of string APIs
121 * (like g_ascii_formatd() or g_ascii_strtod()) that can often be
122 * used as an alternative. Or you can use the uselocale() function
123 * to change the locale only for the current thread.
125 * - fork() only takes the calling thread into the child's copy of the
126 * process image. If other threads were executing in critical
127 * sections they could have left mutexes locked which could easily
128 * cause deadlocks in the new child. For this reason, you should
129 * call exit() or exec() as soon as possible in the child and only
130 * make signal-safe library calls before that.
132 * - daemon() uses fork() in a way contrary to what is described
133 * above. It should not be used with GLib programs.
135 * GLib itself is internally completely thread-safe (all global data is
136 * automatically locked), but individual data structure instances are
137 * not automatically locked for performance reasons. For example,
138 * you must coordinate accesses to the same #GHashTable from multiple
139 * threads. The two notable exceptions from this rule are #GMainLoop
140 * and #GAsyncQueue, which are thread-safe and need no further
141 * application-level locking to be accessed from multiple threads.
142 * Most refcounting functions such as g_object_ref() are also thread-safe.
145 /* G_LOCK Documentation {{{1 ---------------------------------------------- */
149 * @name: the name of the lock
151 * The #G_LOCK_ macros provide a convenient interface to #GMutex.
152 * #G_LOCK_DEFINE defines a lock. It can appear in any place where
153 * variable definitions may appear in programs, i.e. in the first block
154 * of a function or outside of functions. The @name parameter will be
155 * mangled to get the name of the #GMutex. This means that you
156 * can use names of existing variables as the parameter - e.g. the name
157 * of the variable you intend to protect with the lock. Look at our
158 * give_me_next_number() example using the #G_LOCK macros:
160 * Here is an example for using the #G_LOCK convenience macros:
162 * G_LOCK_DEFINE (current_number);
165 * give_me_next_number (void)
167 * static int current_number = 0;
170 * G_LOCK (current_number);
171 * ret_val = current_number = calc_next_number (current_number);
172 * G_UNLOCK (current_number);
180 * G_LOCK_DEFINE_STATIC:
181 * @name: the name of the lock
183 * This works like #G_LOCK_DEFINE, but it creates a static object.
188 * @name: the name of the lock
190 * This declares a lock, that is defined with #G_LOCK_DEFINE in another
196 * @name: the name of the lock
198 * Works like g_mutex_lock(), but for a lock defined with
204 * @name: the name of the lock
206 * Works like g_mutex_trylock(), but for a lock defined with
209 * Returns: %TRUE, if the lock could be locked.
214 * @name: the name of the lock
216 * Works like g_mutex_unlock(), but for a lock defined with
220 /* GMutex Documentation {{{1 ------------------------------------------ */
225 * The #GMutex struct is an opaque data structure to represent a mutex
226 * (mutual exclusion). It can be used to protect data against shared
229 * Take for example the following function:
232 * give_me_next_number (void)
234 * static int current_number = 0;
236 * /* now do a very complicated calculation to calculate the new
237 * * number, this might for example be a random number generator
239 * current_number = calc_next_number (current_number);
241 * return current_number;
244 * It is easy to see that this won't work in a multi-threaded
245 * application. There current_number must be protected against shared
246 * access. A #GMutex can be used as a solution to this problem:
249 * give_me_next_number (void)
251 * static GMutex mutex;
252 * static int current_number = 0;
255 * g_mutex_lock (&mutex);
256 * ret_val = current_number = calc_next_number (current_number);
257 * g_mutex_unlock (&mutex);
262 * Notice that the #GMutex is not initialised to any particular value.
263 * Its placement in static storage ensures that it will be initialised
264 * to all-zeros, which is appropriate.
266 * If a #GMutex is placed in other contexts (eg: embedded in a struct)
267 * then it must be explicitly initialised using g_mutex_init().
269 * A #GMutex should only be accessed via <function>g_mutex_</function>
273 /* GRecMutex Documentation {{{1 -------------------------------------- */
278 * The GRecMutex struct is an opaque data structure to represent a
279 * recursive mutex. It is similar to a #GMutex with the difference
280 * that it is possible to lock a GRecMutex multiple times in the same
281 * thread without deadlock. When doing so, care has to be taken to
282 * unlock the recursive mutex as often as it has been locked.
284 * If a #GRecMutex is allocated in static storage then it can be used
285 * without initialisation. Otherwise, you should call
286 * g_rec_mutex_init() on it and g_rec_mutex_clear() when done.
288 * A GRecMutex should only be accessed with the
289 * <function>g_rec_mutex_</function> functions.
294 /* GRWLock Documentation {{{1 ---------------------------------------- */
299 * The GRWLock struct is an opaque data structure to represent a
300 * reader-writer lock. It is similar to a #GMutex in that it allows
301 * multiple threads to coordinate access to a shared resource.
303 * The difference to a mutex is that a reader-writer lock discriminates
304 * between read-only ('reader') and full ('writer') access. While only
305 * one thread at a time is allowed write access (by holding the 'writer'
306 * lock via g_rw_lock_writer_lock()), multiple threads can gain
307 * simultaneous read-only access (by holding the 'reader' lock via
308 * g_rw_lock_reader_lock()).
310 * Here is an example for an array with access functions:
316 * my_array_get (guint index)
318 * gpointer retval = NULL;
323 * g_rw_lock_reader_lock (&lock);
324 * if (index < array->len)
325 * retval = g_ptr_array_index (array, index);
326 * g_rw_lock_reader_unlock (&lock);
332 * my_array_set (guint index, gpointer data)
334 * g_rw_lock_writer_lock (&lock);
337 * array = g_ptr_array_new ();
339 * if (index >= array->len)
340 * g_ptr_array_set_size (array, index+1);
341 * g_ptr_array_index (array, index) = data;
343 * g_rw_lock_writer_unlock (&lock);
346 * This example shows an array which can be accessed by many readers
347 * (the my_array_get() function) simultaneously, whereas the writers
348 * (the my_array_set() function) will only be allowed one at a time
349 * and only if no readers currently access the array. This is because
350 * of the potentially dangerous resizing of the array. Using these
351 * functions is fully multi-thread safe now.
353 * If a #GRWLock is allocated in static storage then it can be used
354 * without initialisation. Otherwise, you should call
355 * g_rw_lock_init() on it and g_rw_lock_clear() when done.
357 * A GRWLock should only be accessed with the g_rw_lock_ functions.
362 /* GCond Documentation {{{1 ------------------------------------------ */
367 * The #GCond struct is an opaque data structure that represents a
368 * condition. Threads can block on a #GCond if they find a certain
369 * condition to be false. If other threads change the state of this
370 * condition they signal the #GCond, and that causes the waiting
371 * threads to be woken up.
373 * Consider the following example of a shared variable. One or more
374 * threads can wait for data to be published to the variable and when
375 * another thread publishes the data, it can signal one of the waiting
376 * threads to wake up to collect the data.
378 * Here is an example for using GCond to block a thread until a condition
381 * gpointer current_data = NULL;
386 * push_data (gpointer data)
388 * g_mutex_lock (&data_mutex);
389 * current_data = data;
390 * g_cond_signal (&data_cond);
391 * g_mutex_unlock (&data_mutex);
399 * g_mutex_lock (&data_mutex);
400 * while (!current_data)
401 * g_cond_wait (&data_cond, &data_mutex);
402 * data = current_data;
403 * current_data = NULL;
404 * g_mutex_unlock (&data_mutex);
409 * Whenever a thread calls pop_data() now, it will wait until
410 * current_data is non-%NULL, i.e. until some other thread
411 * has called push_data().
413 * The example shows that use of a condition variable must always be
414 * paired with a mutex. Without the use of a mutex, there would be a
415 * race between the check of @current_data by the while loop in
416 * pop_data() and waiting. Specifically, another thread could set
417 * @current_data after the check, and signal the cond (with nobody
418 * waiting on it) before the first thread goes to sleep. #GCond is
419 * specifically useful for its ability to release the mutex and go
420 * to sleep atomically.
422 * It is also important to use the g_cond_wait() and g_cond_wait_until()
423 * functions only inside a loop which checks for the condition to be
424 * true. See g_cond_wait() for an explanation of why the condition may
425 * not be true even after it returns.
427 * If a #GCond is allocated in static storage then it can be used
428 * without initialisation. Otherwise, you should call g_cond_init()
429 * on it and g_cond_clear() when done.
431 * A #GCond should only be accessed via the g_cond_ functions.
434 /* GThread Documentation {{{1 ---------------------------------------- */
439 * The #GThread struct represents a running thread. This struct
440 * is returned by g_thread_new() or g_thread_try_new(). You can
441 * obtain the #GThread struct representing the current thead by
442 * calling g_thread_self().
444 * GThread is refcounted, see g_thread_ref() and g_thread_unref().
445 * The thread represented by it holds a reference while it is running,
446 * and g_thread_join() consumes the reference that it is given, so
447 * it is normally not necessary to manage GThread references
450 * The structure is opaque -- none of its fields may be directly
456 * @data: data passed to the thread
458 * Specifies the type of the @func functions passed to g_thread_new()
459 * or g_thread_try_new().
461 * Returns: the return value of the thread
465 * g_thread_supported:
467 * This macro returns %TRUE if the thread system is initialized,
468 * and %FALSE if it is not.
470 * For language bindings, g_thread_get_initialized() provides
471 * the same functionality as a function.
473 * Returns: %TRUE, if the thread system is initialized
476 /* GThreadError {{{1 ------------------------------------------------------- */
479 * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
480 * shortage. Try again later.
482 * Possible errors of thread related functions.
488 * The error domain of the GLib thread subsystem.
490 G_DEFINE_QUARK (g_thread_error, g_thread_error)
492 /* Local Data {{{1 -------------------------------------------------------- */
494 static GMutex g_once_mutex;
495 static GCond g_once_cond;
496 static GSList *g_once_init_list = NULL;
498 static void g_thread_cleanup (gpointer data);
499 static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
501 G_LOCK_DEFINE_STATIC (g_thread_new);
503 /* GOnce {{{1 ------------------------------------------------------------- */
507 * @status: the status of the #GOnce
508 * @retval: the value returned by the call to the function, if @status
509 * is %G_ONCE_STATUS_READY
511 * A #GOnce struct controls a one-time initialization function. Any
512 * one-time initialization function must have its own unique #GOnce
521 * A #GOnce must be initialized with this macro before it can be used.
524 * GOnce my_once = G_ONCE_INIT;
532 * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
533 * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
534 * @G_ONCE_STATUS_READY: the function has been called.
536 * The possible statuses of a one-time initialization function
537 * controlled by a #GOnce struct.
544 * @once: a #GOnce structure
545 * @func: the #GThreadFunc function associated to @once. This function
546 * is called only once, regardless of the number of times it and
547 * its associated #GOnce struct are passed to g_once().
548 * @arg: data to be passed to @func
550 * The first call to this routine by a process with a given #GOnce
551 * struct calls @func with the given argument. Thereafter, subsequent
552 * calls to g_once() with the same #GOnce struct do not call @func
553 * again, but return the stored result of the first call. On return
554 * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
556 * For example, a mutex or a thread-specific data key must be created
557 * exactly once. In a threaded environment, calling g_once() ensures
558 * that the initialization is serialized across multiple threads.
560 * Calling g_once() recursively on the same #GOnce struct in
561 * @func will lead to a deadlock.
565 * get_debug_flags (void)
567 * static GOnce my_once = G_ONCE_INIT;
569 * g_once (&my_once, parse_debug_flags, NULL);
571 * return my_once.retval;
578 g_once_impl (GOnce *once,
582 g_mutex_lock (&g_once_mutex);
584 while (once->status == G_ONCE_STATUS_PROGRESS)
585 g_cond_wait (&g_once_cond, &g_once_mutex);
587 if (once->status != G_ONCE_STATUS_READY)
589 once->status = G_ONCE_STATUS_PROGRESS;
590 g_mutex_unlock (&g_once_mutex);
592 once->retval = func (arg);
594 g_mutex_lock (&g_once_mutex);
595 once->status = G_ONCE_STATUS_READY;
596 g_cond_broadcast (&g_once_cond);
599 g_mutex_unlock (&g_once_mutex);
606 * @location: location of a static initializable variable containing 0
608 * Function to be called when starting a critical initialization
609 * section. The argument @location must point to a static
610 * 0-initialized variable that will be set to a value other than 0 at
611 * the end of the initialization section. In combination with
612 * g_once_init_leave() and the unique address @value_location, it can
613 * be ensured that an initialization section will be executed only once
614 * during a program's life time, and that concurrent threads are
615 * blocked until initialization completed. To be used in constructs
619 * static gsize initialization_value = 0;
621 * if (g_once_init_enter (&initialization_value))
623 * gsize setup_value = 42; /* initialization code here */
625 * g_once_init_leave (&initialization_value, setup_value);
628 * /* use initialization_value here */
631 * Returns: %TRUE if the initialization section should be entered,
632 * %FALSE and blocks otherwise
637 (g_once_init_enter) (volatile void *location)
639 volatile gsize *value_location = location;
640 gboolean need_init = FALSE;
641 g_mutex_lock (&g_once_mutex);
642 if (g_atomic_pointer_get (value_location) == NULL)
644 if (!g_slist_find (g_once_init_list, (void*) value_location))
647 g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
651 g_cond_wait (&g_once_cond, &g_once_mutex);
652 while (g_slist_find (g_once_init_list, (void*) value_location));
654 g_mutex_unlock (&g_once_mutex);
660 * @location: location of a static initializable variable containing 0
661 * @result: new non-0 value for *@value_location
663 * Counterpart to g_once_init_enter(). Expects a location of a static
664 * 0-initialized initialization variable, and an initialization value
665 * other than 0. Sets the variable to the initialization value, and
666 * releases concurrent threads blocking in g_once_init_enter() on this
667 * initialization variable.
672 (g_once_init_leave) (volatile void *location,
675 volatile gsize *value_location = location;
677 g_return_if_fail (g_atomic_pointer_get (value_location) == NULL);
678 g_return_if_fail (result != 0);
679 g_return_if_fail (g_once_init_list != NULL);
681 g_atomic_pointer_set (value_location, result);
682 g_mutex_lock (&g_once_mutex);
683 g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
684 g_cond_broadcast (&g_once_cond);
685 g_mutex_unlock (&g_once_mutex);
688 /* GThread {{{1 -------------------------------------------------------- */
692 * @thread: a #GThread
694 * Increase the reference count on @thread.
696 * Returns: a new reference to @thread
701 g_thread_ref (GThread *thread)
703 GRealThread *real = (GRealThread *) thread;
705 g_atomic_int_inc (&real->ref_count);
712 * @thread: a #GThread
714 * Decrease the reference count on @thread, possibly freeing all
715 * resources associated with it.
717 * Note that each thread holds a reference to its #GThread while
718 * it is running, so it is safe to drop your own reference to it
719 * if you don't need it anymore.
724 g_thread_unref (GThread *thread)
726 GRealThread *real = (GRealThread *) thread;
728 if (g_atomic_int_dec_and_test (&real->ref_count))
731 g_system_thread_free (real);
733 g_slice_free (GRealThread, real);
738 g_thread_cleanup (gpointer data)
740 g_thread_unref (data);
744 g_thread_proxy (gpointer data)
746 GRealThread* thread = data;
750 /* This has to happen before G_LOCK, as that might call g_thread_self */
751 g_private_set (&g_thread_specific_private, data);
753 /* The lock makes sure that g_thread_new_internal() has a chance to
754 * setup 'func' and 'data' before we make the call.
756 G_LOCK (g_thread_new);
757 G_UNLOCK (g_thread_new);
761 g_system_thread_set_name (thread->name);
762 g_free (thread->name);
766 thread->retval = thread->thread.func (thread->thread.data);
773 * @name: (allow-none): an (optional) name for the new thread
774 * @func: a function to execute in the new thread
775 * @data: an argument to supply to the new thread
777 * This function creates a new thread. The new thread starts by invoking
778 * @func with the argument data. The thread will run until @func returns
779 * or until g_thread_exit() is called from the new thread. The return value
780 * of @func becomes the return value of the thread, which can be obtained
781 * with g_thread_join().
783 * The @name can be useful for discriminating threads in a debugger.
784 * It is not used for other purposes and does not have to be unique.
785 * Some systems restrict the length of @name to 16 bytes.
787 * If the thread can not be created the program aborts. See
788 * g_thread_try_new() if you want to attempt to deal with failures.
790 * To free the struct returned by this function, use g_thread_unref().
791 * Note that g_thread_join() implicitly unrefs the #GThread as well.
793 * Returns: the new #GThread
798 g_thread_new (const gchar *name,
802 GError *error = NULL;
805 thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error);
807 if G_UNLIKELY (thread == NULL)
808 g_error ("creating thread '%s': %s", name ? name : "", error->message);
815 * @name: (allow-none): an (optional) name for the new thread
816 * @func: a function to execute in the new thread
817 * @data: an argument to supply to the new thread
818 * @error: return location for error, or %NULL
820 * This function is the same as g_thread_new() except that
821 * it allows for the possibility of failure.
823 * If a thread can not be created (due to resource limits),
824 * @error is set and %NULL is returned.
826 * Returns: the new #GThread, or %NULL if an error occurred
831 g_thread_try_new (const gchar *name,
836 return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
840 g_thread_new_internal (const gchar *name,
849 g_return_val_if_fail (func != NULL, NULL);
851 G_LOCK (g_thread_new);
852 thread = g_system_thread_new (proxy, stack_size, error);
855 thread->ref_count = 2;
857 thread->thread.joinable = TRUE;
858 thread->thread.func = func;
859 thread->thread.data = data;
860 thread->name = g_strdup (name);
862 G_UNLOCK (g_thread_new);
864 return (GThread*) thread;
869 * @retval: the return value of this thread
871 * Terminates the current thread.
873 * If another thread is waiting for us using g_thread_join() then the
874 * waiting thread will be woken up and get @retval as the return value
875 * of g_thread_join().
877 * Calling g_thread_exit() with a parameter @retval is equivalent to
878 * returning @retval from the function @func, as given to g_thread_new().
880 * You must only call g_thread_exit() from a thread that you created
881 * yourself with g_thread_new() or related APIs. You must not call
882 * this function from a thread created with another threading library
883 * or or from within a #GThreadPool.
886 g_thread_exit (gpointer retval)
888 GRealThread* real = (GRealThread*) g_thread_self ();
890 if G_UNLIKELY (!real->ours)
891 g_error ("attempt to g_thread_exit() a thread not created by GLib");
893 real->retval = retval;
895 g_system_thread_exit ();
900 * @thread: a #GThread
902 * Waits until @thread finishes, i.e. the function @func, as
903 * given to g_thread_new(), returns or g_thread_exit() is called.
904 * If @thread has already terminated, then g_thread_join()
905 * returns immediately.
907 * Any thread can wait for any other thread by calling g_thread_join(),
908 * not just its 'creator'. Calling g_thread_join() from multiple threads
909 * for the same @thread leads to undefined behaviour.
911 * The value returned by @func or given to g_thread_exit() is
912 * returned by this function.
914 * g_thread_join() consumes the reference to the passed-in @thread.
915 * This will usually cause the #GThread struct and associated resources
916 * to be freed. Use g_thread_ref() to obtain an extra reference if you
917 * want to keep the GThread alive beyond the g_thread_join() call.
919 * Returns: the return value of the thread
922 g_thread_join (GThread *thread)
924 GRealThread *real = (GRealThread*) thread;
927 g_return_val_if_fail (thread, NULL);
928 g_return_val_if_fail (real->ours, NULL);
930 g_system_thread_wait (real);
932 retval = real->retval;
934 /* Just to make sure, this isn't used any more */
935 thread->joinable = 0;
937 g_thread_unref (thread);
945 * This functions returns the #GThread corresponding to the
946 * current thread. Note that this function does not increase
947 * the reference count of the returned struct.
949 * This function will return a #GThread even for threads that
950 * were not created by GLib (i.e. those created by other threading
951 * APIs). This may be useful for thread identification purposes
952 * (i.e. comparisons) but you must not use GLib functions (such
953 * as g_thread_join()) on these threads.
955 * Returns: the #GThread representing the current thread
960 GRealThread* thread = g_private_get (&g_thread_specific_private);
964 /* If no thread data is available, provide and set one.
965 * This can happen for the main thread and for threads
966 * that are not created by GLib.
968 thread = g_slice_new0 (GRealThread);
969 thread->ref_count = 1;
971 g_private_set (&g_thread_specific_private, thread);
974 return (GThread*) thread;
978 * g_get_num_processors:
980 * Determine the approximate number of threads that the system will
981 * schedule simultaneously for this process. This is intended to be
982 * used as a parameter to g_thread_pool_new() for CPU bound tasks and
985 * Returns: Number of schedulable threads, always greater than 0
990 g_get_num_processors (void)
993 DWORD_PTR process_cpus;
994 DWORD_PTR system_cpus;
996 if (GetProcessAffinityMask (GetCurrentProcess (),
997 &process_cpus, &system_cpus))
1001 for (count = 0; process_cpus != 0; process_cpus >>= 1)
1002 if (process_cpus & 1)
1008 #elif defined(_SC_NPROCESSORS_ONLN)
1012 count = sysconf (_SC_NPROCESSORS_ONLN);
1016 #elif defined HW_NCPU
1018 int mib[2], count = 0;
1023 len = sizeof(count);
1025 if (sysctl (mib, 2, &count, &len, NULL, 0) == 0 && count > 0)
1030 return 1; /* Fallback */
1034 /* vim: set foldmethod=marker: */