lock-intern.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (C) 1994-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _LOCK_INTERN_H
  15. #define _LOCK_INTERN_H
  16. #include <sys/cdefs.h>
  17. #if defined __USE_EXTERN_INLINES && defined _LIBC
  18. # include <lowlevellock.h>
  19. #endif
  20. #ifndef _EXTERN_INLINE
  21. #define _EXTERN_INLINE __extern_inline
  22. #endif
  23. /* The type of a spin lock variable. */
  24. typedef unsigned int __spin_lock_t;
  25. /* Static initializer for spinlocks. */
  26. #define __SPIN_LOCK_INITIALIZER LLL_LOCK_INITIALIZER
  27. /* Initialize LOCK. */
  28. extern void __spin_lock_init (__spin_lock_t *__lock);
  29. #if defined __USE_EXTERN_INLINES && defined _LIBC
  30. _EXTERN_INLINE void
  31. __spin_lock_init (__spin_lock_t *__lock)
  32. {
  33. *__lock = __SPIN_LOCK_INITIALIZER;
  34. }
  35. #endif
  36. /* Lock LOCK, blocking if we can't get it. */
  37. extern void __spin_lock_solid (__spin_lock_t *__lock);
  38. /* Lock the spin lock LOCK. */
  39. extern void __spin_lock (__spin_lock_t *__lock);
  40. #if defined __USE_EXTERN_INLINES && defined _LIBC
  41. _EXTERN_INLINE void
  42. __spin_lock (__spin_lock_t *__lock)
  43. {
  44. __lll_lock (__lock, 0);
  45. }
  46. #endif
  47. /* Unlock LOCK. */
  48. extern void __spin_unlock (__spin_lock_t *__lock);
  49. #if defined __USE_EXTERN_INLINES && defined _LIBC
  50. _EXTERN_INLINE void
  51. __spin_unlock (__spin_lock_t *__lock)
  52. {
  53. __lll_unlock (__lock, 0);
  54. }
  55. #endif
  56. /* Try to lock LOCK; return nonzero if we locked it, zero if another has. */
  57. extern int __spin_try_lock (__spin_lock_t *__lock);
  58. #if defined __USE_EXTERN_INLINES && defined _LIBC
  59. _EXTERN_INLINE int
  60. __spin_try_lock (__spin_lock_t *__lock)
  61. {
  62. return (__lll_trylock (__lock) == 0);
  63. }
  64. #endif
  65. /* Return nonzero if LOCK is locked. */
  66. extern int __spin_lock_locked (__spin_lock_t *__lock);
  67. #if defined __USE_EXTERN_INLINES && defined _LIBC
  68. _EXTERN_INLINE int
  69. __spin_lock_locked (__spin_lock_t *__lock)
  70. {
  71. return (*(volatile __spin_lock_t *)__lock != 0);
  72. }
  73. #endif
  74. /* Name space-clean internal interface to mutex locks. */
  75. struct mutex {
  76. __spin_lock_t __held;
  77. __spin_lock_t __lock;
  78. const char *__name;
  79. void *__head, *__tail;
  80. void *__holder;
  81. };
  82. #define MUTEX_INITIALIZER { __SPIN_LOCK_INITIALIZER }
  83. /* Initialize the newly allocated mutex lock LOCK for further use. */
  84. extern void __mutex_init (void *__lock);
  85. /* Lock the mutex lock LOCK. */
  86. extern void __mutex_lock (void *__lock);
  87. #if defined __USE_EXTERN_INLINES && defined _LIBC
  88. _EXTERN_INLINE void
  89. __mutex_lock (void *__lock)
  90. {
  91. __spin_lock ((__spin_lock_t *)__lock);
  92. }
  93. #endif
  94. /* Unlock the mutex lock LOCK. */
  95. extern void __mutex_unlock (void *__lock);
  96. #if defined __USE_EXTERN_INLINES && defined _LIBC
  97. _EXTERN_INLINE void
  98. __mutex_unlock (void *__lock)
  99. {
  100. __spin_unlock ((__spin_lock_t *)__lock);
  101. }
  102. #endif
  103. extern int __mutex_trylock (void *__lock);
  104. #if defined __USE_EXTERN_INLINES && defined _LIBC
  105. _EXTERN_INLINE int
  106. __mutex_trylock (void *__lock)
  107. {
  108. return (__spin_try_lock ((__spin_lock_t *)__lock));
  109. }
  110. #endif
  111. #endif /* lock-intern.h */