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