From 984aadb5ca4f9a8bf01dffae42440320a1ea0f13 Mon Sep 17 00:00:00 2001
From: David Fuhrmann <dfuhrmann@videolan.org>
Date: Sat, 21 Mar 2015 17:31:20 +0100
Subject: [PATCH] macosx: Update progress dialog on the main thread, make check
 thread safe

This should fix some crashes as reported by the users.
---
 modules/gui/macosx/coredialogs.h |  3 ++-
 modules/gui/macosx/coredialogs.m | 17 +++++++----------
 modules/gui/macosx/intf.m        | 11 ++++-------
 3 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/modules/gui/macosx/coredialogs.h b/modules/gui/macosx/coredialogs.h
index 239600b08c40..2c9624713d0e 100644
--- a/modules/gui/macosx/coredialogs.h
+++ b/modules/gui/macosx/coredialogs.h
@@ -76,6 +76,8 @@
 }
 + (VLCCoreDialogProvider *)sharedInstance;
 
+@property (atomic,readwrite) BOOL progressCancelled;
+
 -(void)performEventWithObject: (NSValue *)o_value ofType: (const char*)type;
 
 -(void)showFatalDialog: (NSValue *)o_value;
@@ -88,7 +90,6 @@
 -(void)showProgressDialogOnMainThread: (NSValue *)o_value;
 -(void)showProgressDialog: (NSValue *)o_value;
 -(IBAction)progDialogAction:(id)sender;
--(BOOL)progressCancelled;
 -(void)updateProgressPanelWithText: (NSString *)string andNumber: (double)d_number;
 -(void)destroyProgressPanel;
 
diff --git a/modules/gui/macosx/coredialogs.m b/modules/gui/macosx/coredialogs.m
index fd51456e8828..af438eb16df0 100644
--- a/modules/gui/macosx/coredialogs.m
+++ b/modules/gui/macosx/coredialogs.m
@@ -36,6 +36,8 @@
 
 static VLCCoreDialogProvider *_o_sharedInstance = nil;
 
+@synthesize progressCancelled=b_progress_cancelled;
+
 + (VLCCoreDialogProvider *)sharedInstance
 {
     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
@@ -153,10 +155,10 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
 {
     /* we work-around a Cocoa limitation here, since you cannot delay an execution
      * on the main thread within a single call */
-    b_progress_cancelled = NO;
+    [self setProgressCancelled:NO];
 
     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
-    if (!p_dialog || b_progress_cancelled)
+    if (!p_dialog)
         return;
 
     [o_prog_win setTitle: toNSStr(p_dialog->title)];
@@ -177,7 +179,7 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
 {
     dialog_progress_bar_t *p_dialog = [o_value pointerValue];
 
-    if (!p_dialog || b_progress_cancelled)
+    if (!p_dialog || [self progressCancelled])
         return;
 
     [o_prog_bar setDoubleValue: 0];
@@ -197,19 +199,14 @@ static VLCCoreDialogProvider *_o_sharedInstance = nil;
 
 -(void)destroyProgressPanel
 {
-    b_progress_cancelled = YES;
+    [self setProgressCancelled:YES];
     [o_prog_bar performSelectorOnMainThread:@selector(stopAnimation:) withObject:self waitUntilDone:YES];
     [o_prog_win performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:YES];
 }
 
 -(IBAction)progDialogAction:(id)sender
 {
-    b_progress_cancelled = YES;
-}
-
--(BOOL)progressCancelled
-{
-    return b_progress_cancelled;
+    [self setProgressCancelled:YES];
 }
 
 -(id)errorPanel
diff --git a/modules/gui/macosx/intf.m b/modules/gui/macosx/intf.m
index db4e72149fe4..7ff668c15881 100644
--- a/modules/gui/macosx/intf.m
+++ b/modules/gui/macosx/intf.m
@@ -524,13 +524,10 @@ void updateProgressPanel (void *priv, const char *text, float value)
 {
     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
 
-    NSString *o_txt;
-    if (text != NULL)
-        o_txt = [NSString stringWithUTF8String:text];
-    else
-        o_txt = @"";
-
-    [[[VLCMain sharedInstance] coreDialogProvider] updateProgressPanelWithText: o_txt andNumber: (double)(value * 1000.)];
+    NSString *o_txt = toNSStr(text);
+    dispatch_async(dispatch_get_main_queue(), ^{
+        [[[VLCMain sharedInstance] coreDialogProvider] updateProgressPanelWithText: o_txt andNumber: (double)(value * 1000.)];
+    });
 
     [o_pool release];
 }
-- 
GitLab