pthread_cond.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 == RT_NULL)
  116. return EINVAL;
  117. if (cond->attr == -1)
  118. pthread_cond_init(cond, RT_NULL);
  119. rt_enter_critical();
  120. while (1)
  121. {
  122. /* try to take condition semaphore */
  123. result = rt_sem_trytake(&(cond->sem));
  124. if (result == -RT_ETIMEOUT)
  125. {
  126. /* it's timeout, release this semaphore */
  127. rt_sem_release(&(cond->sem));
  128. }
  129. else if (result == RT_EOK)
  130. {
  131. /* has taken this semaphore, release it */
  132. rt_sem_release(&(cond->sem));
  133. break;
  134. }
  135. else
  136. {
  137. rt_exit_critical();
  138. return EINVAL;
  139. }
  140. }
  141. rt_exit_critical();
  142. return 0;
  143. }
  144. RTM_EXPORT(pthread_cond_broadcast);
  145. int pthread_cond_signal(pthread_cond_t *cond)
  146. {
  147. rt_err_t result;
  148. if (cond == RT_NULL)
  149. return EINVAL;
  150. if (cond->attr == -1)
  151. pthread_cond_init(cond, RT_NULL);
  152. result = rt_sem_release(&(cond->sem));
  153. if (result == RT_EOK)
  154. return 0;
  155. return 0;
  156. }
  157. RTM_EXPORT(pthread_cond_signal);
  158. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
  159. pthread_mutex_t *mutex,
  160. rt_int32_t timeout)
  161. {
  162. rt_err_t result;
  163. if (!cond || !mutex)
  164. return -RT_ERROR;
  165. /* check whether initialized */
  166. if (cond->attr == -1)
  167. pthread_cond_init(cond, RT_NULL);
  168. /* The mutex was not owned by the current thread at the time of the call. */
  169. if (mutex->lock.owner != pthread_self())
  170. return -RT_ERROR;
  171. /* unlock a mutex failed */
  172. if (pthread_mutex_unlock(mutex) != 0)
  173. return -RT_ERROR;
  174. result = rt_sem_take(&(cond->sem), timeout);
  175. /* lock mutex again */
  176. pthread_mutex_lock(mutex);
  177. return result;
  178. }
  179. RTM_EXPORT(_pthread_cond_timedwait);
  180. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  181. {
  182. rt_err_t result;
  183. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  184. if (result == RT_EOK)
  185. return 0;
  186. return EINVAL;
  187. }
  188. RTM_EXPORT(pthread_cond_wait);
  189. int pthread_cond_timedwait(pthread_cond_t *cond,
  190. pthread_mutex_t *mutex,
  191. const struct timespec *abstime)
  192. {
  193. int timeout;
  194. rt_err_t result;
  195. timeout = clock_time_to_tick(abstime);
  196. result = _pthread_cond_timedwait(cond, mutex, timeout);
  197. if (result == RT_EOK)
  198. return 0;
  199. if (result == -RT_ETIMEOUT)
  200. return ETIMEDOUT;
  201. return EINVAL;
  202. }
  203. RTM_EXPORT(pthread_cond_timedwait);