pthread_mutex_init.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <bits/kernel-features.h>
  20. #include "pthreadP.h"
  21. static const struct pthread_mutexattr default_attr =
  22. {
  23. /* Default is a normal mutex, not shared between processes. */
  24. .mutexkind = PTHREAD_MUTEX_NORMAL
  25. };
  26. #ifndef __ASSUME_FUTEX_LOCK_PI
  27. static int tpi_supported;
  28. #endif
  29. int
  30. attribute_protected
  31. __pthread_mutex_init (
  32. pthread_mutex_t *mutex,
  33. const pthread_mutexattr_t *mutexattr)
  34. {
  35. const struct pthread_mutexattr *imutexattr;
  36. assert (sizeof (pthread_mutex_t) <= __SIZEOF_PTHREAD_MUTEX_T);
  37. imutexattr = (const struct pthread_mutexattr *) mutexattr ?: &default_attr;
  38. /* Sanity checks. */
  39. switch (__builtin_expect (imutexattr->mutexkind
  40. & PTHREAD_MUTEXATTR_PROTOCOL_MASK,
  41. PTHREAD_PRIO_NONE
  42. << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT))
  43. {
  44. case PTHREAD_PRIO_NONE << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  45. break;
  46. case PTHREAD_PRIO_INHERIT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  47. #ifndef __ASSUME_FUTEX_LOCK_PI
  48. if (__builtin_expect (tpi_supported == 0, 0))
  49. {
  50. int lock = 0;
  51. INTERNAL_SYSCALL_DECL (err);
  52. #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_futex_time64)
  53. int ret = INTERNAL_SYSCALL (futex_time64, err, 4, &lock, FUTEX_UNLOCK_PI,
  54. 0, 0);
  55. #else
  56. int ret = INTERNAL_SYSCALL (futex, err, 4, &lock, FUTEX_UNLOCK_PI,
  57. 0, 0);
  58. #endif
  59. assert (INTERNAL_SYSCALL_ERROR_P (ret, err));
  60. tpi_supported = INTERNAL_SYSCALL_ERRNO (ret, err) == ENOSYS ? -1 : 1;
  61. }
  62. if (__builtin_expect (tpi_supported < 0, 0))
  63. return ENOTSUP;
  64. #endif
  65. break;
  66. default:
  67. /* XXX: For now we don't support robust priority protected mutexes. */
  68. if (imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST)
  69. return ENOTSUP;
  70. break;
  71. }
  72. /* Clear the whole variable. */
  73. memset (mutex, '\0', __SIZEOF_PTHREAD_MUTEX_T);
  74. /* Copy the values from the attribute. */
  75. mutex->__data.__kind = imutexattr->mutexkind & ~PTHREAD_MUTEXATTR_FLAG_BITS;
  76. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0)
  77. {
  78. #ifndef __ASSUME_SET_ROBUST_LIST
  79. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0
  80. && __set_robust_list_avail < 0)
  81. return ENOTSUP;
  82. #endif
  83. mutex->__data.__kind |= PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  84. }
  85. switch (imutexattr->mutexkind & PTHREAD_MUTEXATTR_PROTOCOL_MASK)
  86. {
  87. case PTHREAD_PRIO_INHERIT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  88. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_INHERIT_NP;
  89. break;
  90. case PTHREAD_PRIO_PROTECT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  91. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_PROTECT_NP;
  92. int ceiling = (imutexattr->mutexkind
  93. & PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
  94. >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT;
  95. if (! ceiling)
  96. {
  97. if (__sched_fifo_min_prio == -1)
  98. __init_sched_fifo_prio ();
  99. if (ceiling < __sched_fifo_min_prio)
  100. ceiling = __sched_fifo_min_prio;
  101. }
  102. mutex->__data.__lock = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  103. break;
  104. default:
  105. break;
  106. }
  107. /* The kernel when waking robust mutexes on exit never uses
  108. FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
  109. if ((imutexattr->mutexkind & (PTHREAD_MUTEXATTR_FLAG_PSHARED
  110. | PTHREAD_MUTEXATTR_FLAG_ROBUST)) != 0)
  111. mutex->__data.__kind |= PTHREAD_MUTEX_PSHARED_BIT;
  112. /* Default values: mutex not used yet. */
  113. // mutex->__count = 0; already done by memset
  114. // mutex->__owner = 0; already done by memset
  115. // mutex->__nusers = 0; already done by memset
  116. // mutex->__spins = 0; already done by memset
  117. // mutex->__next = NULL; already done by memset
  118. return 0;
  119. }
  120. strong_alias (__pthread_mutex_init, pthread_mutex_init)
  121. INTDEF(__pthread_mutex_init)