2023-07-11 21:57:49 +00:00
|
|
|
init python early:
|
2022-05-16 23:48:22 +00:00
|
|
|
import threading
|
2023-07-11 21:57:49 +00:00
|
|
|
import queue
|
2022-05-16 23:48:22 +00:00
|
|
|
|
|
|
|
class DollThread(threading.Thread, NoRollback):
|
2023-07-11 21:57:49 +00:00
|
|
|
__lock = threading.RLock()
|
|
|
|
_count = 0
|
|
|
|
_instances = _list()
|
|
|
|
_interval = 0.05 if renpy.android else 0.025
|
|
|
|
|
|
|
|
def __init__(self, interval=None, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None):
|
2022-09-21 20:57:04 +00:00
|
|
|
threading.Thread.__init__(self, group, target, name, args, kwargs, daemon=daemon)
|
|
|
|
|
2022-05-16 23:48:22 +00:00
|
|
|
self._return = None
|
2023-07-11 21:57:49 +00:00
|
|
|
self.interval = interval or self._interval
|
|
|
|
self._delay = threading.Timer(self.interval, self._execute)
|
|
|
|
self._stop = threading.Event()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
DollThread._instances.append(self)
|
|
|
|
DollThread._count += 1
|
|
|
|
super().start()
|
2022-05-16 23:48:22 +00:00
|
|
|
|
|
|
|
def run(self):
|
2023-07-11 21:57:49 +00:00
|
|
|
with DollThread.__lock:
|
|
|
|
self._delay.start()
|
|
|
|
self._delay.join()
|
|
|
|
self.stop()
|
2022-05-16 23:48:22 +00:00
|
|
|
|
2023-07-11 21:57:49 +00:00
|
|
|
def _execute(self):
|
|
|
|
while not self._stop.is_set():
|
|
|
|
if self._target is not None:
|
|
|
|
self._return = self._target(*self._args, **self._kwargs)
|
|
|
|
renpy.restart_interaction()
|
|
|
|
break
|
2022-05-16 23:48:22 +00:00
|
|
|
|
2023-07-11 21:57:49 +00:00
|
|
|
def join(self, timeout=None):
|
|
|
|
super().join(timeout=timeout)
|
2022-05-16 23:48:22 +00:00
|
|
|
return self._return
|
2023-07-11 21:57:49 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._stop.set()
|
2023-07-14 22:52:27 +00:00
|
|
|
|
|
|
|
if self in DollThread._instances:
|
|
|
|
DollThread._instances.remove(self)
|
|
|
|
DollThread._count -= 1
|
|
|
|
|
|
|
|
if DollThread._count <= 0:
|
|
|
|
renpy.restart_interaction()
|
2023-07-11 21:57:49 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def stop_all(cls):
|
2023-07-14 22:52:27 +00:00
|
|
|
for thread in cls._instances:
|
2023-07-11 21:57:49 +00:00
|
|
|
# Allow threads to exit gracefully before forceful termination
|
2023-07-14 22:52:27 +00:00
|
|
|
thread.stop()
|
|
|
|
|
|
|
|
# if not renpy.android:
|
|
|
|
# # Then forcefully terminate the remainders (except on android because that stalls the renderer)
|
|
|
|
# for thread in cls._instances:
|
|
|
|
# thread.join()
|
2023-07-11 21:57:49 +00:00
|
|
|
|
|
|
|
class DefaultQueue(queue.Queue, NoRollback):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.last_item = None
|
|
|
|
|
|
|
|
def put(self, item, block=True, timeout=None):
|
|
|
|
super().put(item, block, timeout)
|
|
|
|
self.last_item = item
|
|
|
|
|
|
|
|
def get_with_default(self, default):
|
|
|
|
try:
|
|
|
|
return self.get(block=False)
|
|
|
|
except queue.Empty:
|
|
|
|
return self.last_item or default
|
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
state = self.__dict__.copy()
|
|
|
|
del state['last_item']
|
|
|
|
del state['mutex']
|
|
|
|
del state['not_empty']
|
|
|
|
del state['not_full']
|
|
|
|
del state['all_tasks_done']
|
|
|
|
return state
|
|
|
|
|
|
|
|
def __setstate__(self, state):
|
|
|
|
self.__dict__.update(state)
|
|
|
|
self.last_item = None
|
|
|
|
self.mutex = threading.Lock()
|
|
|
|
self.not_empty = threading.Condition(self.mutex)
|
|
|
|
self.not_full = threading.Condition(self.mutex)
|
|
|
|
self.all_tasks_done = threading.Condition(self.mutex)
|
|
|
|
|
|
|
|
def __reduce__(self):
|
|
|
|
return (DefaultQueue, ())
|
|
|
|
|
|
|
|
def __reduce_ex__(self, protocol):
|
2023-07-14 22:52:27 +00:00
|
|
|
return DefaultQueue, (), self.__getstate__(), None, iter([])
|