pthread_barrier_wait.S 3.8 KB

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