futex.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 2008-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Provide access to the futex system call. */
  20. #include "libitm_i.h"
  21. #include "futex.h"
  22. #include <futex_bits.h>
  23. #include <errno.h>
  24. namespace GTM HIDDEN {
  25. #define FUTEX_WAIT 0
  26. #define FUTEX_WAKE 1
  27. #define FUTEX_PRIVATE_FLAG 128L
  28. static long int gtm_futex_wait = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
  29. static long int gtm_futex_wake = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
  30. void
  31. futex_wait (std::atomic<int> *addr, int val)
  32. {
  33. long res;
  34. res = sys_futex0 (addr, gtm_futex_wait, val);
  35. if (__builtin_expect (res == -ENOSYS, 0))
  36. {
  37. gtm_futex_wait = FUTEX_WAIT;
  38. gtm_futex_wake = FUTEX_WAKE;
  39. res = sys_futex0 (addr, FUTEX_WAIT, val);
  40. }
  41. if (__builtin_expect (res < 0, 0))
  42. {
  43. if (res == -EWOULDBLOCK || res == -ETIMEDOUT)
  44. ;
  45. else if (res == -EFAULT)
  46. GTM_fatal ("futex failed (EFAULT %p)", addr);
  47. else
  48. GTM_fatal ("futex failed (%s)", strerror(-res));
  49. }
  50. }
  51. long
  52. futex_wake (std::atomic<int> *addr, int count)
  53. {
  54. long res = sys_futex0 (addr, gtm_futex_wake, count);
  55. if (__builtin_expect (res == -ENOSYS, 0))
  56. {
  57. gtm_futex_wait = FUTEX_WAIT;
  58. gtm_futex_wake = FUTEX_WAKE;
  59. res = sys_futex0 (addr, FUTEX_WAKE, count);
  60. }
  61. if (__builtin_expect (res < 0, 0))
  62. GTM_fatal ("futex failed (%s)", strerror(-res));
  63. else
  64. return res;
  65. }
  66. } // namespace GTM