From: Loren Merritt Date: Fri, 15 Dec 2006 23:06:21 +0000 (+0000) Subject: win32: use pthreads instead of win32threads. for some reason, pthreads is much faster. X-Git-Url: http://www.zmx.ovh/git.videolan.org/?a=commitdiff_plain;h=87f1430384a1c61035a4ca595e5defecf4b64cb8;p=x264.git win32: use pthreads instead of win32threads. for some reason, pthreads is much faster. git-svn-id: svn://svn.videolan.org/x264/trunk@608 df754926-b1dd-0310-bc7b-ec298dee348c --- diff --git a/common/common.h b/common/common.h index a166a831..d69d894b 100644 --- a/common/common.h +++ b/common/common.h @@ -46,14 +46,9 @@ #endif /* threads */ -#ifdef __WIN32__ -#include -#define pthread_t HANDLE -#define pthread_create(t,u,f,d) *(t)=CreateThread(NULL,0,f,d,0,NULL) -#define pthread_join(t,s) { WaitForSingleObject(t,INFINITE); \ - CloseHandle(t); } -#define usleep(t) Sleep((t+999)/1000); -#define HAVE_PTHREAD 1 +#if defined(__WIN32__) && defined(HAVE_PTHREAD) +#include +#define USE_CONDITION_VAR #elif defined(SYS_BEOS) #include @@ -73,6 +68,19 @@ #define pthread_t int #define pthread_create(t,u,f,d) #define pthread_join(t,s) +#endif //SYS_* + +#ifndef USE_CONDITION_VAR +#define pthread_mutex_t int +#define pthread_mutex_init(m,f) +#define pthread_mutex_destroy(m) +#define pthread_mutex_lock(m) +#define pthread_mutex_unlock(m) +#define pthread_cond_t int +#define pthread_cond_init(c,f) +#define pthread_cond_destroy(c) +#define pthread_cond_broadcast(c) +#define pthread_cond_wait(c,m) usleep(100) #endif /**************************************************************************** diff --git a/common/frame.c b/common/frame.c index ea280d48..6b173f3a 100644 --- a/common/frame.c +++ b/common/frame.c @@ -23,6 +23,7 @@ #include #include +#include #include "common.h" @@ -131,6 +132,9 @@ x264_frame_t *x264_frame_new( x264_t *h ) for( j = 0; j < h->param.i_bframe + 2; j++ ) CHECKED_MALLOC( frame->i_row_satds[i][j], i_lines/16 * sizeof(int) ); + pthread_mutex_init( &frame->mutex, NULL ); + pthread_cond_init( &frame->cv, NULL ); + return frame; fail: @@ -155,6 +159,8 @@ void x264_frame_delete( x264_frame_t *frame ) x264_free( frame->mv[1] ); x264_free( frame->ref[0] ); x264_free( frame->ref[1] ); + pthread_mutex_destroy( &frame->mutex ); + pthread_cond_destroy( &frame->cv ); x264_free( frame ); } @@ -751,3 +757,32 @@ void x264_deblock_init( int cpu, x264_deblock_function_t *pf ) #endif } + +/* threading */ + +#ifdef HAVE_PTHREAD +void x264_frame_cond_broadcast( x264_frame_t *frame ) +{ + pthread_mutex_lock( &frame->mutex ); + pthread_cond_broadcast( &frame->cv ); + pthread_mutex_unlock( &frame->mutex ); +} + +void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed ) +{ + if( frame->i_lines_completed < i_lines_completed ) + { + pthread_mutex_lock( &frame->mutex ); + while( frame->i_lines_completed < i_lines_completed ) + pthread_cond_wait( &frame->cv, &frame->mutex ); + pthread_mutex_unlock( &frame->mutex ); + } +} + +#else +void x264_frame_cond_broadcast( x264_frame_t *frame ) +{} +void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed ) +{} +#endif + diff --git a/common/frame.h b/common/frame.h index 09fece18..638ecab2 100644 --- a/common/frame.h +++ b/common/frame.h @@ -73,6 +73,8 @@ typedef struct /* threading */ int i_lines_completed; /* in pixels */ int i_reference_count; /* number of threads using this frame (not necessarily the number of pointers) */ + pthread_mutex_t mutex; + pthread_cond_t cv; } x264_frame_t; @@ -108,4 +110,7 @@ void x264_frame_init_lowres( int cpu, x264_frame_t *frame ); void x264_deblock_init( int cpu, x264_deblock_function_t *pf ); +void x264_frame_cond_broadcast( x264_frame_t *frame ); +void x264_frame_cond_wait( x264_frame_t *frame, int i_lines_completed ); + #endif diff --git a/configure b/configure index 41c48bca..8f43eda4 100755 --- a/configure +++ b/configure @@ -29,9 +29,9 @@ cc_check() { #include <$1> int main () { $3 return 0; } EOF - $CC $CFLAGS $LDFLAGS $2 conftest.c -o conftest 2>$DEVNULL + $CC conftest.c $CFLAGS $LDFLAGS $2 -o conftest 2>$DEVNULL TMP="$?" - rm -f conftest.c conftest* + rm -f conftest* return $TMP } @@ -267,25 +267,34 @@ done # autodetect options that weren't forced nor disabled +libpthread="" if test "$pthread" = "auto" ; then + pthread="no" case $SYS in - MINGW|CYGWIN|BEOS) + BEOS) pthread="yes" ;; + MINGW|CYGWIN) + if cc_check pthread.h -lpthread "pthread_create(0,0,0,0);" ; then + pthread="yes" + libpthread="-lpthread" + elif cc_check pthread.h -lpthreadGC2 "pthread_create(0,0,0,0);" ; then + pthread="yes" + libpthread="-lpthreadGC2" + elif cc_check pthread.h "-lpthreadGC2 -lwsock32 -DPTW32_STATIC_LIB" "pthread_create(0,0,0,0);" ; then + pthread="yes" + libpthread="-lpthreadGC2 -lwsock32"\ + CFLAGS="$CFLAGS -DPTW32_STATIC_LIB" + fi + ;; *) - pthread="no" - cc_check pthread.h -lpthread && pthread="yes" + cc_check pthread.h -lpthread && pthread="yes" && libpthread="-lpthread" ;; esac fi if test "$pthread" = "yes" ; then CFLAGS="$CFLAGS -DHAVE_PTHREAD" - case $SYS in - MINGW|CYGWIN|BEOS) - ;; - *) LDFLAGS="$LDFLAGS -lpthread" - ;; - esac + LDFLAGS="$LDFLAGS $libpthread" fi MP4_LDFLAGS="-lgpac_static" @@ -383,15 +392,7 @@ fi ./version.sh -pclibs="-L$libdir -lx264" -if test "$pthread" = "yes" ; then - case $SYS in - MINGW|CYGWIN|BEOS) - ;; - *) pclibs="$pclibs -lpthread" - ;; - esac -fi +pclibs="-L$libdir -lx264 $libpthread" cat > x264.pc << EOF prefix=$prefix diff --git a/encoder/analyse.c b/encoder/analyse.c index e854da8c..e2ddf30f 100644 --- a/encoder/analyse.c +++ b/encoder/analyse.c @@ -248,10 +248,7 @@ static void x264_mb_analyse_init( x264_t *h, x264_mb_analysis_t *a, int i_qp ) int i_ref = i ? h->i_ref1 : h->i_ref0; for( j=0; ji_lines_completed < thresh ) - usleep(100); + x264_frame_cond_wait( fref[j], thresh ); thread_mvy_range = X264_MIN( thread_mvy_range, fref[j]->i_lines_completed - pix_y ); } } diff --git a/encoder/encoder.c b/encoder/encoder.c index 84aa9325..3edf1c99 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -997,8 +997,8 @@ static void x264_fdec_filter_row( x264_t *h, int mb_y ) if( h->param.i_threads > 1 ) { - /* this must be an atomic store. a 32bit int should be so on sane architectures. */ h->fdec->i_lines_completed = mb_y*16 + (b_end ? 10000 : -(X264_THREAD_HEIGHT << h->sh.b_mbaff)); + x264_frame_cond_broadcast( h->fdec ); } } diff --git a/x264.c b/x264.c index cf194ef9..7718a066 100644 --- a/x264.c +++ b/x264.c @@ -92,6 +92,12 @@ int main( int argc, char **argv ) { x264_param_t param; cli_opt_t opt; + int ret; + +#ifdef PTW32_STATIC_LIB + pthread_win32_process_attach_np(); + pthread_win32_thread_attach_np(); +#endif #ifdef _MSC_VER _setmode(_fileno(stdin), _O_BINARY); @@ -107,7 +113,14 @@ int main( int argc, char **argv ) /* Control-C handler */ signal( SIGINT, SigIntHandler ); - return Encode( ¶m, &opt ); + ret = Encode( ¶m, &opt ); + +#ifdef PTW32_STATIC_LIB + pthread_win32_thread_detach_np(); + pthread_win32_process_detach_np(); +#endif + + return ret; } static char const *strtable_lookup( const char * const table[], int index )