thread_queue.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. while (av_fifo_read(tq->fifo, &elem, 1) >= 0) {
  131. if (tq->finished[elem.stream_idx] & FINISHED_RECV) {
  132. objpool_release(tq->obj_pool, &elem.obj);
  133. continue;
  134. }
  135. tq->obj_move(data, elem.obj);
  136. objpool_release(tq->obj_pool, &elem.obj);
  137. *stream_idx = elem.stream_idx;
  138. return 0;
  139. }
  140. for (unsigned int i = 0; i < tq->nb_streams; i++) {
  141. if (!tq->finished[i])
  142. continue;
  143. /* return EOF to the consumer at most once for each stream */
  144. if (!(tq->finished[i] & FINISHED_RECV)) {
  145. tq->finished[i] |= FINISHED_RECV;
  146. *stream_idx = i;
  147. return AVERROR_EOF;
  148. }
  149. nb_finished++;
  150. }
  151. return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
  152. }
  153. int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
  154. {
  155. int ret;
  156. *stream_idx = -1;
  157. pthread_mutex_lock(&tq->lock);
  158. while (1) {
  159. size_t can_read = av_fifo_can_read(tq->fifo);
  160. ret = receive_locked(tq, stream_idx, data);
  161. // signal other threads if the fifo state changed
  162. if (can_read != av_fifo_can_read(tq->fifo))
  163. pthread_cond_broadcast(&tq->cond);
  164. if (ret == AVERROR(EAGAIN)) {
  165. pthread_cond_wait(&tq->cond, &tq->lock);
  166. continue;
  167. }
  168. break;
  169. }
  170. pthread_mutex_unlock(&tq->lock);
  171. return ret;
  172. }
  173. void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
  174. {
  175. av_assert0(stream_idx < tq->nb_streams);
  176. pthread_mutex_lock(&tq->lock);
  177. /* mark the stream as send-finished;
  178. * next time the consumer thread tries to read this stream it will get
  179. * an EOF and recv-finished flag will be set */
  180. tq->finished[stream_idx] |= FINISHED_SEND;
  181. pthread_cond_broadcast(&tq->cond);
  182. pthread_mutex_unlock(&tq->lock);
  183. }
  184. void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
  185. {
  186. av_assert0(stream_idx < tq->nb_streams);
  187. pthread_mutex_lock(&tq->lock);
  188. /* mark the stream as recv-finished;
  189. * next time the producer thread tries to send for this stream, it will
  190. * get an EOF and send-finished flag will be set */
  191. tq->finished[stream_idx] |= FINISHED_RECV;
  192. pthread_cond_broadcast(&tq->cond);
  193. pthread_mutex_unlock(&tq->lock);
  194. }