Fix downloads not occuring sequentially
This commit is contained in:
parent
b135147cbd
commit
4a501b497a
|
@ -20,6 +20,8 @@ struct download_context;
|
||||||
NSURL *destination;
|
NSURL *destination;
|
||||||
NSOutputStream *outputStream;
|
NSOutputStream *outputStream;
|
||||||
NSString *error;
|
NSString *error;
|
||||||
|
BOOL runThread;
|
||||||
|
NSThread *downloadThread;
|
||||||
DownloadManager *manager;
|
DownloadManager *manager;
|
||||||
NSTimeInterval lastWrite;
|
NSTimeInterval lastWrite;
|
||||||
struct download_context *ctx;
|
struct download_context *ctx;
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
#import "DownloadManager.h"
|
#import "DownloadManager.h"
|
||||||
#import "desktop/download.h"
|
#import "desktop/download.h"
|
||||||
|
|
||||||
// TODO: - Verify behavior of performOnBackgroundThread on a multiprocessor system!!
|
|
||||||
|
|
||||||
@implementation DownloadItem
|
@implementation DownloadItem
|
||||||
|
|
||||||
-(id)initWithManager: (DownloadManager*)aManager destination: (NSURL*)aDestination size: (NSInteger)aSize index: (NSUInteger)anIndex ctx: (struct download_context*)aCtx {
|
-(id)initWithManager: (DownloadManager*)aManager destination: (NSURL*)aDestination size: (NSInteger)aSize index: (NSUInteger)anIndex ctx: (struct download_context*)aCtx {
|
||||||
|
@ -22,11 +20,16 @@
|
||||||
[outputStream retain];
|
[outputStream retain];
|
||||||
[outputStream open];
|
[outputStream open];
|
||||||
ctx = aCtx;
|
ctx = aCtx;
|
||||||
|
downloadThread = nil;
|
||||||
|
runThread = YES;
|
||||||
|
[NSThread detachNewThreadSelector: @selector(runDownloadThread) toTarget:
|
||||||
|
self withObject: nil];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)dealloc {
|
-(void)dealloc {
|
||||||
|
runThread = NO;
|
||||||
[destination release];
|
[destination release];
|
||||||
[outputStream close];
|
[outputStream close];
|
||||||
[outputStream release];
|
[outputStream release];
|
||||||
|
@ -38,10 +41,27 @@
|
||||||
[super dealloc];
|
[super dealloc];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-(void)runDownloadThread {
|
||||||
|
NSAutoreleasePool *pool = [NSAutoreleasePool new];
|
||||||
|
downloadThread = [NSThread currentThread];
|
||||||
|
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
|
||||||
|
[runloop addPort: [NSPort port] forMode: NSDefaultRunLoopMode];
|
||||||
|
while (runThread) {
|
||||||
|
NSAutoreleasePool *pool = [NSAutoreleasePool new];
|
||||||
|
[runloop runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]];
|
||||||
|
[pool release];
|
||||||
|
}
|
||||||
|
[pool release];
|
||||||
|
}
|
||||||
|
|
||||||
-(BOOL)appendToDownload: (NSData*)data {
|
-(BOOL)appendToDownload: (NSData*)data {
|
||||||
// NOTE: - Not sure if this really works on mp systems...
|
if (downloadThread == nil) {
|
||||||
// Does this get queued sequentially? If not need to upkeep our own thread...
|
NSLog(@"Error: expected download thread to be initialized");
|
||||||
[self performSelectorInBackground: @selector(reallyWriteData:) withObject: data];
|
return NO;
|
||||||
|
}
|
||||||
|
[self performSelector: @selector(reallyWriteData:) onThread: downloadThread
|
||||||
|
withObject: data waitUntilDone: NO modes: [NSArray arrayWithObject:
|
||||||
|
NSDefaultRunLoopMode]];
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue