w32thread.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  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
  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. * 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 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 FFmpeg; 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. #define WIN32_LEAN_AND_MEAN
  23. #include <windows.h>
  24. #include <process.h>
  25. typedef struct ThreadContext{
  26. AVCodecContext *avctx;
  27. HANDLE thread;
  28. HANDLE work_sem;
  29. HANDLE done_sem;
  30. int (*func)(AVCodecContext *c, void *arg);
  31. void *arg;
  32. int ret;
  33. }ThreadContext;
  34. static unsigned WINAPI attribute_align_arg thread_func(void *v){
  35. ThreadContext *c= v;
  36. for(;;){
  37. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  38. WaitForSingleObject(c->work_sem, INFINITE);
  39. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  40. if(c->func)
  41. c->ret= c->func(c->avctx, c->arg);
  42. else
  43. return 0;
  44. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  45. ReleaseSemaphore(c->done_sem, 1, 0);
  46. }
  47. return 0;
  48. }
  49. /**
  50. * Free what has been allocated by avcodec_thread_init().
  51. * Must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running.
  52. */
  53. void avcodec_thread_free(AVCodecContext *s){
  54. ThreadContext *c= s->thread_opaque;
  55. int i;
  56. for(i=0; i<s->thread_count; i++){
  57. c[i].func= NULL;
  58. ReleaseSemaphore(c[i].work_sem, 1, 0);
  59. WaitForSingleObject(c[i].thread, INFINITE);
  60. if(c[i].work_sem) CloseHandle(c[i].work_sem);
  61. if(c[i].done_sem) CloseHandle(c[i].done_sem);
  62. }
  63. av_freep(&s->thread_opaque);
  64. }
  65. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  66. ThreadContext *c= s->thread_opaque;
  67. int i;
  68. assert(s == c->avctx);
  69. assert(count <= s->thread_count);
  70. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  71. for(i=0; i<count; i++){
  72. c[i].arg= (char*)arg + i*size;
  73. c[i].func= func;
  74. c[i].ret= 12345;
  75. ReleaseSemaphore(c[i].work_sem, 1, 0);
  76. }
  77. for(i=0; i<count; i++){
  78. WaitForSingleObject(c[i].done_sem, INFINITE);
  79. c[i].func= NULL;
  80. if(ret) ret[i]= c[i].ret;
  81. }
  82. return 0;
  83. }
  84. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  85. int i;
  86. ThreadContext *c;
  87. uint32_t threadid;
  88. s->thread_count= thread_count;
  89. assert(!s->thread_opaque);
  90. c= av_mallocz(sizeof(ThreadContext)*thread_count);
  91. s->thread_opaque= c;
  92. for(i=0; i<thread_count; i++){
  93. //printf("init semaphors %d\n", i); fflush(stdout);
  94. c[i].avctx= s;
  95. if(!(c[i].work_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
  96. goto fail;
  97. if(!(c[i].done_sem = CreateSemaphore(NULL, 0, s->thread_count, NULL)))
  98. goto fail;
  99. //printf("create thread %d\n", i); fflush(stdout);
  100. c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  101. if( !c[i].thread ) goto fail;
  102. }
  103. //printf("init done\n"); fflush(stdout);
  104. s->execute= avcodec_thread_execute;
  105. return 0;
  106. fail:
  107. avcodec_thread_free(s);
  108. return -1;
  109. }