xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1994-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #ifndef _LOCK_INTERN_H |
| 19 | #define _LOCK_INTERN_H |
| 20 | |
| 21 | #include <sys/cdefs.h> |
| 22 | #include <machine-lock.h> |
| 23 | |
| 24 | #ifndef _EXTERN_INLINE |
| 25 | #define _EXTERN_INLINE __extern_inline |
| 26 | #endif |
| 27 | |
| 28 | |
| 29 | /* Initialize LOCK. */ |
| 30 | |
| 31 | _EXTERN_INLINE void |
| 32 | __spin_lock_init (__spin_lock_t *__lock) |
| 33 | { |
| 34 | *__lock = __SPIN_LOCK_INITIALIZER; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /* Lock LOCK, blocking if we can't get it. */ |
| 39 | extern void __spin_lock_solid (__spin_lock_t *__lock); |
| 40 | |
| 41 | /* Lock the spin lock LOCK. */ |
| 42 | |
| 43 | _EXTERN_INLINE void |
| 44 | __spin_lock (__spin_lock_t *__lock) |
| 45 | { |
| 46 | if (! __spin_try_lock (__lock)) |
| 47 | __spin_lock_solid (__lock); |
| 48 | } |
| 49 | |
| 50 | /* Name space-clean internal interface to mutex locks. |
| 51 | |
| 52 | Code internal to the C library uses these functions to lock and unlock |
| 53 | mutex locks. These locks are of type `struct mutex', defined in |
| 54 | <cthreads.h>. The functions here are name space-clean. If the program |
| 55 | is linked with the cthreads library, `__mutex_lock_solid' and |
| 56 | `__mutex_unlock_solid' will invoke the corresponding cthreads functions |
| 57 | to implement real mutex locks. If not, simple stub versions just use |
| 58 | spin locks. */ |
| 59 | |
| 60 | |
| 61 | /* Initialize the newly allocated mutex lock LOCK for further use. */ |
| 62 | extern void __mutex_init (void *__lock); |
| 63 | |
| 64 | /* Lock LOCK, blocking if we can't get it. */ |
| 65 | extern void __mutex_lock_solid (void *__lock); |
| 66 | |
| 67 | /* Finish unlocking LOCK, after the spin lock LOCK->held has already been |
| 68 | unlocked. This function will wake up any thread waiting on LOCK. */ |
| 69 | extern void __mutex_unlock_solid (void *__lock); |
| 70 | |
| 71 | /* Lock the mutex lock LOCK. */ |
| 72 | |
| 73 | _EXTERN_INLINE void |
| 74 | __mutex_lock (void *__lock) |
| 75 | { |
| 76 | if (! __spin_try_lock ((__spin_lock_t *) __lock)) |
| 77 | __mutex_lock_solid (__lock); |
| 78 | } |
| 79 | |
| 80 | /* Unlock the mutex lock LOCK. */ |
| 81 | |
| 82 | _EXTERN_INLINE void |
| 83 | __mutex_unlock (void *__lock) |
| 84 | { |
| 85 | __spin_unlock ((__spin_lock_t *) __lock); |
| 86 | __mutex_unlock_solid (__lock); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | _EXTERN_INLINE int |
| 91 | __mutex_trylock (void *__lock) |
| 92 | { |
| 93 | return __spin_try_lock ((__spin_lock_t *) __lock); |
| 94 | } |
| 95 | |
| 96 | #endif /* lock-intern.h */ |