barrier.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. int _starpu_barrier_init(_starpu_barrier_t *barrier, int count)
  18. {
  19. barrier->count = count;
  20. barrier->reached = 0;
  21. pthread_mutex_init(&barrier->mutex,NULL);
  22. pthread_cond_init(&barrier->cond,NULL);
  23. return 0;
  24. }
  25. int _starpu_barrier_destroy(_starpu_barrier_t *barrier)
  26. {
  27. pthread_mutex_destroy(&barrier->mutex);
  28. pthread_cond_destroy(&barrier->cond);
  29. return 0;
  30. }
  31. int _starpu_barrier_wait(_starpu_barrier_t *barrier)
  32. {
  33. int ret=0;
  34. pthread_mutex_lock(&barrier->mutex);
  35. barrier->reached++;
  36. if (barrier->reached == barrier->count)
  37. {
  38. barrier->reached = 0;
  39. pthread_cond_broadcast(&barrier->cond);
  40. ret = PTHREAD_BARRIER_SERIAL_THREAD;
  41. }
  42. else
  43. {
  44. pthread_cond_wait(&barrier->cond,&barrier->mutex);
  45. }
  46. pthread_mutex_unlock(&barrier->mutex);
  47. return ret;
  48. }