thread_queue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef FFTOOLS_THREAD_QUEUE_H
  19. #define FFTOOLS_THREAD_QUEUE_H
  20. #include <string.h>
  21. #include "objpool.h"
  22. typedef struct ThreadQueue ThreadQueue;
  23. /**
  24. * Allocate a queue for sending data between threads.
  25. *
  26. * @param nb_streams number of streams for which a distinct EOF state is
  27. * maintained
  28. * @param queue_size number of items that can be stored in the queue without
  29. * blocking
  30. * @param obj_pool object pool that will be used to allocate items stored in the
  31. * queue; the pool becomes owned by the queue
  32. * @param callback that moves the contents between two data pointers
  33. */
  34. ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
  35. ObjPool *obj_pool, void (*obj_move)(void *dst, void *src));
  36. void tq_free(ThreadQueue **tq);
  37. /**
  38. * Send an item for the given stream to the queue.
  39. *
  40. * @param data the item to send, its contents will be moved using the callback
  41. * provided to tq_alloc(); on failure the item will be left
  42. * untouched
  43. * @return
  44. * - 0 the item was successfully sent
  45. * - AVERROR(ENOMEM) could not allocate an item for writing to the FIFO
  46. * - AVERROR(EINVAL) the sending side has previously been marked as finished
  47. * - AVERROR_EOF the receiving side has marked the given stream as finished
  48. */
  49. int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data);
  50. /**
  51. * Mark the given stream finished from the sending side.
  52. */
  53. void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx);
  54. /**
  55. * Read the next item from the queue.
  56. *
  57. * @param stream_idx the index of the stream that was processed or -1 will be
  58. * written here
  59. * @param data the data item will be written here on success using the
  60. * callback provided to tq_alloc()
  61. * @return
  62. * - 0 a data item was successfully read; *stream_idx contains a non-negative
  63. * stream index
  64. * - AVERROR_EOF When *stream_idx is non-negative, this signals that the sending
  65. * side has marked the given stream as finished. This will happen at most once
  66. * for each stream. When *stream_idx is -1, all streams are done.
  67. */
  68. int tq_receive(ThreadQueue *tq, int *stream_idx, void *data);
  69. /**
  70. * Mark the given stream finished from the receiving side.
  71. */
  72. void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx);
  73. #endif // FFTOOLS_THREAD_QUEUE_H