barrier.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  38. return EBUSY;
  39. }
  40. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  41. return 0;
  42. }
  43. int _starpu_barrier_destroy(struct _starpu_barrier *barrier)
  44. {
  45. int ret = _starpu_barrier_test(barrier);
  46. while (ret == EBUSY) {
  47. ret = _starpu_barrier_test(barrier);
  48. }
  49. _STARPU_DEBUG("reached_exit %d\n", barrier->reached_exit);
  50. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex);
  51. _STARPU_PTHREAD_MUTEX_DESTROY(&barrier->mutex_exit);
  52. _STARPU_PTHREAD_COND_DESTROY(&barrier->cond);
  53. return 0;
  54. }
  55. int _starpu_barrier_wait(struct _starpu_barrier *barrier)
  56. {
  57. int ret=0;
  58. // Wait until all threads enter the barrier
  59. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex);
  60. barrier->reached_exit=0;
  61. barrier->reached_start++;
  62. if (barrier->reached_start == barrier->count)
  63. {
  64. barrier->reached_start = 0;
  65. _STARPU_PTHREAD_COND_BROADCAST(&barrier->cond);
  66. ret = PTHREAD_BARRIER_SERIAL_THREAD;
  67. }
  68. else
  69. {
  70. _STARPU_PTHREAD_COND_WAIT(&barrier->cond,&barrier->mutex);
  71. }
  72. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex);
  73. // Count number of threads that exit the barrier
  74. _STARPU_PTHREAD_MUTEX_LOCK(&barrier->mutex_exit);
  75. barrier->reached_exit ++;
  76. _STARPU_PTHREAD_MUTEX_UNLOCK(&barrier->mutex_exit);
  77. return ret;
  78. }