pthread_cond_signal.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2002-2005, 2007, 2009 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 <lowlevelcond.h>
  18. #include <pthread-pi-defines.h>
  19. #include <bits/kernel-features.h>
  20. #include <pthread-errnos.h>
  21. .text
  22. /* int pthread_cond_signal (pthread_cond_t *cond) */
  23. .globl __pthread_cond_signal
  24. .type __pthread_cond_signal, @function
  25. .protected __pthread_cond_signal
  26. .align 16
  27. __pthread_cond_signal:
  28. /* Get internal lock. */
  29. movq %rdi, %r8
  30. movl $1, %esi
  31. xorl %eax, %eax
  32. LOCK
  33. #if cond_lock == 0
  34. cmpxchgl %esi, (%rdi)
  35. #else
  36. cmpxchgl %esi, cond_lock(%rdi)
  37. #endif
  38. jnz 1f
  39. 2: addq $cond_futex, %rdi
  40. movq total_seq(%r8), %rcx
  41. cmpq wakeup_seq(%r8), %rcx
  42. jbe 4f
  43. /* Bump the wakeup number. */
  44. addq $1, wakeup_seq(%r8)
  45. addl $1, (%rdi)
  46. /* Wake up one thread. */
  47. cmpq $-1, dep_mutex(%r8)
  48. movl $FUTEX_WAKE_OP, %esi
  49. movl $1, %edx
  50. movl $SYS_futex, %eax
  51. je 8f
  52. /* Get the address of the mutex used. */
  53. movq dep_mutex(%r8), %rcx
  54. movl MUTEX_KIND(%rcx), %r11d
  55. andl $(ROBUST_BIT|PI_BIT), %r11d
  56. cmpl $PI_BIT, %r11d
  57. je 9f
  58. #ifdef __ASSUME_PRIVATE_FUTEX
  59. movl $(FUTEX_WAKE_OP|FUTEX_PRIVATE_FLAG), %esi
  60. #else
  61. orl %fs:PRIVATE_FUTEX, %esi
  62. #endif
  63. 8: movl $1, %r10d
  64. #if cond_lock != 0
  65. addq $cond_lock, %r8
  66. #endif
  67. movl $FUTEX_OP_CLEAR_WAKE_IF_GT_ONE, %r9d
  68. syscall
  69. #if cond_lock != 0
  70. subq $cond_lock, %r8
  71. #endif
  72. /* For any kind of error, we try again with WAKE.
  73. The general test also covers running on old kernels. */
  74. cmpq $-4095, %rax
  75. jae 7f
  76. xorl %eax, %eax
  77. retq
  78. /* Wake up one thread and requeue none in the PI Mutex case. */
  79. 9: movl $(FUTEX_CMP_REQUEUE_PI|FUTEX_PRIVATE_FLAG), %esi
  80. movq %rcx, %r8
  81. xorq %r10, %r10
  82. movl (%rdi), %r9d // XXX Can this be right?
  83. syscall
  84. leaq -cond_futex(%rdi), %r8
  85. /* For any kind of error, we try again with WAKE.
  86. The general test also covers running on old kernels. */
  87. cmpq $-4095, %rax
  88. jb 4f
  89. 7:
  90. #ifdef __ASSUME_PRIVATE_FUTEX
  91. andl $FUTEX_PRIVATE_FLAG, %esi
  92. #else
  93. andl %fs:PRIVATE_FUTEX, %esi
  94. #endif
  95. orl $FUTEX_WAKE, %esi
  96. movl $SYS_futex, %eax
  97. /* %rdx should be 1 already from $FUTEX_WAKE_OP syscall.
  98. movl $1, %edx */
  99. syscall
  100. /* Unlock. */
  101. 4: LOCK
  102. #if cond_lock == 0
  103. decl (%r8)
  104. #else
  105. decl cond_lock(%r8)
  106. #endif
  107. jne 5f
  108. 6: xorl %eax, %eax
  109. retq
  110. /* Initial locking failed. */
  111. 1:
  112. #if cond_lock != 0
  113. addq $cond_lock, %rdi
  114. #endif
  115. cmpq $-1, dep_mutex-cond_lock(%rdi)
  116. movl $LLL_PRIVATE, %eax
  117. movl $LLL_SHARED, %esi
  118. cmovne %eax, %esi
  119. callq __lll_lock_wait
  120. #if cond_lock != 0
  121. subq $cond_lock, %rdi
  122. #endif
  123. jmp 2b
  124. /* Unlock in loop requires wakeup. */
  125. 5:
  126. movq %r8, %rdi
  127. #if cond_lock != 0
  128. addq $cond_lock, %rdi
  129. #endif
  130. cmpq $-1, dep_mutex-cond_lock(%rdi)
  131. movl $LLL_PRIVATE, %eax
  132. movl $LLL_SHARED, %esi
  133. cmovne %eax, %esi
  134. callq __lll_unlock_wake
  135. jmp 6b
  136. .size __pthread_cond_signal, .-__pthread_cond_signal
  137. weak_alias(__pthread_cond_signal, pthread_cond_signal)