You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.4KB

  1. /*
  2. * Copyright (c) 2014 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "fifo.h"
  21. #include "threadmessage.h"
  22. #if HAVE_THREADS
  23. #if HAVE_PTHREADS
  24. #include <pthread.h>
  25. #elif HAVE_W32THREADS
  26. #include "compat/w32pthreads.h"
  27. #elif HAVE_OS2THREADS
  28. #include "compat/os2threads.h"
  29. #else
  30. #error "Unknown threads implementation"
  31. #endif
  32. #endif
  33. struct AVThreadMessageQueue {
  34. #if HAVE_THREADS
  35. AVFifoBuffer *fifo;
  36. pthread_mutex_t lock;
  37. pthread_cond_t cond_recv;
  38. pthread_cond_t cond_send;
  39. int err_send;
  40. int err_recv;
  41. unsigned elsize;
  42. void (*free_func)(void *msg);
  43. #else
  44. int dummy;
  45. #endif
  46. };
  47. int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
  48. unsigned nelem,
  49. unsigned elsize)
  50. {
  51. #if HAVE_THREADS
  52. AVThreadMessageQueue *rmq;
  53. int ret = 0;
  54. if (nelem > INT_MAX / elsize)
  55. return AVERROR(EINVAL);
  56. if (!(rmq = av_mallocz(sizeof(*rmq))))
  57. return AVERROR(ENOMEM);
  58. if ((ret = pthread_mutex_init(&rmq->lock, NULL))) {
  59. av_free(rmq);
  60. return AVERROR(ret);
  61. }
  62. if ((ret = pthread_cond_init(&rmq->cond_recv, NULL))) {
  63. pthread_mutex_destroy(&rmq->lock);
  64. av_free(rmq);
  65. return AVERROR(ret);
  66. }
  67. if ((ret = pthread_cond_init(&rmq->cond_send, NULL))) {
  68. pthread_cond_destroy(&rmq->cond_recv);
  69. pthread_mutex_destroy(&rmq->lock);
  70. av_free(rmq);
  71. return AVERROR(ret);
  72. }
  73. if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
  74. pthread_cond_destroy(&rmq->cond_send);
  75. pthread_cond_destroy(&rmq->cond_recv);
  76. pthread_mutex_destroy(&rmq->lock);
  77. av_free(rmq);
  78. return AVERROR(ret);
  79. }
  80. rmq->elsize = elsize;
  81. *mq = rmq;
  82. return 0;
  83. #else
  84. *mq = NULL;
  85. return AVERROR(ENOSYS);
  86. #endif /* HAVE_THREADS */
  87. }
  88. void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
  89. void (*free_func)(void *msg))
  90. {
  91. mq->free_func = free_func;
  92. }
  93. void av_thread_message_queue_free(AVThreadMessageQueue **mq)
  94. {
  95. #if HAVE_THREADS
  96. if (*mq) {
  97. av_thread_message_flush(*mq);
  98. av_fifo_freep(&(*mq)->fifo);
  99. pthread_cond_destroy(&(*mq)->cond_send);
  100. pthread_cond_destroy(&(*mq)->cond_recv);
  101. pthread_mutex_destroy(&(*mq)->lock);
  102. av_freep(mq);
  103. }
  104. #endif
  105. }
  106. #if HAVE_THREADS
  107. static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
  108. void *msg,
  109. unsigned flags)
  110. {
  111. while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
  112. if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
  113. return AVERROR(EAGAIN);
  114. pthread_cond_wait(&mq->cond_send, &mq->lock);
  115. }
  116. if (mq->err_send)
  117. return mq->err_send;
  118. av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
  119. /* one message is sent, signal one receiver */
  120. pthread_cond_signal(&mq->cond_recv);
  121. return 0;
  122. }
  123. static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
  124. void *msg,
  125. unsigned flags)
  126. {
  127. while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
  128. if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
  129. return AVERROR(EAGAIN);
  130. pthread_cond_wait(&mq->cond_recv, &mq->lock);
  131. }
  132. if (av_fifo_size(mq->fifo) < mq->elsize)
  133. return mq->err_recv;
  134. av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
  135. /* one message space appeared, signal one sender */
  136. pthread_cond_signal(&mq->cond_send);
  137. return 0;
  138. }
  139. #endif /* HAVE_THREADS */
  140. int av_thread_message_queue_send(AVThreadMessageQueue *mq,
  141. void *msg,
  142. unsigned flags)
  143. {
  144. #if HAVE_THREADS
  145. int ret;
  146. pthread_mutex_lock(&mq->lock);
  147. ret = av_thread_message_queue_send_locked(mq, msg, flags);
  148. pthread_mutex_unlock(&mq->lock);
  149. return ret;
  150. #else
  151. return AVERROR(ENOSYS);
  152. #endif /* HAVE_THREADS */
  153. }
  154. int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
  155. void *msg,
  156. unsigned flags)
  157. {
  158. #if HAVE_THREADS
  159. int ret;
  160. pthread_mutex_lock(&mq->lock);
  161. ret = av_thread_message_queue_recv_locked(mq, msg, flags);
  162. pthread_mutex_unlock(&mq->lock);
  163. return ret;
  164. #else
  165. return AVERROR(ENOSYS);
  166. #endif /* HAVE_THREADS */
  167. }
  168. void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
  169. int err)
  170. {
  171. #if HAVE_THREADS
  172. pthread_mutex_lock(&mq->lock);
  173. mq->err_send = err;
  174. pthread_cond_broadcast(&mq->cond_send);
  175. pthread_mutex_unlock(&mq->lock);
  176. #endif /* HAVE_THREADS */
  177. }
  178. void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
  179. int err)
  180. {
  181. #if HAVE_THREADS
  182. pthread_mutex_lock(&mq->lock);
  183. mq->err_recv = err;
  184. pthread_cond_broadcast(&mq->cond_recv);
  185. pthread_mutex_unlock(&mq->lock);
  186. #endif /* HAVE_THREADS */
  187. }
  188. static void free_func_wrap(void *arg, void *msg, int size)
  189. {
  190. AVThreadMessageQueue *mq = arg;
  191. mq->free_func(msg);
  192. }
  193. void av_thread_message_flush(AVThreadMessageQueue *mq)
  194. {
  195. #if HAVE_THREADS
  196. int used, off;
  197. void *free_func = mq->free_func;
  198. pthread_mutex_lock(&mq->lock);
  199. used = av_fifo_size(mq->fifo);
  200. if (free_func)
  201. for (off = 0; off < used; off += mq->elsize)
  202. av_fifo_generic_peek_at(mq->fifo, mq, off, mq->elsize, free_func_wrap);
  203. av_fifo_drain(mq->fifo, used);
  204. /* only the senders need to be notified since the queue is empty and there
  205. * is nothing to read */
  206. pthread_cond_broadcast(&mq->cond_send);
  207. pthread_mutex_unlock(&mq->lock);
  208. #endif /* HAVE_THREADS */
  209. }