pthread_cond.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * File : pthread_cond.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. #include "pthread_internal.h"
  26. int pthread_condattr_destroy(pthread_condattr_t *attr)
  27. {
  28. if (!attr)
  29. return EINVAL;
  30. return 0;
  31. }
  32. RTM_EXPORT(pthread_condattr_destroy);
  33. int pthread_condattr_init(pthread_condattr_t *attr)
  34. {
  35. if (!attr)
  36. return EINVAL;
  37. *attr = PTHREAD_PROCESS_PRIVATE;
  38. return 0;
  39. }
  40. RTM_EXPORT(pthread_condattr_init);
  41. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  42. clockid_t *clock_id)
  43. {
  44. return 0;
  45. }
  46. RTM_EXPORT(pthread_condattr_getclock);
  47. int pthread_condattr_setclock(pthread_condattr_t *attr,
  48. clockid_t clock_id)
  49. {
  50. return 0;
  51. }
  52. RTM_EXPORT(pthread_condattr_setclock);
  53. int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
  54. {
  55. if (!attr || !pshared)
  56. return EINVAL;
  57. *pshared = PTHREAD_PROCESS_PRIVATE;
  58. return 0;
  59. }
  60. RTM_EXPORT(pthread_condattr_getpshared);
  61. int pthread_condattr_setpshared(pthread_condattr_t*attr, int pshared)
  62. {
  63. if ((pshared != PTHREAD_PROCESS_PRIVATE) &&
  64. (pshared != PTHREAD_PROCESS_SHARED))
  65. {
  66. return EINVAL;
  67. }
  68. if (pshared != PTHREAD_PROCESS_PRIVATE)
  69. return ENOSYS;
  70. return 0;
  71. }
  72. RTM_EXPORT(pthread_condattr_setpshared);
  73. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  74. {
  75. rt_err_t result;
  76. char cond_name[RT_NAME_MAX];
  77. static rt_uint16_t cond_num = 0;
  78. /* parameter check */
  79. if (cond == RT_NULL)
  80. return EINVAL;
  81. if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE))
  82. return EINVAL;
  83. rt_snprintf(cond_name, sizeof(cond_name), "cond%02d", cond_num++);
  84. if (attr == RT_NULL) /* use default value */
  85. cond->attr = PTHREAD_PROCESS_PRIVATE;
  86. else
  87. cond->attr = *attr;
  88. result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
  89. if (result != RT_EOK)
  90. return EINVAL;
  91. /* detach the object from system object container */
  92. rt_object_detach(&(cond->sem.parent.parent));
  93. return 0;
  94. }
  95. RTM_EXPORT(pthread_cond_init);
  96. int pthread_cond_destroy(pthread_cond_t *cond)
  97. {
  98. rt_err_t result;
  99. if (cond == RT_NULL)
  100. return EINVAL;
  101. if (cond->attr == -1)
  102. return 0; /* which is not initialized */
  103. result = rt_sem_trytake(&(cond->sem));
  104. if (result != RT_EOK)
  105. return EBUSY;
  106. /* clean condition */
  107. rt_memset(cond, 0, sizeof(pthread_cond_t));
  108. cond->attr = -1;
  109. return 0;
  110. }
  111. RTM_EXPORT(pthread_cond_destroy);
  112. int pthread_cond_broadcast(pthread_cond_t *cond)
  113. {
  114. rt_err_t result;
  115. if (cond->attr == -1)
  116. pthread_cond_init(cond, RT_NULL);
  117. rt_enter_critical();
  118. while (1)
  119. {
  120. /* try to take condition semaphore */
  121. result = rt_sem_trytake(&(cond->sem));
  122. if (result == -RT_ETIMEOUT)
  123. {
  124. /* it's timeout, release this semaphore */
  125. rt_sem_release(&(cond->sem));
  126. }
  127. else if (result == RT_EOK)
  128. {
  129. /* has taken this semaphore, release it */
  130. rt_sem_release(&(cond->sem));
  131. break;
  132. }
  133. else
  134. {
  135. rt_exit_critical();
  136. return EINVAL;
  137. }
  138. }
  139. rt_exit_critical();
  140. return 0;
  141. }
  142. RTM_EXPORT(pthread_cond_broadcast);
  143. int pthread_cond_signal(pthread_cond_t *cond)
  144. {
  145. rt_err_t result;
  146. if (cond->attr == -1)
  147. pthread_cond_init(cond, RT_NULL);
  148. result = rt_sem_release(&(cond->sem));
  149. if (result == RT_EOK)
  150. return 0;
  151. return 0;
  152. }
  153. RTM_EXPORT(pthread_cond_signal);
  154. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
  155. pthread_mutex_t *mutex,
  156. rt_int32_t timeout)
  157. {
  158. rt_err_t result;
  159. if (!cond || !mutex)
  160. return -RT_ERROR;
  161. /* check whether initialized */
  162. if (cond->attr == -1)
  163. pthread_cond_init(cond, RT_NULL);
  164. /* The mutex was not owned by the current thread at the time of the call. */
  165. if (mutex->lock.owner != pthread_self())
  166. return -RT_ERROR;
  167. /* unlock a mutex failed */
  168. if (pthread_mutex_unlock(mutex) != 0)
  169. return -RT_ERROR;
  170. result = rt_sem_take(&(cond->sem), timeout);
  171. /* lock mutex again */
  172. pthread_mutex_lock(mutex);
  173. return result;
  174. }
  175. RTM_EXPORT(_pthread_cond_timedwait);
  176. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  177. {
  178. rt_err_t result;
  179. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  180. if (result == RT_EOK)
  181. return 0;
  182. return EINVAL;
  183. }
  184. RTM_EXPORT(pthread_cond_wait);
  185. int pthread_cond_timedwait(pthread_cond_t *cond,
  186. pthread_mutex_t *mutex,
  187. const struct timespec *abstime)
  188. {
  189. int timeout;
  190. rt_err_t result;
  191. timeout = clock_time_to_tick(abstime);
  192. result = _pthread_cond_timedwait(cond, mutex, timeout);
  193. if (result == RT_EOK)
  194. return 0;
  195. if (result == -RT_ETIMEOUT)
  196. return ETIMEDOUT;
  197. return EINVAL;
  198. }
  199. RTM_EXPORT(pthread_cond_timedwait);