]> git.sesse.net Git - x264/blobdiff - common/win32thread.c
x86: AVX2 high bit-depth pixel_ssd
[x264] / common / win32thread.c
index 2d981d358d7bdab085a81b5c3011eb4be9e4db9e..4be914f19b7593c229c3ecadf468169e22157ef0 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * win32thread.c: windows threading
  *****************************************************************************
- * Copyright (C) 2010-2011 x264 project
+ * Copyright (C) 2010-2013 x264 project
  *
  * Authors: Steven Walters <kemuri9@gmail.com>
  *          Pegasys Inc. <http://www.pegasys-inc.com>
@@ -62,7 +62,7 @@ static x264_win32thread_control_t thread_control;
 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;
 }
 
@@ -71,6 +71,8 @@ int x264_pthread_create( x264_pthread_t *thread, const x264_pthread_attr_t *attr
 {
     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;
 }
@@ -81,7 +83,7 @@ int x264_pthread_join( x264_pthread_t thread, void **value_ptr )
     if( ret != WAIT_OBJECT_0 )
         return -1;
     if( value_ptr )
-        *value_ptr = thread.ret;
+        *value_ptr = *thread.p_ret;
     CloseHandle( thread.handle );
     return 0;
 }
@@ -117,10 +119,10 @@ typedef struct
 {
     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 )
@@ -211,13 +213,19 @@ int x264_pthread_cond_signal( x264_pthread_cond_t *cond )
 
     /* 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 )
@@ -229,11 +237,10 @@ int x264_pthread_cond_wait( x264_pthread_cond_t *cond, x264_pthread_mutex_t *mut
     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 );
@@ -241,7 +248,7 @@ int x264_pthread_cond_wait( x264_pthread_cond_t *cond, x264_pthread_mutex_t *mut
 
     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 )
@@ -272,7 +279,7 @@ void x264_win32_threading_destroy( void )
     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;