thread_queue.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/error.h"
  22. #include "libavutil/fifo.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/thread.h"
  26. #include "objpool.h"
  27. #include "thread_queue.h"
  28. enum {
  29. FINISHED_SEND = (1 << 0),
  30. FINISHED_RECV = (1 << 1),
  31. };
  32. typedef struct FifoElem {
  33. void *obj;
  34. unsigned int stream_idx;
  35. } FifoElem;
  36. struct ThreadQueue {
  37. int *finished;
  38. unsigned int nb_streams;
  39. AVFifo *fifo;
  40. ObjPool *obj_pool;
  41. void (*obj_move)(void *dst, void *src);
  42. pthread_mutex_t lock;
  43. pthread_cond_t cond;
  44. };
  45. void tq_free(ThreadQueue **ptq)
  46. {
  47. ThreadQueue *tq = *ptq;
  48. if (!tq)
  49. return;
  50. if (tq->fifo) {
  51. FifoElem elem;
  52. while (av_fifo_read(tq->fifo, &elem, 1) >= 0)
  53. objpool_release(tq->obj_pool, &elem.obj);
  54. }
  55. av_fifo_freep2(&tq->fifo);
  56. objpool_free(&tq->obj_pool);
  57. av_freep(&tq->finished);
  58. pthread_cond_destroy(&tq->cond);
  59. pthread_mutex_destroy(&tq->lock);
  60. av_freep(ptq);
  61. }
  62. ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
  63. ObjPool *obj_pool, void (*obj_move)(void *dst, void *src))
  64. {
  65. ThreadQueue *tq;
  66. int ret;
  67. tq = av_mallocz(sizeof(*tq));
  68. if (!tq)
  69. return NULL;
  70. ret = pthread_cond_init(&tq->cond, NULL);
  71. if (ret) {
  72. av_freep(&tq);
  73. return NULL;
  74. }
  75. ret = pthread_mutex_init(&tq->lock, NULL);
  76. if (ret) {
  77. pthread_cond_destroy(&tq->cond);
  78. av_freep(&tq);
  79. return NULL;
  80. }
  81. tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
  82. if (!tq->finished)
  83. goto fail;
  84. tq->nb_streams = nb_streams;
  85. tq->fifo = av_fifo_alloc2(queue_size, sizeof(FifoElem), 0);
  86. if (!tq->fifo)
  87. goto fail;
  88. tq->obj_pool = obj_pool;
  89. tq->obj_move = obj_move;
  90. return tq;
  91. fail:
  92. tq_free(&tq);
  93. return NULL;
  94. }
  95. int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
  96. {
  97. int *finished;
  98. int ret;
  99. av_assert0(stream_idx < tq->nb_streams);
  100. finished = &tq->finished[stream_idx];
  101. pthread_mutex_lock(&tq->lock);
  102. if (*finished & FINISHED_SEND) {
  103. ret = AVERROR(EINVAL);
  104. goto finish;
  105. }
  106. while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo))
  107. pthread_cond_wait(&tq->cond, &tq->lock);
  108. if (*finished & FINISHED_RECV) {
  109. ret = AVERROR_EOF;
  110. *finished |= FINISHED_SEND;
  111. } else {
  112. FifoElem elem = { .stream_idx = stream_idx };
  113. ret = objpool_get(tq->obj_pool, &elem.obj);
  114. if (ret < 0)
  115. goto finish;
  116. tq->obj_move(elem.obj, data);
  117. ret = av_fifo_write(tq->fifo, &elem, 1);
  118. av_assert0(ret >= 0);
  119. pthread_cond_broadcast(&tq->cond);
  120. }
  121. finish:
  122. pthread_mutex_unlock(&tq->lock);
  123. return ret;
  124. }
  125. static int receive_locked(ThreadQueue *tq, int *stream_idx,
  126. void *data)
  127. {
  128. FifoElem elem;
  129. unsigned int nb_finished = 0;
  130. if (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
  131. tq->obj_move(data, elem.obj);
  132. objpool_release(tq->obj_pool, &elem.obj);
  133. *stream_idx = elem.stream_idx;
  134. return 0;
  135. }
  136. for (unsigned int i = 0; i < tq->nb_streams; i++) {
  137. if (!(tq->finished[i] & FINISHED_SEND))
  138. continue;
  139. /* return EOF to the consumer at most once for each stream */
  140. if (!(tq->finished[i] & FINISHED_RECV)) {
  141. tq->finished[i] |= FINISHED_RECV;
  142. *stream_idx = i;
  143. return AVERROR_EOF;
  144. }
  145. nb_finished++;
  146. }
  147. return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
  148. }
  149. int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
  150. {
  151. int ret;
  152. *stream_idx = -1;
  153. pthread_mutex_lock(&tq->lock);
  154. while (1) {
  155. ret = receive_locked(tq, stream_idx, data);
  156. if (ret == AVERROR(EAGAIN)) {
  157. pthread_cond_wait(&tq->cond, &tq->lock);
  158. continue;
  159. }
  160. break;
  161. }
  162. if (ret == 0)
  163. pthread_cond_broadcast(&tq->cond);
  164. pthread_mutex_unlock(&tq->lock);
  165. return ret;
  166. }
  167. void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
  168. {
  169. av_assert0(stream_idx < tq->nb_streams);
  170. pthread_mutex_lock(&tq->lock);
  171. /* mark the stream as send-finished;
  172. * next time the consumer thread tries to read this stream it will get
  173. * an EOF and recv-finished flag will be set */
  174. tq->finished[stream_idx] |= FINISHED_SEND;
  175. pthread_cond_broadcast(&tq->cond);
  176. pthread_mutex_unlock(&tq->lock);
  177. }
  178. void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
  179. {
  180. av_assert0(stream_idx < tq->nb_streams);
  181. pthread_mutex_lock(&tq->lock);
  182. /* mark the stream as recv-finished;
  183. * next time the producer thread tries to send for this stream, it will
  184. * get an EOF and send-finished flag will be set */
  185. tq->finished[stream_idx] |= FINISHED_RECV;
  186. pthread_cond_broadcast(&tq->cond);
  187. pthread_mutex_unlock(&tq->lock);
  188. }