pthread_mutex_timedlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include "pthreadP.h"
  18. #include <lowlevellock.h>
  19. int
  20. pthread_mutex_timedlock (mutex, abstime)
  21. pthread_mutex_t *mutex;
  22. const struct timespec *abstime;
  23. {
  24. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  25. int result = 0;
  26. /* We must not check ABSTIME here. If the thread does not block
  27. abstime must not be checked for a valid value. */
  28. switch (mutex->__data.__kind)
  29. {
  30. /* Recursive mutex. */
  31. case PTHREAD_MUTEX_RECURSIVE_NP:
  32. /* Check whether we already hold the mutex. */
  33. if (mutex->__data.__owner == id)
  34. {
  35. /* Just bump the counter. */
  36. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  37. /* Overflow of the counter. */
  38. return EAGAIN;
  39. ++mutex->__data.__count;
  40. goto out;
  41. }
  42. else
  43. {
  44. /* We have to get the mutex. */
  45. result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
  46. if (result != 0)
  47. goto out;
  48. /* Only locked once so far. */
  49. mutex->__data.__count = 1;
  50. }
  51. break;
  52. /* Error checking mutex. */
  53. case PTHREAD_MUTEX_ERRORCHECK_NP:
  54. /* Check whether we already hold the mutex. */
  55. if (mutex->__data.__owner == id)
  56. return EDEADLK;
  57. /* FALLTHROUGH */
  58. default:
  59. /* Correct code cannot set any other type. */
  60. case PTHREAD_MUTEX_TIMED_NP:
  61. simple:
  62. /* Normal mutex. */
  63. result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
  64. break;
  65. case PTHREAD_MUTEX_ADAPTIVE_NP:
  66. if (! __is_smp)
  67. goto simple;
  68. if (lll_mutex_trylock (mutex->__data.__lock) != 0)
  69. {
  70. int cnt = 0;
  71. int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
  72. mutex->__data.__spins * 2 + 10);
  73. do
  74. {
  75. if (cnt++ >= max_cnt)
  76. {
  77. result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
  78. break;
  79. }
  80. #ifdef BUSY_WAIT_NOP
  81. BUSY_WAIT_NOP;
  82. #endif
  83. }
  84. while (lll_mutex_trylock (mutex->__data.__lock) != 0);
  85. mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
  86. }
  87. break;
  88. }
  89. if (result == 0)
  90. {
  91. /* Record the ownership. */
  92. mutex->__data.__owner = id;
  93. ++mutex->__data.__nusers;
  94. }
  95. out:
  96. return result;
  97. }