struct_mutex.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Default mutex implementation struct definitions.
  2. Copyright (C) 2019-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _THREAD_MUTEX_INTERNAL_H
  16. #define _THREAD_MUTEX_INTERNAL_H 1
  17. /* Generic struct for both POSIX and C11 mutexes. New ports are expected
  18. to use the default layout, however architecture can redefine it to
  19. add arch-specific extension (such as lock-elision). The struct have
  20. a size of 32 bytes on LP32 and 40 bytes on LP64 architectures. */
  21. struct __pthread_mutex_s
  22. {
  23. int __lock __LOCK_ALIGNMENT;
  24. unsigned int __count;
  25. int __owner;
  26. #if __WORDSIZE == 64
  27. unsigned int __nusers;
  28. #endif
  29. /* KIND must stay at this position in the structure to maintain
  30. binary compatibility with static initializers.
  31. Concurrency notes:
  32. The __kind of a mutex is initialized either by the static
  33. PTHREAD_MUTEX_INITIALIZER or by a call to pthread_mutex_init.
  34. After a mutex has been initialized, the __kind of a mutex is usually not
  35. changed. BUT it can be set to -1 in pthread_mutex_destroy or elision can
  36. be enabled. This is done concurrently in the pthread_mutex_*lock
  37. functions by using the macro FORCE_ELISION. This macro is only defined
  38. for architectures which supports lock elision.
  39. For elision, there are the flags PTHREAD_MUTEX_ELISION_NP and
  40. PTHREAD_MUTEX_NO_ELISION_NP which can be set in addition to the already
  41. set type of a mutex. Before a mutex is initialized, only
  42. PTHREAD_MUTEX_NO_ELISION_NP can be set with pthread_mutexattr_settype.
  43. After a mutex has been initialized, the functions pthread_mutex_*lock can
  44. enable elision - if the mutex-type and the machine supports it - by
  45. setting the flag PTHREAD_MUTEX_ELISION_NP. This is done concurrently.
  46. Afterwards the lock / unlock functions are using specific elision
  47. code-paths. */
  48. int __kind;
  49. #if __WORDSIZE != 64
  50. unsigned int __nusers;
  51. #endif
  52. #if __WORDSIZE == 64
  53. int __spins;
  54. __pthread_list_t __list;
  55. # define __PTHREAD_MUTEX_HAVE_PREV 1
  56. #else
  57. __extension__ union
  58. {
  59. int __spins;
  60. __pthread_slist_t __list;
  61. };
  62. # define __PTHREAD_MUTEX_HAVE_PREV 0
  63. #endif
  64. };
  65. #if __PTHREAD_MUTEX_HAVE_PREV == 1
  66. # define __PTHREAD_MUTEX_INITIALIZER(__kind) \
  67. 0, 0, 0, 0, __kind, 0, { 0, 0 }
  68. #else
  69. # define __PTHREAD_MUTEX_INITIALIZER(__kind) \
  70. 0, 0, 0, __kind, 0, { 0 }
  71. #endif
  72. #endif