Skip to content

Commit

Permalink
Another amendment to multithreading protection.
Browse files Browse the repository at this point in the history
Converted to use of NSLock for avoiding competition on instances of GoogleReader and RefreshManager.
Expand the protected range of code.
  • Loading branch information
barijaona committed Apr 9, 2012
1 parent f21a566 commit 9e3b4bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions GoogleReader.m
Expand Up @@ -242,7 +242,7 @@ - (void)feedRequestDone:(ASIHTTPRequest *)request

Database *db = [Database sharedDatabase];
NSInteger newArticlesFromFeed = 0;
@synchronized(db){
[[RefreshManager articlesUpdateSemaphore] lock];
[db setFolderLastUpdateString:[refreshedFolder itemId] lastUpdateString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"updated"]]];

// Here's where we add the articles to the database
Expand All @@ -268,7 +268,7 @@ - (void)feedRequestDone:(ASIHTTPRequest *)request

// Set the last update date for this folder.
[db setFolderLastUpdate:[refreshedFolder itemId] lastUpdate:[NSDate date]];
} //@synchronized
[[RefreshManager articlesUpdateSemaphore] unlock];

// Add to count of new articles so far
countOfNewArticles += newArticlesFromFeed;
Expand Down
1 change: 1 addition & 0 deletions RefreshManager.h
Expand Up @@ -38,6 +38,7 @@
}

+(RefreshManager *)sharedManager;
+ (NSLock *)articlesUpdateSemaphore;
-(void)refreshFolderIconCacheForSubscriptions:(NSArray *)foldersArray;
//-(void)refreshSubscriptions:(NSArray *)foldersArray ignoringSubscriptionStatus:(BOOL)ignoreSubStatus;
-(void)refreshSubscriptionsAfterRefresh:(NSArray *)foldersArray ignoringSubscriptionStatus:(BOOL)ignoreSubStatus;
Expand Down
32 changes: 21 additions & 11 deletions RefreshManager.m
Expand Up @@ -36,6 +36,7 @@

// Singleton
static RefreshManager * _refreshManager = nil;
static NSLock * articlesUpdate_lock;

// Private functions
@interface RefreshManager (Private)
Expand All @@ -58,6 +59,17 @@ - (void)syncFinishedForFolder:(Folder *)folder;

@implementation RefreshManager

+ (void)initialize
{
// Initializes our multi-thread lock
articlesUpdate_lock = [[NSLock alloc] init];
}

+ (NSLock *)articlesUpdateSemaphore
{
return articlesUpdate_lock;
}

/* init
* Initialise the class.
*/
Expand Down Expand Up @@ -784,7 +796,7 @@ -(void)finalizeFolderRefresh:(NSDictionary*)parameters;
[article release];
}

@synchronized(db){
[articlesUpdate_lock lock];
// Remember the last modified date
if (lastModifiedString != nil)
[db setFolderLastUpdateString:folderId lastUpdateString:lastModifiedString];
Expand All @@ -798,15 +810,13 @@ -(void)finalizeFolderRefresh:(NSDictionary*)parameters;

[folder clearCache];
// Should we wrap the entire loop or just individual article updates?
@synchronized(db) {
[db beginTransaction];
for (Article * article in articleArray)
{
if ([db createArticle:folderId article:article guidHistory:guidHistory] && ([article status] == MA_MsgStatus_New))
++newArticlesFromFeed;
}
[db commitTransaction];
[db beginTransaction];
for (Article * article in articleArray)
{
if ([db createArticle:folderId article:article guidHistory:guidHistory] && ([article status] == MA_MsgStatus_New))
++newArticlesFromFeed;
}
[db commitTransaction];
}

[db beginTransaction];
Expand All @@ -832,8 +842,6 @@ -(void)finalizeFolderRefresh:(NSDictionary*)parameters;
[db setFolderHomePage:folderId newHomePage:feedLink];

[db commitTransaction];

};

// Mark the feed as succeeded
[self setFolderErrorFlag:folder flag:NO];
Expand All @@ -843,6 +851,8 @@ -(void)finalizeFolderRefresh:(NSDictionary*)parameters;

// Set the last update date for this folder.
[db setFolderLastUpdate:folderId lastUpdate:[NSDate date]];

[articlesUpdate_lock unlock];
};

// Send status to the activity log
Expand Down

0 comments on commit 9e3b4bc

Please sign in to comment.