comparison src/image-load.c @ 1036:ab24c46aa6e4

load thumbnails with lower priority TODO: maybe implement an exception for visible thumbnails
author nadvornik
date Wed, 03 Sep 2008 21:54:49 +0000
parents 8acc100d5527
children 5fc64d6252e7
comparison
equal deleted inserted replaced
1035:04a8e8a26fff 1036:ab24c46aa6e4
699 699
700 /**************************************************************************************/ 700 /**************************************************************************************/
701 /* execution via thread */ 701 /* execution via thread */
702 static GThreadPool *image_loader_thread_pool = NULL; 702 static GThreadPool *image_loader_thread_pool = NULL;
703 703
704 static GCond *image_loader_prio_cond = NULL;
705 static GMutex *image_loader_prio_mutex = NULL;
706 static gint image_loader_prio_num = 0;
707
708
709 static void image_loader_thread_enter_high()
710 {
711 g_mutex_lock(image_loader_prio_mutex);
712 image_loader_prio_num++;
713 g_mutex_unlock(image_loader_prio_mutex);
714 }
715
716 static void image_loader_thread_leave_high()
717 {
718 g_mutex_lock(image_loader_prio_mutex);
719 image_loader_prio_num--;
720 if (image_loader_prio_num == 0) g_cond_signal(image_loader_prio_cond);
721 g_mutex_unlock(image_loader_prio_mutex);
722 }
723
724 static void image_loader_thread_wait_high()
725 {
726 g_mutex_lock(image_loader_prio_mutex);
727 while (image_loader_prio_num)
728 {
729 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
730 }
731
732 g_mutex_unlock(image_loader_prio_mutex);
733 }
734
735
704 void image_loader_thread_run(gpointer data, gpointer user_data) 736 void image_loader_thread_run(gpointer data, gpointer user_data)
705 { 737 {
706 ImageLoader *il = data; 738 ImageLoader *il = data;
707 gint cont = image_loader_begin(il); 739 gint cont;
740
741 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
742 {
743 /* low prio, wait untill high prio tasks finishes */
744 image_loader_thread_wait_high();
745 }
746 else
747 {
748 /* high prio */
749 image_loader_thread_enter_high();
750 }
751
752 cont = image_loader_begin(il);
708 753
709 if (!cont && !image_loader_get_pixbuf(il)) 754 if (!cont && !image_loader_get_pixbuf(il))
710 { 755 {
711 /* 756 /*
712 loader failed, we have to send signal 757 loader failed, we have to send signal
716 image_loader_emit_error(il); 761 image_loader_emit_error(il);
717 } 762 }
718 763
719 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il)) 764 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
720 { 765 {
766 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
767 {
768 /* low prio, wait untill high prio tasks finishes */
769 image_loader_thread_wait_high();
770 }
721 cont = image_loader_continue(il); 771 cont = image_loader_continue(il);
722 } 772 }
723 image_loader_stop_loader(il); 773 image_loader_stop_loader(il);
774
775 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
776 {
777 /* high prio */
778 image_loader_thread_leave_high();
779 }
724 780
725 g_mutex_lock(il->data_mutex); 781 g_mutex_lock(il->data_mutex);
726 il->can_destroy = TRUE; 782 il->can_destroy = TRUE;
727 g_cond_signal(il->can_destroy_cond); 783 g_cond_signal(il->can_destroy_cond);
728 g_mutex_unlock(il->data_mutex); 784 g_mutex_unlock(il->data_mutex);
741 if (!image_loader_setup_source(il)) return FALSE; 797 if (!image_loader_setup_source(il)) return FALSE;
742 798
743 if (!image_loader_thread_pool) 799 if (!image_loader_thread_pool)
744 { 800 {
745 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL); 801 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
802 image_loader_prio_cond = g_cond_new();
803 image_loader_prio_mutex = g_mutex_new();
746 } 804 }
747 805
748 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */ 806 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
749 807
750 g_thread_pool_push(image_loader_thread_pool, il, NULL); 808 g_thread_pool_push(image_loader_thread_pool, il, NULL);
826 884
827 void image_loader_set_priority(ImageLoader *il, gint priority) 885 void image_loader_set_priority(ImageLoader *il, gint priority)
828 { 886 {
829 if (!il) return; 887 if (!il) return;
830 888
831 g_mutex_lock(il->data_mutex); 889 if (il->thread) return; /* can't change prio if the thread already runs */
832 il->idle_priority = priority; 890 il->idle_priority = priority;
833 g_mutex_unlock(il->data_mutex);
834 } 891 }
835 892
836 893
837 gdouble image_loader_get_percent(ImageLoader *il) 894 gdouble image_loader_get_percent(ImageLoader *il)
838 { 895 {