beosthread.c 4.7 KB

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