pthread_barrier_wait.S 3.6 KB

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