os2thread.c 4.3 KB

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