[vlc-devel] [PATCH 2/3] win32: window: split window thread creation/destroy

Thomas Guillem thomas at gllm.fr
Mon May 6 12:07:07 CEST 2019


---
 modules/video_output/win32/window.c | 78 +++++++++++++++++++----------
 1 file changed, 51 insertions(+), 27 deletions(-)

diff --git a/modules/video_output/win32/window.c b/modules/video_output/win32/window.c
index bb0b117cee..87d8612d0c 100644
--- a/modules/video_output/win32/window.c
+++ b/modules/video_output/win32/window.c
@@ -29,6 +29,7 @@
 #endif
 
 #include <stdarg.h>
+#include <assert.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -46,6 +47,8 @@
 
 #define IDM_TOGGLE_ON_TOP  (WM_USER + 1)
 
+static void *EventThread( void *p_this );
+
 typedef struct vout_window_sys_t
 {
     vlc_thread_t thread;
@@ -97,6 +100,48 @@ static void Resize(vout_window_t *wnd, unsigned width, unsigned height)
                  SWP_NOZORDER|SWP_NOMOVE);
 }
 
+static void DestroyWindowThreadLocked(vout_window_t *wnd)
+{
+    vout_window_sys_t *sys = wnd->sys;
+
+    if( sys->hwnd )
+    {
+        PostMessage( sys->hwnd, WM_CLOSE, 0, 0 );
+        /* wait until the thread is done */
+        while( !sys->b_done )
+            vlc_cond_wait( &sys->wait, &sys->lock );
+
+        DestroyWindow( sys->hwnd );
+        sys->hwnd = NULL;
+    }
+    if( sys->b_ready )
+    {
+        vlc_join(sys->thread, NULL);
+        sys->b_ready = false;
+    }
+    sys->b_done = false;
+}
+
+static int CreateWindowThreadLocked(vout_window_t *wnd)
+{
+    vout_window_sys_t *sys = wnd->sys;
+    assert( !sys->hwnd );
+
+    if( vlc_clone( &sys->thread, EventThread, wnd, VLC_THREAD_PRIORITY_LOW ) )
+        return VLC_EGENERIC;
+
+    while( !sys->b_ready )
+        vlc_cond_wait( &sys->wait, &sys->lock );
+    if (sys->hwnd == NULL)
+    {
+        DestroyWindowThreadLocked(wnd);
+        return VLC_EGENERIC;
+    }
+    wnd->handle.hwnd = sys->hwnd;
+
+    return VLC_SUCCESS;
+}
+
 static int Enable(vout_window_t *wnd, const vout_window_cfg_t *cfg)
 {
     vout_window_sys_t *sys = wnd->sys;
@@ -482,20 +527,11 @@ static void Close(vout_window_t *wnd)
     vout_window_sys_t *sys = wnd->sys;
 
     free( sys->psz_title );
-    if (sys->hwnd)
-    {
-        PostMessage( sys->hwnd, WM_CLOSE, 0, 0 );
-        /* wait until the thread is done */
-        vlc_mutex_lock( &sys->lock );
-        while( !sys->b_done )
-        {
-            vlc_cond_wait( &sys->wait, &sys->lock );
-        }
-        vlc_mutex_unlock( &sys->lock );
 
-        DestroyWindow( sys->hwnd );
-    }
-    vlc_join(sys->thread, NULL);
+    vlc_mutex_lock( &sys->lock );
+    DestroyWindowThreadLocked( wnd );
+    vlc_mutex_unlock( &sys->lock );
+
     vlc_mutex_destroy( &sys->lock );
     vlc_cond_destroy( &sys->wait );
 
@@ -752,20 +788,10 @@ static int Open(vout_window_t *wnd)
     vlc_cond_init( &sys->wait );
     sys->b_ready = false;
     sys->b_done = false;
-
-    wnd->sys = sys;
-    if( vlc_clone( &sys->thread, EventThread, wnd, VLC_THREAD_PRIORITY_LOW ) )
-    {
-        Close(wnd);
-        return VLC_EGENERIC;
-    }
+    sys->hwnd = NULL;
 
     vlc_mutex_lock( &sys->lock );
-    while( !sys->b_ready )
-    {
-        vlc_cond_wait( &sys->wait, &sys->lock );
-    }
-    if (sys->hwnd == NULL)
+    if( CreateWindowThreadLocked( wnd ) )
     {
         vlc_mutex_unlock( &sys->lock );
         Close(wnd);
@@ -773,9 +799,7 @@ static int Open(vout_window_t *wnd)
     }
     vlc_mutex_unlock( &sys->lock );
 
-    wnd->sys = sys;
     wnd->type = VOUT_WINDOW_TYPE_HWND;
-    wnd->handle.hwnd = sys->hwnd;
     wnd->ops = &ops;
     wnd->info.has_double_click = true;
     return VLC_SUCCESS;
-- 
2.20.1



More information about the vlc-devel mailing list