beosthread.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2004 François Revol <revol@free.fr>
  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. */
  21. //#define DEBUG
  22. #include "avcodec.h"
  23. #include "common.h"
  24. #include <OS.h>
  25. typedef struct ThreadContext{
  26. AVCodecContext *avctx;
  27. thread_id thread;
  28. sem_id work_sem;
  29. sem_id done_sem;
  30. int (*func)(AVCodecContext *c, void *arg);
  31. void *arg;
  32. int ret;
  33. }ThreadContext;
  34. // it's odd Be never patented that :D
  35. struct benaphore {
  36. vint32 atom;
  37. sem_id sem;
  38. };
  39. static inline int lock_ben(struct benaphore *ben)
  40. {
  41. if (atomic_add(&ben->atom, 1) > 0)
  42. return acquire_sem(ben->sem);
  43. return B_OK;
  44. }
  45. static inline int unlock_ben(struct benaphore *ben)
  46. {
  47. if (atomic_add(&ben->atom, -1) > 1)
  48. return release_sem(ben->sem);
  49. return B_OK;
  50. }
  51. static struct benaphore av_thread_lib_ben;
  52. static int32 ff_thread_func(void *v){
  53. ThreadContext *c= v;
  54. for(;;){
  55. //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  56. acquire_sem(c->work_sem);
  57. //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  58. if(c->func)
  59. c->ret= c->func(c->avctx, c->arg);
  60. else
  61. return 0;
  62. //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  63. release_sem(c->done_sem);
  64. }
  65. return B_OK;
  66. }
  67. /**
  68. * free what has been allocated by avcodec_thread_init().
  69. * must be called after decoding has finished, especially dont call while avcodec_thread_execute() is running
  70. */
  71. void avcodec_thread_free(AVCodecContext *s){
  72. ThreadContext *c= s->thread_opaque;
  73. int i;
  74. int32 ret;
  75. for(i=0; i<s->thread_count; i++){
  76. c[i].func= NULL;
  77. release_sem(c[i].work_sem);
  78. wait_for_thread(c[i].thread, &ret);
  79. if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
  80. if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
  81. }
  82. av_freep(&s->thread_opaque);
  83. }
  84. int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void **arg, int *ret, int count){
  85. ThreadContext *c= s->thread_opaque;
  86. int i;
  87. assert(s == c->avctx);
  88. assert(count <= s->thread_count);
  89. /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  90. for(i=0; i<count; i++){
  91. c[i].arg= arg[i];
  92. c[i].func= func;
  93. c[i].ret= 12345;
  94. release_sem(c[i].work_sem);
  95. }
  96. for(i=0; i<count; i++){
  97. acquire_sem(c[i].done_sem);
  98. c[i].func= NULL;
  99. if(ret) ret[i]= c[i].ret;
  100. }
  101. return 0;
  102. }
  103. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  104. int i;
  105. ThreadContext *c;
  106. s->thread_count= thread_count;
  107. assert(!s->thread_opaque);
  108. c= av_mallocz(sizeof(ThreadContext)*thread_count);
  109. s->thread_opaque= c;
  110. for(i=0; i<thread_count; i++){
  111. //printf("init semaphors %d\n", i); fflush(stdout);
  112. c[i].avctx= s;
  113. if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
  114. goto fail;
  115. if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
  116. goto fail;
  117. //printf("create thread %d\n", i); fflush(stdout);
  118. c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
  119. if( c[i].thread < B_OK ) goto fail;
  120. resume_thread(c[i].thread );
  121. }
  122. //printf("init done\n"); fflush(stdout);
  123. s->execute= avcodec_thread_execute;
  124. return 0;
  125. fail:
  126. avcodec_thread_free(s);
  127. return -1;
  128. }
  129. /* provide a mean to serialize calls to avcodec_*() for thread safety. */
  130. int avcodec_thread_lock_lib(void)
  131. {
  132. return lock_ben(&av_thread_lib_ben);
  133. }
  134. int avcodec_thread_unlock_lib(void)
  135. {
  136. return unlock_ben(&av_thread_lib_ben);
  137. }
  138. /* our versions of _init and _fini (which are called by those actually from crt.o) */
  139. void initialize_after(void)
  140. {
  141. av_thread_lib_ben.atom = 0;
  142. av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
  143. }
  144. void uninitialize_before(void)
  145. {
  146. delete_sem(av_thread_lib_ben.sem);
  147. }