Merge pull request #2515 from zhongwuzw/fix_header_thread_safe
Replace atomic property with SD_LOCK
This commit is contained in:
commit
cabda5890f
|
@ -171,7 +171,7 @@ typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
|
|||
/**
|
||||
* Set a value for a HTTP header to be appended to each download HTTP request.
|
||||
*
|
||||
* @param value The value for the header field. Use `nil` value to remove the header.
|
||||
* @param value The value for the header field. Use `nil` value to remove the header field.
|
||||
* @param field The name of the header field to set.
|
||||
*/
|
||||
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
|
||||
|
|
|
@ -34,8 +34,9 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
|
|||
@property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
|
||||
@property (weak, nonatomic, nullable) NSOperation *lastAddedOperation;
|
||||
@property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, NSOperation<SDWebImageDownloaderOperation> *> *URLOperations;
|
||||
@property (copy, atomic, nullable) NSDictionary<NSString *, NSString *> *HTTPHeaders; // Since modify this value is rare, use immutable object can enhance performance. But should mark as atomic to keep thread-safe
|
||||
@property (strong, nonatomic, nonnull) dispatch_semaphore_t operationsLock; // a lock to keep the access to `URLOperations` thread-safe
|
||||
@property (strong, nonatomic, nullable) NSMutableDictionary<NSString *, NSString *> *HTTPHeaders;
|
||||
@property (strong, nonatomic, nonnull) dispatch_semaphore_t HTTPHeadersLock; // A lock to keep the access to `HTTPHeaders` thread-safe
|
||||
@property (strong, nonatomic, nonnull) dispatch_semaphore_t operationsLock; // A lock to keep the access to `URLOperations` thread-safe
|
||||
|
||||
// The session in which data tasks will run
|
||||
@property (strong, nonatomic) NSURLSession *session;
|
||||
|
@ -113,7 +114,8 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
|
|||
headerDictionary[@"User-Agent"] = userAgent;
|
||||
}
|
||||
headerDictionary[@"Accept"] = @"image/*;q=0.8";
|
||||
_HTTPHeaders = [headerDictionary copy];
|
||||
_HTTPHeaders = headerDictionary;
|
||||
_HTTPHeadersLock = dispatch_semaphore_create(1);
|
||||
_operationsLock = dispatch_semaphore_create(1);
|
||||
NSURLSessionConfiguration *sessionConfiguration = _config.sessionConfiguration;
|
||||
if (!sessionConfiguration) {
|
||||
|
@ -154,16 +156,19 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
|
|||
if (!field) {
|
||||
return;
|
||||
}
|
||||
NSMutableDictionary *mutableHTTPHeaders = [self.HTTPHeaders mutableCopy];
|
||||
[mutableHTTPHeaders setValue:value forKey:field];
|
||||
self.HTTPHeaders = [mutableHTTPHeaders copy];
|
||||
SD_LOCK(self.HTTPHeadersLock);
|
||||
[self.HTTPHeaders setValue:value forKey:field];
|
||||
SD_UNLOCK(self.HTTPHeadersLock);
|
||||
}
|
||||
|
||||
- (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field {
|
||||
if (!field) {
|
||||
return nil;
|
||||
}
|
||||
return [self.HTTPHeaders objectForKey:field];
|
||||
SD_LOCK(self.HTTPHeadersLock);
|
||||
NSString *value = [self.HTTPHeaders objectForKey:field];
|
||||
SD_UNLOCK(self.HTTPHeadersLock);
|
||||
return value;
|
||||
}
|
||||
|
||||
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
||||
|
@ -238,7 +243,9 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
|
|||
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval];
|
||||
mutableRequest.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
|
||||
mutableRequest.HTTPShouldUsePipelining = YES;
|
||||
SD_LOCK(self.HTTPHeadersLock);
|
||||
mutableRequest.allHTTPHeaderFields = self.HTTPHeaders;
|
||||
SD_UNLOCK(self.HTTPHeadersLock);
|
||||
id<SDWebImageDownloaderRequestModifier> requestModifier;
|
||||
if ([context valueForKey:SDWebImageContextDownloadRequestModifier]) {
|
||||
requestModifier = [context valueForKey:SDWebImageContextDownloadRequestModifier];
|
||||
|
|
Loading…
Reference in New Issue