Revision: 5173
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5173&view=rev
Author:   hofman
Date:     2009-01-27 19:00:44 +0000 (Tue, 27 Jan 2009)

Log Message:
-----------
Run NSTask for dvi conversion only on secondary thread as it's not thread safe. 
Use an atomic stop flag to cancel the task.

Modified Paths:
--------------
    trunk/SKConversionProgressController.h
    trunk/SKConversionProgressController.m

Modified: trunk/SKConversionProgressController.h
===================================================================
--- trunk/SKConversionProgressController.h      2009-01-27 18:00:32 UTC (rev 
5172)
+++ trunk/SKConversionProgressController.h      2009-01-27 19:00:44 UTC (rev 
5173)
@@ -58,8 +58,8 @@
 
 
 @interface SKDVIProgressController : SKPSProgressController {
-    NSTask *task;
     volatile int32_t convertingPS;
+    volatile int32_t taskShouldStop;
 }
 - (NSData *)PDFDataWithDVIFile:(NSString *)dviFile;
 @end

Modified: trunk/SKConversionProgressController.m
===================================================================
--- trunk/SKConversionProgressController.m      2009-01-27 18:00:32 UTC (rev 
5172)
+++ trunk/SKConversionProgressController.m      2009-01-27 19:00:44 UTC (rev 
5173)
@@ -325,18 +325,12 @@
 - (IBAction)cancel:(id)sender
 {
     OSMemoryBarrier();
-    if (convertingPS) {
+    if (convertingPS)
         [super cancel:sender];
-    } else {
-        BOOL wasRunning = NO;
-        @synchronized(self) {
-            wasRunning = [task isRunning];
-            if (wasRunning)
-                [task terminate];
-        }
-        if (wasRunning == NO)
-            [self converterWasStopped];
-    }
+    else if (taskShouldStop)
+        OSAtomicCompareAndSwap32Barrier(0, 1, (int32_t *)&taskShouldStop);
+    else
+        [self converterWasStopped];
 }
 
 - (NSData *)PDFDataWithDVIFile:(NSString *)dviFile {
@@ -344,6 +338,7 @@
     NSMutableData *pdfData = nil;
     
     if (dviToolPath) {
+        taskShouldStop = 0;
         pdfData = [[NSMutableData alloc] init];
         
         NSDictionary *dictionary = [NSDictionary 
dictionaryWithObjectsAndKeys:dviFile, SKPSProgressDviFileKey, pdfData, 
SKPSProgressPdfDataKey, dviToolPath, SKPSProgressDviToolPathKey, nil];
@@ -370,29 +365,38 @@
     BOOL outputPS = [commandName isEqualToString:@"dvips"];
     NSString *outFile = [tmpDir stringByAppendingPathComponent:[[dviFile 
lastPathComponent] stringByReplacingPathExtension:outputPS ? @"ps" : @"pdf"]];
     NSArray *arguments = [commandName isEqualToString:@"dvipdf"] ? [NSArray 
arrayWithObjects:dviFile, outFile, nil] : [NSArray arrayWithObjects:@"-o", 
outFile, dviFile, nil];
-    BOOL success = SKFileExistsAtPath(dviFile);
+    BOOL success = NO;
     
     NSInvocation *invocation;
     
-    if (success) {
+    OSMemoryBarrier();
+    if (SKFileExistsAtPath(dviFile) && taskShouldStop == 0) {
         
-        @synchronized(self) {
-            task = [[NSTask launchedTaskWithLaunchPath:commandPath 
arguments:arguments currentDirectoryPath:[dviFile 
stringByDeletingLastPathComponent]] retain];
-        }
+        NSTask *task = [[NSTask launchedTaskWithLaunchPath:commandPath 
arguments:arguments currentDirectoryPath:[dviFile 
stringByDeletingLastPathComponent]] retain];
         
         invocation = [NSInvocation invocationWithTarget:self 
selector:@selector(conversionStarted)];
         [invocation performSelectorOnMainThread:@selector(invoke) 
withObject:nil waitUntilDone:NO];
         
-        if (success) {
-            [task waitUntilExit];
-            [task terminate];
-            success = 0 == [task terminationStatus];
+        OSMemoryBarrier();
+        if (task && taskShouldStop == 0) {
+            while ([task isRunning]) {
+                OSMemoryBarrier();
+                if (taskShouldStop) {
+                    if ([task isRunning])
+                        [task terminate];
+                    break;
+                } else {
+                    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
+                }
+            }
+            if ([task isRunning]) {
+                [task terminate];
+            } else {
+                success = (0 == [task terminationStatus]);
+            }
         }
-    }
-    
-    @synchronized(self) {
+        
         [task release];
-        task = nil;
     }
     
     NSData *outData = success ? [NSData dataWithContentsOfFile:outFile] : nil;


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to