Skip to content

Commit

Permalink
Add locks
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Dec 25, 2022
1 parent ae8535f commit 7b578d9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
2 changes: 2 additions & 0 deletions resources/lib/monitor/itemdetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from resources.lib.monitor.images import ImageFunctions
from resources.lib.items.listitem import ListItem
from copy import deepcopy
from threading import Lock


BASEITEM_PROPERTIES = [
Expand Down Expand Up @@ -33,6 +34,7 @@

class ListItemDetails():
def __init__(self, parent, position=0):
self._lock = Lock()
self._parent = parent
self._position = position
self._season = None
Expand Down
60 changes: 31 additions & 29 deletions resources/lib/monitor/listitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,16 @@ def on_finalise_listcontainer(self, process_artwork=True, process_ratings=True):
self.add_item_listcontainer(_listitem)

def _process_artwork():
kodi_log(f'SM: on_finalise _process_artwork get_builtartwork', 1)
_artwork = _item.get_builtartwork()
kodi_log(f'SM: on_finalise _process_artwork get_image_manipulations', 1)
_artwork.update(_item.get_image_manipulations())
kodi_log(f'SM: on_finalise _process_artwork setArt', 1)
_listitem.setArt(_artwork)
if process_artwork == ADD_AFTER_PROCESSING and self.is_same_item():
kodi_log(f'SM: on_finalise _process_artwork add_item_listcontainer', 1)
self.add_item_listcontainer(_listitem)
with _item._lock:
kodi_log(f'SM: on_finalise _process_artwork get_builtartwork', 1)
_artwork = _item.get_builtartwork()
kodi_log(f'SM: on_finalise _process_artwork get_image_manipulations', 1)
_artwork.update(_item.get_image_manipulations())
kodi_log(f'SM: on_finalise _process_artwork setArt', 1)
_listitem.setArt(_artwork)
if process_artwork == ADD_AFTER_PROCESSING and self.is_same_item():
kodi_log(f'SM: on_finalise _process_artwork add_item_listcontainer', 1)
self.add_item_listcontainer(_listitem)

if process_artwork:
t = Thread(target=_process_artwork)
Expand All @@ -219,15 +220,16 @@ def _process_artwork():
t.start()

def _process_ratings():
kodi_log(f'SM: on_finalise _process_ratings get_all_ratings', 1)
get_property('IsUpdatingRatings', 'True')
_details = _item.get_all_ratings() or {}
kodi_log(f'SM: on_finalise _process_ratings setProperties', 1)
_listitem.setProperties(_details.get('infoproperties') or {})
if process_ratings == ADD_AFTER_PROCESSING and self.is_same_item():
kodi_log(f'SM: on_finalise _process_ratings add_item_listcontainer', 1)
self.add_item_listcontainer(_listitem)
get_property('IsUpdatingRatings', clear_property=True)
with _item._lock:
kodi_log(f'SM: on_finalise _process_ratings get_all_ratings', 1)
get_property('IsUpdatingRatings', 'True')
_details = _item.get_all_ratings() or {}
kodi_log(f'SM: on_finalise _process_ratings setProperties', 1)
_listitem.setProperties(_details.get('infoproperties') or {})
if process_ratings == ADD_AFTER_PROCESSING and self.is_same_item():
kodi_log(f'SM: on_finalise _process_ratings add_item_listcontainer', 1)
self.add_item_listcontainer(_listitem)
get_property('IsUpdatingRatings', clear_property=True)

if process_ratings:
Thread(target=_process_ratings).start()
Expand All @@ -243,22 +245,24 @@ def on_finalise_winproperties(self):

# Proces artwork in a thread
def _process_artwork():
_artwork = _item.get_builtartwork()
_artwork.update(_item.get_image_manipulations())
self.clear_property_list(SETMAIN_ARTWORK)
self.set_iter_properties(_artwork, SETMAIN_ARTWORK) if self.is_same_item() else None
with _item._lock:
_artwork = _item.get_builtartwork()
_artwork.update(_item.get_image_manipulations())
self.clear_property_list(SETMAIN_ARTWORK)
self.set_iter_properties(_artwork, SETMAIN_ARTWORK) if self.is_same_item() else None

if get_condvisibility("!Skin.HasSetting(TMDbHelper.DisableArtwork)"):
thread_artwork = Thread(target=_process_artwork)
thread_artwork.start()

# Process ratings in a thread
def _process_ratings():
get_property('IsUpdatingRatings', 'True')
_details = _item.get_all_ratings() or {}
self.clear_property_list(SETPROP_RATINGS)
self.set_iter_properties(_details.get('infoproperties', {}), SETPROP_RATINGS) if self.is_same_item() else None
get_property('IsUpdatingRatings', clear_property=True)
with _item._lock:
get_property('IsUpdatingRatings', 'True')
_details = _item.get_all_ratings() or {}
self.clear_property_list(SETPROP_RATINGS)
self.set_iter_properties(_details.get('infoproperties', {}), SETPROP_RATINGS) if self.is_same_item() else None
get_property('IsUpdatingRatings', clear_property=True)

if get_condvisibility("!Skin.HasSetting(TMDbHelper.DisableRatings)"):
thread_ratings = Thread(target=_process_ratings)
Expand Down Expand Up @@ -292,8 +296,6 @@ def get_readahead(self):

# Readahead next item and if the main item changes in the meantime we reset to None
def _next_readahead():
if self._readahead._locked:
return
if self._readahead.next_readahead() != READAHEAD_CHANGED:
return
self._readahead = None
Expand Down
10 changes: 6 additions & 4 deletions resources/lib/monitor/readahead.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from resources.lib.monitor.itemdetails import ListItemDetails
from resources.lib.addon.window import get_property, get_current_window
from resources.lib.addon.plugin import get_setting
from threading import Lock

READAHEAD_QUEUE = [1, 2, 3, -1, 4, -2, 5, -3, 6, 7, 8, 9] # Mostly only check items ahead but cache a few behind in case user scrolls back
READAHEAD_CHANGED = -1 # Underlying item changed in the meantime so we reset readahead on this condition
Expand All @@ -10,7 +11,7 @@

class ListItemReadAhead():
def __init__(self, parent, cur_window, cur_item):
self._locked = False
self._lock = Lock()
self._parent = parent
self._pre_window = cur_window
self._pre_item = cur_item
Expand Down Expand Up @@ -48,7 +49,8 @@ def _next_readahead(self):
return READAHEAD_COMPLETED

def next_readahead(self):
self._locked = True
status = self._next_readahead()
self._locked = False
if self._lock.locked():
return None
with self._lock:
status = self._next_readahead()
return status

0 comments on commit 7b578d9

Please sign in to comment.