futex.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* Provide target-specific access to the futex system call. */
  21. #ifdef __x86_64__
  22. # ifndef SYS_futex
  23. # define SYS_futex 202
  24. # endif
  25. static inline void
  26. futex_wait (int *addr, int val)
  27. {
  28. register long r10 __asm__("%r10");
  29. long res;
  30. r10 = 0;
  31. __asm volatile ("syscall"
  32. : "=a" (res)
  33. : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
  34. "d" (val), "r" (r10)
  35. : "r11", "rcx", "memory");
  36. if (__builtin_expect (res == -ENOSYS, 0))
  37. {
  38. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  39. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  40. r10 = 0;
  41. __asm volatile ("syscall"
  42. : "=a" (res)
  43. : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wait),
  44. "d" (val), "r" (r10)
  45. : "r11", "rcx", "memory");
  46. }
  47. }
  48. static inline void
  49. futex_wake (int *addr, int count)
  50. {
  51. long res;
  52. __asm volatile ("syscall"
  53. : "=a" (res)
  54. : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
  55. "d" (count)
  56. : "r11", "rcx", "memory");
  57. if (__builtin_expect (res == -ENOSYS, 0))
  58. {
  59. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  60. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  61. __asm volatile ("syscall"
  62. : "=a" (res)
  63. : "0" (SYS_futex), "D" (addr), "S" (gomp_futex_wake),
  64. "d" (count)
  65. : "r11", "rcx", "memory");
  66. }
  67. }
  68. #else
  69. # ifndef SYS_futex
  70. # define SYS_futex 240
  71. # endif
  72. # ifdef __PIC__
  73. static inline long
  74. sys_futex0 (int *addr, int op, int val)
  75. {
  76. long res;
  77. __asm volatile ("xchgl\t%%ebx, %2\n\t"
  78. "int\t$0x80\n\t"
  79. "xchgl\t%%ebx, %2"
  80. : "=a" (res)
  81. : "0"(SYS_futex), "r" (addr), "c"(op),
  82. "d"(val), "S"(0)
  83. : "memory");
  84. return res;
  85. }
  86. # else
  87. static inline long
  88. sys_futex0 (int *addr, int op, int val)
  89. {
  90. long res;
  91. __asm volatile ("int $0x80"
  92. : "=a" (res)
  93. : "0"(SYS_futex), "b" (addr), "c"(op),
  94. "d"(val), "S"(0)
  95. : "memory");
  96. return res;
  97. }
  98. # endif /* __PIC__ */
  99. static inline void
  100. futex_wait (int *addr, int val)
  101. {
  102. long res = sys_futex0 (addr, gomp_futex_wait, val);
  103. if (__builtin_expect (res == -ENOSYS, 0))
  104. {
  105. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  106. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  107. sys_futex0 (addr, gomp_futex_wait, val);
  108. }
  109. }
  110. static inline void
  111. futex_wake (int *addr, int count)
  112. {
  113. long res = sys_futex0 (addr, gomp_futex_wake, count);
  114. if (__builtin_expect (res == -ENOSYS, 0))
  115. {
  116. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  117. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  118. sys_futex0 (addr, gomp_futex_wake, count);
  119. }
  120. }
  121. #endif /* __x86_64__ */
  122. static inline void
  123. cpu_relax (void)
  124. {
  125. __builtin_ia32_pause ();
  126. }