make processing more parallel

restores 669b342332
This commit is contained in:
Konstantin Käfer
2014-04-15 11:06:29 -04:00
parent ab66ab39e6
commit 79d34115d0
2 changed files with 42 additions and 15 deletions

View File

@@ -4,8 +4,25 @@
#include <string>
#include <functional>
#include <llmr/platform/platform.hpp>
#include <uv.h>
uv_once_t request_initialize = UV_ONCE_INIT;
dispatch_queue_t queue = nullptr;
NSURLSession *session = nullptr;
void request_initialize_cb() {
queue = dispatch_queue_create("Parsing", DISPATCH_QUEUE_CONCURRENT);
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.timeoutIntervalForResource = 6;
sessionConfig.HTTPMaximumConnectionsPerHost = 8;
sessionConfig.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
session = [NSURLSession sessionWithConfiguration:sessionConfig];
}
namespace llmr {
class platform::Request {
@@ -19,8 +36,11 @@ public:
std::shared_ptr<platform::Request>
platform::request_http(const std::string &url, std::function<void(Response *)> background_function,
std::function<void()> foreground_callback) {
uv_once(&request_initialize, request_initialize_cb);
__block std::shared_ptr<Request> req;
NSURLSessionDataTask *task = [[NSURLSession sharedSession]
NSURLSessionDataTask *task = [session
dataTaskWithURL:[NSURL URLWithString:@(url.c_str())]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Make sure we clear the shared_ptr to resolve the circular reference. We're referencing
@@ -34,22 +54,24 @@ platform::request_http(const std::string &url, std::function<void(Response *)> b
return;
}
Response res;
dispatch_async(queue, ^(){
Response res;
if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
res.code = [(NSHTTPURLResponse *)response statusCode];
res.body = {(const char *)[data bytes], [data length]};
} else {
res.code = -1;
res.error_message = [[error localizedDescription] UTF8String];
}
if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
res.code = [(NSHTTPURLResponse *)response statusCode];
res.body = {(const char *)[data bytes], [data length]};
} else {
res.code = -1;
res.error_message = [[error localizedDescription] UTF8String];
}
background_function(&res);
background_function(&res);
dispatch_async(dispatch_get_main_queue(), ^(void) {
foreground_callback();
dispatch_async(dispatch_get_main_queue(), ^(void) {
foreground_callback();
[[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:MBXUpdateActivityNotification object:nil];
});
});
}];

View File

@@ -63,6 +63,7 @@
'PUBLIC_HEADERS_FOLDER_PATH': 'include',
'OTHER_CPLUSPLUSFLAGS':[
'<@(png_cflags)',
'<@(uv_cflags)',
'-I<(boost_root)/include',
]
},
@@ -79,22 +80,26 @@
],
'cflags': [
'<@(png_cflags)',
'<@(uv_cflags)',
],
'xcode_settings': {
'OTHER_CPLUSPLUSFLAGS':[
'<@(png_cflags)',
'<@(uv_cflags)',
]
},
'conditions': [
['OS == "mac"', {
'xcode_settings': {
'OTHER_LDFLAGS': [
'<@(png_libraries)'
'<@(png_libraries)',
'<@(uv_libraries)',
]
}
}, {
'libraries': [
'<@(png_libraries)'
'<@(png_libraries)',
'<@(uv_libraries)',
]
}]
]