barrier.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2011 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. int _starpu_barrier_init(struct _starpu_barrier *barrier, int count)
  19. {
  20. barrier->count = count;
  21. barrier->reached_start = 0;
  22. barrier->reached_exit = 0;
  23. _STARPU_PTHREAD_MUTEX_INIT(&barrier->mutex, NULL);
  24. _STARPU_PTHREAD_MUTEX_INIT(&barrier->mutex_exit, NULL);
  25. _STARPU_PTHREAD_COND_INIT(&barrier->cond, NULL);
  26. return 0;
  27. }
  28. static
  29. int _starpu_barrier_test(struct _starpu_barrier *barrier)
  30. {
  31. /*
  32. * Check whether any threads are known to be waiting; report
  33. * "BUSY" if so.
  34. */
  35. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex_exit);
  36. if (barrier->reached_exit != barrier->count)
  37. {
  38. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  39. return EBUSY;
  40. }
  41. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  42. return 0;
  43. }
  44. int _starpu_barrier_destroy(struct _starpu_barrier *barrier)
  45. {
  46. int ret = _starpu_barrier_test(barrier);
  47. while (ret == EBUSY)
  48. {
  49. ret = _starpu_barrier_test(barrier);
  50. }
  51. _STARPU_DEBUG("reached_exit %d\n", barrier->reached_exit);
  52. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex);
  53. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex_exit);
  54. _STARPU_PTHREAD_COND_DESTROY(&barrier->cond);
  55. return 0;
  56. }
  57. int _starpu_barrier_wait(struct _starpu_barrier *barrier)
  58. {
  59. int ret=0;
  60. // Wait until all threads enter the barrier
  61. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex);
  62. barrier->reached_exit=0;
  63. barrier->reached_start++;
  64. if (barrier->reached_start == barrier->count)
  65. {
  66. barrier->reached_start = 0;
  67. _STARPU_PTHREAD_COND_BROADCAST(&barrier->cond);
  68. ret = PTHREAD_BARRIER_SERIAL_THREAD;
  69. }
  70. else
  71. {
  72. _STARPU_PTHREAD_COND_WAIT(&barrier->cond,&barrier->mutex);
  73. }
  74. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex);
  75. // Count number of threads that exit the barrier
  76. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex_exit);
  77. barrier->reached_exit ++;
  78. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  79. return ret;
  80. }