threadpool.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*****************************************************************************
  2. * threadpool.c: thread pooling
  3. *****************************************************************************
  4. * Copyright (C) 2010-2018 x264 project
  5. *
  6. * Authors: Steven Walters <kemuri9@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  21. *
  22. * This program is also available under a commercial proprietary license.
  23. * For more information, contact us at licensing@x264.com.
  24. *****************************************************************************/
  25. #include "common.h"
  26. typedef struct
  27. {
  28. void *(*func)(void *);
  29. void *arg;
  30. void *ret;
  31. } x264_threadpool_job_t;
  32. struct x264_threadpool_t
  33. {
  34. volatile int exit;
  35. int threads;
  36. x264_pthread_t *thread_handle;
  37. void (*init_func)(void *);
  38. void *init_arg;
  39. /* requires a synchronized list structure and associated methods,
  40. so use what is already implemented for frames */
  41. x264_sync_frame_list_t uninit; /* list of jobs that are awaiting use */
  42. x264_sync_frame_list_t run; /* list of jobs that are queued for processing by the pool */
  43. x264_sync_frame_list_t done; /* list of jobs that have finished processing */
  44. };
  45. static void *threadpool_thread_internal( x264_threadpool_t *pool )
  46. {
  47. if( pool->init_func )
  48. pool->init_func( pool->init_arg );
  49. while( !pool->exit )
  50. {
  51. x264_threadpool_job_t *job = NULL;
  52. x264_pthread_mutex_lock( &pool->run.mutex );
  53. while( !pool->exit && !pool->run.i_size )
  54. x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex );
  55. if( pool->run.i_size )
  56. {
  57. job = (void*)x264_frame_shift( pool->run.list );
  58. pool->run.i_size--;
  59. }
  60. x264_pthread_mutex_unlock( &pool->run.mutex );
  61. if( !job )
  62. continue;
  63. job->ret = job->func( job->arg );
  64. x264_sync_frame_list_push( &pool->done, (void*)job );
  65. }
  66. return NULL;
  67. }
  68. static void *threadpool_thread( x264_threadpool_t *pool )
  69. {
  70. return (void*)x264_stack_align( threadpool_thread_internal, pool );
  71. }
  72. int x264_threadpool_init( x264_threadpool_t **p_pool, int threads,
  73. void (*init_func)(void *), void *init_arg )
  74. {
  75. if( threads <= 0 )
  76. return -1;
  77. if( x264_threading_init() < 0 )
  78. return -1;
  79. x264_threadpool_t *pool;
  80. CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
  81. *p_pool = pool;
  82. pool->init_func = init_func;
  83. pool->init_arg = init_arg;
  84. pool->threads = threads;
  85. CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
  86. if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
  87. x264_sync_frame_list_init( &pool->run, pool->threads ) ||
  88. x264_sync_frame_list_init( &pool->done, pool->threads ) )
  89. goto fail;
  90. for( int i = 0; i < pool->threads; i++ )
  91. {
  92. x264_threadpool_job_t *job;
  93. CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
  94. x264_sync_frame_list_push( &pool->uninit, (void*)job );
  95. }
  96. for( int i = 0; i < pool->threads; i++ )
  97. if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)threadpool_thread, pool ) )
  98. goto fail;
  99. return 0;
  100. fail:
  101. return -1;
  102. }
  103. void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg )
  104. {
  105. x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
  106. job->func = func;
  107. job->arg = arg;
  108. x264_sync_frame_list_push( &pool->run, (void*)job );
  109. }
  110. void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg )
  111. {
  112. x264_pthread_mutex_lock( &pool->done.mutex );
  113. while( 1 )
  114. {
  115. for( int i = 0; i < pool->done.i_size; i++ )
  116. if( ((x264_threadpool_job_t*)pool->done.list[i])->arg == arg )
  117. {
  118. x264_threadpool_job_t *job = (void*)x264_frame_shift( pool->done.list+i );
  119. pool->done.i_size--;
  120. x264_pthread_mutex_unlock( &pool->done.mutex );
  121. void *ret = job->ret;
  122. x264_sync_frame_list_push( &pool->uninit, (void*)job );
  123. return ret;
  124. }
  125. x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
  126. }
  127. }
  128. static void threadpool_list_delete( x264_sync_frame_list_t *slist )
  129. {
  130. for( int i = 0; slist->list[i]; i++ )
  131. {
  132. x264_free( slist->list[i] );
  133. slist->list[i] = NULL;
  134. }
  135. x264_sync_frame_list_delete( slist );
  136. }
  137. void x264_threadpool_delete( x264_threadpool_t *pool )
  138. {
  139. x264_pthread_mutex_lock( &pool->run.mutex );
  140. pool->exit = 1;
  141. x264_pthread_cond_broadcast( &pool->run.cv_fill );
  142. x264_pthread_mutex_unlock( &pool->run.mutex );
  143. for( int i = 0; i < pool->threads; i++ )
  144. x264_pthread_join( pool->thread_handle[i], NULL );
  145. threadpool_list_delete( &pool->uninit );
  146. threadpool_list_delete( &pool->run );
  147. threadpool_list_delete( &pool->done );
  148. x264_free( pool->thread_handle );
  149. x264_free( pool );
  150. }