stdpy: update threading modules

This commit is contained in:
rdb 2017-08-27 16:48:13 +02:00
parent b9437316b4
commit 2d1f0e4866
3 changed files with 72 additions and 43 deletions

View File

@ -11,18 +11,32 @@ __all__ = [
'interrupt_main', 'interrupt_main',
'exit', 'allocate_lock', 'get_ident', 'exit', 'allocate_lock', 'get_ident',
'stack_size', 'stack_size',
'force_yield', 'consider_yield',
'forceYield', 'considerYield', 'forceYield', 'considerYield',
'TIMEOUT_MAX'
] ]
from panda3d import core from panda3d import core
import sys
if sys.platform == "win32":
TIMEOUT_MAX = float(0xffffffff // 1000)
else:
TIMEOUT_MAX = float(0x7fffffffffffffff // 1000000000)
# These methods are defined in Panda, and are particularly useful if # These methods are defined in Panda, and are particularly useful if
# you may be running in Panda's SIMPLE_THREADS compilation mode. # you may be running in Panda's SIMPLE_THREADS compilation mode.
forceYield = core.Thread.forceYield force_yield = core.Thread.force_yield
considerYield = core.Thread.considerYield consider_yield = core.Thread.consider_yield
class error(Exception): forceYield = force_yield
pass considerYield = consider_yield
if sys.version_info >= (3, 3):
error = RuntimeError
else:
class error(Exception):
pass
class LockType: class LockType:
""" Implements a mutex lock. Instead of directly subclassing """ Implements a mutex lock. Instead of directly subclassing
@ -36,13 +50,18 @@ class LockType:
self.__cvar = core.ConditionVar(self.__lock) self.__cvar = core.ConditionVar(self.__lock)
self.__locked = False self.__locked = False
def acquire(self, waitflag = 1): def acquire(self, waitflag = 1, timeout = -1):
self.__lock.acquire() self.__lock.acquire()
try: try:
if self.__locked and not waitflag: if self.__locked and not waitflag:
return False return False
while self.__locked:
self.__cvar.wait() if timeout >= 0:
while self.__locked:
self.__cvar.wait(timeout)
else:
while self.__locked:
self.__cvar.wait()
self.__locked = True self.__locked = True
return True return True

View File

@ -35,11 +35,15 @@ __all__ = [
'Event', 'Event',
'Timer', 'Timer',
'local', 'local',
'current_thread', 'currentThread', 'current_thread',
'enumerate', 'active_count', 'activeCount', 'main_thread',
'enumerate', 'active_count',
'settrace', 'setprofile', 'stack_size', 'settrace', 'setprofile', 'stack_size',
'TIMEOUT_MAX',
] ]
TIMEOUT_MAX = _thread.TIMEOUT_MAX
local = _thread._local local = _thread._local
_newname = _thread._newname _newname = _thread._newname
@ -111,8 +115,7 @@ class Thread(ThreadBase):
def is_alive(self): def is_alive(self):
return self.__thread.isStarted() return self.__thread.isStarted()
def isAlive(self): isAlive = is_alive
return self.__thread.isStarted()
def start(self): def start(self):
if self.__thread.isStarted(): if self.__thread.isStarted():
@ -379,6 +382,10 @@ def current_thread():
t = core.Thread.getCurrentThread() t = core.Thread.getCurrentThread()
return _thread._get_thread_wrapper(t, _create_thread_wrapper) return _thread._get_thread_wrapper(t, _create_thread_wrapper)
def main_thread():
t = core.Thread.getMainThread()
return _thread._get_thread_wrapper(t, _create_thread_wrapper)
currentThread = current_thread currentThread = current_thread
def enumerate(): def enumerate():
@ -394,6 +401,7 @@ def enumerate():
def active_count(): def active_count():
return len(enumerate()) return len(enumerate())
activeCount = active_count activeCount = active_count
_settrace_func = None _settrace_func = None

View File

@ -15,7 +15,7 @@ implementation. """
import sys as _sys import sys as _sys
from direct.stdpy import thread from direct.stdpy import thread as _thread
from direct.stdpy.thread import stack_size, _newname, _local as local from direct.stdpy.thread import stack_size, _newname, _local as local
from panda3d import core from panda3d import core
_sleep = core.Thread.sleep _sleep = core.Thread.sleep
@ -23,25 +23,19 @@ _sleep = core.Thread.sleep
from time import time as _time from time import time as _time
from traceback import format_exc as _format_exc from traceback import format_exc as _format_exc
# Rename some stuff so "from threading import *" is safe __all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
__all__ = [ 'enumerate', 'main_thread', 'TIMEOUT_MAX',
'enumerate', 'active_count', 'activeCount', 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
'Condition', 'Timer', 'ThreadError',
'current_thread', 'currentThread', 'setprofile', 'settrace', 'local', 'stack_size']
'Event',
'Lock', 'RLock',
'Semaphore', 'BoundedSemaphore',
'Thread',
'Timer',
'local',
'setprofile', 'settrace', 'stack_size'
]
_start_new_thread = thread.start_new_thread # Rename some stuff so "from threading import *" is safe
_allocate_lock = thread.allocate_lock _start_new_thread = _thread.start_new_thread
_get_ident = thread.get_ident _allocate_lock = _thread.allocate_lock
ThreadError = thread.error get_ident = _thread.get_ident
del thread ThreadError = _thread.error
TIMEOUT_MAX = _thread.TIMEOUT_MAX
del _thread
# Debug support (adapted from ihooks.py). # Debug support (adapted from ihooks.py).
@ -455,7 +449,7 @@ class Thread(_Verbose):
try: try:
self.__started = True self.__started = True
_active_limbo_lock.acquire() _active_limbo_lock.acquire()
_active[_get_ident()] = self _active[get_ident()] = self
del _limbo[self] del _limbo[self]
_active_limbo_lock.release() _active_limbo_lock.release()
if __debug__: if __debug__:
@ -546,7 +540,7 @@ class Thread(_Verbose):
_active_limbo_lock.acquire() _active_limbo_lock.acquire()
try: try:
try: try:
del _active[_get_ident()] del _active[get_ident()]
except KeyError: except KeyError:
if 'dummy_threading' not in _sys.modules: if 'dummy_threading' not in _sys.modules:
raise raise
@ -645,7 +639,7 @@ class _MainThread(Thread):
Thread.__init__(self, name="MainThread") Thread.__init__(self, name="MainThread")
self._Thread__started = True self._Thread__started = True
_active_limbo_lock.acquire() _active_limbo_lock.acquire()
_active[_get_ident()] = self _active[get_ident()] = self
_active_limbo_lock.release() _active_limbo_lock.release()
def _set_daemon(self): def _set_daemon(self):
@ -664,12 +658,6 @@ class _MainThread(Thread):
self._note("%s: exiting", self) self._note("%s: exiting", self)
self._Thread__delete() self._Thread__delete()
def _pickSomeNonDaemonThread():
for t in enumerate():
if not t.isDaemon() and t.isAlive():
return t
return None
# Dummy thread class to represent threads not started here. # Dummy thread class to represent threads not started here.
# These aren't garbage collected when they die, nor can they be waited for. # These aren't garbage collected when they die, nor can they be waited for.
@ -691,7 +679,7 @@ class _DummyThread(Thread):
self._Thread__started = True self._Thread__started = True
_active_limbo_lock.acquire() _active_limbo_lock.acquire()
_active[_get_ident()] = self _active[get_ident()] = self
_active_limbo_lock.release() _active_limbo_lock.release()
def _set_daemon(self): def _set_daemon(self):
@ -705,9 +693,9 @@ class _DummyThread(Thread):
def current_thread(): def current_thread():
try: try:
return _active[_get_ident()] return _active[get_ident()]
except KeyError: except KeyError:
##print "current_thread(): no current thread for", _get_ident() ##print "current_thread(): no current thread for", get_ident()
return _DummyThread() return _DummyThread()
currentThread = current_thread currentThread = current_thread
@ -732,7 +720,21 @@ def enumerate():
# and make it available for the interpreter # and make it available for the interpreter
# (Py_Main) as threading._shutdown. # (Py_Main) as threading._shutdown.
_shutdown = _MainThread()._exitfunc _main_thread = _MainThread()
_shutdown = _main_thread._exitfunc
def _pickSomeNonDaemonThread():
for t in enumerate():
if not t.isDaemon() and t.isAlive():
return t
return None
def main_thread():
"""Return the main thread object.
In normal conditions, the main thread is the thread from which the
Python interpreter was started.
"""
return _main_thread
# get thread-local implementation, either from the thread # get thread-local implementation, either from the thread
# module, or from the python fallback # module, or from the python fallback