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.

216 lines
5.8KB

  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;
  38. int err_send;
  39. int err_recv;
  40. unsigned elsize;
  41. void (*free_func)(void *msg);
  42. #else
  43. int dummy;
  44. #endif
  45. };
  46. int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
  47. unsigned nelem,
  48. unsigned elsize)
  49. {
  50. #if HAVE_THREADS
  51. AVThreadMessageQueue *rmq;
  52. int ret = 0;
  53. if (nelem > INT_MAX / elsize)
  54. return AVERROR(EINVAL);
  55. if (!(rmq = av_mallocz(sizeof(*rmq))))
  56. return AVERROR(ENOMEM);
  57. if ((ret = pthread_mutex_init(&rmq->lock, NULL))) {
  58. av_free(rmq);
  59. return AVERROR(ret);
  60. }
  61. if ((ret = pthread_cond_init(&rmq->cond, NULL))) {
  62. pthread_mutex_destroy(&rmq->lock);
  63. av_free(rmq);
  64. return AVERROR(ret);
  65. }
  66. if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
  67. pthread_cond_destroy(&rmq->cond);
  68. pthread_mutex_destroy(&rmq->lock);
  69. av_free(rmq);
  70. return AVERROR(ret);
  71. }
  72. rmq->elsize = elsize;
  73. *mq = rmq;
  74. return 0;
  75. #else
  76. *mq = NULL;
  77. return AVERROR(ENOSYS);
  78. #endif /* HAVE_THREADS */
  79. }
  80. void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
  81. void (*free_func)(void *msg))
  82. {
  83. mq->free_func = free_func;
  84. }
  85. void av_thread_message_queue_free(AVThreadMessageQueue **mq)
  86. {
  87. #if HAVE_THREADS
  88. if (*mq) {
  89. av_thread_message_flush(*mq);
  90. av_fifo_freep(&(*mq)->fifo);
  91. pthread_cond_destroy(&(*mq)->cond);
  92. pthread_mutex_destroy(&(*mq)->lock);
  93. av_freep(mq);
  94. }
  95. #endif
  96. }
  97. #if HAVE_THREADS
  98. static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
  99. void *msg,
  100. unsigned flags)
  101. {
  102. while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
  103. if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
  104. return AVERROR(EAGAIN);
  105. pthread_cond_wait(&mq->cond, &mq->lock);
  106. }
  107. if (mq->err_send)
  108. return mq->err_send;
  109. av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
  110. pthread_cond_signal(&mq->cond);
  111. return 0;
  112. }
  113. static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
  114. void *msg,
  115. unsigned flags)
  116. {
  117. while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
  118. if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
  119. return AVERROR(EAGAIN);
  120. pthread_cond_wait(&mq->cond, &mq->lock);
  121. }
  122. if (av_fifo_size(mq->fifo) < mq->elsize)
  123. return mq->err_recv;
  124. av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
  125. pthread_cond_signal(&mq->cond);
  126. return 0;
  127. }
  128. #endif /* HAVE_THREADS */
  129. int av_thread_message_queue_send(AVThreadMessageQueue *mq,
  130. void *msg,
  131. unsigned flags)
  132. {
  133. #if HAVE_THREADS
  134. int ret;
  135. pthread_mutex_lock(&mq->lock);
  136. ret = av_thread_message_queue_send_locked(mq, msg, flags);
  137. pthread_mutex_unlock(&mq->lock);
  138. return ret;
  139. #else
  140. return AVERROR(ENOSYS);
  141. #endif /* HAVE_THREADS */
  142. }
  143. int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
  144. void *msg,
  145. unsigned flags)
  146. {
  147. #if HAVE_THREADS
  148. int ret;
  149. pthread_mutex_lock(&mq->lock);
  150. ret = av_thread_message_queue_recv_locked(mq, msg, flags);
  151. pthread_mutex_unlock(&mq->lock);
  152. return ret;
  153. #else
  154. return AVERROR(ENOSYS);
  155. #endif /* HAVE_THREADS */
  156. }
  157. void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
  158. int err)
  159. {
  160. #if HAVE_THREADS
  161. pthread_mutex_lock(&mq->lock);
  162. mq->err_send = err;
  163. pthread_cond_broadcast(&mq->cond);
  164. pthread_mutex_unlock(&mq->lock);
  165. #endif /* HAVE_THREADS */
  166. }
  167. void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
  168. int err)
  169. {
  170. #if HAVE_THREADS
  171. pthread_mutex_lock(&mq->lock);
  172. mq->err_recv = err;
  173. pthread_cond_broadcast(&mq->cond);
  174. pthread_mutex_unlock(&mq->lock);
  175. #endif /* HAVE_THREADS */
  176. }
  177. static void free_func_wrap(void *arg, void *msg, int size)
  178. {
  179. AVThreadMessageQueue *mq = arg;
  180. mq->free_func(msg);
  181. }
  182. void av_thread_message_flush(AVThreadMessageQueue *mq)
  183. {
  184. #if HAVE_THREADS
  185. int used, off;
  186. void *free_func = mq->free_func;
  187. pthread_mutex_lock(&mq->lock);
  188. used = av_fifo_size(mq->fifo);
  189. if (free_func)
  190. for (off = 0; off < used; off += mq->elsize)
  191. av_fifo_generic_peek_at(mq->fifo, mq, off, mq->elsize, free_func_wrap);
  192. av_fifo_drain(mq->fifo, used);
  193. pthread_cond_broadcast(&mq->cond);
  194. pthread_mutex_unlock(&mq->lock);
  195. #endif /* HAVE_THREADS */
  196. }