pthread_mutex_init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <bits/kernel-features.h>
  21. #include "pthreadP.h"
  22. static const struct pthread_mutexattr default_attr =
  23. {
  24. /* Default is a normal mutex, not shared between processes. */
  25. .mutexkind = PTHREAD_MUTEX_NORMAL
  26. };
  27. #ifndef __ASSUME_FUTEX_LOCK_PI
  28. static int tpi_supported;
  29. #endif
  30. int
  31. attribute_protected
  32. __pthread_mutex_init (
  33. pthread_mutex_t *mutex,
  34. const pthread_mutexattr_t *mutexattr)
  35. {
  36. const struct pthread_mutexattr *imutexattr;
  37. assert (sizeof (pthread_mutex_t) <= __SIZEOF_PTHREAD_MUTEX_T);
  38. imutexattr = (const struct pthread_mutexattr *) mutexattr ?: &default_attr;
  39. /* Sanity checks. */
  40. switch (__builtin_expect (imutexattr->mutexkind
  41. & PTHREAD_MUTEXATTR_PROTOCOL_MASK,
  42. PTHREAD_PRIO_NONE
  43. << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT))
  44. {
  45. case PTHREAD_PRIO_NONE << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  46. break;
  47. case PTHREAD_PRIO_INHERIT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  48. #ifndef __ASSUME_FUTEX_LOCK_PI
  49. if (__builtin_expect (tpi_supported == 0, 0))
  50. {
  51. int lock = 0;
  52. INTERNAL_SYSCALL_DECL (err);
  53. int ret = INTERNAL_SYSCALL (futex, err, 4, &lock, FUTEX_UNLOCK_PI,
  54. 0, 0);
  55. assert (INTERNAL_SYSCALL_ERROR_P (ret, err));
  56. tpi_supported = INTERNAL_SYSCALL_ERRNO (ret, err) == ENOSYS ? -1 : 1;
  57. }
  58. if (__builtin_expect (tpi_supported < 0, 0))
  59. return ENOTSUP;
  60. #endif
  61. break;
  62. default:
  63. /* XXX: For now we don't support robust priority protected mutexes. */
  64. if (imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST)
  65. return ENOTSUP;
  66. break;
  67. }
  68. /* Clear the whole variable. */
  69. memset (mutex, '\0', __SIZEOF_PTHREAD_MUTEX_T);
  70. /* Copy the values from the attribute. */
  71. mutex->__data.__kind = imutexattr->mutexkind & ~PTHREAD_MUTEXATTR_FLAG_BITS;
  72. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_ROBUST) != 0)
  73. {
  74. #ifndef __ASSUME_SET_ROBUST_LIST
  75. if ((imutexattr->mutexkind & PTHREAD_MUTEXATTR_FLAG_PSHARED) != 0
  76. && __set_robust_list_avail < 0)
  77. return ENOTSUP;
  78. #endif
  79. mutex->__data.__kind |= PTHREAD_MUTEX_ROBUST_NORMAL_NP;
  80. }
  81. switch (imutexattr->mutexkind & PTHREAD_MUTEXATTR_PROTOCOL_MASK)
  82. {
  83. case PTHREAD_PRIO_INHERIT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  84. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_INHERIT_NP;
  85. break;
  86. case PTHREAD_PRIO_PROTECT << PTHREAD_MUTEXATTR_PROTOCOL_SHIFT:
  87. mutex->__data.__kind |= PTHREAD_MUTEX_PRIO_PROTECT_NP;
  88. int ceiling = (imutexattr->mutexkind
  89. & PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
  90. >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT;
  91. if (! ceiling)
  92. {
  93. if (__sched_fifo_min_prio == -1)
  94. __init_sched_fifo_prio ();
  95. if (ceiling < __sched_fifo_min_prio)
  96. ceiling = __sched_fifo_min_prio;
  97. }
  98. mutex->__data.__lock = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  99. break;
  100. default:
  101. break;
  102. }
  103. /* The kernel when waking robust mutexes on exit never uses
  104. FUTEX_PRIVATE_FLAG FUTEX_WAKE. */
  105. if ((imutexattr->mutexkind & (PTHREAD_MUTEXATTR_FLAG_PSHARED
  106. | PTHREAD_MUTEXATTR_FLAG_ROBUST)) != 0)
  107. mutex->__data.__kind |= PTHREAD_MUTEX_PSHARED_BIT;
  108. /* Default values: mutex not used yet. */
  109. // mutex->__count = 0; already done by memset
  110. // mutex->__owner = 0; already done by memset
  111. // mutex->__nusers = 0; already done by memset
  112. // mutex->__spins = 0; already done by memset
  113. // mutex->__next = NULL; already done by memset
  114. return 0;
  115. }
  116. strong_alias (__pthread_mutex_init, pthread_mutex_init)
  117. INTDEF(__pthread_mutex_init)