pthread_barrier_wait.S 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2002, 2003, 2004, 2005, 2007 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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sysdep.h>
  16. #include <lowlevellock.h>
  17. #include <lowlevelbarrier.h>
  18. .text
  19. .globl pthread_barrier_wait
  20. .type pthread_barrier_wait,@function
  21. .align 16
  22. pthread_barrier_wait:
  23. /* Get the mutex. */
  24. xorl %eax, %eax
  25. movl $1, %esi
  26. LOCK
  27. cmpxchgl %esi, MUTEX(%rdi)
  28. jnz 1f
  29. /* One less waiter. If this was the last one needed wake
  30. everybody. */
  31. 2: decl LEFT(%rdi)
  32. je 3f
  33. /* There are more threads to come. */
  34. #if CURR_EVENT == 0
  35. movl (%rdi), %edx
  36. #else
  37. movl CURR_EVENT(%rdi), %edx
  38. #endif
  39. /* Release the mutex. */
  40. LOCK
  41. decl MUTEX(%rdi)
  42. jne 6f
  43. /* Wait for the remaining threads. The call will return immediately
  44. if the CURR_EVENT memory has meanwhile been changed. */
  45. 7:
  46. #if FUTEX_WAIT == 0
  47. movl PRIVATE(%rdi), %esi
  48. #else
  49. movl $FUTEX_WAIT, %esi
  50. orl PRIVATE(%rdi), %esi
  51. #endif
  52. xorq %r10, %r10
  53. 8: movl $SYS_futex, %eax
  54. syscall
  55. /* Don't return on spurious wakeups. The syscall does not change
  56. any register except %eax so there is no need to reload any of
  57. them. */
  58. #if CURR_EVENT == 0
  59. cmpl %edx, (%rdi)
  60. #else
  61. cmpl %edx, CURR_EVENT(%rdi)
  62. #endif
  63. je 8b
  64. /* Increment LEFT. If this brings the count back to the
  65. initial count unlock the object. */
  66. movl $1, %edx
  67. movl INIT_COUNT(%rdi), %eax
  68. LOCK
  69. xaddl %edx, LEFT(%rdi)
  70. subl $1, %eax
  71. cmpl %eax, %edx
  72. jne,pt 10f
  73. /* Release the mutex. We cannot release the lock before
  74. waking the waiting threads since otherwise a new thread might
  75. arrive and gets waken up, too. */
  76. LOCK
  77. decl MUTEX(%rdi)
  78. jne 9f
  79. 10: xorl %eax, %eax /* != PTHREAD_BARRIER_SERIAL_THREAD */
  80. retq
  81. /* The necessary number of threads arrived. */
  82. 3:
  83. #if CURR_EVENT == 0
  84. incl (%rdi)
  85. #else
  86. incl CURR_EVENT(%rdi)
  87. #endif
  88. /* Wake up all waiters. The count is a signed number in the kernel
  89. so 0x7fffffff is the highest value. */
  90. movl $0x7fffffff, %edx
  91. movl $FUTEX_WAKE, %esi
  92. orl PRIVATE(%rdi), %esi
  93. movl $SYS_futex, %eax
  94. syscall
  95. /* Increment LEFT. If this brings the count back to the
  96. initial count unlock the object. */
  97. movl $1, %edx
  98. movl INIT_COUNT(%rdi), %eax
  99. LOCK
  100. xaddl %edx, LEFT(%rdi)
  101. subl $1, %eax
  102. cmpl %eax, %edx
  103. jne,pt 5f
  104. /* Release the mutex. We cannot release the lock before
  105. waking the waiting threads since otherwise a new thread might
  106. arrive and gets waken up, too. */
  107. LOCK
  108. decl MUTEX(%rdi)
  109. jne 4f
  110. 5: orl $-1, %eax /* == PTHREAD_BARRIER_SERIAL_THREAD */
  111. retq
  112. 1: movl PRIVATE(%rdi), %esi
  113. addq $MUTEX, %rdi
  114. xorl $LLL_SHARED, %esi
  115. callq __lll_lock_wait
  116. subq $MUTEX, %rdi
  117. jmp 2b
  118. 4: movl PRIVATE(%rdi), %esi
  119. addq $MUTEX, %rdi
  120. xorl $LLL_SHARED, %esi
  121. callq __lll_unlock_wake
  122. jmp 5b
  123. 6: movl PRIVATE(%rdi), %esi
  124. addq $MUTEX, %rdi
  125. xorl $LLL_SHARED, %esi
  126. callq __lll_unlock_wake
  127. subq $MUTEX, %rdi
  128. jmp 7b
  129. 9: movl PRIVATE(%rdi), %esi
  130. addq $MUTEX, %rdi
  131. xorl $LLL_SHARED, %esi
  132. callq __lll_unlock_wake
  133. jmp 10b
  134. .size pthread_barrier_wait,.-pthread_barrier_wait