barrier.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011,2013 Centre National de la Recherche Scientifique
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/barrier.h>
  17. #include <common/utils.h>
  18. #include <common/thread.h>
  19. int _starpu_barrier_init(struct _starpu_barrier *barrier, int count)
  20. {
  21. barrier->count = count;
  22. barrier->reached_start = 0;
  23. barrier->reached_exit = 0;
  24. _STARPU_PTHREAD_MUTEX_INIT(&barrier->mutex, NULL);
  25. _STARPU_PTHREAD_MUTEX_INIT(&barrier->mutex_exit, NULL);
  26. _STARPU_PTHREAD_COND_INIT(&barrier->cond, NULL);
  27. return 0;
  28. }
  29. static
  30. int _starpu_barrier_test(struct _starpu_barrier *barrier)
  31. {
  32. /*
  33. * Check whether any threads are known to be waiting; report
  34. * "BUSY" if so.
  35. */
  36. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex_exit);
  37. if (barrier->reached_exit != barrier->count)
  38. {
  39. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  40. return EBUSY;
  41. }
  42. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  43. return 0;
  44. }
  45. int _starpu_barrier_destroy(struct _starpu_barrier *barrier)
  46. {
  47. int ret = _starpu_barrier_test(barrier);
  48. while (ret == EBUSY)
  49. {
  50. ret = _starpu_barrier_test(barrier);
  51. }
  52. _STARPU_DEBUG("reached_exit %d\n", barrier->reached_exit);
  53. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex);
  54. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex_exit);
  55. _STARPU_PTHREAD_COND_DESTROY(&barrier->cond);
  56. return 0;
  57. }
  58. int _starpu_barrier_wait(struct _starpu_barrier *barrier)
  59. {
  60. int ret=0;
  61. // Wait until all threads enter the barrier
  62. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex);
  63. barrier->reached_exit=0;
  64. barrier->reached_start++;
  65. if (barrier->reached_start == barrier->count)
  66. {
  67. barrier->reached_start = 0;
  68. _STARPU_PTHREAD_COND_BROADCAST(&barrier->cond);
  69. ret = PTHREAD_BARRIER_SERIAL_THREAD;
  70. }
  71. else
  72. {
  73. _STARPU_PTHREAD_COND_WAIT(&barrier->cond,&barrier->mutex);
  74. }
  75. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex);
  76. // Count number of threads that exit the barrier
  77. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex_exit);
  78. barrier->reached_exit ++;
  79. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  80. return ret;
  81. }