pthread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2004 Roman Shaposhnik
  3. *
  4. * Many thanks to Steven M. Schultz for providing clever ideas and
  5. * to Michael Niedermayer <michaelni@gmx.at> for writing initial
  6. * implementation.
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <pthread.h>
  25. #include "avcodec.h"
  26. typedef int (action_func)(AVCodecContext *c, void *arg);
  27. typedef struct ThreadContext {
  28. pthread_t *workers;
  29. action_func *func;
  30. void *args;
  31. int *rets;
  32. int rets_count;
  33. int job_count;
  34. int job_size;
  35. pthread_cond_t last_job_cond;
  36. pthread_cond_t current_job_cond;
  37. pthread_mutex_t current_job_lock;
  38. int current_job;
  39. int done;
  40. } ThreadContext;
  41. static void* attribute_align_arg worker(void *v)
  42. {
  43. AVCodecContext *avctx = v;
  44. ThreadContext *c = avctx->thread_opaque;
  45. int our_job = c->job_count;
  46. int thread_count = avctx->thread_count;
  47. int self_id;
  48. pthread_mutex_lock(&c->current_job_lock);
  49. self_id = c->current_job++;
  50. for (;;){
  51. while (our_job >= c->job_count) {
  52. if (c->current_job == thread_count + c->job_count)
  53. pthread_cond_signal(&c->last_job_cond);
  54. pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
  55. our_job = self_id;
  56. if (c->done) {
  57. pthread_mutex_unlock(&c->current_job_lock);
  58. return NULL;
  59. }
  60. }
  61. pthread_mutex_unlock(&c->current_job_lock);
  62. c->rets[our_job%c->rets_count] = c->func(avctx, (char*)c->args + our_job*c->job_size);
  63. pthread_mutex_lock(&c->current_job_lock);
  64. our_job = c->current_job++;
  65. }
  66. }
  67. static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
  68. {
  69. pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
  70. pthread_mutex_unlock(&c->current_job_lock);
  71. }
  72. void avcodec_thread_free(AVCodecContext *avctx)
  73. {
  74. ThreadContext *c = avctx->thread_opaque;
  75. int i;
  76. pthread_mutex_lock(&c->current_job_lock);
  77. c->done = 1;
  78. pthread_cond_broadcast(&c->current_job_cond);
  79. pthread_mutex_unlock(&c->current_job_lock);
  80. for (i=0; i<avctx->thread_count; i++)
  81. pthread_join(c->workers[i], NULL);
  82. pthread_mutex_destroy(&c->current_job_lock);
  83. pthread_cond_destroy(&c->current_job_cond);
  84. pthread_cond_destroy(&c->last_job_cond);
  85. av_free(c->workers);
  86. av_freep(&avctx->thread_opaque);
  87. }
  88. int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
  89. {
  90. ThreadContext *c= avctx->thread_opaque;
  91. int dummy_ret;
  92. if (job_count <= 0)
  93. return 0;
  94. pthread_mutex_lock(&c->current_job_lock);
  95. c->current_job = avctx->thread_count;
  96. c->job_count = job_count;
  97. c->job_size = job_size;
  98. c->args = arg;
  99. c->func = func;
  100. if (ret) {
  101. c->rets = ret;
  102. c->rets_count = job_count;
  103. } else {
  104. c->rets = &dummy_ret;
  105. c->rets_count = 1;
  106. }
  107. pthread_cond_broadcast(&c->current_job_cond);
  108. avcodec_thread_park_workers(c, avctx->thread_count);
  109. return 0;
  110. }
  111. int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  112. {
  113. int i;
  114. ThreadContext *c;
  115. c = av_mallocz(sizeof(ThreadContext));
  116. if (!c)
  117. return -1;
  118. c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
  119. if (!c->workers) {
  120. av_free(c);
  121. return -1;
  122. }
  123. avctx->thread_opaque = c;
  124. avctx->thread_count = thread_count;
  125. c->current_job = 0;
  126. c->job_count = 0;
  127. c->job_size = 0;
  128. c->done = 0;
  129. pthread_cond_init(&c->current_job_cond, NULL);
  130. pthread_cond_init(&c->last_job_cond, NULL);
  131. pthread_mutex_init(&c->current_job_lock, NULL);
  132. pthread_mutex_lock(&c->current_job_lock);
  133. for (i=0; i<thread_count; i++) {
  134. if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
  135. avctx->thread_count = i;
  136. pthread_mutex_unlock(&c->current_job_lock);
  137. avcodec_thread_free(avctx);
  138. return -1;
  139. }
  140. }
  141. avcodec_thread_park_workers(c, thread_count);
  142. avctx->execute = avcodec_thread_execute;
  143. return 0;
  144. }