Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * gthread.c: MT safety related functions
5 : : * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6 : : * Owen Taylor
7 : : *
8 : : * SPDX-License-Identifier: LGPL-2.1-or-later
9 : : *
10 : : * This library is free software; you can redistribute it and/or
11 : : * modify it under the terms of the GNU Lesser General Public
12 : : * License as published by the Free Software Foundation; either
13 : : * version 2.1 of the License, or (at your option) any later version.
14 : : *
15 : : * This library is distributed in the hope that it will be useful,
16 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 : : * Lesser General Public License for more details.
19 : : *
20 : : * You should have received a copy of the GNU Lesser General Public
21 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 : : */
23 : :
24 : : /* Prelude {{{1 ----------------------------------------------------------- */
25 : :
26 : : /*
27 : : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
28 : : * file for a list of people on the GLib Team. See the ChangeLog
29 : : * files for a list of changes. These files are distributed with
30 : : * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 : : */
32 : :
33 : : /*
34 : : * MT safe
35 : : */
36 : :
37 : : /* implement gthread.h's inline functions */
38 : : #define G_IMPLEMENT_INLINES 1
39 : : #define __G_THREAD_C__
40 : :
41 : : #include "config.h"
42 : :
43 : : #include "gthread.h"
44 : : #include "gthreadprivate.h"
45 : :
46 : : #include <string.h>
47 : :
48 : : #ifdef G_OS_UNIX
49 : : #include <unistd.h>
50 : :
51 : : #if defined(THREADS_POSIX) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
52 : : #include <pthread.h>
53 : : #endif
54 : : #endif /* G_OS_UNIX */
55 : :
56 : : #ifndef G_OS_WIN32
57 : : #include <sys/time.h>
58 : : #include <time.h>
59 : : #else
60 : : #include <windows.h>
61 : : #endif /* G_OS_WIN32 */
62 : :
63 : : #include "gslice.h"
64 : : #include "gstrfuncs.h"
65 : : #include "gtestutils.h"
66 : : #include "glib_trace.h"
67 : : #include "gtrace-private.h"
68 : :
69 : : /* In order that the API can be defined in one place (this file), the platform
70 : : * specific code is moved out into separate files so this one doesn’t turn into
71 : : * a massive #ifdef tangle.
72 : : *
73 : : * To avoid the functions in this file becoming tiny trampolines (`jmp` to the
74 : : * relevant `_impl` function only), which would be a performance hit on some
75 : : * hot paths, #include the platform specific implementations. They are marked as
76 : : * `inline` so should be inlined correctly by the compiler without the need for
77 : : * link time optimisation or any fancy tricks.
78 : : */
79 : : static inline void g_mutex_init_impl (GMutex *mutex);
80 : : static inline void g_mutex_clear_impl (GMutex *mutex);
81 : : static inline void g_mutex_lock_impl (GMutex *mutex);
82 : : static inline void g_mutex_unlock_impl (GMutex *mutex);
83 : : static inline gboolean g_mutex_trylock_impl (GMutex *mutex);
84 : :
85 : : static inline void g_rec_mutex_init_impl (GRecMutex *rec_mutex);
86 : : static inline void g_rec_mutex_clear_impl (GRecMutex *rec_mutex);
87 : : static inline void g_rec_mutex_lock_impl (GRecMutex *mutex);
88 : : static inline void g_rec_mutex_unlock_impl (GRecMutex *rec_mutex);
89 : : static inline gboolean g_rec_mutex_trylock_impl (GRecMutex *rec_mutex);
90 : :
91 : : static inline void g_rw_lock_init_impl (GRWLock *rw_lock);
92 : : static inline void g_rw_lock_clear_impl (GRWLock *rw_lock);
93 : : static inline void g_rw_lock_writer_lock_impl (GRWLock *rw_lock);
94 : : static inline gboolean g_rw_lock_writer_trylock_impl (GRWLock *rw_lock);
95 : : static inline void g_rw_lock_writer_unlock_impl (GRWLock *rw_lock);
96 : : static inline void g_rw_lock_reader_lock_impl (GRWLock *rw_lock);
97 : : static inline gboolean g_rw_lock_reader_trylock_impl (GRWLock *rw_lock);
98 : : static inline void g_rw_lock_reader_unlock_impl (GRWLock *rw_lock);
99 : :
100 : : static inline void g_cond_init_impl (GCond *cond);
101 : : static inline void g_cond_clear_impl (GCond *cond);
102 : : static inline void g_cond_wait_impl (GCond *cond,
103 : : GMutex *mutex);
104 : : static inline void g_cond_signal_impl (GCond *cond);
105 : : static inline void g_cond_broadcast_impl (GCond *cond);
106 : : static inline gboolean g_cond_wait_until_impl (GCond *cond,
107 : : GMutex *mutex,
108 : : gint64 end_time);
109 : :
110 : : static inline gpointer g_private_get_impl (GPrivate *key);
111 : : static inline void g_private_set_impl (GPrivate *key,
112 : : gpointer value);
113 : : static inline void g_private_replace_impl (GPrivate *key,
114 : : gpointer value);
115 : :
116 : : static inline void g_thread_yield_impl (void);
117 : :
118 : : #if defined(THREADS_POSIX)
119 : : #include "gthread-posix.c"
120 : : #elif defined(THREADS_WIN32)
121 : : #include "gthread-win32.c"
122 : : #else
123 : : #error "No threads implementation"
124 : : #endif
125 : :
126 : : /* G_LOCK Documentation {{{1 ---------------------------------------------- */
127 : :
128 : : /**
129 : : * G_LOCK_DEFINE:
130 : : * @name: the name of the lock
131 : : *
132 : : * The `G_LOCK_` macros provide a convenient interface to #GMutex.
133 : : * %G_LOCK_DEFINE defines a lock. It can appear in any place where
134 : : * variable definitions may appear in programs, i.e. in the first block
135 : : * of a function or outside of functions. The @name parameter will be
136 : : * mangled to get the name of the #GMutex. This means that you
137 : : * can use names of existing variables as the parameter - e.g. the name
138 : : * of the variable you intend to protect with the lock. Look at our
139 : : * give_me_next_number() example using the `G_LOCK` macros:
140 : : *
141 : : * Here is an example for using the `G_LOCK` convenience macros:
142 : : *
143 : : * |[<!-- language="C" -->
144 : : * G_LOCK_DEFINE (current_number);
145 : : *
146 : : * int
147 : : * give_me_next_number (void)
148 : : * {
149 : : * static int current_number = 0;
150 : : * int ret_val;
151 : : *
152 : : * G_LOCK (current_number);
153 : : * ret_val = current_number = calc_next_number (current_number);
154 : : * G_UNLOCK (current_number);
155 : : *
156 : : * return ret_val;
157 : : * }
158 : : * ]|
159 : : */
160 : :
161 : : /**
162 : : * G_LOCK_DEFINE_STATIC:
163 : : * @name: the name of the lock
164 : : *
165 : : * This works like %G_LOCK_DEFINE, but it creates a static object.
166 : : */
167 : :
168 : : /**
169 : : * G_LOCK_EXTERN:
170 : : * @name: the name of the lock
171 : : *
172 : : * This declares a lock, that is defined with %G_LOCK_DEFINE in another
173 : : * module.
174 : : */
175 : :
176 : : /**
177 : : * G_LOCK:
178 : : * @name: the name of the lock
179 : : *
180 : : * Works like g_mutex_lock(), but for a lock defined with
181 : : * %G_LOCK_DEFINE.
182 : : */
183 : :
184 : : /**
185 : : * G_TRYLOCK:
186 : : * @name: the name of the lock
187 : : *
188 : : * Works like g_mutex_trylock(), but for a lock defined with
189 : : * %G_LOCK_DEFINE.
190 : : *
191 : : * Returns: %TRUE, if the lock could be locked.
192 : : */
193 : :
194 : : /**
195 : : * G_UNLOCK:
196 : : * @name: the name of the lock
197 : : *
198 : : * Works like g_mutex_unlock(), but for a lock defined with
199 : : * %G_LOCK_DEFINE.
200 : : */
201 : :
202 : : /**
203 : : * G_AUTO_LOCK:
204 : : * @name: the name of the lock
205 : : *
206 : : * Works like [func@GLib.MUTEX_AUTO_LOCK], but for a lock defined with
207 : : * [func@GLib.LOCK_DEFINE].
208 : : *
209 : : * This feature is only supported on GCC and clang. This macro is not defined on
210 : : * other compilers and should not be used in programs that are intended to be
211 : : * portable to those compilers.
212 : : *
213 : : * Since: 2.80
214 : : */
215 : :
216 : : /* GMutex Documentation {{{1 ------------------------------------------ */
217 : :
218 : : /**
219 : : * GMutex:
220 : : *
221 : : * The #GMutex struct is an opaque data structure to represent a mutex
222 : : * (mutual exclusion). It can be used to protect data against shared
223 : : * access.
224 : : *
225 : : * Take for example the following function:
226 : : * |[<!-- language="C" -->
227 : : * int
228 : : * give_me_next_number (void)
229 : : * {
230 : : * static int current_number = 0;
231 : : *
232 : : * // now do a very complicated calculation to calculate the new
233 : : * // number, this might for example be a random number generator
234 : : * current_number = calc_next_number (current_number);
235 : : *
236 : : * return current_number;
237 : : * }
238 : : * ]|
239 : : * It is easy to see that this won't work in a multi-threaded
240 : : * application. There current_number must be protected against shared
241 : : * access. A #GMutex can be used as a solution to this problem:
242 : : * |[<!-- language="C" -->
243 : : * int
244 : : * give_me_next_number (void)
245 : : * {
246 : : * static GMutex mutex;
247 : : * static int current_number = 0;
248 : : * int ret_val;
249 : : *
250 : : * g_mutex_lock (&mutex);
251 : : * ret_val = current_number = calc_next_number (current_number);
252 : : * g_mutex_unlock (&mutex);
253 : : *
254 : : * return ret_val;
255 : : * }
256 : : * ]|
257 : : * Notice that the #GMutex is not initialised to any particular value.
258 : : * Its placement in static storage ensures that it will be initialised
259 : : * to all-zeros, which is appropriate.
260 : : *
261 : : * If a #GMutex is placed in other contexts (eg: embedded in a struct)
262 : : * then it must be explicitly initialised using g_mutex_init().
263 : : *
264 : : * A #GMutex should only be accessed via g_mutex_ functions.
265 : : */
266 : :
267 : : /* GRecMutex Documentation {{{1 -------------------------------------- */
268 : :
269 : : /**
270 : : * GRecMutex:
271 : : *
272 : : * The GRecMutex struct is an opaque data structure to represent a
273 : : * recursive mutex. It is similar to a #GMutex with the difference
274 : : * that it is possible to lock a GRecMutex multiple times in the same
275 : : * thread without deadlock. When doing so, care has to be taken to
276 : : * unlock the recursive mutex as often as it has been locked.
277 : : *
278 : : * If a #GRecMutex is allocated in static storage then it can be used
279 : : * without initialisation. Otherwise, you should call
280 : : * g_rec_mutex_init() on it and g_rec_mutex_clear() when done.
281 : : *
282 : : * A GRecMutex should only be accessed with the
283 : : * g_rec_mutex_ functions.
284 : : *
285 : : * Since: 2.32
286 : : */
287 : :
288 : : /* GRWLock Documentation {{{1 ---------------------------------------- */
289 : :
290 : : /**
291 : : * GRWLock:
292 : : *
293 : : * The GRWLock struct is an opaque data structure to represent a
294 : : * reader-writer lock. It is similar to a #GMutex in that it allows
295 : : * multiple threads to coordinate access to a shared resource.
296 : : *
297 : : * The difference to a mutex is that a reader-writer lock discriminates
298 : : * between read-only ('reader') and full ('writer') access. While only
299 : : * one thread at a time is allowed write access (by holding the 'writer'
300 : : * lock via g_rw_lock_writer_lock()), multiple threads can gain
301 : : * simultaneous read-only access (by holding the 'reader' lock via
302 : : * g_rw_lock_reader_lock()).
303 : : *
304 : : * It is unspecified whether readers or writers have priority in acquiring the
305 : : * lock when a reader already holds the lock and a writer is queued to acquire
306 : : * it.
307 : : *
308 : : * Here is an example for an array with access functions:
309 : : * |[<!-- language="C" -->
310 : : * GRWLock lock;
311 : : * GPtrArray *array;
312 : : *
313 : : * gpointer
314 : : * my_array_get (guint index)
315 : : * {
316 : : * gpointer retval = NULL;
317 : : *
318 : : * if (!array)
319 : : * return NULL;
320 : : *
321 : : * g_rw_lock_reader_lock (&lock);
322 : : * if (index < array->len)
323 : : * retval = g_ptr_array_index (array, index);
324 : : * g_rw_lock_reader_unlock (&lock);
325 : : *
326 : : * return retval;
327 : : * }
328 : : *
329 : : * void
330 : : * my_array_set (guint index, gpointer data)
331 : : * {
332 : : * g_rw_lock_writer_lock (&lock);
333 : : *
334 : : * if (!array)
335 : : * array = g_ptr_array_new ();
336 : : *
337 : : * if (index >= array->len)
338 : : * g_ptr_array_set_size (array, index+1);
339 : : * g_ptr_array_index (array, index) = data;
340 : : *
341 : : * g_rw_lock_writer_unlock (&lock);
342 : : * }
343 : : * ]|
344 : : * This example shows an array which can be accessed by many readers
345 : : * (the my_array_get() function) simultaneously, whereas the writers
346 : : * (the my_array_set() function) will only be allowed one at a time
347 : : * and only if no readers currently access the array. This is because
348 : : * of the potentially dangerous resizing of the array. Using these
349 : : * functions is fully multi-thread safe now.
350 : : *
351 : : * If a #GRWLock is allocated in static storage then it can be used
352 : : * without initialisation. Otherwise, you should call
353 : : * g_rw_lock_init() on it and g_rw_lock_clear() when done.
354 : : *
355 : : * A GRWLock should only be accessed with the g_rw_lock_ functions.
356 : : *
357 : : * Since: 2.32
358 : : */
359 : :
360 : : /* GCond Documentation {{{1 ------------------------------------------ */
361 : :
362 : : /**
363 : : * GCond:
364 : : *
365 : : * The #GCond struct is an opaque data structure that represents a
366 : : * condition. Threads can block on a #GCond if they find a certain
367 : : * condition to be false. If other threads change the state of this
368 : : * condition they signal the #GCond, and that causes the waiting
369 : : * threads to be woken up.
370 : : *
371 : : * Consider the following example of a shared variable. One or more
372 : : * threads can wait for data to be published to the variable and when
373 : : * another thread publishes the data, it can signal one of the waiting
374 : : * threads to wake up to collect the data.
375 : : *
376 : : * Here is an example for using GCond to block a thread until a condition
377 : : * is satisfied:
378 : : * |[<!-- language="C" -->
379 : : * gpointer current_data = NULL;
380 : : * GMutex data_mutex;
381 : : * GCond data_cond;
382 : : *
383 : : * void
384 : : * push_data (gpointer data)
385 : : * {
386 : : * g_mutex_lock (&data_mutex);
387 : : * current_data = data;
388 : : * g_cond_signal (&data_cond);
389 : : * g_mutex_unlock (&data_mutex);
390 : : * }
391 : : *
392 : : * gpointer
393 : : * pop_data (void)
394 : : * {
395 : : * gpointer data;
396 : : *
397 : : * g_mutex_lock (&data_mutex);
398 : : * while (!current_data)
399 : : * g_cond_wait (&data_cond, &data_mutex);
400 : : * data = current_data;
401 : : * current_data = NULL;
402 : : * g_mutex_unlock (&data_mutex);
403 : : *
404 : : * return data;
405 : : * }
406 : : * ]|
407 : : * Whenever a thread calls pop_data() now, it will wait until
408 : : * current_data is non-%NULL, i.e. until some other thread
409 : : * has called push_data().
410 : : *
411 : : * The example shows that use of a condition variable must always be
412 : : * paired with a mutex. Without the use of a mutex, there would be a
413 : : * race between the check of @current_data by the while loop in
414 : : * pop_data() and waiting. Specifically, another thread could set
415 : : * @current_data after the check, and signal the cond (with nobody
416 : : * waiting on it) before the first thread goes to sleep. #GCond is
417 : : * specifically useful for its ability to release the mutex and go
418 : : * to sleep atomically.
419 : : *
420 : : * It is also important to use the g_cond_wait() and g_cond_wait_until()
421 : : * functions only inside a loop which checks for the condition to be
422 : : * true. See g_cond_wait() for an explanation of why the condition may
423 : : * not be true even after it returns.
424 : : *
425 : : * If a #GCond is allocated in static storage then it can be used
426 : : * without initialisation. Otherwise, you should call g_cond_init()
427 : : * on it and g_cond_clear() when done.
428 : : *
429 : : * A #GCond should only be accessed via the g_cond_ functions.
430 : : */
431 : :
432 : : /* GThread Documentation {{{1 ---------------------------------------- */
433 : :
434 : : /**
435 : : * GThread:
436 : : *
437 : : * The #GThread struct represents a running thread. This struct
438 : : * is returned by g_thread_new() or g_thread_try_new(). You can
439 : : * obtain the #GThread struct representing the current thread by
440 : : * calling g_thread_self().
441 : : *
442 : : * GThread is refcounted, see g_thread_ref() and g_thread_unref().
443 : : * The thread represented by it holds a reference while it is running,
444 : : * and g_thread_join() consumes the reference that it is given, so
445 : : * it is normally not necessary to manage GThread references
446 : : * explicitly.
447 : : *
448 : : * The structure is opaque -- none of its fields may be directly
449 : : * accessed.
450 : : */
451 : :
452 : : /**
453 : : * GThreadFunc:
454 : : * @data: data passed to the thread
455 : : *
456 : : * Specifies the type of the @func functions passed to g_thread_new()
457 : : * or g_thread_try_new().
458 : : *
459 : : * Returns: the return value of the thread
460 : : */
461 : :
462 : : /**
463 : : * g_thread_supported:
464 : : *
465 : : * This macro returns %TRUE if the thread system is initialized,
466 : : * and %FALSE if it is not.
467 : : *
468 : : * For language bindings, g_thread_get_initialized() provides
469 : : * the same functionality as a function.
470 : : *
471 : : * Returns: %TRUE, if the thread system is initialized
472 : : */
473 : :
474 : : /* GThreadError {{{1 ------------------------------------------------------- */
475 : : /**
476 : : * GThreadError:
477 : : * @G_THREAD_ERROR_AGAIN: a thread couldn't be created due to resource
478 : : * shortage. Try again later.
479 : : *
480 : : * Possible errors of thread related functions.
481 : : **/
482 : :
483 : : /**
484 : : * G_THREAD_ERROR:
485 : : *
486 : : * The error domain of the GLib thread subsystem.
487 : : **/
488 : 3 : G_DEFINE_QUARK (g_thread_error, g_thread_error)
489 : :
490 : : /* Local Data {{{1 -------------------------------------------------------- */
491 : :
492 : : static GMutex g_once_mutex;
493 : : static GCond g_once_cond;
494 : : static GSList *g_once_init_list = NULL;
495 : :
496 : : static guint g_thread_n_created_counter = 0; /* (atomic) */
497 : :
498 : : static void g_thread_cleanup (gpointer data);
499 : : static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup);
500 : :
501 : : /*
502 : : * g_private_set_alloc0:
503 : : * @key: a #GPrivate
504 : : * @size: size of the allocation, in bytes
505 : : *
506 : : * Sets the thread local variable @key to have a newly-allocated and zero-filled
507 : : * value of given @size, and returns a pointer to that memory. Allocations made
508 : : * using this API will be suppressed in valgrind: it is intended to be used for
509 : : * one-time allocations which are known to be leaked, such as those for
510 : : * per-thread initialisation data. Otherwise, this function behaves the same as
511 : : * g_private_set().
512 : : *
513 : : * Returns: (transfer full): new thread-local heap allocation of size @size
514 : : * Since: 2.60
515 : : */
516 : : /*< private >*/
517 : : gpointer
518 : 3022 : g_private_set_alloc0 (GPrivate *key,
519 : : gsize size)
520 : : {
521 : 3022 : gpointer allocated = g_malloc0 (size);
522 : :
523 : 3022 : g_private_set (key, allocated);
524 : :
525 : 3022 : return g_steal_pointer (&allocated);
526 : : }
527 : :
528 : : /* GOnce {{{1 ------------------------------------------------------------- */
529 : :
530 : : /**
531 : : * GOnce:
532 : : * @status: the status of the #GOnce
533 : : * @retval: the value returned by the call to the function, if @status
534 : : * is %G_ONCE_STATUS_READY
535 : : *
536 : : * A #GOnce struct controls a one-time initialization function. Any
537 : : * one-time initialization function must have its own unique #GOnce
538 : : * struct.
539 : : *
540 : : * Since: 2.4
541 : : */
542 : :
543 : : /**
544 : : * G_ONCE_INIT:
545 : : *
546 : : * A #GOnce must be initialized with this macro before it can be used.
547 : : *
548 : : * |[<!-- language="C" -->
549 : : * GOnce my_once = G_ONCE_INIT;
550 : : * ]|
551 : : *
552 : : * Since: 2.4
553 : : */
554 : :
555 : : /**
556 : : * GOnceStatus:
557 : : * @G_ONCE_STATUS_NOTCALLED: the function has not been called yet.
558 : : * @G_ONCE_STATUS_PROGRESS: the function call is currently in progress.
559 : : * @G_ONCE_STATUS_READY: the function has been called.
560 : : *
561 : : * The possible statuses of a one-time initialization function
562 : : * controlled by a #GOnce struct.
563 : : *
564 : : * Since: 2.4
565 : : */
566 : :
567 : : /**
568 : : * g_once:
569 : : * @once: a #GOnce structure
570 : : * @func: the #GThreadFunc function associated to @once. This function
571 : : * is called only once, regardless of the number of times it and
572 : : * its associated #GOnce struct are passed to g_once().
573 : : * @arg: data to be passed to @func
574 : : *
575 : : * The first call to this routine by a process with a given #GOnce
576 : : * struct calls @func with the given argument. Thereafter, subsequent
577 : : * calls to g_once() with the same #GOnce struct do not call @func
578 : : * again, but return the stored result of the first call. On return
579 : : * from g_once(), the status of @once will be %G_ONCE_STATUS_READY.
580 : : *
581 : : * For example, a mutex or a thread-specific data key must be created
582 : : * exactly once. In a threaded environment, calling g_once() ensures
583 : : * that the initialization is serialized across multiple threads.
584 : : *
585 : : * Calling g_once() recursively on the same #GOnce struct in
586 : : * @func will lead to a deadlock.
587 : : *
588 : : * |[<!-- language="C" -->
589 : : * gpointer
590 : : * get_debug_flags (void)
591 : : * {
592 : : * static GOnce my_once = G_ONCE_INIT;
593 : : *
594 : : * g_once (&my_once, parse_debug_flags, NULL);
595 : : *
596 : : * return my_once.retval;
597 : : * }
598 : : * ]|
599 : : *
600 : : * Since: 2.4
601 : : */
602 : : gpointer
603 : 285 : g_once_impl (GOnce *once,
604 : : GThreadFunc func,
605 : : gpointer arg)
606 : : {
607 : 285 : g_mutex_lock (&g_once_mutex);
608 : :
609 : 590 : while (once->status == G_ONCE_STATUS_PROGRESS)
610 : 305 : g_cond_wait (&g_once_cond, &g_once_mutex);
611 : :
612 : 285 : if (once->status != G_ONCE_STATUS_READY)
613 : : {
614 : : gpointer retval;
615 : :
616 : 203 : once->status = G_ONCE_STATUS_PROGRESS;
617 : 203 : g_mutex_unlock (&g_once_mutex);
618 : :
619 : 203 : retval = func (arg);
620 : :
621 : 203 : g_mutex_lock (&g_once_mutex);
622 : : /* We prefer the new C11-style atomic extension of GCC if available. If not,
623 : : * fall back to always locking. */
624 : : #if defined(G_ATOMIC_LOCK_FREE) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) && defined(__ATOMIC_SEQ_CST)
625 : : /* Only the second store needs to be atomic, as the two writes are related
626 : : * by a happens-before relationship here. */
627 : 203 : once->retval = retval;
628 : 203 : __atomic_store_n (&once->status, G_ONCE_STATUS_READY, __ATOMIC_RELEASE);
629 : : #else
630 : : once->retval = retval;
631 : : once->status = G_ONCE_STATUS_READY;
632 : : #endif
633 : 203 : g_cond_broadcast (&g_once_cond);
634 : : }
635 : :
636 : 285 : g_mutex_unlock (&g_once_mutex);
637 : :
638 : 285 : return once->retval;
639 : : }
640 : :
641 : : /**
642 : : * g_once_init_enter:
643 : : * @location: (inout) (not optional): location of a static initializable variable
644 : : * containing 0
645 : : *
646 : : * Function to be called when starting a critical initialization
647 : : * section. The argument @location must point to a static
648 : : * 0-initialized variable that will be set to a value other than 0 at
649 : : * the end of the initialization section. In combination with
650 : : * g_once_init_leave() and the unique address @value_location, it can
651 : : * be ensured that an initialization section will be executed only once
652 : : * during a program's life time, and that concurrent threads are
653 : : * blocked until initialization completed. To be used in constructs
654 : : * like this:
655 : : *
656 : : * |[<!-- language="C" -->
657 : : * static gsize initialization_value = 0;
658 : : *
659 : : * if (g_once_init_enter (&initialization_value))
660 : : * {
661 : : * gsize setup_value = 42; // initialization code here
662 : : *
663 : : * g_once_init_leave (&initialization_value, setup_value);
664 : : * }
665 : : *
666 : : * // use initialization_value here
667 : : * ]|
668 : : *
669 : : * While @location has a `volatile` qualifier, this is a historical artifact and
670 : : * the pointer passed to it should not be `volatile`.
671 : : *
672 : : * Returns: %TRUE if the initialization section should be entered,
673 : : * %FALSE and blocks otherwise
674 : : *
675 : : * Since: 2.14
676 : : */
677 : : gboolean
678 : 16595 : (g_once_init_enter) (volatile void *location)
679 : : {
680 : 16595 : gsize *value_location = (gsize *) location;
681 : 16595 : gboolean need_init = FALSE;
682 : 16595 : g_mutex_lock (&g_once_mutex);
683 : 16595 : if (g_atomic_pointer_get (value_location) == 0)
684 : : {
685 : 4314 : if (!g_slist_find (g_once_init_list, (void*) value_location))
686 : : {
687 : 4295 : need_init = TRUE;
688 : 4295 : g_once_init_list = g_slist_prepend (g_once_init_list, (void*) value_location);
689 : : }
690 : : else
691 : : do
692 : 19 : g_cond_wait (&g_once_cond, &g_once_mutex);
693 : 19 : while (g_slist_find (g_once_init_list, (void*) value_location));
694 : : }
695 : 16595 : g_mutex_unlock (&g_once_mutex);
696 : 16595 : return need_init;
697 : : }
698 : :
699 : : /**
700 : : * g_once_init_enter_pointer:
701 : : * @location: (not nullable): location of a static initializable variable
702 : : * containing `NULL`
703 : : *
704 : : * This functions behaves in the same way as g_once_init_enter(), but can
705 : : * can be used to initialize pointers (or #guintptr) instead of #gsize.
706 : : *
707 : : * |[<!-- language="C" -->
708 : : * static MyStruct *interesting_struct = NULL;
709 : : *
710 : : * if (g_once_init_enter_pointer (&interesting_struct))
711 : : * {
712 : : * MyStruct *setup_value = allocate_my_struct (); // initialization code here
713 : : *
714 : : * g_once_init_leave_pointer (&interesting_struct, g_steal_pointer (&setup_value));
715 : : * }
716 : : *
717 : : * // use interesting_struct here
718 : : * ]|
719 : : *
720 : : * Returns: %TRUE if the initialization section should be entered,
721 : : * %FALSE and blocks otherwise
722 : : *
723 : : * Since: 2.80
724 : : */
725 : : gboolean
726 : 19824 : (g_once_init_enter_pointer) (gpointer location)
727 : : {
728 : 19824 : gpointer *value_location = (gpointer *) location;
729 : 19824 : gboolean need_init = FALSE;
730 : 19824 : g_mutex_lock (&g_once_mutex);
731 : 19824 : if (g_atomic_pointer_get (value_location) == 0)
732 : : {
733 : 19824 : if (!g_slist_find (g_once_init_list, (void *) value_location))
734 : : {
735 : 19814 : need_init = TRUE;
736 : 19814 : g_once_init_list = g_slist_prepend (g_once_init_list, (void *) value_location);
737 : : }
738 : : else
739 : : do
740 : 10 : g_cond_wait (&g_once_cond, &g_once_mutex);
741 : 10 : while (g_slist_find (g_once_init_list, (void *) value_location));
742 : : }
743 : 19824 : g_mutex_unlock (&g_once_mutex);
744 : 19824 : return need_init;
745 : : }
746 : :
747 : : /**
748 : : * g_once_init_leave:
749 : : * @location: (inout) (not optional): location of a static initializable variable
750 : : * containing 0
751 : : * @result: new non-0 value for *@value_location
752 : : *
753 : : * Counterpart to g_once_init_enter(). Expects a location of a static
754 : : * 0-initialized initialization variable, and an initialization value
755 : : * other than 0. Sets the variable to the initialization value, and
756 : : * releases concurrent threads blocking in g_once_init_enter() on this
757 : : * initialization variable.
758 : : *
759 : : * While @location has a `volatile` qualifier, this is a historical artifact and
760 : : * the pointer passed to it should not be `volatile`.
761 : : *
762 : : * Since: 2.14
763 : : */
764 : : void
765 : 4295 : (g_once_init_leave) (volatile void *location,
766 : : gsize result)
767 : : {
768 : 4295 : gsize *value_location = (gsize *) location;
769 : : gsize old_value;
770 : :
771 : 4295 : g_return_if_fail (result != 0);
772 : :
773 : 4295 : old_value = (gsize) g_atomic_pointer_exchange (value_location, result);
774 : 4295 : g_return_if_fail (old_value == 0);
775 : :
776 : 4295 : g_mutex_lock (&g_once_mutex);
777 : 4295 : g_return_if_fail (g_once_init_list != NULL);
778 : 4295 : g_once_init_list = g_slist_remove (g_once_init_list, (void*) value_location);
779 : 4295 : g_cond_broadcast (&g_once_cond);
780 : 4295 : g_mutex_unlock (&g_once_mutex);
781 : : }
782 : :
783 : : /**
784 : : * g_once_init_leave_pointer:
785 : : * @location: (not nullable): location of a static initializable variable
786 : : * containing `NULL`
787 : : * @result: new non-`NULL` value for `*location`
788 : : *
789 : : * Counterpart to g_once_init_enter_pointer(). Expects a location of a static
790 : : * `NULL`-initialized initialization variable, and an initialization value
791 : : * other than `NULL`. Sets the variable to the initialization value, and
792 : : * releases concurrent threads blocking in g_once_init_enter_pointer() on this
793 : : * initialization variable.
794 : : *
795 : : * This functions behaves in the same way as g_once_init_leave(), but
796 : : * can be used to initialize pointers (or #guintptr) instead of #gsize.
797 : : *
798 : : * Since: 2.80
799 : : */
800 : : void
801 : 19814 : (g_once_init_leave_pointer) (gpointer location,
802 : : gpointer result)
803 : : {
804 : 19814 : gpointer *value_location = (gpointer *) location;
805 : : gpointer old_value;
806 : :
807 : 19814 : g_return_if_fail (result != 0);
808 : :
809 : 19813 : old_value = g_atomic_pointer_exchange (value_location, result);
810 : 19813 : g_return_if_fail (old_value == 0);
811 : :
812 : 19813 : g_mutex_lock (&g_once_mutex);
813 : 19813 : g_return_if_fail (g_once_init_list != NULL);
814 : 19813 : g_once_init_list = g_slist_remove (g_once_init_list, (void *) value_location);
815 : 19813 : g_cond_broadcast (&g_once_cond);
816 : 19813 : g_mutex_unlock (&g_once_mutex);
817 : : }
818 : :
819 : : /* GThread {{{1 -------------------------------------------------------- */
820 : :
821 : : /**
822 : : * g_thread_ref:
823 : : * @thread: a #GThread
824 : : *
825 : : * Increase the reference count on @thread.
826 : : *
827 : : * Returns: (transfer full): a new reference to @thread
828 : : *
829 : : * Since: 2.32
830 : : */
831 : : GThread *
832 : 3 : g_thread_ref (GThread *thread)
833 : : {
834 : 3 : GRealThread *real = (GRealThread *) thread;
835 : :
836 : 3 : g_atomic_int_inc (&real->ref_count);
837 : :
838 : 3 : return thread;
839 : : }
840 : :
841 : : /**
842 : : * g_thread_unref:
843 : : * @thread: (transfer full): a #GThread
844 : : *
845 : : * Decrease the reference count on @thread, possibly freeing all
846 : : * resources associated with it.
847 : : *
848 : : * Note that each thread holds a reference to its #GThread while
849 : : * it is running, so it is safe to drop your own reference to it
850 : : * if you don't need it anymore.
851 : : *
852 : : * Since: 2.32
853 : : */
854 : : void
855 : 23362 : g_thread_unref (GThread *thread)
856 : : {
857 : 23362 : GRealThread *real = (GRealThread *) thread;
858 : :
859 : 23362 : if (g_atomic_int_dec_and_test (&real->ref_count))
860 : : {
861 : 11615 : if (real->ours)
862 : 11615 : g_system_thread_free (real);
863 : : else
864 : 0 : g_slice_free (GRealThread, real);
865 : : }
866 : 23362 : }
867 : :
868 : : static void
869 : 11615 : g_thread_cleanup (gpointer data)
870 : : {
871 : 11615 : g_thread_unref (data);
872 : 11615 : }
873 : :
874 : : gpointer
875 : 12226 : g_thread_proxy (gpointer data)
876 : : {
877 : 12226 : GRealThread* thread = data;
878 : :
879 : 12226 : g_assert (data);
880 : 12226 : g_private_set (&g_thread_specific_private, data);
881 : :
882 : 12226 : TRACE (GLIB_THREAD_SPAWNED (thread->thread.func, thread->thread.data,
883 : : thread->name));
884 : :
885 : 12226 : if (thread->name)
886 : : {
887 : 4799 : g_system_thread_set_name (thread->name);
888 : 4799 : g_free (thread->name);
889 : 4799 : thread->name = NULL;
890 : : }
891 : :
892 : 12226 : thread->retval = thread->thread.func (thread->thread.data);
893 : :
894 : 11602 : return NULL;
895 : : }
896 : :
897 : : guint
898 : 2398 : g_thread_n_created (void)
899 : : {
900 : 2398 : return g_atomic_int_get (&g_thread_n_created_counter);
901 : : }
902 : :
903 : : /**
904 : : * g_thread_new:
905 : : * @name: (nullable): an (optional) name for the new thread
906 : : * @func: (closure data) (scope async): a function to execute in the new thread
907 : : * @data: (nullable): an argument to supply to the new thread
908 : : *
909 : : * This function creates a new thread. The new thread starts by invoking
910 : : * @func with the argument data. The thread will run until @func returns
911 : : * or until g_thread_exit() is called from the new thread. The return value
912 : : * of @func becomes the return value of the thread, which can be obtained
913 : : * with g_thread_join().
914 : : *
915 : : * The @name can be useful for discriminating threads in a debugger.
916 : : * It is not used for other purposes and does not have to be unique.
917 : : * Some systems restrict the length of @name to 16 bytes.
918 : : *
919 : : * If the thread can not be created the program aborts. See
920 : : * g_thread_try_new() if you want to attempt to deal with failures.
921 : : *
922 : : * If you are using threads to offload (potentially many) short-lived tasks,
923 : : * #GThreadPool may be more appropriate than manually spawning and tracking
924 : : * multiple #GThreads.
925 : : *
926 : : * To free the struct returned by this function, use g_thread_unref().
927 : : * Note that g_thread_join() implicitly unrefs the #GThread as well.
928 : : *
929 : : * New threads by default inherit their scheduler policy (POSIX) or thread
930 : : * priority (Windows) of the thread creating the new thread.
931 : : *
932 : : * This behaviour changed in GLib 2.64: before threads on Windows were not
933 : : * inheriting the thread priority but were spawned with the default priority.
934 : : * Starting with GLib 2.64 the behaviour is now consistent between Windows and
935 : : * POSIX and all threads inherit their parent thread's priority.
936 : : *
937 : : * Returns: (transfer full): the new #GThread
938 : : *
939 : : * Since: 2.32
940 : : */
941 : : GThread *
942 : 4550 : g_thread_new (const gchar *name,
943 : : GThreadFunc func,
944 : : gpointer data)
945 : : {
946 : 4550 : GError *error = NULL;
947 : : GThread *thread;
948 : :
949 : 4550 : thread = g_thread_new_internal (name, g_thread_proxy, func, data, 0, &error);
950 : :
951 : 4550 : if G_UNLIKELY (thread == NULL)
952 : 0 : g_error ("creating thread '%s': %s", name ? name : "", error->message);
953 : :
954 : 4550 : return thread;
955 : : }
956 : :
957 : : /**
958 : : * g_thread_try_new:
959 : : * @name: (nullable): an (optional) name for the new thread
960 : : * @func: (closure data) (scope async): a function to execute in the new thread
961 : : * @data: (nullable): an argument to supply to the new thread
962 : : * @error: return location for error, or %NULL
963 : : *
964 : : * This function is the same as g_thread_new() except that
965 : : * it allows for the possibility of failure.
966 : : *
967 : : * If a thread can not be created (due to resource limits),
968 : : * @error is set and %NULL is returned.
969 : : *
970 : : * Returns: (transfer full): the new #GThread, or %NULL if an error occurred
971 : : *
972 : : * Since: 2.32
973 : : */
974 : : GThread *
975 : 399 : g_thread_try_new (const gchar *name,
976 : : GThreadFunc func,
977 : : gpointer data,
978 : : GError **error)
979 : : {
980 : 399 : return g_thread_new_internal (name, g_thread_proxy, func, data, 0, error);
981 : : }
982 : :
983 : : GThread *
984 : 12227 : g_thread_new_internal (const gchar *name,
985 : : GThreadFunc proxy,
986 : : GThreadFunc func,
987 : : gpointer data,
988 : : gsize stack_size,
989 : : GError **error)
990 : : {
991 : 12227 : g_return_val_if_fail (func != NULL, NULL);
992 : :
993 : 12227 : g_atomic_int_inc (&g_thread_n_created_counter);
994 : :
995 : 12227 : g_trace_mark (G_TRACE_CURRENT_TIME, 0, "GLib", "GThread created", "%s", name ? name : "(unnamed)");
996 : 12227 : return (GThread *) g_system_thread_new (proxy, stack_size, name, func, data, error);
997 : : }
998 : :
999 : : /**
1000 : : * g_thread_exit:
1001 : : * @retval: the return value of this thread
1002 : : *
1003 : : * Terminates the current thread.
1004 : : *
1005 : : * If another thread is waiting for us using g_thread_join() then the
1006 : : * waiting thread will be woken up and get @retval as the return value
1007 : : * of g_thread_join().
1008 : : *
1009 : : * Calling g_thread_exit() with a parameter @retval is equivalent to
1010 : : * returning @retval from the function @func, as given to g_thread_new().
1011 : : *
1012 : : * You must only call g_thread_exit() from a thread that you created
1013 : : * yourself with g_thread_new() or related APIs. You must not call
1014 : : * this function from a thread created with another threading library
1015 : : * or or from within a #GThreadPool.
1016 : : */
1017 : : void
1018 : 13 : g_thread_exit (gpointer retval)
1019 : : {
1020 : 13 : GRealThread* real = (GRealThread*) g_thread_self ();
1021 : :
1022 : 13 : if G_UNLIKELY (!real->ours)
1023 : 0 : g_error ("attempt to g_thread_exit() a thread not created by GLib");
1024 : :
1025 : 13 : real->retval = retval;
1026 : :
1027 : 13 : g_system_thread_exit ();
1028 : : }
1029 : :
1030 : : /**
1031 : : * g_thread_join:
1032 : : * @thread: (transfer full): a #GThread
1033 : : *
1034 : : * Waits until @thread finishes, i.e. the function @func, as
1035 : : * given to g_thread_new(), returns or g_thread_exit() is called.
1036 : : * If @thread has already terminated, then g_thread_join()
1037 : : * returns immediately.
1038 : : *
1039 : : * Any thread can wait for any other thread by calling g_thread_join(),
1040 : : * not just its 'creator'. Calling g_thread_join() from multiple threads
1041 : : * for the same @thread leads to undefined behaviour.
1042 : : *
1043 : : * The value returned by @func or given to g_thread_exit() is
1044 : : * returned by this function.
1045 : : *
1046 : : * g_thread_join() consumes the reference to the passed-in @thread.
1047 : : * This will usually cause the #GThread struct and associated resources
1048 : : * to be freed. Use g_thread_ref() to obtain an extra reference if you
1049 : : * want to keep the GThread alive beyond the g_thread_join() call.
1050 : : *
1051 : : * Returns: (transfer full): the return value of the thread
1052 : : */
1053 : : gpointer
1054 : 11318 : g_thread_join (GThread *thread)
1055 : : {
1056 : 11318 : GRealThread *real = (GRealThread*) thread;
1057 : : gpointer retval;
1058 : :
1059 : 11318 : g_return_val_if_fail (thread, NULL);
1060 : 11318 : g_return_val_if_fail (real->ours, NULL);
1061 : :
1062 : 11318 : g_system_thread_wait (real);
1063 : :
1064 : 11318 : retval = real->retval;
1065 : :
1066 : : /* Just to make sure, this isn't used any more */
1067 : 11318 : thread->joinable = 0;
1068 : :
1069 : 11318 : g_thread_unref (thread);
1070 : :
1071 : 11318 : return retval;
1072 : : }
1073 : :
1074 : : /**
1075 : : * g_thread_self:
1076 : : *
1077 : : * This function returns the #GThread corresponding to the
1078 : : * current thread. Note that this function does not increase
1079 : : * the reference count of the returned struct.
1080 : : *
1081 : : * This function will return a #GThread even for threads that
1082 : : * were not created by GLib (i.e. those created by other threading
1083 : : * APIs). This may be useful for thread identification purposes
1084 : : * (i.e. comparisons) but you must not use GLib functions (such
1085 : : * as g_thread_join()) on these threads.
1086 : : *
1087 : : * Returns: (transfer none): the #GThread representing the current thread
1088 : : */
1089 : : GThread*
1090 : 8171448 : g_thread_self (void)
1091 : : {
1092 : 8171448 : GRealThread* thread = g_private_get (&g_thread_specific_private);
1093 : :
1094 : 8171448 : if (!thread)
1095 : : {
1096 : : /* If no thread data is available, provide and set one.
1097 : : * This can happen for the main thread and for threads
1098 : : * that are not created by GLib.
1099 : : */
1100 : 264 : thread = g_slice_new0 (GRealThread);
1101 : 264 : thread->ref_count = 1;
1102 : :
1103 : 264 : g_private_set (&g_thread_specific_private, thread);
1104 : : }
1105 : :
1106 : 8171448 : return (GThread*) thread;
1107 : : }
1108 : :
1109 : : /**
1110 : : * g_get_num_processors:
1111 : : *
1112 : : * Determine the approximate number of threads that the system will
1113 : : * schedule simultaneously for this process. This is intended to be
1114 : : * used as a parameter to g_thread_pool_new() for CPU bound tasks and
1115 : : * similar cases.
1116 : : *
1117 : : * Returns: Number of schedulable threads, always greater than 0
1118 : : *
1119 : : * Since: 2.36
1120 : : */
1121 : : guint
1122 : 5 : g_get_num_processors (void)
1123 : : {
1124 : : #ifdef G_OS_WIN32
1125 : : unsigned int count;
1126 : : SYSTEM_INFO sysinfo;
1127 : : DWORD_PTR process_cpus;
1128 : : DWORD_PTR system_cpus;
1129 : :
1130 : : /* This *never* fails, use it as fallback */
1131 : : GetNativeSystemInfo (&sysinfo);
1132 : : count = (int) sysinfo.dwNumberOfProcessors;
1133 : :
1134 : : if (GetProcessAffinityMask (GetCurrentProcess (),
1135 : : &process_cpus, &system_cpus))
1136 : : {
1137 : : unsigned int af_count;
1138 : :
1139 : : for (af_count = 0; process_cpus != 0; process_cpus >>= 1)
1140 : : if (process_cpus & 1)
1141 : : af_count++;
1142 : :
1143 : : /* Prefer affinity-based result, if available */
1144 : : if (af_count > 0)
1145 : : count = af_count;
1146 : : }
1147 : :
1148 : : if (count > 0)
1149 : : return count;
1150 : : #elif defined(_SC_NPROCESSORS_ONLN) && defined(THREADS_POSIX) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
1151 : : {
1152 : 5 : int ncores = MIN (sysconf (_SC_NPROCESSORS_ONLN), CPU_SETSIZE);
1153 : : cpu_set_t cpu_mask;
1154 : 5 : CPU_ZERO (&cpu_mask);
1155 : :
1156 : 5 : int af_count = 0;
1157 : 5 : int err = pthread_getaffinity_np (pthread_self (), sizeof (cpu_mask), &cpu_mask);
1158 : 5 : if (!err)
1159 : 5 : af_count = CPU_COUNT (&cpu_mask);
1160 : :
1161 : 5 : int count = (af_count > 0) ? af_count : ncores;
1162 : 5 : return count;
1163 : : }
1164 : : #elif defined(_SC_NPROCESSORS_ONLN)
1165 : : {
1166 : : int count;
1167 : :
1168 : : count = sysconf (_SC_NPROCESSORS_ONLN);
1169 : : if (count > 0)
1170 : : return count;
1171 : : }
1172 : : #elif defined HW_NCPU
1173 : : {
1174 : : int mib[2], count = 0;
1175 : : size_t len;
1176 : :
1177 : : mib[0] = CTL_HW;
1178 : : mib[1] = HW_NCPU;
1179 : : len = sizeof(count);
1180 : :
1181 : : if (sysctl (mib, 2, &count, &len, NULL, 0) == 0 && count > 0)
1182 : : return count;
1183 : : }
1184 : : #endif
1185 : :
1186 : : return 1; /* Fallback */
1187 : : }
1188 : :
1189 : : /**
1190 : : * g_mutex_init:
1191 : : * @mutex: an uninitialized #GMutex
1192 : : *
1193 : : * Initializes a #GMutex so that it can be used.
1194 : : *
1195 : : * This function is useful to initialize a mutex that has been
1196 : : * allocated on the stack, or as part of a larger structure.
1197 : : * It is not necessary to initialize a mutex that has been
1198 : : * statically allocated.
1199 : : *
1200 : : * |[<!-- language="C" -->
1201 : : * typedef struct {
1202 : : * GMutex m;
1203 : : * ...
1204 : : * } Blob;
1205 : : *
1206 : : * Blob *b;
1207 : : *
1208 : : * b = g_new (Blob, 1);
1209 : : * g_mutex_init (&b->m);
1210 : : * ]|
1211 : : *
1212 : : * To undo the effect of g_mutex_init() when a mutex is no longer
1213 : : * needed, use g_mutex_clear().
1214 : : *
1215 : : * Calling g_mutex_init() on an already initialized #GMutex leads
1216 : : * to undefined behaviour.
1217 : : *
1218 : : * Since: 2.32
1219 : : */
1220 : : void
1221 : 135806 : g_mutex_init (GMutex *mutex)
1222 : : {
1223 : 135806 : g_mutex_init_impl (mutex);
1224 : 135806 : }
1225 : :
1226 : : /**
1227 : : * g_mutex_clear:
1228 : : * @mutex: an initialized #GMutex
1229 : : *
1230 : : * Frees the resources allocated to a mutex with g_mutex_init().
1231 : : *
1232 : : * This function should not be used with a #GMutex that has been
1233 : : * statically allocated.
1234 : : *
1235 : : * Calling g_mutex_clear() on a locked mutex leads to undefined
1236 : : * behaviour.
1237 : : *
1238 : : * Since: 2.32
1239 : : */
1240 : : void
1241 : 132566 : g_mutex_clear (GMutex *mutex)
1242 : : {
1243 : 132566 : g_mutex_clear_impl (mutex);
1244 : 132566 : }
1245 : :
1246 : : /**
1247 : : * g_mutex_lock:
1248 : : * @mutex: a #GMutex
1249 : : *
1250 : : * Locks @mutex. If @mutex is already locked by another thread, the
1251 : : * current thread will block until @mutex is unlocked by the other
1252 : : * thread.
1253 : : *
1254 : : * #GMutex is neither guaranteed to be recursive nor to be
1255 : : * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
1256 : : * already been locked by the same thread results in undefined behaviour
1257 : : * (including but not limited to deadlocks).
1258 : : */
1259 : : void
1260 : 243389644 : g_mutex_lock (GMutex *mutex)
1261 : : {
1262 : 243389644 : g_mutex_lock_impl (mutex);
1263 : 243389642 : }
1264 : :
1265 : : /**
1266 : : * g_mutex_unlock:
1267 : : * @mutex: a #GMutex
1268 : : *
1269 : : * Unlocks @mutex. If another thread is blocked in a g_mutex_lock()
1270 : : * call for @mutex, it will become unblocked and can lock @mutex itself.
1271 : : *
1272 : : * Calling g_mutex_unlock() on a mutex that is not locked by the
1273 : : * current thread leads to undefined behaviour.
1274 : : */
1275 : : void
1276 : 243967979 : g_mutex_unlock (GMutex *mutex)
1277 : : {
1278 : 243967979 : g_mutex_unlock_impl (mutex);
1279 : 243967979 : }
1280 : :
1281 : : /**
1282 : : * g_mutex_trylock:
1283 : : * @mutex: a #GMutex
1284 : : *
1285 : : * Tries to lock @mutex. If @mutex is already locked by another thread,
1286 : : * it immediately returns %FALSE. Otherwise it locks @mutex and returns
1287 : : * %TRUE.
1288 : : *
1289 : : * #GMutex is neither guaranteed to be recursive nor to be
1290 : : * non-recursive. As such, calling g_mutex_lock() on a #GMutex that has
1291 : : * already been locked by the same thread results in undefined behaviour
1292 : : * (including but not limited to deadlocks or arbitrary return values).
1293 : : *
1294 : : * Returns: %TRUE if @mutex could be locked
1295 : : */
1296 : : gboolean
1297 : 1123132 : g_mutex_trylock (GMutex *mutex)
1298 : : {
1299 : 1123132 : return g_mutex_trylock_impl (mutex);
1300 : : }
1301 : :
1302 : : /**
1303 : : * g_rec_mutex_init:
1304 : : * @rec_mutex: an uninitialized #GRecMutex
1305 : : *
1306 : : * Initializes a #GRecMutex so that it can be used.
1307 : : *
1308 : : * This function is useful to initialize a recursive mutex
1309 : : * that has been allocated on the stack, or as part of a larger
1310 : : * structure.
1311 : : *
1312 : : * It is not necessary to initialise a recursive mutex that has been
1313 : : * statically allocated.
1314 : : *
1315 : : * |[<!-- language="C" -->
1316 : : * typedef struct {
1317 : : * GRecMutex m;
1318 : : * ...
1319 : : * } Blob;
1320 : : *
1321 : : * Blob *b;
1322 : : *
1323 : : * b = g_new (Blob, 1);
1324 : : * g_rec_mutex_init (&b->m);
1325 : : * ]|
1326 : : *
1327 : : * Calling g_rec_mutex_init() on an already initialized #GRecMutex
1328 : : * leads to undefined behaviour.
1329 : : *
1330 : : * To undo the effect of g_rec_mutex_init() when a recursive mutex
1331 : : * is no longer needed, use g_rec_mutex_clear().
1332 : : *
1333 : : * Since: 2.32
1334 : : */
1335 : : void
1336 : 21 : g_rec_mutex_init (GRecMutex *rec_mutex)
1337 : : {
1338 : : g_rec_mutex_init_impl (rec_mutex);
1339 : 21 : }
1340 : :
1341 : : /**
1342 : : * g_rec_mutex_clear:
1343 : : * @rec_mutex: an initialized #GRecMutex
1344 : : *
1345 : : * Frees the resources allocated to a recursive mutex with
1346 : : * g_rec_mutex_init().
1347 : : *
1348 : : * This function should not be used with a #GRecMutex that has been
1349 : : * statically allocated.
1350 : : *
1351 : : * Calling g_rec_mutex_clear() on a locked recursive mutex leads
1352 : : * to undefined behaviour.
1353 : : *
1354 : : * Since: 2.32
1355 : : */
1356 : : void
1357 : 21 : g_rec_mutex_clear (GRecMutex *rec_mutex)
1358 : : {
1359 : : g_rec_mutex_clear_impl (rec_mutex);
1360 : 21 : }
1361 : :
1362 : : /**
1363 : : * g_rec_mutex_lock:
1364 : : * @rec_mutex: a #GRecMutex
1365 : : *
1366 : : * Locks @rec_mutex. If @rec_mutex is already locked by another
1367 : : * thread, the current thread will block until @rec_mutex is
1368 : : * unlocked by the other thread. If @rec_mutex is already locked
1369 : : * by the current thread, the 'lock count' of @rec_mutex is increased.
1370 : : * The mutex will only become available again when it is unlocked
1371 : : * as many times as it has been locked.
1372 : : *
1373 : : * Since: 2.32
1374 : : */
1375 : : void
1376 : 16934443 : g_rec_mutex_lock (GRecMutex *mutex)
1377 : : {
1378 : : g_rec_mutex_lock_impl (mutex);
1379 : 16934443 : }
1380 : :
1381 : : /**
1382 : : * g_rec_mutex_unlock:
1383 : : * @rec_mutex: a #GRecMutex
1384 : : *
1385 : : * Unlocks @rec_mutex. If another thread is blocked in a
1386 : : * g_rec_mutex_lock() call for @rec_mutex, it will become unblocked
1387 : : * and can lock @rec_mutex itself.
1388 : : *
1389 : : * Calling g_rec_mutex_unlock() on a recursive mutex that is not
1390 : : * locked by the current thread leads to undefined behaviour.
1391 : : *
1392 : : * Since: 2.32
1393 : : */
1394 : : void
1395 : 16939317 : g_rec_mutex_unlock (GRecMutex *rec_mutex)
1396 : : {
1397 : : g_rec_mutex_unlock_impl (rec_mutex);
1398 : 16939317 : }
1399 : :
1400 : : /**
1401 : : * g_rec_mutex_trylock:
1402 : : * @rec_mutex: a #GRecMutex
1403 : : *
1404 : : * Tries to lock @rec_mutex. If @rec_mutex is already locked
1405 : : * by another thread, it immediately returns %FALSE. Otherwise
1406 : : * it locks @rec_mutex and returns %TRUE.
1407 : : *
1408 : : * Returns: %TRUE if @rec_mutex could be locked
1409 : : *
1410 : : * Since: 2.32
1411 : : */
1412 : : gboolean
1413 : 10011 : g_rec_mutex_trylock (GRecMutex *rec_mutex)
1414 : : {
1415 : 10011 : return g_rec_mutex_trylock_impl (rec_mutex);
1416 : : }
1417 : :
1418 : : /* {{{1 GRWLock */
1419 : :
1420 : : /**
1421 : : * g_rw_lock_init:
1422 : : * @rw_lock: an uninitialized #GRWLock
1423 : : *
1424 : : * Initializes a #GRWLock so that it can be used.
1425 : : *
1426 : : * This function is useful to initialize a lock that has been
1427 : : * allocated on the stack, or as part of a larger structure. It is not
1428 : : * necessary to initialise a reader-writer lock that has been statically
1429 : : * allocated.
1430 : : *
1431 : : * |[<!-- language="C" -->
1432 : : * typedef struct {
1433 : : * GRWLock l;
1434 : : * ...
1435 : : * } Blob;
1436 : : *
1437 : : * Blob *b;
1438 : : *
1439 : : * b = g_new (Blob, 1);
1440 : : * g_rw_lock_init (&b->l);
1441 : : * ]|
1442 : : *
1443 : : * To undo the effect of g_rw_lock_init() when a lock is no longer
1444 : : * needed, use g_rw_lock_clear().
1445 : : *
1446 : : * Calling g_rw_lock_init() on an already initialized #GRWLock leads
1447 : : * to undefined behaviour.
1448 : : *
1449 : : * Since: 2.32
1450 : : */
1451 : : void
1452 : 52 : g_rw_lock_init (GRWLock *rw_lock)
1453 : : {
1454 : : g_rw_lock_init_impl (rw_lock);
1455 : 52 : }
1456 : :
1457 : : /**
1458 : : * g_rw_lock_clear:
1459 : : * @rw_lock: an initialized #GRWLock
1460 : : *
1461 : : * Frees the resources allocated to a lock with g_rw_lock_init().
1462 : : *
1463 : : * This function should not be used with a #GRWLock that has been
1464 : : * statically allocated.
1465 : : *
1466 : : * Calling g_rw_lock_clear() when any thread holds the lock
1467 : : * leads to undefined behaviour.
1468 : : *
1469 : : * Since: 2.32
1470 : : */
1471 : : void
1472 : 52 : g_rw_lock_clear (GRWLock *rw_lock)
1473 : : {
1474 : : g_rw_lock_clear_impl (rw_lock);
1475 : 52 : }
1476 : :
1477 : : /**
1478 : : * g_rw_lock_writer_lock:
1479 : : * @rw_lock: a #GRWLock
1480 : : *
1481 : : * Obtain a write lock on @rw_lock. If another thread currently holds
1482 : : * a read or write lock on @rw_lock, the current thread will block
1483 : : * until all other threads have dropped their locks on @rw_lock.
1484 : : *
1485 : : * Calling g_rw_lock_writer_lock() while the current thread already
1486 : : * owns a read or write lock on @rw_lock leads to undefined behaviour.
1487 : : *
1488 : : * Since: 2.32
1489 : : */
1490 : : void
1491 : 942856 : g_rw_lock_writer_lock (GRWLock *rw_lock)
1492 : : {
1493 : : g_rw_lock_writer_lock_impl (rw_lock);
1494 : 942856 : }
1495 : :
1496 : : /**
1497 : : * g_rw_lock_writer_trylock:
1498 : : * @rw_lock: a #GRWLock
1499 : : *
1500 : : * Tries to obtain a write lock on @rw_lock. If another thread
1501 : : * currently holds a read or write lock on @rw_lock, it immediately
1502 : : * returns %FALSE.
1503 : : * Otherwise it locks @rw_lock and returns %TRUE.
1504 : : *
1505 : : * Returns: %TRUE if @rw_lock could be locked
1506 : : *
1507 : : * Since: 2.32
1508 : : */
1509 : : gboolean
1510 : 1000006 : g_rw_lock_writer_trylock (GRWLock *rw_lock)
1511 : : {
1512 : 1000006 : return g_rw_lock_writer_trylock_impl (rw_lock);
1513 : : }
1514 : :
1515 : : /**
1516 : : * g_rw_lock_writer_unlock:
1517 : : * @rw_lock: a #GRWLock
1518 : : *
1519 : : * Release a write lock on @rw_lock.
1520 : : *
1521 : : * Calling g_rw_lock_writer_unlock() on a lock that is not held
1522 : : * by the current thread leads to undefined behaviour.
1523 : : *
1524 : : * Since: 2.32
1525 : : */
1526 : : void
1527 : 1386952 : g_rw_lock_writer_unlock (GRWLock *rw_lock)
1528 : : {
1529 : : g_rw_lock_writer_unlock_impl (rw_lock);
1530 : 1386952 : }
1531 : :
1532 : : /**
1533 : : * g_rw_lock_reader_lock:
1534 : : * @rw_lock: a #GRWLock
1535 : : *
1536 : : * Obtain a read lock on @rw_lock. If another thread currently holds
1537 : : * the write lock on @rw_lock, the current thread will block until the
1538 : : * write lock was (held and) released. If another thread does not hold
1539 : : * the write lock, but is waiting for it, it is implementation defined
1540 : : * whether the reader or writer will block. Read locks can be taken
1541 : : * recursively.
1542 : : *
1543 : : * Calling g_rw_lock_reader_lock() while the current thread already
1544 : : * owns a write lock leads to undefined behaviour. Read locks however
1545 : : * can be taken recursively, in which case you need to make sure to
1546 : : * call g_rw_lock_reader_unlock() the same amount of times.
1547 : : *
1548 : : * It is implementation-defined how many read locks are allowed to be
1549 : : * held on the same lock simultaneously. If the limit is hit,
1550 : : * or if a deadlock is detected, a critical warning will be emitted.
1551 : : *
1552 : : * Since: 2.32
1553 : : */
1554 : : void
1555 : 11329432 : g_rw_lock_reader_lock (GRWLock *rw_lock)
1556 : : {
1557 : : g_rw_lock_reader_lock_impl (rw_lock);
1558 : 11329432 : }
1559 : :
1560 : : /**
1561 : : * g_rw_lock_reader_trylock:
1562 : : * @rw_lock: a #GRWLock
1563 : : *
1564 : : * Tries to obtain a read lock on @rw_lock and returns %TRUE if
1565 : : * the read lock was successfully obtained. Otherwise it
1566 : : * returns %FALSE.
1567 : : *
1568 : : * Returns: %TRUE if @rw_lock could be locked
1569 : : *
1570 : : * Since: 2.32
1571 : : */
1572 : : gboolean
1573 : 6 : g_rw_lock_reader_trylock (GRWLock *rw_lock)
1574 : : {
1575 : 6 : return g_rw_lock_reader_trylock_impl (rw_lock);
1576 : : }
1577 : :
1578 : : /**
1579 : : * g_rw_lock_reader_unlock:
1580 : : * @rw_lock: a #GRWLock
1581 : : *
1582 : : * Release a read lock on @rw_lock.
1583 : : *
1584 : : * Calling g_rw_lock_reader_unlock() on a lock that is not held
1585 : : * by the current thread leads to undefined behaviour.
1586 : : *
1587 : : * Since: 2.32
1588 : : */
1589 : : void
1590 : 11329436 : g_rw_lock_reader_unlock (GRWLock *rw_lock)
1591 : : {
1592 : : g_rw_lock_reader_unlock_impl (rw_lock);
1593 : 11329436 : }
1594 : :
1595 : : /* {{{1 GCond */
1596 : :
1597 : : /**
1598 : : * g_cond_init:
1599 : : * @cond: an uninitialized #GCond
1600 : : *
1601 : : * Initialises a #GCond so that it can be used.
1602 : : *
1603 : : * This function is useful to initialise a #GCond that has been
1604 : : * allocated as part of a larger structure. It is not necessary to
1605 : : * initialise a #GCond that has been statically allocated.
1606 : : *
1607 : : * To undo the effect of g_cond_init() when a #GCond is no longer
1608 : : * needed, use g_cond_clear().
1609 : : *
1610 : : * Calling g_cond_init() on an already-initialised #GCond leads
1611 : : * to undefined behaviour.
1612 : : *
1613 : : * Since: 2.32
1614 : : */
1615 : : void
1616 : 282008 : g_cond_init (GCond *cond)
1617 : : {
1618 : 282008 : g_cond_init_impl (cond);
1619 : 282008 : }
1620 : :
1621 : : /**
1622 : : * g_cond_clear:
1623 : : * @cond: an initialised #GCond
1624 : : *
1625 : : * Frees the resources allocated to a #GCond with g_cond_init().
1626 : : *
1627 : : * This function should not be used with a #GCond that has been
1628 : : * statically allocated.
1629 : : *
1630 : : * Calling g_cond_clear() for a #GCond on which threads are
1631 : : * blocking leads to undefined behaviour.
1632 : : *
1633 : : * Since: 2.32
1634 : : */
1635 : : void
1636 : 280608 : g_cond_clear (GCond *cond)
1637 : : {
1638 : 280608 : g_cond_clear_impl (cond);
1639 : 280608 : }
1640 : :
1641 : : /**
1642 : : * g_cond_wait:
1643 : : * @cond: a #GCond
1644 : : * @mutex: a #GMutex that is currently locked
1645 : : *
1646 : : * Atomically releases @mutex and waits until @cond is signalled.
1647 : : * When this function returns, @mutex is locked again and owned by the
1648 : : * calling thread.
1649 : : *
1650 : : * When using condition variables, it is possible that a spurious wakeup
1651 : : * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
1652 : : * not called). It's also possible that a stolen wakeup may occur.
1653 : : * This is when g_cond_signal() is called, but another thread acquires
1654 : : * @mutex before this thread and modifies the state of the program in
1655 : : * such a way that when g_cond_wait() is able to return, the expected
1656 : : * condition is no longer met.
1657 : : *
1658 : : * For this reason, g_cond_wait() must always be used in a loop. See
1659 : : * the documentation for #GCond for a complete example.
1660 : : **/
1661 : : void
1662 : 778414 : g_cond_wait (GCond *cond,
1663 : : GMutex *mutex)
1664 : : {
1665 : 778414 : g_cond_wait_impl (cond, mutex);
1666 : 778247 : }
1667 : :
1668 : : /**
1669 : : * g_cond_signal:
1670 : : * @cond: a #GCond
1671 : : *
1672 : : * If threads are waiting for @cond, at least one of them is unblocked.
1673 : : * If no threads are waiting for @cond, this function has no effect.
1674 : : * It is good practice to hold the same lock as the waiting thread
1675 : : * while calling this function, though not required.
1676 : : */
1677 : : void
1678 : 987332 : g_cond_signal (GCond *cond)
1679 : : {
1680 : 987332 : g_cond_signal_impl (cond);
1681 : 987332 : }
1682 : :
1683 : : /**
1684 : : * g_cond_broadcast:
1685 : : * @cond: a #GCond
1686 : : *
1687 : : * If threads are waiting for @cond, all of them are unblocked.
1688 : : * If no threads are waiting for @cond, this function has no effect.
1689 : : * It is good practice to lock the same mutex as the waiting threads
1690 : : * while calling this function, though not required.
1691 : : */
1692 : : void
1693 : 137621 : g_cond_broadcast (GCond *cond)
1694 : : {
1695 : 137621 : g_cond_broadcast_impl (cond);
1696 : 137621 : }
1697 : :
1698 : : /**
1699 : : * g_cond_wait_until:
1700 : : * @cond: a #GCond
1701 : : * @mutex: a #GMutex that is currently locked
1702 : : * @end_time: the monotonic time to wait until
1703 : : *
1704 : : * Waits until either @cond is signalled or @end_time has passed.
1705 : : *
1706 : : * As with g_cond_wait() it is possible that a spurious or stolen wakeup
1707 : : * could occur. For that reason, waiting on a condition variable should
1708 : : * always be in a loop, based on an explicitly-checked predicate.
1709 : : *
1710 : : * %TRUE is returned if the condition variable was signalled (or in the
1711 : : * case of a spurious wakeup). %FALSE is returned if @end_time has
1712 : : * passed.
1713 : : *
1714 : : * The following code shows how to correctly perform a timed wait on a
1715 : : * condition variable (extending the example presented in the
1716 : : * documentation for #GCond):
1717 : : *
1718 : : * |[<!-- language="C" -->
1719 : : * gpointer
1720 : : * pop_data_timed (void)
1721 : : * {
1722 : : * gint64 end_time;
1723 : : * gpointer data;
1724 : : *
1725 : : * g_mutex_lock (&data_mutex);
1726 : : *
1727 : : * end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
1728 : : * while (!current_data)
1729 : : * if (!g_cond_wait_until (&data_cond, &data_mutex, end_time))
1730 : : * {
1731 : : * // timeout has passed.
1732 : : * g_mutex_unlock (&data_mutex);
1733 : : * return NULL;
1734 : : * }
1735 : : *
1736 : : * // there is data for us
1737 : : * data = current_data;
1738 : : * current_data = NULL;
1739 : : *
1740 : : * g_mutex_unlock (&data_mutex);
1741 : : *
1742 : : * return data;
1743 : : * }
1744 : : * ]|
1745 : : *
1746 : : * Notice that the end time is calculated once, before entering the
1747 : : * loop and reused. This is the motivation behind the use of absolute
1748 : : * time on this API -- if a relative time of 5 seconds were passed
1749 : : * directly to the call and a spurious wakeup occurred, the program would
1750 : : * have to start over waiting again (which would lead to a total wait
1751 : : * time of more than 5 seconds).
1752 : : *
1753 : : * Returns: %TRUE on a signal, %FALSE on a timeout
1754 : : * Since: 2.32
1755 : : **/
1756 : : gboolean
1757 : 2999 : g_cond_wait_until (GCond *cond,
1758 : : GMutex *mutex,
1759 : : gint64 end_time)
1760 : : {
1761 : 2999 : return g_cond_wait_until_impl (cond, mutex, end_time);
1762 : : }
1763 : :
1764 : : /* {{{1 GPrivate */
1765 : :
1766 : : /**
1767 : : * GPrivate:
1768 : : *
1769 : : * The #GPrivate struct is an opaque data structure to represent a
1770 : : * thread-local data key. It is approximately equivalent to the
1771 : : * pthread_setspecific()/pthread_getspecific() APIs on POSIX and to
1772 : : * TlsSetValue()/TlsGetValue() on Windows.
1773 : : *
1774 : : * If you don't already know why you might want this functionality,
1775 : : * then you probably don't need it.
1776 : : *
1777 : : * #GPrivate is a very limited resource (as far as 128 per program,
1778 : : * shared between all libraries). It is also not possible to destroy a
1779 : : * #GPrivate after it has been used. As such, it is only ever acceptable
1780 : : * to use #GPrivate in static scope, and even then sparingly so.
1781 : : *
1782 : : * See G_PRIVATE_INIT() for a couple of examples.
1783 : : *
1784 : : * The #GPrivate structure should be considered opaque. It should only
1785 : : * be accessed via the g_private_ functions.
1786 : : */
1787 : :
1788 : : /**
1789 : : * G_PRIVATE_INIT:
1790 : : * @notify: a #GDestroyNotify
1791 : : *
1792 : : * A macro to assist with the static initialisation of a #GPrivate.
1793 : : *
1794 : : * This macro is useful for the case that a #GDestroyNotify function
1795 : : * should be associated with the key. This is needed when the key will be
1796 : : * used to point at memory that should be deallocated when the thread
1797 : : * exits.
1798 : : *
1799 : : * Additionally, the #GDestroyNotify will also be called on the previous
1800 : : * value stored in the key when g_private_replace() is used.
1801 : : *
1802 : : * If no #GDestroyNotify is needed, then use of this macro is not
1803 : : * required -- if the #GPrivate is declared in static scope then it will
1804 : : * be properly initialised by default (ie: to all zeros). See the
1805 : : * examples below.
1806 : : *
1807 : : * |[<!-- language="C" -->
1808 : : * static GPrivate name_key = G_PRIVATE_INIT (g_free);
1809 : : *
1810 : : * // return value should not be freed
1811 : : * const gchar *
1812 : : * get_local_name (void)
1813 : : * {
1814 : : * return g_private_get (&name_key);
1815 : : * }
1816 : : *
1817 : : * void
1818 : : * set_local_name (const gchar *name)
1819 : : * {
1820 : : * g_private_replace (&name_key, g_strdup (name));
1821 : : * }
1822 : : *
1823 : : *
1824 : : * static GPrivate count_key; // no free function
1825 : : *
1826 : : * gint
1827 : : * get_local_count (void)
1828 : : * {
1829 : : * return GPOINTER_TO_INT (g_private_get (&count_key));
1830 : : * }
1831 : : *
1832 : : * void
1833 : : * set_local_count (gint count)
1834 : : * {
1835 : : * g_private_set (&count_key, GINT_TO_POINTER (count));
1836 : : * }
1837 : : * ]|
1838 : : *
1839 : : * Since: 2.32
1840 : : **/
1841 : :
1842 : : /**
1843 : : * g_private_get:
1844 : : * @key: a #GPrivate
1845 : : *
1846 : : * Returns the current value of the thread local variable @key.
1847 : : *
1848 : : * If the value has not yet been set in this thread, %NULL is returned.
1849 : : * Values are never copied between threads (when a new thread is
1850 : : * created, for example).
1851 : : *
1852 : : * Returns: the thread-local value
1853 : : */
1854 : : gpointer
1855 : 11406612 : g_private_get (GPrivate *key)
1856 : : {
1857 : 11406612 : return g_private_get_impl (key);
1858 : : }
1859 : :
1860 : : /**
1861 : : * g_private_set:
1862 : : * @key: a #GPrivate
1863 : : * @value: the new value
1864 : : *
1865 : : * Sets the thread local variable @key to have the value @value in the
1866 : : * current thread.
1867 : : *
1868 : : * This function differs from g_private_replace() in the following way:
1869 : : * the #GDestroyNotify for @key is not called on the old value.
1870 : : */
1871 : : void
1872 : 3024714 : g_private_set (GPrivate *key,
1873 : : gpointer value)
1874 : : {
1875 : : g_private_set_impl (key, value);
1876 : 3024714 : }
1877 : :
1878 : : /**
1879 : : * g_private_replace:
1880 : : * @key: a #GPrivate
1881 : : * @value: the new value
1882 : : *
1883 : : * Sets the thread local variable @key to have the value @value in the
1884 : : * current thread.
1885 : : *
1886 : : * This function differs from g_private_set() in the following way: if
1887 : : * the previous value was non-%NULL then the #GDestroyNotify handler for
1888 : : * @key is run on it.
1889 : : *
1890 : : * Since: 2.32
1891 : : **/
1892 : : void
1893 : 771 : g_private_replace (GPrivate *key,
1894 : : gpointer value)
1895 : : {
1896 : : g_private_replace_impl (key, value);
1897 : 771 : }
1898 : :
1899 : : /* {{{1 GThread */
1900 : :
1901 : : /**
1902 : : * g_thread_yield:
1903 : : *
1904 : : * Causes the calling thread to voluntarily relinquish the CPU, so
1905 : : * that other threads can run.
1906 : : *
1907 : : * This function is often used as a method to make busy wait less evil.
1908 : : */
1909 : : void
1910 : 19940349 : g_thread_yield (void)
1911 : : {
1912 : : g_thread_yield_impl ();
1913 : 19940349 : }
1914 : :
1915 : : /* Epilogue {{{1 */
1916 : : /* vim: set foldmethod=marker: */
|