sem.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /* This is a Linux specific implementation of a semaphore synchronization
  21. mechanism for libgomp. This type is private to the library. This
  22. implementation uses atomic instructions and the futex syscall. */
  23. #include "wait.h"
  24. void
  25. gomp_sem_wait_slow (gomp_sem_t *sem, int count)
  26. {
  27. /* First loop spins a while. */
  28. while (count == 0)
  29. if (do_spin (sem, 0)
  30. /* Spin timeout, nothing changed. Set waiting flag. */
  31. && __atomic_compare_exchange_n (sem, &count, SEM_WAIT, false,
  32. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  33. {
  34. futex_wait (sem, SEM_WAIT);
  35. count = *sem;
  36. break;
  37. }
  38. /* Something changed. If it wasn't the wait flag, we're good to go. */
  39. else if (__builtin_expect (((count = *sem) & SEM_WAIT) == 0 && count != 0,
  40. 1))
  41. {
  42. if (__atomic_compare_exchange_n (sem, &count, count - SEM_INC, false,
  43. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  44. return;
  45. }
  46. /* Second loop waits until semaphore is posted. We always exit this
  47. loop with wait flag set, so next post will awaken a thread. */
  48. while (1)
  49. {
  50. unsigned int wake = count & ~SEM_WAIT;
  51. int newval = SEM_WAIT;
  52. if (wake != 0)
  53. newval |= wake - SEM_INC;
  54. if (__atomic_compare_exchange_n (sem, &count, newval, false,
  55. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  56. {
  57. if (wake != 0)
  58. {
  59. /* If we can wake more threads, do so now. */
  60. if (wake > SEM_INC)
  61. gomp_sem_post_slow (sem);
  62. break;
  63. }
  64. do_wait (sem, SEM_WAIT);
  65. count = *sem;
  66. }
  67. }
  68. }
  69. void
  70. gomp_sem_post_slow (gomp_sem_t *sem)
  71. {
  72. futex_wake (sem, 1);
  73. }