Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Turn some successive onMainThread ops into a block
* Move a lock() that could become unbalanced and theoretically cause havoc
  • Loading branch information
dmoagx committed Oct 8, 2015
1 parent 9943a8b commit e3b8f07
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
53 changes: 27 additions & 26 deletions Source/SPDataImport.m
Expand Up @@ -160,15 +160,12 @@ - (IBAction)closeSheet:(id)sender
*/
- (void)closeAndStopProgressSheet
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(closeAndStopProgressSheet) withObject:nil waitUntilDone:YES];
return;
}

[NSApp endSheet:singleProgressSheet];
[singleProgressSheet orderOut:nil];
[[singleProgressBar onMainThread] stopAnimation:self];
[[singleProgressBar onMainThread] setMaxValue:100];
SPMainQSync(^{
[NSApp endSheet:singleProgressSheet];
[singleProgressSheet orderOut:nil];
[singleProgressBar stopAnimation:self];
[singleProgressBar setMaxValue:100];
});
}

#pragma mark -
Expand Down Expand Up @@ -762,16 +759,18 @@ - (void)importCSVFile:(NSString *)filename
if ([csvFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;

// Reset progress interface
[[errorsView onMainThread] setString:@""];
[[singleProgressTitle onMainThread] setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
[[singleProgressText onMainThread] setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[[singleProgressBar onMainThread] setIndeterminate:YES];
[[singleProgressBar onMainThread] setUsesThreadedAnimation:YES];
[[singleProgressBar onMainThread] startAnimation:self];

// Open the progress sheet
[[NSApp onMainThread] beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[[singleProgressSheet onMainThread] makeKeyWindow];
SPMainQSync(^{
[errorsView setString:@""];
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[singleProgressBar setIndeterminate:YES];
[singleProgressBar setUsesThreadedAnimation:YES];
[singleProgressBar startAnimation:self];

// Open the progress sheet
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[singleProgressSheet makeKeyWindow];
});

[tableDocumentInstance setQueryMode:SPImportExportQueryMode];

Expand Down Expand Up @@ -936,11 +935,13 @@ - (void)importCSVFile:(NSString *)filename
}

// Reset progress interface and open the progress sheet
[[singleProgressBar onMainThread] setIndeterminate:useIndeterminate];
[[singleProgressBar onMainThread] setMaxValue:fileTotalLength];
[[singleProgressBar onMainThread] startAnimation:self];
[[NSApp onMainThread] beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[[singleProgressSheet onMainThread] makeKeyWindow];
SPMainQSync(^{
[singleProgressBar setIndeterminate:useIndeterminate];
[singleProgressBar setMaxValue:fileTotalLength];
[singleProgressBar startAnimation:self];
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[singleProgressSheet makeKeyWindow];
});

// Set up index sets for use during row enumeration
for (i = 0; i < [fieldMappingArray count]; i++) {
Expand Down Expand Up @@ -1183,7 +1184,7 @@ - (void)importCSVFile:(NSString *)filename
// Select the new table

// Update current database tables
[tablesListInstance performSelectorOnMainThread:@selector(updateTables:) withObject:self waitUntilDone:YES];
[[tablesListInstance onMainThread] updateTables:self];

// Re-query the structure of all databases in the background
[NSThread detachNewThreadWithName:SPCtxt(@"SPNavigatorController database structure querier",tableDocumentInstance) target:[tableDocumentInstance databaseStructureRetrieval] selector:@selector(queryDbStructureWithUserInfo:) object:@{@"forceUpdate" : @YES}];
Expand Down Expand Up @@ -1674,7 +1675,7 @@ - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
- (void)showErrorSheetWithMessage:(NSString*)message
{
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(showErrorSheetWithMessage:) withObject:message waitUntilDone:YES];
[[self onMainThread] showErrorSheetWithMessage:message];
return;
}

Expand Down
3 changes: 2 additions & 1 deletion Source/SPFileHandle.m
Expand Up @@ -299,9 +299,10 @@ - (void)writeData:(NSData *)data
// Throw an exception if the file is closed
if (fileIsClosed) [NSException raise:NSInternalInconsistencyException format:@"Cannot write to a file handle after it has been closed"];

pthread_mutex_lock(&bufferLock);

// Add the data to the buffer
if ([data length]) {
pthread_mutex_lock(&bufferLock);
[buffer appendData:data];
allDataWritten = NO;
bufferDataLength += [data length];
Expand Down

0 comments on commit e3b8f07

Please sign in to comment.