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