Add locks to fix thumbnail generating race condition (Closes: #7)
Thomas Ross
3 years ago
192 | 192 | self.thumbnail_sizes = ["128", "96", "72", "64", "48", "32"] |
193 | 193 | self.thumbnail_size = 128 # Default to 128 x 128 |
194 | 194 | self.thumbnail_loaded = [] |
195 | self.thumbpane_updating = False | |
195 | self.thumbpane_stop_updating = {} | |
196 | self.thumbpane_stop_updating_lock = threading.Lock() | |
197 | self.thumbpane_update_lock = threading.Lock() | |
196 | 198 | self.recentfiles = ["", "", "", "", ""] |
197 | 199 | self.screenshot_delay = 2 |
198 | 200 | self.thumbpane_bottom_coord_loaded = 0 |
742 | 744 | end_image = force_upto_imgnum |
743 | 745 | |
744 | 746 | # update images: |
747 | with self.thumbpane_stop_updating_lock: | |
748 | for thread_id in self.thumbpane_stop_updating: | |
749 | self.thumbpane_stop_updating[thread_id] = True | |
750 | ||
745 | 751 | thread = threading.Thread( |
746 | 752 | target=self.thumbpane_update_pending_images, |
747 | 753 | args=(start_image, end_image, force_upto_imgnum), |
765 | 771 | os.mkdir(os.path.expanduser("~/.thumbnails/normal/")) |
766 | 772 | |
767 | 773 | def thumbpane_update_pending_images(self, start, end, select_image): |
768 | self.thumbpane_updating = True | |
774 | thread_id = threading.get_ident() | |
775 | with self.thumbpane_stop_updating_lock: | |
776 | self.thumbpane_stop_updating[thread_id] = False | |
777 | ||
778 | if not self.thumbpane_update_lock.acquire(): | |
779 | return | |
780 | ||
769 | 781 | self.thumbpane_create_dir() |
770 | 782 | # Check to see if any images need their thumbnails generated. |
771 | 783 | curr_coord = 0 |
772 | 784 | imgnum = 0 |
773 | 785 | |
774 | 786 | for imgnum in range(start, end + 1): |
775 | if self.closing_app or self.stop_now or not self.thumbpane_show: | |
787 | if ( | |
788 | self.thumbpane_stop_updating.get(thread_id) | |
789 | or self.closing_app | |
790 | or self.stop_now | |
791 | or not self.thumbpane_show | |
792 | ): | |
776 | 793 | break |
777 | 794 | |
778 | 795 | if imgnum >= len(self.image_list): |
783 | 800 | if select_image == imgnum and select_image == self.curr_img_in_list: |
784 | 801 | GLib.idle_add(self.thumbpane_select, select_image) |
785 | 802 | |
786 | self.thumbpane_updating = False | |
803 | with self.thumbpane_stop_updating_lock: | |
804 | del self.thumbpane_stop_updating[thread_id] | |
805 | ||
806 | self.thumbpane_update_lock.release() | |
787 | 807 | |
788 | 808 | def thumbpane_clear_list(self): |
789 | 809 | self.thumbpane_bottom_coord_loaded = 0 |