| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* sem_wait -- wait on a semaphore.  Generic futex-using version. | 
|  | 2 | Copyright (C) 2003-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 | Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003. | 
|  | 5 |  | 
|  | 6 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 7 | modify it under the terms of the GNU Lesser General Public | 
|  | 8 | License as published by the Free Software Foundation; either | 
|  | 9 | version 2.1 of the License, or (at your option) any later version. | 
|  | 10 |  | 
|  | 11 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU | 
|  | 14 | Lesser General Public License for more details. | 
|  | 15 |  | 
|  | 16 | You should have received a copy of the GNU Lesser General Public | 
|  | 17 | License along with the GNU C Library; if not, see | 
|  | 18 | <http://www.gnu.org/licenses/>.  */ | 
|  | 19 |  | 
|  | 20 | #include <lowlevellock.h>	/* lll_futex* used by the old code.  */ | 
|  | 21 | #include "sem_waitcommon.c" | 
|  | 22 |  | 
|  | 23 | int | 
|  | 24 | __new_sem_wait (sem_t *sem) | 
|  | 25 | { | 
|  | 26 | if (__new_sem_wait_fast ((struct new_sem *) sem, 0) == 0) | 
|  | 27 | return 0; | 
|  | 28 | else | 
|  | 29 | return __new_sem_wait_slow((struct new_sem *) sem, NULL); | 
|  | 30 | } | 
|  | 31 | versioned_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1); | 
|  | 32 |  | 
|  | 33 | #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) | 
|  | 34 | int | 
|  | 35 | attribute_compat_text_section | 
|  | 36 | __old_sem_wait (sem_t *sem) | 
|  | 37 | { | 
|  | 38 | int *futex = (int *) sem; | 
|  | 39 | int err; | 
|  | 40 |  | 
|  | 41 | do | 
|  | 42 | { | 
|  | 43 | if (atomic_decrement_if_positive (futex) > 0) | 
|  | 44 | return 0; | 
|  | 45 |  | 
|  | 46 | /* Enable asynchronous cancellation.  Required by the standard.  */ | 
|  | 47 | int oldtype = __pthread_enable_asynccancel (); | 
|  | 48 |  | 
|  | 49 | /* Always assume the semaphore is shared.  */ | 
|  | 50 | err = lll_futex_wait (futex, 0, LLL_SHARED); | 
|  | 51 |  | 
|  | 52 | /* Disable asynchronous cancellation.  */ | 
|  | 53 | __pthread_disable_asynccancel (oldtype); | 
|  | 54 | } | 
|  | 55 | while (err == 0 || err == -EWOULDBLOCK); | 
|  | 56 |  | 
|  | 57 | __set_errno (-err); | 
|  | 58 | return -1; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0); | 
|  | 62 | #endif | 
|  | 63 |  | 
|  | 64 | int | 
|  | 65 | __new_sem_trywait (sem_t *sem) | 
|  | 66 | { | 
|  | 67 | /* We must not fail spuriously, so require a definitive result even if this | 
|  | 68 | may lead to a long execution time.  */ | 
|  | 69 | if (__new_sem_wait_fast ((struct new_sem *) sem, 1) == 0) | 
|  | 70 | return 0; | 
|  | 71 | __set_errno (EAGAIN); | 
|  | 72 | return -1; | 
|  | 73 | } | 
|  | 74 | versioned_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1); | 
|  | 75 | #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1) | 
|  | 76 | int | 
|  | 77 | __old_sem_trywait (sem_t *sem) | 
|  | 78 | { | 
|  | 79 | int *futex = (int *) sem; | 
|  | 80 | int val; | 
|  | 81 |  | 
|  | 82 | if (*futex > 0) | 
|  | 83 | { | 
|  | 84 | val = atomic_decrement_if_positive (futex); | 
|  | 85 | if (val > 0) | 
|  | 86 | return 0; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | __set_errno (EAGAIN); | 
|  | 90 | return -1; | 
|  | 91 | } | 
|  | 92 | compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0); | 
|  | 93 | #endif |