Skip to content

Commit

Permalink
Expand a mutex lock because it previously did not cover all possible …
Browse files Browse the repository at this point in the history
…multithreading cases
  • Loading branch information
dmoagx committed May 6, 2017
1 parent 994ff16 commit 9d8926a
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions Source/SPDataStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,38 @@ @implementation SPDataStorage
*/
- (void) setDataStorage:(SPMySQLStreamingResultStore *)newDataStorage updatingExisting:(BOOL)updateExistingStore
{
SPMySQLStreamingResultStore *oldDataStorage = dataStorage;
BOOL *oldUnloadedColumns;
NSPointerArray *oldEditedRows;
SPMySQLStreamingResultStore *oldDataStorage;

@synchronized(self) {
oldDataStorage = dataStorage;

if (oldDataStorage) {
// If the table is reloading data, link to the current data store for smoother loads
if (updateExistingStore) {
[newDataStorage replaceExistingResultStore:oldDataStorage];
if (oldDataStorage) {
// If the table is reloading data, link to the current data store for smoother loads
if (updateExistingStore) {
[newDataStorage replaceExistingResultStore:oldDataStorage];
}
}
}

[newDataStorage retain];
[newDataStorage retain];

NSPointerArray *newEditedRows = [[NSPointerArray alloc] init];
NSUInteger newNumberOfColumns = [newDataStorage numberOfFields];
BOOL *newUnloadedColumns = calloc(newNumberOfColumns, sizeof(BOOL));
for (NSUInteger i = 0; i < newNumberOfColumns; i++) {
newUnloadedColumns[i] = NO;
}

BOOL *oldUnloadedColumns = unloadedColumns;
NSPointerArray *oldEditedRows = editedRows;
@synchronized(self) {
NSPointerArray *newEditedRows = [[NSPointerArray alloc] init];
NSUInteger newNumberOfColumns = [newDataStorage numberOfFields];
BOOL *newUnloadedColumns = calloc(newNumberOfColumns, sizeof(BOOL));
for (NSUInteger i = 0; i < newNumberOfColumns; i++) {
newUnloadedColumns[i] = NO;
}

oldUnloadedColumns = unloadedColumns;
oldEditedRows = editedRows;
dataStorage = newDataStorage;
numberOfColumns = newNumberOfColumns;
unloadedColumns = newUnloadedColumns;
editedRowCount = 0;
editedRows = newEditedRows;
}

free(oldUnloadedColumns);
[oldEditedRows release];
[oldDataStorage release];
Expand Down Expand Up @@ -509,6 +514,10 @@ - (void) awaitDataDownloaded
- (void)resultStoreDidFinishLoadingData:(SPMySQLStreamingResultStore *)resultStore
{
@synchronized(self) {
if(resultStore != dataStorage) {
NSLog(@"%s: received delegate callback from an unknown result store %p (expected: %p). Ignored!", __PRETTY_FUNCTION__, resultStore, dataStorage);
return;
}
[editedRows setCount:(NSUInteger)[resultStore numberOfRows]];
editedRowCount = [editedRows count];
}
Expand Down

0 comments on commit 9d8926a

Please sign in to comment.