futex.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (C) 2010-2022 Free Software Foundation, Inc.
  2. Contributed by ARM Ltd.
  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. /* The include file hierachy above us (wait.h) has pushed visibility
  22. hidden, this will be applied to prototypes with headers we include
  23. with the effect that we cannot link against an external function
  24. (syscall). The solution here is to push default visibility, include
  25. our required headers then reinstante the original visibility. */
  26. #pragma GCC visibility push(default)
  27. #define _GNU_SOURCE
  28. #include <unistd.h>
  29. #include <sys/syscall.h>
  30. #pragma GCC visibility pop
  31. static inline void
  32. futex_wait (int *addr, int val)
  33. {
  34. int err = syscall (SYS_futex, addr, gomp_futex_wait, val, NULL);
  35. if (__builtin_expect (err < 0 && errno == ENOSYS, 0))
  36. {
  37. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  38. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  39. syscall (SYS_futex, addr, gomp_futex_wait, val, NULL);
  40. }
  41. }
  42. static inline void
  43. futex_wake (int *addr, int count)
  44. {
  45. int err = syscall (SYS_futex, addr, gomp_futex_wake, count);
  46. if (__builtin_expect (err < 0 && errno == ENOSYS, 0))
  47. {
  48. gomp_futex_wait &= ~FUTEX_PRIVATE_FLAG;
  49. gomp_futex_wake &= ~FUTEX_PRIVATE_FLAG;
  50. syscall (SYS_futex, addr, gomp_futex_wake, count);
  51. }
  52. }
  53. static inline void
  54. cpu_relax (void)
  55. {
  56. __asm volatile ("" : : : "memory");
  57. }