Commit 9b89c33e authored by DanmeiChen's avatar DanmeiChen Committed by DanmeiChen

(external pull request)fix thread bug when not running linphone on the main UI...

(external pull request)fix thread bug when not running linphone on the main UI thread but instead on a worker thread
parent beab264a
......@@ -26,42 +26,71 @@
#include <UIKit/UIApplication.h>
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
UIApplication *app=[UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgid = UIBackgroundTaskInvalid;
if (cb==NULL){
belle_sip_error("belle_sip_begin_background_task(): the callback must not be NULL. Application must be aware that the background task needs to be terminated.");
return UIBackgroundTaskInvalid;
}
void (^handler)() = ^{
cb(data);
__block UIBackgroundTaskIdentifier bgid = UIBackgroundTaskInvalid;
dispatch_block_t block = ^{
UIApplication *app=[UIApplication sharedApplication];
@try {
if (cb==NULL){
belle_sip_error("belle_sip_begin_background_task(): the callback must not be NULL. Application must be aware that the background task needs to be terminated.");
bgid = UIBackgroundTaskInvalid;
@throw [NSException new];
}
void (^handler)() = ^{
cb(data);
};
if([app respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]){
bgid = [app beginBackgroundTaskWithName:[NSString stringWithUTF8String:name] expirationHandler:handler];
} else {
bgid = [app beginBackgroundTaskWithExpirationHandler:handler];
}
if (bgid==UIBackgroundTaskInvalid){
belle_sip_error("Could not start background task %s.", name);
bgid = 0;
@throw [NSException new];
}
// backgroundTimeRemaining is properly set only when running background... but not immediately!
if (app.applicationState != UIApplicationStateBackground || (app.backgroundTimeRemaining == DBL_MAX)) {
belle_sip_message("Background task %s started. Unknown remaining time since application is not fully in background.", name);
} else {
belle_sip_message("Background task %s started. Remaining time %.1f secs", name, app.backgroundTimeRemaining);
}
}
@catch (NSException*) {
// do nothing
}
};
if([app respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]){
bgid = [app beginBackgroundTaskWithName:[NSString stringWithUTF8String:name] expirationHandler:handler];
} else {
bgid = [app beginBackgroundTaskWithExpirationHandler:handler];
if( [NSThread isMainThread] ) {
block();
}
if (bgid==UIBackgroundTaskInvalid){
belle_sip_error("Could not start background task %s.", name);
return 0;
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
// backgroundTimeRemaining is properly set only when running background... but not immediately!
if (app.applicationState != UIApplicationStateBackground || (app.backgroundTimeRemaining == DBL_MAX)) {
belle_sip_message("Background task %s started. Unknown remaining time since application is not fully in background.", name);
} else {
belle_sip_message("Background task %s started. Remaining time %.1f secs", name, app.backgroundTimeRemaining);
}
return (unsigned long)bgid;
}
void belle_sip_end_background_task(unsigned long id){
UIApplication *app=[UIApplication sharedApplication];
if (id != UIBackgroundTaskInvalid){
[app endBackgroundTask:(UIBackgroundTaskIdentifier)id];
dispatch_block_t block = ^{
UIApplication *app=[UIApplication sharedApplication];
if (id != UIBackgroundTaskInvalid){
[app endBackgroundTask:(UIBackgroundTaskIdentifier)id];
}
};
if( [NSThread isMainThread] ) {
block();
}
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment