Skip to content

Commit

Permalink
Move some code to a place where it makes more sense to be (part of #2770
Browse files Browse the repository at this point in the history
)

(This should not cause any behavioral changes)
  • Loading branch information
dmoagx committed Apr 17, 2017
1 parent 4daa0e1 commit e1b881b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
1 change: 0 additions & 1 deletion Source/SPCustomQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@

SPDataStorage *resultData;
pthread_mutex_t resultDataLock;
NSCondition *resultLoadingCondition;
NSInteger resultDataCount;
NSArray *cqColumnDefinition;
NSString *lastExecutedQuery;
Expand Down
11 changes: 1 addition & 10 deletions Source/SPCustomQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -993,11 +993,7 @@ - (void)updateResultStore:(SPMySQLStreamingResultStore *)theResultStore
// Set up the table updates timer and wait for it to notify this thread about completion
[[self onMainThread] initQueryLoadTimer];

[resultLoadingCondition lock];
while (![resultData dataDownloaded]) {
[resultLoadingCondition waitUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
[resultLoadingCondition unlock];
[resultData awaitDataDownloaded];

// If the final column autoresize wasn't performed, perform it
if (queryLoadLastRowCount < 200) [[self onMainThread] autosizeColumns];
Expand Down Expand Up @@ -1500,10 +1496,7 @@ - (void) queryLoadUpdate:(NSTimer *)theTimer
}

if ([resultData dataDownloaded]) {
[resultLoadingCondition lock];
[resultLoadingCondition signal];
[self clearQueryLoadTimer];
[resultLoadingCondition unlock];
}

// Check whether a table update is required, based on whether new rows are
Expand Down Expand Up @@ -3787,7 +3780,6 @@ - (id)init
runPrimaryActionButtonAsSelection = nil;

queryLoadTimer = nil;
resultLoadingCondition = [NSCondition new];

prefs = [NSUserDefaults standardUserDefaults];

Expand Down Expand Up @@ -4070,7 +4062,6 @@ - (void)dealloc
[NSObject cancelPreviousPerformRequestsWithTarget:customQueryView];

[self clearQueryLoadTimer];
SPClear(resultLoadingCondition);
SPClear(usedQuery);
SPClear(lastExecutedQuery);
SPClear(resultData);
Expand Down
7 changes: 7 additions & 0 deletions Source/SPDataStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
SPMySQLStreamingResultStore *dataStorage;
NSPointerArray *editedRows;
BOOL *unloadedColumns;
NSCondition *dataDownloadedLock;

NSUInteger numberOfColumns;
NSUInteger editedRowCount;
Expand Down Expand Up @@ -75,6 +76,12 @@
- (NSUInteger) columnCount;
- (BOOL) dataDownloaded;

/**
* This method will block the caller until -dataDownloaded returns YES.
* Multiple parallel calls from different threads are possible.
*/
- (void) awaitDataDownloaded;

/* Delegate callback methods */
- (void)resultStoreDidFinishLoadingData:(SPMySQLStreamingResultStore *)resultStore;

Expand Down
12 changes: 12 additions & 0 deletions Source/SPDataStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ - (BOOL) dataDownloaded
}
}

- (void) awaitDataDownloaded
{
[dataDownloadedLock lock];
while(![self dataDownloaded]) [dataDownloadedLock wait];
[dataDownloadedLock unlock];
}

#pragma mark - Delegate callback methods

/**
Expand All @@ -479,6 +486,9 @@ - (void)resultStoreDidFinishLoadingData:(SPMySQLStreamingResultStore *)resultSto
[editedRows setCount:(NSUInteger)[resultStore numberOfRows]];
editedRowCount = [editedRows count];
}
[dataDownloadedLock lock];
[dataDownloadedLock broadcast];
[dataDownloadedLock unlock];
}

/**
Expand All @@ -492,6 +502,7 @@ - (id) init
dataStorage = nil;
editedRows = nil;
unloadedColumns = NULL;
dataDownloadedLock = [NSCondition new];

numberOfColumns = 0;
editedRowCount = 0;
Expand All @@ -504,6 +515,7 @@ - (void) dealloc
@synchronized(self) {
SPClear(dataStorage);
SPClear(editedRows);
SPClear(dataDownloadedLock);
if (unloadedColumns) {
free(unloadedColumns), unloadedColumns = NULL;
}
Expand Down

0 comments on commit e1b881b

Please sign in to comment.