w32thread.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. //#define DEBUG
  21. #include "avcodec.h"
  22. #include "thread.h"
  23. #define WIN32_LEAN_AND_MEAN
  24. #include <windows.h>
  25. #include <process.h>
  26. typedef struct ThreadContext{
  27. AVCodecContext *avctx;
  28. HANDLE thread;
  29. HANDLE work_sem;
  30. HANDLE job_sem;
  31. HANDLE done_sem;
  32. int (*func)(AVCodecContext *c, void *arg);
  33. int (*func2)(AVCodecContext *c, void *arg, int, int);
  34. void *arg;
  35. int argsize;
  36. int *jobnr;
  37. int *ret;
  38. int threadnr;
  39. }ThreadContext;
  40. static unsigned WINAPI attribute_align_arg thread_func(void *v){
  41. ThreadContext *c= v;
  42. for(;;){
  43. int ret, jobnr;
  44. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  45. WaitForSingleObject(c->work_sem, INFINITE);
  46. // avoid trying to access jobnr if we should quit
  47. if (!c->func && !c->func2)
  48. break;
  49. WaitForSingleObject(c->job_sem, INFINITE);
  50. jobnr = (*c->jobnr)++;
  51. ReleaseSemaphore(c->job_sem, 1, 0);
  52. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  53. if(c->func)
  54. ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
  55. else
  56. ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
  57. if (c->ret)
  58. c->ret[jobnr] = ret;
  59. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  60. ReleaseSemaphore(c->done_sem, 1, 0);
  61. }
  62. return 0;
  63. }
  64. /**
  65. * Free what has been allocated by ff_thread_init().
  66. * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
  67. */
  68. void ff_thread_free(AVCodecContext *s){
  69. ThreadContext *c= s->thread_opaque;
  70. int i;
  71. for(i=0; i<s->thread_count; i++){
  72. c[i].func= NULL;
  73. c[i].func2= NULL;
  74. }
  75. ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
  76. for(i=0; i<s->thread_count; i++){
  77. WaitForSingleObject(c[i].thread, INFINITE);
  78. if(c[i].thread) CloseHandle(c[i].thread);
  79. }
  80. if(c[0].work_sem) CloseHandle(c[0].work_sem);
  81. if(c[0].job_sem) CloseHandle(c[0].job_sem);
  82. if(c[0].done_sem) CloseHandle(c[0].done_sem);
  83. av_freep(&s->thread_opaque);
  84. }
  85. static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  86. ThreadContext *c= s->thread_opaque;
  87. int i;
  88. int jobnr = 0;
  89. assert(s == c->avctx);
  90. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  91. for(i=0; i<s->thread_count; i++){
  92. c[i].arg= arg;
  93. c[i].argsize= size;
  94. c[i].func= func;
  95. c[i].ret= ret;
  96. c[i].jobnr = &jobnr;
  97. }
  98. ReleaseSemaphore(c[0].work_sem, count, 0);
  99. for(i=0; i<count; i++)
  100. WaitForSingleObject(c[0].done_sem, INFINITE);
  101. return 0;
  102. }
  103. static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
  104. ThreadContext *c= s->thread_opaque;
  105. int i;
  106. for(i=0; i<s->thread_count; i++)
  107. c[i].func2 = func;
  108. avcodec_thread_execute(s, NULL, arg, ret, count, 0);
  109. }
  110. int ff_thread_init(AVCodecContext *s){
  111. int i;
  112. ThreadContext *c;
  113. uint32_t threadid;
  114. if(!(s->thread_type & FF_THREAD_SLICE)){
  115. av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
  116. return 0;
  117. }
  118. s->active_thread_type= FF_THREAD_SLICE;
  119. if (s->thread_count <= 1)
  120. return 0;
  121. assert(!s->thread_opaque);
  122. c= av_mallocz(sizeof(ThreadContext)*s->thread_count);
  123. s->thread_opaque= c;
  124. if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  125. goto fail;
  126. if(!(c[0].job_sem = CreateSemaphore(NULL, 1, 1, NULL)))
  127. goto fail;
  128. if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  129. goto fail;
  130. for(i=0; i<s->thread_count; i++){
  131. //printf("init semaphors %d\n", i); fflush(stdout);
  132. c[i].avctx= s;
  133. c[i].work_sem = c[0].work_sem;
  134. c[i].job_sem = c[0].job_sem;
  135. c[i].done_sem = c[0].done_sem;
  136. c[i].threadnr = i;
  137. //printf("create thread %d\n", i); fflush(stdout);
  138. c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  139. if( !c[i].thread ) goto fail;
  140. }
  141. //printf("init done\n"); fflush(stdout);
  142. s->execute= avcodec_thread_execute;
  143. s->execute2= avcodec_thread_execute2;
  144. return 0;
  145. fail:
  146. ff_thread_free(s);
  147. return -1;
  148. }