#endif
/* threads */
-#ifdef __WIN32__
-#include <windows.h>
-#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 <pthread.h>
+#define USE_CONDITION_VAR
#elif defined(SYS_BEOS)
#include <kernel/OS.h>
#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
/****************************************************************************
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
#include "common.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:
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 );
}
#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
+
/* 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;
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
#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
}
# 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"
./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
int i_ref = i ? h->i_ref1 : h->i_ref0;
for( j=0; j<i_ref; j++ )
{
- // could use a condition variable or the like, but
- // this way is faster at least on LinuxThreads.
- while( fref[j]->i_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 );
}
}
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 );
}
}
{
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);
/* 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 )