pthread_barrier.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * File : pthread_barrier.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-10-26 Bernard the first version
  23. */
  24. #include <pthread.h>
  25. int pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
  26. {
  27. if (!attr)
  28. return EINVAL;
  29. return 0;
  30. }
  31. RTM_EXPORT(pthread_barrierattr_destroy);
  32. int pthread_barrierattr_init(pthread_barrierattr_t *attr)
  33. {
  34. if (!attr)
  35. return EINVAL;
  36. *attr = PTHREAD_PROCESS_PRIVATE;
  37. return 0;
  38. }
  39. RTM_EXPORT(pthread_barrierattr_init);
  40. int pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
  41. int *pshared)
  42. {
  43. if (!attr)
  44. return EINVAL;
  45. *pshared = (int)*attr;
  46. return 0;
  47. }
  48. RTM_EXPORT(pthread_barrierattr_getpshared);
  49. int pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
  50. {
  51. if (!attr)
  52. return EINVAL;
  53. if (pshared == PTHREAD_PROCESS_PRIVATE)
  54. attr = PTHREAD_PROCESS_PRIVATE;
  55. return EINVAL;
  56. }
  57. RTM_EXPORT(pthread_barrierattr_setpshared);
  58. int pthread_barrier_destroy(pthread_barrier_t *barrier)
  59. {
  60. rt_err_t result;
  61. if (!barrier)
  62. return EINVAL;
  63. result = pthread_cond_destroy(&(barrier->cond));
  64. return result;
  65. }
  66. RTM_EXPORT(pthread_barrier_destroy);
  67. int pthread_barrier_init(pthread_barrier_t *barrier,
  68. const pthread_barrierattr_t *attr,
  69. unsigned count)
  70. {
  71. if (!barrier)
  72. return EINVAL;
  73. if (attr && (*attr != PTHREAD_PROCESS_PRIVATE))
  74. return EINVAL;
  75. barrier->count = count;
  76. pthread_cond_init(&(barrier->cond), NULL);
  77. pthread_mutex_init(&(barrier->mutex), NULL);
  78. return 0;
  79. }
  80. RTM_EXPORT(pthread_barrier_init);
  81. int pthread_barrier_wait(pthread_barrier_t *barrier)
  82. {
  83. rt_err_t result;
  84. if (!barrier)
  85. return EINVAL;
  86. result = pthread_mutex_lock(&(barrier->mutex));
  87. if (result != 0)
  88. return EINVAL;
  89. if (barrier->count == 0)
  90. result = EINVAL;
  91. else
  92. {
  93. barrier->count -= 1;
  94. if (barrier->count == 0) /* broadcast condition */
  95. pthread_cond_broadcast(&(barrier->cond));
  96. else
  97. pthread_cond_wait(&(barrier->cond), &(barrier->mutex));
  98. }
  99. pthread_mutex_unlock(&(barrier->mutex));
  100. return result;
  101. }
  102. RTM_EXPORT(pthread_barrier_wait);