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: posix thread system implementation
5 : : * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General Public
20 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 : : */
22 : :
23 : : /*
24 : : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 : : * file for a list of people on the GLib Team. See the ChangeLog
26 : : * files for a list of changes. These files are distributed with
27 : : * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 : : */
29 : :
30 : : /* The GMutex, GCond and GPrivate implementations in this file are some
31 : : * of the lowest-level code in GLib. All other parts of GLib (messages,
32 : : * memory, slices, etc) assume that they can freely use these facilities
33 : : * without risking recursion.
34 : : *
35 : : * As such, these functions are NOT permitted to call any other part of
36 : : * GLib.
37 : : *
38 : : * The thread manipulation functions (create, exit, join, etc.) have
39 : : * more freedom -- they can do as they please.
40 : : */
41 : :
42 : : #include "config.h"
43 : :
44 : : #include "gthread.h"
45 : :
46 : : #include "gmain.h"
47 : : #include "gmessages.h"
48 : : #include "gslice.h"
49 : : #include "gstrfuncs.h"
50 : : #include "gtestutils.h"
51 : : #include "gthreadprivate.h"
52 : : #include "gutils.h"
53 : :
54 : : #include <stdlib.h>
55 : : #include <stdio.h>
56 : : #include <string.h>
57 : : #include <errno.h>
58 : : #include <pthread.h>
59 : :
60 : : #include <sys/time.h>
61 : : #include <unistd.h>
62 : :
63 : : #ifdef HAVE_PTHREAD_SET_NAME_NP
64 : : #include <pthread_np.h>
65 : : #endif
66 : : #ifdef HAVE_SCHED_H
67 : : #include <sched.h>
68 : : #endif
69 : : #ifdef G_OS_WIN32
70 : : #include <windows.h>
71 : : #endif
72 : : #ifdef __GNU__
73 : : #include <mach/mach_traps.h>
74 : : #include <mach/thread_switch.h>
75 : : #endif
76 : :
77 : : #if defined(HAVE_SYS_SCHED_GETATTR)
78 : : #include <sys/syscall.h>
79 : : #endif
80 : :
81 : : #if (defined(HAVE_FUTEX) || defined(HAVE_FUTEX_TIME64)) && \
82 : : (defined(HAVE_STDATOMIC_H) || defined(__ATOMIC_SEQ_CST))
83 : : #define USE_NATIVE_MUTEX
84 : : #endif
85 : :
86 : : static void
87 : 0 : g_thread_abort (gint status,
88 : : const gchar *function)
89 : : {
90 : 0 : fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s. Aborting.\n",
91 : : function, strerror (status));
92 : 0 : g_abort ();
93 : : }
94 : :
95 : : /* {{{1 GMutex */
96 : :
97 : : #if !defined(USE_NATIVE_MUTEX)
98 : :
99 : : static pthread_mutex_t *
100 : : g_mutex_impl_new (void)
101 : : {
102 : : pthread_mutexattr_t *pattr = NULL;
103 : : pthread_mutex_t *mutex;
104 : : gint status;
105 : : #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
106 : : pthread_mutexattr_t attr;
107 : : #endif
108 : :
109 : : mutex = malloc (sizeof (pthread_mutex_t));
110 : : if G_UNLIKELY (mutex == NULL)
111 : : g_thread_abort (errno, "malloc");
112 : :
113 : : #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
114 : : pthread_mutexattr_init (&attr);
115 : : pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
116 : : pattr = &attr;
117 : : #endif
118 : :
119 : : if G_UNLIKELY ((status = pthread_mutex_init (mutex, pattr)) != 0)
120 : : g_thread_abort (status, "pthread_mutex_init");
121 : :
122 : : #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
123 : : pthread_mutexattr_destroy (&attr);
124 : : #endif
125 : :
126 : : return mutex;
127 : : }
128 : :
129 : : static void
130 : : g_mutex_impl_free (pthread_mutex_t *mutex)
131 : : {
132 : : pthread_mutex_destroy (mutex);
133 : : free (mutex);
134 : : }
135 : :
136 : : static inline pthread_mutex_t *
137 : : g_mutex_get_impl (GMutex *mutex)
138 : : {
139 : : pthread_mutex_t *impl = g_atomic_pointer_get (&mutex->p);
140 : :
141 : : if G_UNLIKELY (impl == NULL)
142 : : {
143 : : impl = g_mutex_impl_new ();
144 : : if (!g_atomic_pointer_compare_and_exchange (&mutex->p, NULL, impl))
145 : : g_mutex_impl_free (impl);
146 : : impl = mutex->p;
147 : : }
148 : :
149 : : return impl;
150 : : }
151 : :
152 : :
153 : : G_ALWAYS_INLINE static inline void
154 : : g_mutex_init_impl (GMutex *mutex)
155 : : {
156 : : mutex->p = g_mutex_impl_new ();
157 : : }
158 : :
159 : : G_ALWAYS_INLINE static inline void
160 : : g_mutex_clear_impl (GMutex *mutex)
161 : : {
162 : : g_mutex_impl_free (mutex->p);
163 : : }
164 : :
165 : : G_ALWAYS_INLINE static inline void
166 : : g_mutex_lock_impl (GMutex *mutex)
167 : : {
168 : : gint status;
169 : :
170 : : if G_UNLIKELY ((status = pthread_mutex_lock (g_mutex_get_impl (mutex))) != 0)
171 : : g_thread_abort (status, "pthread_mutex_lock");
172 : : }
173 : :
174 : : G_ALWAYS_INLINE static inline void
175 : : g_mutex_unlock_impl (GMutex *mutex)
176 : : {
177 : : gint status;
178 : :
179 : : if G_UNLIKELY ((status = pthread_mutex_unlock (g_mutex_get_impl (mutex))) != 0)
180 : : g_thread_abort (status, "pthread_mutex_unlock");
181 : : }
182 : :
183 : : G_ALWAYS_INLINE static inline gboolean
184 : : g_mutex_trylock_impl (GMutex *mutex)
185 : : {
186 : : gint status;
187 : :
188 : : if G_LIKELY ((status = pthread_mutex_trylock (g_mutex_get_impl (mutex))) == 0)
189 : : return TRUE;
190 : :
191 : : if G_UNLIKELY (status != EBUSY)
192 : : g_thread_abort (status, "pthread_mutex_trylock");
193 : :
194 : : return FALSE;
195 : : }
196 : :
197 : : #endif /* !defined(USE_NATIVE_MUTEX) */
198 : :
199 : : /* {{{1 GRecMutex */
200 : :
201 : : static pthread_mutex_t *
202 : 939 : g_rec_mutex_impl_new (void)
203 : : {
204 : : pthread_mutexattr_t attr;
205 : : pthread_mutex_t *mutex;
206 : :
207 : 939 : mutex = malloc (sizeof (pthread_mutex_t));
208 : 939 : if G_UNLIKELY (mutex == NULL)
209 : 0 : g_thread_abort (errno, "malloc");
210 : :
211 : 939 : pthread_mutexattr_init (&attr);
212 : 939 : pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
213 : 939 : pthread_mutex_init (mutex, &attr);
214 : 939 : pthread_mutexattr_destroy (&attr);
215 : :
216 : 939 : return mutex;
217 : : }
218 : :
219 : : static void
220 : 21 : g_rec_mutex_impl_free (pthread_mutex_t *mutex)
221 : : {
222 : 21 : pthread_mutex_destroy (mutex);
223 : 21 : free (mutex);
224 : 21 : }
225 : :
226 : : static inline pthread_mutex_t *
227 : 1743105 : g_rec_mutex_get_impl (GRecMutex *rec_mutex)
228 : : {
229 : 1743105 : pthread_mutex_t *impl = g_atomic_pointer_get (&rec_mutex->p);
230 : :
231 : 1743105 : if G_UNLIKELY (impl == NULL)
232 : : {
233 : 918 : impl = g_rec_mutex_impl_new ();
234 : 918 : if (!g_atomic_pointer_compare_and_exchange (&rec_mutex->p, NULL, impl))
235 : 0 : g_rec_mutex_impl_free (impl);
236 : 918 : impl = rec_mutex->p;
237 : : }
238 : :
239 : 1743105 : return impl;
240 : : }
241 : :
242 : : G_ALWAYS_INLINE static inline void
243 : : g_rec_mutex_init_impl (GRecMutex *rec_mutex)
244 : : {
245 : 21 : rec_mutex->p = g_rec_mutex_impl_new ();
246 : 21 : }
247 : :
248 : : G_ALWAYS_INLINE static inline void
249 : : g_rec_mutex_clear_impl (GRecMutex *rec_mutex)
250 : : {
251 : 21 : g_rec_mutex_impl_free (rec_mutex->p);
252 : 21 : }
253 : :
254 : : G_ALWAYS_INLINE static inline void
255 : : g_rec_mutex_lock_impl (GRecMutex *mutex)
256 : : {
257 : 1733094 : pthread_mutex_lock (g_rec_mutex_get_impl (mutex));
258 : 1733094 : }
259 : :
260 : : G_ALWAYS_INLINE static inline void
261 : : g_rec_mutex_unlock_impl (GRecMutex *rec_mutex)
262 : : {
263 : 1740442 : pthread_mutex_unlock (rec_mutex->p);
264 : 1740442 : }
265 : :
266 : : G_ALWAYS_INLINE static inline gboolean
267 : : g_rec_mutex_trylock_impl (GRecMutex *rec_mutex)
268 : : {
269 : 10011 : if (pthread_mutex_trylock (g_rec_mutex_get_impl (rec_mutex)) != 0)
270 : 2663 : return FALSE;
271 : :
272 : 7348 : return TRUE;
273 : : }
274 : :
275 : : /* {{{1 GRWLock */
276 : :
277 : : static pthread_rwlock_t *
278 : 1407 : g_rw_lock_impl_new (void)
279 : : {
280 : : pthread_rwlock_t *rwlock;
281 : : gint status;
282 : :
283 : 1407 : rwlock = malloc (sizeof (pthread_rwlock_t));
284 : 1407 : if G_UNLIKELY (rwlock == NULL)
285 : 0 : g_thread_abort (errno, "malloc");
286 : :
287 : 1407 : if G_UNLIKELY ((status = pthread_rwlock_init (rwlock, NULL)) != 0)
288 : 0 : g_thread_abort (status, "pthread_rwlock_init");
289 : :
290 : 1407 : return rwlock;
291 : : }
292 : :
293 : : static void
294 : 52 : g_rw_lock_impl_free (pthread_rwlock_t *rwlock)
295 : : {
296 : 52 : pthread_rwlock_destroy (rwlock);
297 : 52 : free (rwlock);
298 : 52 : }
299 : :
300 : : static inline pthread_rwlock_t *
301 : 33410242 : g_rw_lock_get_impl (GRWLock *lock)
302 : : {
303 : 33410242 : pthread_rwlock_t *impl = g_atomic_pointer_get (&lock->p);
304 : :
305 : 33410242 : if G_UNLIKELY (impl == NULL)
306 : : {
307 : 1355 : impl = g_rw_lock_impl_new ();
308 : 1355 : if (!g_atomic_pointer_compare_and_exchange (&lock->p, NULL, impl))
309 : 0 : g_rw_lock_impl_free (impl);
310 : 1355 : impl = lock->p;
311 : : }
312 : :
313 : 33410242 : return impl;
314 : : }
315 : :
316 : : G_ALWAYS_INLINE static inline void
317 : : g_rw_lock_init_impl (GRWLock *rw_lock)
318 : : {
319 : 52 : rw_lock->p = g_rw_lock_impl_new ();
320 : 52 : }
321 : :
322 : : G_ALWAYS_INLINE static inline void
323 : : g_rw_lock_clear_impl (GRWLock *rw_lock)
324 : : {
325 : 52 : g_rw_lock_impl_free (rw_lock->p);
326 : 52 : }
327 : :
328 : : G_ALWAYS_INLINE static inline void
329 : : g_rw_lock_writer_lock_impl (GRWLock *rw_lock)
330 : : {
331 : 1250677 : int retval = pthread_rwlock_wrlock (g_rw_lock_get_impl (rw_lock));
332 : :
333 : 1250677 : if (retval != 0)
334 : 0 : g_critical ("Failed to get RW lock %p: %s", rw_lock, g_strerror (retval));
335 : 1250677 : }
336 : :
337 : : G_ALWAYS_INLINE static inline gboolean
338 : : g_rw_lock_writer_trylock_impl (GRWLock *rw_lock)
339 : : {
340 : 1000006 : if (pthread_rwlock_trywrlock (g_rw_lock_get_impl (rw_lock)) != 0)
341 : 190204 : return FALSE;
342 : :
343 : 809802 : return TRUE;
344 : : }
345 : :
346 : : G_ALWAYS_INLINE static inline void
347 : : g_rw_lock_writer_unlock_impl (GRWLock *rw_lock)
348 : : {
349 : 2060479 : pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
350 : 2060479 : }
351 : :
352 : : G_ALWAYS_INLINE static inline void
353 : : g_rw_lock_reader_lock_impl (GRWLock *rw_lock)
354 : : {
355 : 14549535 : int retval = pthread_rwlock_rdlock (g_rw_lock_get_impl (rw_lock));
356 : :
357 : 14549535 : if (retval != 0)
358 : 0 : g_critical ("Failed to get RW lock %p: %s", rw_lock, g_strerror (retval));
359 : 14549535 : }
360 : :
361 : : G_ALWAYS_INLINE static inline gboolean
362 : : g_rw_lock_reader_trylock_impl (GRWLock *rw_lock)
363 : : {
364 : 6 : if (pthread_rwlock_tryrdlock (g_rw_lock_get_impl (rw_lock)) != 0)
365 : 2 : return FALSE;
366 : :
367 : 4 : return TRUE;
368 : : }
369 : :
370 : : G_ALWAYS_INLINE static inline void
371 : : g_rw_lock_reader_unlock_impl (GRWLock *rw_lock)
372 : : {
373 : 14549539 : pthread_rwlock_unlock (g_rw_lock_get_impl (rw_lock));
374 : 14549539 : }
375 : :
376 : : /* {{{1 GCond */
377 : :
378 : : #if !defined(USE_NATIVE_MUTEX)
379 : :
380 : : static pthread_cond_t *
381 : : g_cond_impl_new (void)
382 : : {
383 : : pthread_condattr_t attr;
384 : : pthread_cond_t *cond;
385 : : gint status;
386 : :
387 : : pthread_condattr_init (&attr);
388 : :
389 : : #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
390 : : #elif defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
391 : : if G_UNLIKELY ((status = pthread_condattr_setclock (&attr, CLOCK_MONOTONIC)) != 0)
392 : : g_thread_abort (status, "pthread_condattr_setclock");
393 : : #else
394 : : #error Cannot support GCond on your platform.
395 : : #endif
396 : :
397 : : cond = malloc (sizeof (pthread_cond_t));
398 : : if G_UNLIKELY (cond == NULL)
399 : : g_thread_abort (errno, "malloc");
400 : :
401 : : if G_UNLIKELY ((status = pthread_cond_init (cond, &attr)) != 0)
402 : : g_thread_abort (status, "pthread_cond_init");
403 : :
404 : : pthread_condattr_destroy (&attr);
405 : :
406 : : return cond;
407 : : }
408 : :
409 : : static void
410 : : g_cond_impl_free (pthread_cond_t *cond)
411 : : {
412 : : pthread_cond_destroy (cond);
413 : : free (cond);
414 : : }
415 : :
416 : : static inline pthread_cond_t *
417 : : g_cond_get_impl (GCond *cond)
418 : : {
419 : : pthread_cond_t *impl = g_atomic_pointer_get (&cond->p);
420 : :
421 : : if G_UNLIKELY (impl == NULL)
422 : : {
423 : : impl = g_cond_impl_new ();
424 : : if (!g_atomic_pointer_compare_and_exchange (&cond->p, NULL, impl))
425 : : g_cond_impl_free (impl);
426 : : impl = cond->p;
427 : : }
428 : :
429 : : return impl;
430 : : }
431 : :
432 : : G_ALWAYS_INLINE static inline void
433 : : g_cond_init_impl (GCond *cond)
434 : : {
435 : : cond->p = g_cond_impl_new ();
436 : : }
437 : :
438 : : G_ALWAYS_INLINE static inline void
439 : : g_cond_clear_impl (GCond *cond)
440 : : {
441 : : g_cond_impl_free (cond->p);
442 : : }
443 : :
444 : : G_ALWAYS_INLINE static inline void
445 : : g_cond_wait_impl (GCond *cond,
446 : : GMutex *mutex)
447 : : {
448 : : gint status;
449 : :
450 : : if G_UNLIKELY ((status = pthread_cond_wait (g_cond_get_impl (cond), g_mutex_get_impl (mutex))) != 0)
451 : : g_thread_abort (status, "pthread_cond_wait");
452 : : }
453 : :
454 : : G_ALWAYS_INLINE static inline void
455 : : g_cond_signal_impl (GCond *cond)
456 : : {
457 : : gint status;
458 : :
459 : : if G_UNLIKELY ((status = pthread_cond_signal (g_cond_get_impl (cond))) != 0)
460 : : g_thread_abort (status, "pthread_cond_signal");
461 : : }
462 : :
463 : : G_ALWAYS_INLINE static inline void
464 : : g_cond_broadcast_impl (GCond *cond)
465 : : {
466 : : gint status;
467 : :
468 : : if G_UNLIKELY ((status = pthread_cond_broadcast (g_cond_get_impl (cond))) != 0)
469 : : g_thread_abort (status, "pthread_cond_broadcast");
470 : : }
471 : :
472 : : G_ALWAYS_INLINE static inline gboolean
473 : : g_cond_wait_until_impl (GCond *cond,
474 : : GMutex *mutex,
475 : : gint64 end_time)
476 : : {
477 : : struct timespec ts;
478 : : gint status;
479 : :
480 : : #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
481 : : /* end_time is given relative to the monotonic clock as returned by
482 : : * g_get_monotonic_time().
483 : : *
484 : : * Since this pthreads wants the relative time, convert it back again.
485 : : */
486 : : {
487 : : gint64 now = g_get_monotonic_time ();
488 : : gint64 relative;
489 : :
490 : : if (end_time <= now)
491 : : return FALSE;
492 : :
493 : : relative = end_time - now;
494 : :
495 : : ts.tv_sec = relative / 1000000;
496 : : ts.tv_nsec = (relative % 1000000) * 1000;
497 : :
498 : : if ((status = pthread_cond_timedwait_relative_np (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
499 : : return TRUE;
500 : : }
501 : : #elif defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
502 : : /* This is the exact check we used during init to set the clock to
503 : : * monotonic, so if we're in this branch, timedwait() will already be
504 : : * expecting a monotonic clock.
505 : : */
506 : : {
507 : : ts.tv_sec = end_time / 1000000;
508 : : ts.tv_nsec = (end_time % 1000000) * 1000;
509 : :
510 : : if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
511 : : return TRUE;
512 : : }
513 : : #else
514 : : #error Cannot support GCond on your platform.
515 : : #endif
516 : :
517 : : if G_UNLIKELY (status != ETIMEDOUT)
518 : : g_thread_abort (status, "pthread_cond_timedwait");
519 : :
520 : : return FALSE;
521 : : }
522 : :
523 : : #endif /* defined(USE_NATIVE_MUTEX) */
524 : :
525 : : /* {{{1 GPrivate */
526 : :
527 : : static pthread_key_t *
528 : 0 : g_private_impl_new (GDestroyNotify notify)
529 : : {
530 : : pthread_key_t *key;
531 : : gint status;
532 : :
533 : 0 : key = malloc (sizeof (pthread_key_t));
534 : 0 : if G_UNLIKELY (key == NULL)
535 : 0 : g_thread_abort (errno, "malloc");
536 : 0 : status = pthread_key_create (key, notify);
537 : 0 : if G_UNLIKELY (status != 0)
538 : 0 : g_thread_abort (status, "pthread_key_create");
539 : :
540 : 0 : return key;
541 : : }
542 : :
543 : : static void
544 : 0 : g_private_impl_free (pthread_key_t *key)
545 : : {
546 : : gint status;
547 : :
548 : 0 : status = pthread_key_delete (*key);
549 : 0 : if G_UNLIKELY (status != 0)
550 : 0 : g_thread_abort (status, "pthread_key_delete");
551 : 0 : free (key);
552 : 0 : }
553 : :
554 : : static gpointer
555 : 2169 : g_private_impl_new_direct (GDestroyNotify notify)
556 : : {
557 : 2169 : gpointer impl = (void *) (gssize) -1;
558 : : pthread_key_t key;
559 : : gint status;
560 : :
561 : 2169 : status = pthread_key_create (&key, notify);
562 : 2169 : if G_UNLIKELY (status != 0)
563 : 0 : g_thread_abort (status, "pthread_key_create");
564 : :
565 : 2169 : memcpy (&impl, &key, sizeof (pthread_key_t));
566 : :
567 : : /* pthread_key_create could theoretically put a NULL value into key.
568 : : * If that happens, waste the result and create a new one, since we
569 : : * use NULL to mean "not yet allocated".
570 : : *
571 : : * This will only happen once per program run.
572 : : *
573 : : * We completely avoid this problem for the case where pthread_key_t
574 : : * is smaller than void* (for example, on 64 bit Linux) by putting
575 : : * some high bits in the value of 'impl' to start with. Since we only
576 : : * overwrite part of the pointer, we will never end up with NULL.
577 : : */
578 : : if (sizeof (pthread_key_t) == sizeof (gpointer))
579 : : {
580 : : if G_UNLIKELY (impl == NULL)
581 : : {
582 : : status = pthread_key_create (&key, notify);
583 : : if G_UNLIKELY (status != 0)
584 : : g_thread_abort (status, "pthread_key_create");
585 : :
586 : : memcpy (&impl, &key, sizeof (pthread_key_t));
587 : :
588 : : if G_UNLIKELY (impl == NULL)
589 : : g_thread_abort (status, "pthread_key_create (gave NULL result twice)");
590 : : }
591 : : }
592 : :
593 : 2169 : return impl;
594 : : }
595 : :
596 : : static void
597 : 3 : g_private_impl_free_direct (gpointer impl)
598 : : {
599 : : pthread_key_t tmp;
600 : : gint status;
601 : :
602 : 3 : memcpy (&tmp, &impl, sizeof (pthread_key_t));
603 : :
604 : 3 : status = pthread_key_delete (tmp);
605 : 3 : if G_UNLIKELY (status != 0)
606 : 0 : g_thread_abort (status, "pthread_key_delete");
607 : 3 : }
608 : :
609 : : static inline pthread_key_t
610 : 20782401 : _g_private_get_impl (GPrivate *key)
611 : : {
612 : : if (sizeof (pthread_key_t) > sizeof (gpointer))
613 : : {
614 : : pthread_key_t *impl = g_atomic_pointer_get (&key->p);
615 : :
616 : : if G_UNLIKELY (impl == NULL)
617 : : {
618 : : impl = g_private_impl_new (key->notify);
619 : : if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
620 : : {
621 : : g_private_impl_free (impl);
622 : : impl = key->p;
623 : : }
624 : : }
625 : :
626 : : return *impl;
627 : : }
628 : : else
629 : : {
630 : 20782401 : gpointer impl = g_atomic_pointer_get (&key->p);
631 : : pthread_key_t tmp;
632 : :
633 : 20782401 : if G_UNLIKELY (impl == NULL)
634 : : {
635 : 2169 : impl = g_private_impl_new_direct (key->notify);
636 : 2169 : if (!g_atomic_pointer_compare_and_exchange (&key->p, NULL, impl))
637 : : {
638 : 3 : g_private_impl_free_direct (impl);
639 : 3 : impl = key->p;
640 : : }
641 : : }
642 : :
643 : 20782401 : memcpy (&tmp, &impl, sizeof (pthread_key_t));
644 : :
645 : 20782401 : return tmp;
646 : : }
647 : : }
648 : :
649 : : G_ALWAYS_INLINE static inline gpointer
650 : : g_private_get_impl (GPrivate *key)
651 : : {
652 : : /* quote POSIX: No errors are returned from pthread_getspecific(). */
653 : 13508906 : return pthread_getspecific (_g_private_get_impl (key));
654 : : }
655 : :
656 : : G_ALWAYS_INLINE static inline void
657 : : g_private_set_impl (GPrivate *key,
658 : : gpointer value)
659 : : {
660 : : gint status;
661 : :
662 : 7272446 : if G_UNLIKELY ((status = pthread_setspecific (_g_private_get_impl (key), value)) != 0)
663 : 0 : g_thread_abort (status, "pthread_setspecific");
664 : 7272446 : }
665 : :
666 : : G_ALWAYS_INLINE static inline void
667 : : g_private_replace_impl (GPrivate *key,
668 : : gpointer value)
669 : : {
670 : 1049 : pthread_key_t impl = _g_private_get_impl (key);
671 : : gpointer old;
672 : : gint status;
673 : :
674 : 1049 : old = pthread_getspecific (impl);
675 : :
676 : 1049 : if G_UNLIKELY ((status = pthread_setspecific (impl, value)) != 0)
677 : 0 : g_thread_abort (status, "pthread_setspecific");
678 : :
679 : 1049 : if (old && key->notify)
680 : 103 : key->notify (old);
681 : 1049 : }
682 : :
683 : : /* {{{1 GThread */
684 : :
685 : : #define posix_check_err(err, name) G_STMT_START{ \
686 : : int error = (err); \
687 : : if (error) \
688 : : g_error ("file %s: line %d (%s): error '%s' during '%s'", \
689 : : __FILE__, __LINE__, G_STRFUNC, \
690 : : g_strerror (error), name); \
691 : : }G_STMT_END
692 : :
693 : : #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
694 : :
695 : : typedef struct
696 : : {
697 : : GRealThread thread;
698 : :
699 : : pthread_t system_thread;
700 : : gboolean joined;
701 : : GMutex lock;
702 : :
703 : : void *(*proxy) (void *);
704 : : } GThreadPosix;
705 : :
706 : : void
707 : 35433 : g_system_thread_free (GRealThread *thread)
708 : : {
709 : 35433 : GThreadPosix *pt = (GThreadPosix *) thread;
710 : :
711 : 35433 : if (!pt->joined)
712 : 222 : pthread_detach (pt->system_thread);
713 : :
714 : 35433 : g_mutex_clear (&pt->lock);
715 : :
716 : 35433 : g_slice_free (GThreadPosix, pt);
717 : 35433 : }
718 : :
719 : : GRealThread *
720 : 36120 : g_system_thread_new (GThreadFunc proxy,
721 : : gulong stack_size,
722 : : const char *name,
723 : : GThreadFunc func,
724 : : gpointer data,
725 : : GError **error)
726 : : {
727 : : GThreadPosix *thread;
728 : : GRealThread *base_thread;
729 : : pthread_attr_t attr;
730 : : gint ret;
731 : :
732 : 36120 : thread = g_slice_new0 (GThreadPosix);
733 : 36120 : base_thread = (GRealThread*)thread;
734 : 36120 : base_thread->ref_count = 2;
735 : 36120 : base_thread->ours = TRUE;
736 : 36120 : base_thread->thread.joinable = TRUE;
737 : 36120 : base_thread->thread.func = func;
738 : 36120 : base_thread->thread.data = data;
739 : 36120 : if (name)
740 : 4687 : g_strlcpy (base_thread->name, name, sizeof (base_thread->name));
741 : 36120 : thread->proxy = proxy;
742 : :
743 : 36120 : posix_check_cmd (pthread_attr_init (&attr));
744 : :
745 : : #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
746 : 36120 : if (stack_size)
747 : : {
748 : : #ifdef _SC_THREAD_STACK_MIN
749 : 0 : long min_stack_size = sysconf (_SC_THREAD_STACK_MIN);
750 : 0 : if (min_stack_size >= 0)
751 : 0 : stack_size = MAX ((gulong) min_stack_size, stack_size);
752 : : #endif /* _SC_THREAD_STACK_MIN */
753 : : /* No error check here, because some systems can't do it and
754 : : * we simply don't want threads to fail because of that. */
755 : 0 : pthread_attr_setstacksize (&attr, stack_size);
756 : : }
757 : : #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
758 : :
759 : : #ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED
760 : : {
761 : : /* While this is the default, better be explicit about it */
762 : 36120 : pthread_attr_setinheritsched (&attr, PTHREAD_INHERIT_SCHED);
763 : : }
764 : : #endif /* HAVE_PTHREAD_ATTR_SETINHERITSCHED */
765 : :
766 : 36120 : ret = pthread_create (&thread->system_thread, &attr, (void* (*)(void*))proxy, thread);
767 : :
768 : 36120 : posix_check_cmd (pthread_attr_destroy (&attr));
769 : :
770 : 36120 : if (ret == EAGAIN)
771 : : {
772 : 4 : g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
773 : : "Error creating thread: %s", g_strerror (ret));
774 : 4 : g_slice_free (GThreadPosix, thread);
775 : 4 : return NULL;
776 : : }
777 : :
778 : 36116 : posix_check_err (ret, "pthread_create");
779 : :
780 : 36116 : g_mutex_init (&thread->lock);
781 : :
782 : 36116 : return (GRealThread *) thread;
783 : : }
784 : :
785 : : G_ALWAYS_INLINE static inline void
786 : : g_thread_yield_impl (void)
787 : : {
788 : : #if defined (__linux__) || defined (__FreeBSD__) || defined (__APPLE__)
789 : : /* On select kernels where nanosleep with an extremely short timeout is known
790 : : * not to put the caller to sleep for a tick, prefer nanosleep to sched_yield,
791 : : * since the latter is not universally well-defined outside of real-time
792 : : * threads, and might never defer to lower-priority threads.
793 : : */
794 : : #ifdef HAVE_CLOCK_NANOSLEEP
795 : 19803078 : clock_nanosleep (CLOCK_MONOTONIC, 0, &(struct timespec){ 0, 1 }, NULL);
796 : : #else
797 : : nanosleep (&(struct timespec){ 0, 1 }, NULL);
798 : : #endif
799 : :
800 : : #elif defined (__GNU__)
801 : : /* On GNU Hurd, use the dedicated Mach trap */
802 : : thread_switch (MACH_PORT_NULL, SWITCH_OPTION_DEPRESS, 1);
803 : : #else
804 : : sched_yield ();
805 : : #endif
806 : 19803078 : }
807 : :
808 : : void
809 : 35211 : g_system_thread_wait (GRealThread *thread)
810 : : {
811 : 35211 : GThreadPosix *pt = (GThreadPosix *) thread;
812 : :
813 : 35211 : g_mutex_lock (&pt->lock);
814 : :
815 : 35211 : if (!pt->joined)
816 : : {
817 : 35211 : posix_check_cmd (pthread_join (pt->system_thread, NULL));
818 : 35211 : pt->joined = TRUE;
819 : : }
820 : :
821 : 35211 : g_mutex_unlock (&pt->lock);
822 : 35211 : }
823 : :
824 : : void
825 : 13 : g_system_thread_exit (void)
826 : : {
827 : 13 : pthread_exit (NULL);
828 : : }
829 : :
830 : : void
831 : 4683 : g_system_thread_set_name (const gchar *name)
832 : : {
833 : : #if defined(HAVE_PTHREAD_SETNAME_NP_WITHOUT_TID)
834 : : pthread_setname_np (name); /* on OS X and iOS */
835 : : #elif defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID)
836 : : #ifdef __linux__
837 : : #define MAX_THREADNAME_LEN 16
838 : : #else
839 : : #define MAX_THREADNAME_LEN 32
840 : : #endif
841 : : char name_[MAX_THREADNAME_LEN];
842 : 4683 : g_strlcpy (name_, name, MAX_THREADNAME_LEN);
843 : 4683 : pthread_setname_np (pthread_self (), name_); /* on Linux and Solaris */
844 : : #elif defined(HAVE_PTHREAD_SETNAME_NP_WITH_TID_AND_ARG)
845 : : pthread_setname_np (pthread_self (), "%s", (gchar *) name); /* on NetBSD */
846 : : #elif defined(HAVE_PTHREAD_SET_NAME_NP)
847 : : pthread_set_name_np (pthread_self (), name); /* on FreeBSD, DragonFlyBSD, OpenBSD */
848 : : #endif
849 : 4683 : }
850 : :
851 : : void
852 : 31433 : g_system_thread_get_name (char *buffer,
853 : : gsize length)
854 : : {
855 : : #ifdef HAVE_PTHREAD_GETNAME_NP
856 : 31433 : pthread_getname_np (pthread_self (), buffer, length);
857 : : #else
858 : : g_assert (length >= 1);
859 : : buffer[0] = '\0';
860 : : #endif
861 : 31433 : }
862 : :
863 : : /* {{{1 GMutex and GCond futex implementation */
864 : :
865 : : #if defined(USE_NATIVE_MUTEX)
866 : : /* We should expand the set of operations available in gatomic once we
867 : : * have better C11 support in GCC in common distributions (ie: 4.9).
868 : : *
869 : : * Before then, let's define a couple of useful things for our own
870 : : * purposes...
871 : : */
872 : :
873 : : #ifdef HAVE_STDATOMIC_H
874 : :
875 : : #include <stdatomic.h>
876 : :
877 : : #define exchange_acquire(ptr, new) \
878 : : atomic_exchange_explicit((atomic_uint *) (ptr), (new), __ATOMIC_ACQUIRE)
879 : : #define compare_exchange_acquire(ptr, old, new) \
880 : : atomic_compare_exchange_strong_explicit((atomic_uint *) (ptr), (old), (new), \
881 : : __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
882 : :
883 : : #define exchange_release(ptr, new) \
884 : : atomic_exchange_explicit((atomic_uint *) (ptr), (new), __ATOMIC_RELEASE)
885 : : #define store_release(ptr, new) \
886 : : atomic_store_explicit((atomic_uint *) (ptr), (new), __ATOMIC_RELEASE)
887 : :
888 : : #else
889 : :
890 : : #define exchange_acquire(ptr, new) \
891 : : __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
892 : : #define compare_exchange_acquire(ptr, old, new) \
893 : : __atomic_compare_exchange_4((ptr), (old), (new), 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)
894 : :
895 : : #define exchange_release(ptr, new) \
896 : : __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
897 : : #define store_release(ptr, new) \
898 : : __atomic_store_4((ptr), (new), __ATOMIC_RELEASE)
899 : :
900 : : #endif
901 : :
902 : : /* Our strategy for the mutex is pretty simple:
903 : : *
904 : : * 0: not in use
905 : : *
906 : : * 1: acquired by one thread only, no contention
907 : : *
908 : : * 2: contended
909 : : */
910 : :
911 : : typedef enum {
912 : : G_MUTEX_STATE_EMPTY = 0,
913 : : G_MUTEX_STATE_OWNED,
914 : : G_MUTEX_STATE_CONTENDED,
915 : : } GMutexState;
916 : :
917 : : /*
918 : : * As such, attempting to acquire the lock should involve an increment.
919 : : * If we find that the previous value was 0 then we can return
920 : : * immediately.
921 : : *
922 : : * On unlock, we always store 0 to indicate that the lock is available.
923 : : * If the value there was 1 before then we didn't have contention and
924 : : * can return immediately. If the value was something other than 1 then
925 : : * we have the contended case and need to wake a waiter.
926 : : *
927 : : * If it was not 0 then there is another thread holding it and we must
928 : : * wait. We must always ensure that we mark a value >1 while we are
929 : : * waiting in order to instruct the holder to do a wake operation on
930 : : * unlock.
931 : : */
932 : :
933 : : void
934 : 184696 : g_mutex_init_impl (GMutex *mutex)
935 : : {
936 : 184696 : mutex->i[0] = G_MUTEX_STATE_EMPTY;
937 : 184696 : }
938 : :
939 : : void
940 : 181087 : g_mutex_clear_impl (GMutex *mutex)
941 : : {
942 : 181087 : if G_UNLIKELY (mutex->i[0] != G_MUTEX_STATE_EMPTY)
943 : : {
944 : 0 : fprintf (stderr, "g_mutex_clear() called on uninitialised or locked mutex\n");
945 : 0 : g_abort ();
946 : : }
947 : 181087 : }
948 : :
949 : : G_GNUC_NO_INLINE
950 : : static void
951 : 7055310 : g_mutex_lock_slowpath (GMutex *mutex)
952 : : {
953 : : /* Set to contended. If it was empty before then we
954 : : * just acquired the lock.
955 : : *
956 : : * Otherwise, sleep for as long as the contended state remains...
957 : : */
958 : 16670254 : while (exchange_acquire (&mutex->i[0], G_MUTEX_STATE_CONTENDED) != G_MUTEX_STATE_EMPTY)
959 : : {
960 : 9614946 : g_futex_simple (&mutex->i[0], (gsize) FUTEX_WAIT_PRIVATE,
961 : : G_MUTEX_STATE_CONTENDED, NULL);
962 : : }
963 : 7055308 : }
964 : :
965 : : G_GNUC_NO_INLINE
966 : : static void
967 : 11649747 : g_mutex_unlock_slowpath (GMutex *mutex,
968 : : guint prev)
969 : : {
970 : : /* We seem to get better code for the uncontended case by splitting
971 : : * this out...
972 : : */
973 : 11649747 : if G_UNLIKELY (prev == G_MUTEX_STATE_EMPTY)
974 : : {
975 : 0 : fprintf (stderr, "Attempt to unlock mutex that was not locked\n");
976 : 0 : g_abort ();
977 : : }
978 : :
979 : 11649747 : g_futex_simple (&mutex->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
980 : 11649747 : }
981 : :
982 : : inline void
983 : 167557223 : g_mutex_lock_impl (GMutex *mutex)
984 : : {
985 : : /* empty -> owned and we're done. Anything else, and we need to wait... */
986 : 167557223 : if G_UNLIKELY (!g_atomic_int_compare_and_exchange (&mutex->i[0],
987 : : G_MUTEX_STATE_EMPTY,
988 : : G_MUTEX_STATE_OWNED))
989 : 7055310 : g_mutex_lock_slowpath (mutex);
990 : 167557221 : }
991 : :
992 : : void
993 : 168369907 : g_mutex_unlock_impl (GMutex *mutex)
994 : : {
995 : : guint prev;
996 : :
997 : 168369907 : prev = exchange_release (&mutex->i[0], G_MUTEX_STATE_EMPTY);
998 : :
999 : : /* 1-> 0 and we're done. Anything else and we need to signal... */
1000 : 168369907 : if G_UNLIKELY (prev != G_MUTEX_STATE_OWNED)
1001 : 11649747 : g_mutex_unlock_slowpath (mutex, prev);
1002 : 168369907 : }
1003 : :
1004 : : gboolean
1005 : 1121669 : g_mutex_trylock_impl (GMutex *mutex)
1006 : : {
1007 : 1121669 : GMutexState empty = G_MUTEX_STATE_EMPTY;
1008 : :
1009 : : /* We don't want to touch the value at all unless we can move it from
1010 : : * exactly empty to owned.
1011 : : */
1012 : 1121669 : return compare_exchange_acquire (&mutex->i[0], &empty, G_MUTEX_STATE_OWNED);
1013 : : }
1014 : :
1015 : : /* Condition variables are implemented in a rather simple way as well.
1016 : : * In many ways, futex() as an abstraction is even more ideally suited
1017 : : * to condition variables than it is to mutexes.
1018 : : *
1019 : : * We store a generation counter. We sample it with the lock held and
1020 : : * unlock before sleeping on the futex.
1021 : : *
1022 : : * Signalling simply involves increasing the counter and making the
1023 : : * appropriate futex call.
1024 : : *
1025 : : * The only thing that is the slightest bit complicated is timed waits
1026 : : * because we must convert our absolute time to relative.
1027 : : */
1028 : :
1029 : : void
1030 : 92281 : g_cond_init_impl (GCond *cond)
1031 : : {
1032 : 92281 : cond->i[0] = 0;
1033 : 92281 : }
1034 : :
1035 : : void
1036 : 90753 : g_cond_clear_impl (GCond *cond)
1037 : : {
1038 : 90753 : }
1039 : :
1040 : : void
1041 : 163059 : g_cond_wait_impl (GCond *cond,
1042 : : GMutex *mutex)
1043 : : {
1044 : 163059 : guint sampled = (guint) g_atomic_int_get (&cond->i[0]);
1045 : :
1046 : 163059 : g_mutex_unlock (mutex);
1047 : 163059 : g_futex_simple (&cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, NULL);
1048 : 162878 : g_mutex_lock (mutex);
1049 : 162878 : }
1050 : :
1051 : : void
1052 : 332893 : g_cond_signal_impl (GCond *cond)
1053 : : {
1054 : 332893 : g_atomic_int_inc (&cond->i[0]);
1055 : :
1056 : 332893 : g_futex_simple (&cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) 1, NULL);
1057 : 332893 : }
1058 : :
1059 : : void
1060 : 177616 : g_cond_broadcast_impl (GCond *cond)
1061 : : {
1062 : 177616 : g_atomic_int_inc (&cond->i[0]);
1063 : :
1064 : 177616 : g_futex_simple (&cond->i[0], (gsize) FUTEX_WAKE_PRIVATE, (gsize) INT_MAX, NULL);
1065 : 177616 : }
1066 : :
1067 : : gboolean
1068 : 2974 : g_cond_wait_until_impl (GCond *cond,
1069 : : GMutex *mutex,
1070 : : gint64 end_time)
1071 : : {
1072 : : struct timespec now;
1073 : : struct timespec span;
1074 : :
1075 : : guint sampled;
1076 : : int res;
1077 : : gboolean success;
1078 : :
1079 : 2974 : if (end_time < 0)
1080 : 0 : return FALSE;
1081 : :
1082 : 2974 : clock_gettime (CLOCK_MONOTONIC, &now);
1083 : 2974 : span.tv_sec = (end_time / 1000000) - now.tv_sec;
1084 : 2974 : span.tv_nsec = ((end_time % 1000000) * 1000) - now.tv_nsec;
1085 : 2974 : if (span.tv_nsec < 0)
1086 : : {
1087 : 1838 : span.tv_nsec += 1000000000;
1088 : 1838 : span.tv_sec--;
1089 : : }
1090 : :
1091 : 2974 : if (span.tv_sec < 0)
1092 : 0 : return FALSE;
1093 : :
1094 : : /* `struct timespec` as defined by the libc headers does not necessarily
1095 : : * have any relation to the one used by the kernel for the `futex` syscall.
1096 : : *
1097 : : * Specifically, the libc headers might use 64-bit `time_t` while the kernel
1098 : : * headers use 32-bit types on certain systems.
1099 : : *
1100 : : * To get around this problem we
1101 : : * a) check if `futex_time64` is available, which only exists on 32-bit
1102 : : * platforms and always uses 64-bit `time_t`.
1103 : : * b) if `futex_time64` is available, but the Android runtime's API level
1104 : : * is < 30, `futex_time64` is blocked by seccomp and using it will cause
1105 : : * the app to be terminated. Skip to c).
1106 : : * https://android-review.googlesource.com/c/platform/bionic/+/1094758
1107 : : * c) otherwise (or if that returns `ENOSYS`), we call the normal `futex`
1108 : : * syscall with the `struct timespec` used by the kernel. By default, we
1109 : : * use `__kernel_long_t` for both its fields, which is equivalent to
1110 : : * `__kernel_old_time_t` and is available in the kernel headers for a
1111 : : * longer time.
1112 : : * d) With very old headers (~2.6.x), `__kernel_long_t` is not available, and
1113 : : * we use an older definition that uses `__kernel_time_t` and `long`.
1114 : : *
1115 : : * Also some 32-bit systems do not define `__NR_futex` at all and only
1116 : : * define `__NR_futex_time64`.
1117 : : */
1118 : :
1119 : 2974 : sampled = cond->i[0];
1120 : 2974 : g_mutex_unlock (mutex);
1121 : :
1122 : : #if defined(HAVE_FUTEX_TIME64)
1123 : : #if defined(__ANDROID__)
1124 : : if (__builtin_available (android 30, *)) {
1125 : : #else
1126 : : {
1127 : : #endif
1128 : : struct
1129 : : {
1130 : : gint64 tv_sec;
1131 : : gint64 tv_nsec;
1132 : : } span_arg;
1133 : :
1134 : : span_arg.tv_sec = span.tv_sec;
1135 : : span_arg.tv_nsec = span.tv_nsec;
1136 : :
1137 : : res = syscall (__NR_futex_time64, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, &span_arg);
1138 : :
1139 : : /* If the syscall does not exist (`ENOSYS`), we retry again below with the
1140 : : * normal `futex` syscall. This can happen if newer kernel headers are
1141 : : * used than the kernel that is actually running.
1142 : : */
1143 : : # if defined(HAVE_FUTEX)
1144 : : if (res >= 0 || errno != ENOSYS)
1145 : : # endif /* defined(HAVE_FUTEX) */
1146 : : {
1147 : : success = (res < 0 && errno == ETIMEDOUT) ? FALSE : TRUE;
1148 : : g_mutex_lock (mutex);
1149 : :
1150 : : return success;
1151 : : }
1152 : : }
1153 : : #endif
1154 : :
1155 : : #if defined(HAVE_FUTEX)
1156 : : {
1157 : : # ifdef __kernel_long_t
1158 : : # define KERNEL_SPAN_SEC_TYPE __kernel_long_t
1159 : : struct
1160 : : {
1161 : : __kernel_long_t tv_sec;
1162 : : __kernel_long_t tv_nsec;
1163 : : } span_arg;
1164 : : # else
1165 : : /* Very old kernel headers: version 2.6.32 and thereabouts */
1166 : : # define KERNEL_SPAN_SEC_TYPE __kernel_time_t
1167 : : struct
1168 : : {
1169 : : __kernel_time_t tv_sec;
1170 : : long tv_nsec;
1171 : : } span_arg;
1172 : : # endif
1173 : : /* Make sure to only ever call this if the end time actually fits into the target type */
1174 : : if (G_UNLIKELY (sizeof (KERNEL_SPAN_SEC_TYPE) < 8 && span.tv_sec > G_MAXINT32))
1175 : : g_error ("%s: Can’t wait for more than %us", G_STRFUNC, G_MAXINT32);
1176 : :
1177 : 2974 : span_arg.tv_sec = span.tv_sec;
1178 : 2974 : span_arg.tv_nsec = span.tv_nsec;
1179 : :
1180 : 2974 : res = syscall (__NR_futex, &cond->i[0], (gsize) FUTEX_WAIT_PRIVATE, (gsize) sampled, &span_arg);
1181 : 2812 : success = (res < 0 && errno == ETIMEDOUT) ? FALSE : TRUE;
1182 : 2812 : g_mutex_lock (mutex);
1183 : :
1184 : 2812 : return success;
1185 : : }
1186 : : # undef KERNEL_SPAN_SEC_TYPE
1187 : : #endif /* defined(HAVE_FUTEX) */
1188 : :
1189 : : /* We can't end up here because of the checks above */
1190 : : g_assert_not_reached ();
1191 : : }
1192 : :
1193 : : #endif
1194 : :
1195 : : /* {{{1 Epilogue */
1196 : : /* vim:set foldmethod=marker: */
|