pthread_mutex_init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. int ret = INTERNAL_SYSCALL (futex, err, 4, &lock, FUTEX_UNLOCK_PI,
  53. 0, 0);
  54. assert (INTERNAL_SYSCALL_ERROR_P (ret, err));
  55. tpi_supported = INTERNAL_SYSCALL_ERRNO (ret, err) == ENOSYS ? -1 : 1;
  56. }
  57. if (__builtin_expect (tpi_supported < 0, 0))
  58. return ENOTSUP;
  59. #endif
  60. break;
  61. default:
  62. /* XXX: For now we don't support robust priority protected mutexes. */
  63. if (imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST)
  64. return ENOTSUP;
  65. break;
  66. }
  67. /* Clear the whole variable. */
  68. memset (mutex, '\0', __SIZEOF_PTHREAD_MUTEX_T);
  69. /* Copy the values from the attribute. */
  70. mutex->__data.__kind = imutexattr->mutexkind & ~PTHREAD_MUTEXATTR_FLAG_BITS;
  71. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0)
  72. {
  73. #ifndef __ASSUME_SET_ROBUST_LIST
  74. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0
  75. && __set_robust_list_avail < 0)
  76. return ENOTSUP;
  77. #endif
  78. mutex->__data.__kind |= PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  79. }
  80. switch (imutexattr->mutexkind & PTHREAD_MUTEXATTR_PROTOCOL_MASK)
  81. {
  82. case PTHREAD_PRIO_INHERIT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  83. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_INHERIT_NP;
  84. break;
  85. case PTHREAD_PRIO_PROTECT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  86. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_PROTECT_NP;
  87. int ceiling = (imutexattr->mutexkind
  88. & PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
  89. >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT;
  90. if (! ceiling)
  91. {
  92. if (__sched_fifo_min_prio == -1)
  93. __init_sched_fifo_prio ();
  94. if (ceiling < __sched_fifo_min_prio)
  95. ceiling = __sched_fifo_min_prio;
  96. }
  97. mutex->__data.__lock = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  98. break;
  99. default:
  100. break;
  101. }
  102. /* The kernel when waking robust mutexes on exit never uses
  103. FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
  104. if ((imutexattr->mutexkind & (PTHREAD_MUTEXATTR_FLAG_PSHARED
  105. | PTHREAD_MUTEXATTR_FLAG_ROBUST)) != 0)
  106. mutex->__data.__kind |= PTHREAD_MUTEX_PSHARED_BIT;
  107. /* Default values: mutex not used yet. */
  108. // mutex->__count = 0; already done by memset
  109. // mutex->__owner = 0; already done by memset
  110. // mutex->__nusers = 0; already done by memset
  111. // mutex->__spins = 0; already done by memset
  112. // mutex->__next = NULL; already done by memset
  113. return 0;
  114. }
  115. strong_alias (__pthread_mutex_init, pthread_mutex_init)
  116. INTDEF(__pthread_mutex_init)