pthread_mutex_lock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <assert.h>
  17. #include <errno.h>
  18. #include "pthreadP.h"
  19. #include <lowlevellock.h>
  20. #ifndef LLL_MUTEX_LOCK
  21. # define LLL_MUTEX_LOCK(mutex) lll_mutex_lock (mutex)
  22. # define LLL_MUTEX_TRYLOCK(mutex) lll_mutex_trylock (mutex)
  23. #endif
  24. int
  25. __pthread_mutex_lock (mutex)
  26. pthread_mutex_t *mutex;
  27. {
  28. assert (sizeof (mutex->__size) >= sizeof (mutex->__data));
  29. pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  30. switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
  31. {
  32. /* Recursive mutex. */
  33. case PTHREAD_MUTEX_RECURSIVE_NP:
  34. /* Check whether we already hold the mutex. */
  35. if (mutex->__data.__owner == id)
  36. {
  37. /* Just bump the counter. */
  38. if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
  39. /* Overflow of the counter. */
  40. return EAGAIN;
  41. ++mutex->__data.__count;
  42. return 0;
  43. }
  44. /* We have to get the mutex. */
  45. LLL_MUTEX_LOCK (mutex->__data.__lock);
  46. mutex->__data.__count = 1;
  47. break;
  48. /* Error checking mutex. */
  49. case PTHREAD_MUTEX_ERRORCHECK_NP:
  50. /* Check whether we already hold the mutex. */
  51. if (mutex->__data.__owner == id)
  52. return EDEADLK;
  53. /* FALLTHROUGH */
  54. default:
  55. /* Correct code cannot set any other type. */
  56. case PTHREAD_MUTEX_TIMED_NP:
  57. simple:
  58. /* Normal mutex. */
  59. LLL_MUTEX_LOCK (mutex->__data.__lock);
  60. break;
  61. case PTHREAD_MUTEX_ADAPTIVE_NP:
  62. if (! __is_smp)
  63. goto simple;
  64. if (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0)
  65. {
  66. int cnt = 0;
  67. int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
  68. mutex->__data.__spins * 2 + 10);
  69. do
  70. {
  71. if (cnt++ >= max_cnt)
  72. {
  73. LLL_MUTEX_LOCK (mutex->__data.__lock);
  74. break;
  75. }
  76. #ifdef BUSY_WAIT_NOP
  77. BUSY_WAIT_NOP;
  78. #endif
  79. }
  80. while (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0);
  81. mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
  82. }
  83. break;
  84. }
  85. /* Record the ownership. */
  86. assert (mutex->__data.__owner == 0);
  87. mutex->__data.__owner = id;
  88. #ifndef NO_INCR
  89. ++mutex->__data.__nusers;
  90. #endif
  91. return 0;
  92. }
  93. #ifndef __pthread_mutex_lock
  94. strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  95. strong_alias (__pthread_mutex_lock, __pthread_mutex_lock_internal)
  96. #endif