Skip to content
Permalink
Browse files
Prevent the audio periodic thread from blocking the main thread.
It seems like when an android phone goes to sleep, it's possible
for something in the audio thread to block. That was causing the
audio periodic thread to block, and since the recent changes, the
main thread as well.

Now, we use a separate condition to tell the periodic thread to
run, so if the periodic thread is blocked, the main thread will
always make progress.

This probably also can improve performance of the main thread
in some rare cases.

Fixes #1301.
  • Loading branch information
renpytom committed Dec 1, 2017
1 parent b6cb2d8 commit 54b962454f4bb3476233b9178a092d7e390b8f85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
@@ -188,6 +188,7 @@ class OSVERSIONINFOEXW(ctypes.Structure):
"renpy.loader.auto_lock",
"renpy.display.screen.cprof",
"renpy.audio.audio.lock",
"renpy.audio.audio.periodic_condition",
}


@@ -139,7 +139,7 @@ def copy(self):
next_channel_number = 0

# the lock that mediates between the periodic and main threads.
lock = threading.Condition()
lock = threading.RLock()


class Channel(object):
@@ -818,7 +818,7 @@ def init():
renpy.game.preferences.volumes.setdefault(m, default_volume)
renpy.game.preferences.mute.setdefault(m, False)

with lock:
with periodic_condition:

periodic_thread_quit = False

@@ -835,10 +835,11 @@ def quit(): # @ReservedAssignment
global pcm_ok
global mix_ok

with lock:
with periodic_condition:

if periodic_thread is not None:
periodic_thread_quit = True
lock.notify()
periodic_condition.notify()

periodic_thread.join()

@@ -943,21 +944,34 @@ def periodic_pass():
raise


# The exception that's been thrown by the periodic thread.
periodic_exc = None


def periodic_thread_main():
# Should we run the periodic thread now?
run_periodic = False

global periodic_exc
# The condition the perodic thread runs on.
periodic_condition = threading.Condition()

with lock:
while True:

lock.wait()
def periodic_thread_main():

global periodic_exc
global run_periodic

while True:
with periodic_condition:
if periodic_thread_quit:
return

if not run_periodic:
periodic_condition.wait()

run_periodic = False

with lock:

try:
periodic_pass()
except Exception:
@@ -966,12 +980,13 @@ def periodic_thread_main():

def periodic():
global periodic_exc
global run_periodic

if not renpy.config.audio_periodic_thread:
periodic_pass()
return

with lock:
with periodic_condition:

for c in all_channels:
c.get_context()
@@ -982,7 +997,8 @@ def periodic():

raise exc[0], exc[1], exc[2]

lock.notify()
run_periodic = True
periodic_condition.notify()


def interact():

0 comments on commit 54b9624

Please sign in to comment.