/*****************************************************************************
* win32thread.c: windows threading
*****************************************************************************
- * Copyright (C) 2010-2011 x264 project
+ * Copyright (C) 2010-2014 x264 project
*
* Authors: Steven Walters <kemuri9@gmail.com>
* Pegasys Inc. <http://www.pegasys-inc.com>
static unsigned __stdcall x264_win32thread_worker( void *arg )
{
x264_pthread_t *h = arg;
- h->ret = h->func( h->arg );
+ *h->p_ret = h->func( h->arg );
return 0;
}
{
thread->func = start_routine;
thread->arg = arg;
+ thread->p_ret = &thread->ret;
+ thread->ret = NULL;
thread->handle = (void*)_beginthreadex( NULL, 0, x264_win32thread_worker, thread, 0, NULL );
return !thread->handle;
}
if( ret != WAIT_OBJECT_0 )
return -1;
if( value_ptr )
- *value_ptr = thread.ret;
+ *value_ptr = *thread.p_ret;
CloseHandle( thread.handle );
return 0;
}
{
x264_pthread_mutex_t mtx_broadcast;
x264_pthread_mutex_t mtx_waiter_count;
- int waiter_count;
+ volatile int waiter_count;
HANDLE semaphore;
HANDLE waiters_done;
- int is_broadcast;
+ volatile int is_broadcast;
} x264_win32_cond_t;
int x264_pthread_cond_init( x264_pthread_cond_t *cond, const x264_pthread_condattr_t *attr )
/* non-native condition variables */
x264_win32_cond_t *win32_cond = cond->ptr;
+
+ x264_pthread_mutex_lock( &win32_cond->mtx_broadcast );
x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
int have_waiter = win32_cond->waiter_count;
x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
if( have_waiter )
+ {
ReleaseSemaphore( win32_cond->semaphore, 1, NULL );
- return 0;
+ WaitForSingleObject( win32_cond->waiters_done, INFINITE );
+ }
+
+ return x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
}
int x264_pthread_cond_wait( x264_pthread_cond_t *cond, x264_pthread_mutex_t *mutex )
x264_win32_cond_t *win32_cond = cond->ptr;
x264_pthread_mutex_lock( &win32_cond->mtx_broadcast );
- x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
-
x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
win32_cond->waiter_count++;
x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
+ x264_pthread_mutex_unlock( &win32_cond->mtx_broadcast );
// unlock the external mutex
x264_pthread_mutex_unlock( mutex );
x264_pthread_mutex_lock( &win32_cond->mtx_waiter_count );
win32_cond->waiter_count--;
- int last_waiter = !win32_cond->waiter_count && win32_cond->is_broadcast;
+ int last_waiter = !win32_cond->waiter_count || !win32_cond->is_broadcast;
x264_pthread_mutex_unlock( &win32_cond->mtx_waiter_count );
if( last_waiter )
int x264_win32_threading_init( void )
{
/* find function pointers to API functions, if they exist */
- HANDLE kernel_dll = GetModuleHandle( TEXT( "kernel32.dll" ) );
+ HANDLE kernel_dll = GetModuleHandleW( L"kernel32.dll" );
thread_control.cond_init = (void*)GetProcAddress( kernel_dll, "InitializeConditionVariable" );
if( thread_control.cond_init )
{
memset( &thread_control, 0, sizeof(x264_win32thread_control_t) );
}
-int x264_pthread_num_processors_np()
+int x264_pthread_num_processors_np( void )
{
DWORD_PTR system_cpus, process_cpus = 0;
int cpus = 0;
* On platforms that support processor grouping, use GetThreadGroupAffinity to get the current thread's affinity instead. */
#if ARCH_X86_64
/* find function pointers to API functions specific to x86_64 platforms, if they exist */
- HANDLE kernel_dll = GetModuleHandle( TEXT( "kernel32.dll" ) );
+ HANDLE kernel_dll = GetModuleHandleW( L"kernel32.dll" );
BOOL (*get_thread_affinity)( HANDLE thread, x264_group_affinity_t *group_affinity ) = (void*)GetProcAddress( kernel_dll, "GetThreadGroupAffinity" );
if( get_thread_affinity )
{