Improve responsiveness by allowing bg threads to co-exist

This commit is contained in:
Harry Jeffery 2015-11-28 01:18:09 +00:00
parent 3fa5a6a554
commit d340175910

View file

@ -69,16 +69,16 @@ void imv_loader_load_path(struct imv_loader *ldr, const char *path)
/* cancel existing thread if already running */ /* cancel existing thread if already running */
if(ldr->bg_thread) { if(ldr->bg_thread) {
pthread_kill(ldr->bg_thread, SIGUSR1); pthread_kill(ldr->bg_thread, SIGUSR1);
pthread_join(ldr->bg_thread, NULL);
} }
/* kick off a new thread to load the image */ /* kick off a new thread to load the image */
/* no need to lock as we're the only thread at this point */ pthread_mutex_lock(&ldr->lock);
if(ldr->path) { if(ldr->path) {
free(ldr->path); free(ldr->path);
} }
ldr->path = strdup(path); ldr->path = strdup(path);
pthread_create(&ldr->bg_thread, NULL, &imv_loader_bg_new_img, ldr); pthread_create(&ldr->bg_thread, NULL, &imv_loader_bg_new_img, ldr);
pthread_mutex_unlock(&ldr->lock);
} }
FIBITMAP *imv_loader_get_image(struct imv_loader *ldr) FIBITMAP *imv_loader_get_image(struct imv_loader *ldr)
@ -166,11 +166,6 @@ static void *imv_loader_bg_new_img(void *data)
return 0; return 0;
} }
if(is_thread_cancelled()) {
free(path);
return 0;
}
int num_frames = 1; int num_frames = 1;
FIMULTIBITMAP *mbmp = NULL; FIMULTIBITMAP *mbmp = NULL;
FIBITMAP *bmp = NULL; FIBITMAP *bmp = NULL;
@ -189,11 +184,6 @@ static void *imv_loader_bg_new_img(void *data)
return 0; return 0;
} }
if(is_thread_cancelled()) {
FreeImage_CloseMultiBitmap(mbmp, 0);
return 0;
}
num_frames = FreeImage_GetPageCount(mbmp); num_frames = FreeImage_GetPageCount(mbmp);
FIBITMAP *frame = FreeImage_LockPage(mbmp, 0); FIBITMAP *frame = FreeImage_LockPage(mbmp, 0);
@ -216,10 +206,6 @@ static void *imv_loader_bg_new_img(void *data)
imv_loader_error_occurred(ldr); imv_loader_error_occurred(ldr);
return 0; return 0;
} }
if(is_thread_cancelled()) {
FreeImage_Unload(image);
return 0;
}
width = FreeImage_GetWidth(bmp); width = FreeImage_GetWidth(bmp);
height = FreeImage_GetHeight(bmp); height = FreeImage_GetHeight(bmp);
bmp = FreeImage_ConvertTo32Bits(image); bmp = FreeImage_ConvertTo32Bits(image);
@ -229,6 +215,18 @@ static void *imv_loader_bg_new_img(void *data)
/* now update the loader */ /* now update the loader */
pthread_mutex_lock(&ldr->lock); pthread_mutex_lock(&ldr->lock);
/* check for cancellation before finishing */
if(is_thread_cancelled()) {
if(mbmp) {
FreeImage_CloseMultiBitmap(mbmp, 0);
}
if(bmp) {
FreeImage_Unload(bmp);
}
pthread_mutex_unlock(&ldr->lock);
return 0;
}
if(ldr->mbmp) { if(ldr->mbmp) {
FreeImage_CloseMultiBitmap(ldr->mbmp, 0); FreeImage_CloseMultiBitmap(ldr->mbmp, 0);
} }