pthread_cond.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <pthread.h>
  2. #include "pthread_internal.h"
  3. int pthread_condattr_destroy(pthread_condattr_t *attr)
  4. {
  5. if (!attr) return EINVAL;
  6. return 0;
  7. }
  8. int pthread_condattr_init(pthread_condattr_t *attr)
  9. {
  10. if (!attr) return EINVAL;
  11. *attr = PTHREAD_PROCESS_PRIVATE;
  12. return 0;
  13. }
  14. int pthread_condattr_getclock(const pthread_condattr_t *attr,
  15. clockid_t *clock_id)
  16. {
  17. return 0;
  18. }
  19. int pthread_condattr_setclock(pthread_condattr_t *attr,
  20. clockid_t clock_id)
  21. {
  22. return 0;
  23. }
  24. int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
  25. {
  26. if (!attr || !pshared) return EINVAL;
  27. *pshared = PTHREAD_PROCESS_PRIVATE;
  28. return 0;
  29. }
  30. int pthread_condattr_setpshared(pthread_condattr_t*attr, int pshared)
  31. {
  32. if ((pshared != PTHREAD_PROCESS_PRIVATE) && (pshared != PTHREAD_PROCESS_SHARED))
  33. return EINVAL;
  34. if (pshared != PTHREAD_PROCESS_PRIVATE)
  35. return ENOSYS;
  36. return 0;
  37. }
  38. int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
  39. {
  40. rt_err_t result;
  41. char cond_name[RT_NAME_MAX];
  42. static rt_uint16_t cond_num = 0;
  43. /* parameter check */
  44. if (cond == RT_NULL) return EINVAL;
  45. if ((attr != RT_NULL) && (*attr != PTHREAD_PROCESS_PRIVATE)) return EINVAL;
  46. rt_snprintf(cond_name, sizeof(cond_name),
  47. "cond%02d", cond_num++);
  48. cond->attr = *attr;
  49. result = rt_sem_init(&cond->sem, cond_name, 0, RT_IPC_FLAG_FIFO);
  50. if (result != RT_EOK) return EINVAL;
  51. /* detach the object from system object container */
  52. rt_object_detach(&(cond->sem.parent.parent));
  53. return 0;
  54. }
  55. int pthread_cond_destroy(pthread_cond_t *cond)
  56. {
  57. rt_err_t result;
  58. if (cond == RT_NULL) return EINVAL;
  59. if (cond->attr == -1) return 0; /* which is not initialized */
  60. result = rt_sem_trytake(&(cond->sem));
  61. if (result != RT_EOK) return EBUSY;
  62. /* clean condition */
  63. rt_memset(cond, 0, sizeof(pthread_cond_t));
  64. cond->attr = -1;
  65. return 0;
  66. }
  67. int pthread_cond_broadcast(pthread_cond_t *cond)
  68. {
  69. rt_err_t result;
  70. if (cond->attr == -1)
  71. pthread_cond_init(cond, RT_NULL);
  72. rt_enter_critical();
  73. while (1)
  74. {
  75. /* try to take condition semaphore */
  76. result = rt_sem_trytake(&(cond->sem));
  77. if (result == -RT_ETIMEOUT)
  78. {
  79. /* it's timeout, release this semaphore */
  80. rt_sem_release(&(cond->sem));
  81. }
  82. else if (result == RT_EOK)
  83. {
  84. /* has taken this semaphore, release it */
  85. rt_sem_release(&(cond->sem));
  86. break;
  87. }
  88. else
  89. {
  90. rt_exit_critical();
  91. return EINVAL;
  92. }
  93. }
  94. rt_exit_critical();
  95. return 0;
  96. }
  97. int pthread_cond_signal(pthread_cond_t *cond)
  98. {
  99. rt_err_t result;
  100. if (cond->attr == -1)
  101. pthread_cond_init(cond, RT_NULL);
  102. result = rt_sem_release(&(cond->sem));
  103. if (result == RT_EOK) return 0;
  104. return 0;
  105. }
  106. rt_err_t _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
  107. rt_int32_t timeout)
  108. {
  109. rt_err_t result;
  110. if (!cond || !mutex) return -RT_ERROR;
  111. /* check whether initialized */
  112. if (cond->attr == -1) pthread_cond_init(cond, RT_NULL);
  113. /* The mutex was not owned by the current thread at the time of the call. */
  114. if (mutex->lock.owner != pthread_self()) return -RT_ERROR;
  115. /* unlock a mutex failed */
  116. if (pthread_mutex_unlock(mutex) != 0)
  117. return -RT_ERROR;
  118. result = rt_sem_take(&(cond->sem), timeout);
  119. /* lock mutex again */
  120. pthread_mutex_lock(mutex);
  121. return result;
  122. }
  123. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
  124. {
  125. rt_err_t result;
  126. result = _pthread_cond_timedwait(cond, mutex, RT_WAITING_FOREVER);
  127. if (result == RT_EOK) return 0;
  128. return EINVAL;
  129. }
  130. int pthread_cond_timedwait(pthread_cond_t *cond,
  131. pthread_mutex_t * mutex,
  132. const struct timespec *abstime)
  133. {
  134. int timeout;
  135. rt_err_t result;
  136. timeout = clock_time_to_tick(abstime);
  137. result = _pthread_cond_timedwait(cond, mutex, timeout);
  138. if (result == RT_EOK) return 0;
  139. if (result == -RT_ETIMEOUT) return ETIMEDOUT;
  140. return EINVAL;
  141. }