Skip to content

Commit

Permalink
Use with statement for main loop lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jtackaberry committed Nov 27, 2012
1 parent 19de525 commit 2d84c3e
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/main.py
Expand Up @@ -167,25 +167,22 @@ def loop(condition, timeout=None):
.. warning::
Refer to the warning detailed in :func:`kaa.main.run`.
"""
_loop_lock.acquire()
if is_running() and not CoreThreading.is_mainthread():
# race condition. Two threads started a mainloop and the other
# one is executed right now. Raise a RuntimeError
_loop_lock.release()
raise RuntimeError('loop running in a different thread')

initial_mainloop = False
if not _initialized:
init()
if not is_running():
# no mainloop is running, set this thread as mainloop and
# set the internal running state.
initial_mainloop = True
CoreThreading.set_as_mainthread()
_set_running(True)

# ok, that was the critical part
_loop_lock.release()
with _loop_lock:
if is_running() and not CoreThreading.is_mainthread():
# race condition. Two threads started a mainloop and the other
# one is executed right now. Raise a RuntimeError
raise RuntimeError('loop running in a different thread')

initial_mainloop = False
if not _initialized:
init()
if not is_running():
# no mainloop is running, set this thread as mainloop and
# set the internal running state.
initial_mainloop = True
CoreThreading.set_as_mainthread()
_set_running(True)


if not callable(condition):
condition = lambda: condition
Expand Down

0 comments on commit 2d84c3e

Please sign in to comment.