futex.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
  2. Contributed by Ilie Garbacea <ilie@mips.com>, Chao-ying Fu <fu@mips.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. #include <sys/syscall.h>
  22. #if !defined (SYS_futex)
  23. #define SYS_futex __NR_futex
  24. #endif
  25. #define FUTEX_WAIT 0
  26. #define FUTEX_WAKE 1
  27. #ifdef __mips16
  28. static void __attribute__((noinline,nomips16))
  29. #else
  30. static inline void
  31. #endif
  32. sys_futex0 (int *addr, int op, int val)
  33. {
  34. register unsigned long __v0 asm("$2");
  35. register unsigned long __a0 asm("$4") = (unsigned long) addr;
  36. register unsigned long __a1 asm("$5") = (unsigned long) op;
  37. register unsigned long __a2 asm("$6") = (unsigned long) val;
  38. register unsigned long __a3 asm("$7") = 0;
  39. __asm volatile ("li $2, %6\n\t"
  40. "syscall"
  41. /* returns $a3 (errno), $v0 (return value) */
  42. : "=r" (__v0), "=r" (__a3)
  43. /* arguments in a0-a3, and syscall number */
  44. : "r" (__a0), "r" (__a1), "r" (__a2), "r" (__a3),
  45. "IK" (SYS_futex)
  46. /* clobbers at, v1, t0-t9, memory */
  47. : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13", "$14",
  48. "$15", "$24", "$25", "memory");
  49. }
  50. static inline void
  51. futex_wait (int *addr, int val)
  52. {
  53. sys_futex0 (addr, FUTEX_WAIT, val);
  54. }
  55. static inline void
  56. futex_wake (int *addr, int count)
  57. {
  58. sys_futex0 (addr, FUTEX_WAKE, count);
  59. }
  60. static inline void
  61. cpu_relax (void)
  62. {
  63. __asm volatile ("" : : : "memory");
  64. }