pthread_cond.c 6.1 KB

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