pthread_cond.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. cond->attr = *attr;
  85. result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
  86. if (result != RT_EOK)
  87. return EINVAL;
  88. /* detach the object from system object container */
  89. rt_object_detach(&(cond->sem.parent.parent));
  90. return 0;
  91. }
  92. RTM_EXPORT(pthread_cond_init);
  93. int pthread_cond_destroy(pthread_cond_t *cond)
  94. {
  95. rt_err_t result;
  96. if (cond == RT_NULL)
  97. return EINVAL;
  98. if (cond->attr == -1)
  99. return 0; /* which is not initialized */
  100. result = rt_sem_trytake(&(cond->sem));
  101. if (result != RT_EOK)
  102. return EBUSY;
  103. /* clean condition */
  104. rt_memset(cond, 0, sizeof(pthread_cond_t));
  105. cond->attr = -1;
  106. return 0;
  107. }
  108. RTM_EXPORT(pthread_cond_destroy);
  109. int pthread_cond_broadcast(pthread_cond_t *cond)
  110. {
  111. rt_err_t result;
  112. if (cond->attr == -1)
  113. pthread_cond_init(cond, RT_NULL);
  114. rt_enter_critical();
  115. while (1)
  116. {
  117. /* try to take condition semaphore */
  118. result = rt_sem_trytake(&(cond->sem));
  119. if (result == -RT_ETIMEOUT)
  120. {
  121. /* it's timeout, release this semaphore */
  122. rt_sem_release(&(cond->sem));
  123. }
  124. else if (result == RT_EOK)
  125. {
  126. /* has taken this semaphore, release it */
  127. rt_sem_release(&(cond->sem));
  128. break;
  129. }
  130. else
  131. {
  132. rt_exit_critical();
  133. return EINVAL;
  134. }
  135. }
  136. rt_exit_critical();
  137. return 0;
  138. }
  139. RTM_EXPORT(pthread_cond_broadcast);
  140. int pthread_cond_signal(pthread_cond_t *cond)
  141. {
  142. rt_err_t result;
  143. if (cond->attr == -1)
  144. pthread_cond_init(cond, RT_NULL);
  145. result = rt_sem_release(&(cond->sem));
  146. if (result == RT_EOK)
  147. return 0;
  148. return 0;
  149. }
  150. RTM_EXPORT(pthread_cond_signal);
  151. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond,
  152. pthread_mutex_t *mutex,
  153. rt_int32_t timeout)
  154. {
  155. rt_err_t result;
  156. if (!cond || !mutex)
  157. return -RT_ERROR;
  158. /* check whether initialized */
  159. if (cond->attr == -1)
  160. pthread_cond_init(cond, RT_NULL);
  161. /* The mutex was not owned by the current thread at the time of the call. */
  162. if (mutex->lock.owner != pthread_self())
  163. return -RT_ERROR;
  164. /* unlock a mutex failed */
  165. if (pthread_mutex_unlock(mutex) != 0)
  166. return -RT_ERROR;
  167. result = rt_sem_take(&(cond->sem), timeout);
  168. /* lock mutex again */
  169. pthread_mutex_lock(mutex);
  170. return result;
  171. }
  172. RTM_EXPORT(_pthread_cond_timedwait);
  173. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  174. {
  175. rt_err_t result;
  176. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  177. if (result == RT_EOK)
  178. return 0;
  179. return EINVAL;
  180. }
  181. RTM_EXPORT(pthread_cond_wait);
  182. int pthread_cond_timedwait(pthread_cond_t *cond,
  183. pthread_mutex_t *mutex,
  184. const struct timespec *abstime)
  185. {
  186. int timeout;
  187. rt_err_t result;
  188. timeout = clock_time_to_tick(abstime);
  189. result = _pthread_cond_timedwait(cond, mutex, timeout);
  190. if (result == RT_EOK)
  191. return 0;
  192. if (result == -RT_ETIMEOUT)
  193. return ETIMEDOUT;
  194. return EINVAL;
  195. }
  196. RTM_EXPORT(pthread_cond_timedwait);