Skip to content

Commit e3b8f07

Browse files
committed
* Turn some successive onMainThread ops into a block
* Move a lock() that could become unbalanced and theoretically cause havoc
1 parent 9943a8b commit e3b8f07

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

Source/SPDataImport.m

+27-26
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,12 @@ - (IBAction)closeSheet:(id)sender
160160
*/
161161
- (void)closeAndStopProgressSheet
162162
{
163-
if (![NSThread isMainThread]) {
164-
[self performSelectorOnMainThread:@selector(closeAndStopProgressSheet) withObject:nil waitUntilDone:YES];
165-
return;
166-
}
167-
168-
[NSApp endSheet:singleProgressSheet];
169-
[singleProgressSheet orderOut:nil];
170-
[[singleProgressBar onMainThread] stopAnimation:self];
171-
[[singleProgressBar onMainThread] setMaxValue:100];
163+
SPMainQSync(^{
164+
[NSApp endSheet:singleProgressSheet];
165+
[singleProgressSheet orderOut:nil];
166+
[singleProgressBar stopAnimation:self];
167+
[singleProgressBar setMaxValue:100];
168+
});
172169
}
173170

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

764761
// Reset progress interface
765-
[[errorsView onMainThread] setString:@""];
766-
[[singleProgressTitle onMainThread] setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
767-
[[singleProgressText onMainThread] setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
768-
[[singleProgressBar onMainThread] setIndeterminate:YES];
769-
[[singleProgressBar onMainThread] setUsesThreadedAnimation:YES];
770-
[[singleProgressBar onMainThread] startAnimation:self];
771-
772-
// Open the progress sheet
773-
[[NSApp onMainThread] beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
774-
[[singleProgressSheet onMainThread] makeKeyWindow];
762+
SPMainQSync(^{
763+
[errorsView setString:@""];
764+
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing CSV", @"text showing that the application is importing CSV")];
765+
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
766+
[singleProgressBar setIndeterminate:YES];
767+
[singleProgressBar setUsesThreadedAnimation:YES];
768+
[singleProgressBar startAnimation:self];
769+
770+
// Open the progress sheet
771+
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
772+
[singleProgressSheet makeKeyWindow];
773+
});
775774

776775
[tableDocumentInstance setQueryMode:SPImportExportQueryMode];
777776

@@ -936,11 +935,13 @@ - (void)importCSVFile:(NSString *)filename
936935
}
937936

938937
// Reset progress interface and open the progress sheet
939-
[[singleProgressBar onMainThread] setIndeterminate:useIndeterminate];
940-
[[singleProgressBar onMainThread] setMaxValue:fileTotalLength];
941-
[[singleProgressBar onMainThread] startAnimation:self];
942-
[[NSApp onMainThread] beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
943-
[[singleProgressSheet onMainThread] makeKeyWindow];
938+
SPMainQSync(^{
939+
[singleProgressBar setIndeterminate:useIndeterminate];
940+
[singleProgressBar setMaxValue:fileTotalLength];
941+
[singleProgressBar startAnimation:self];
942+
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
943+
[singleProgressSheet makeKeyWindow];
944+
});
944945

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

11851186
// Update current database tables
1186-
[tablesListInstance performSelectorOnMainThread:@selector(updateTables:) withObject:self waitUntilDone:YES];
1187+
[[tablesListInstance onMainThread] updateTables:self];
11871188

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

Source/SPFileHandle.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ - (void)writeData:(NSData *)data
299299
// Throw an exception if the file is closed
300300
if (fileIsClosed) [NSException raise:NSInternalInconsistencyException format:@"Cannot write to a file handle after it has been closed"];
301301

302+
pthread_mutex_lock(&bufferLock);
303+
302304
// Add the data to the buffer
303305
if ([data length]) {
304-
pthread_mutex_lock(&bufferLock);
305306
[buffer appendData:data];
306307
allDataWritten = NO;
307308
bufferDataLength += [data length];

0 commit comments

Comments
 (0)