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',
'exit', 'allocate_lock', 'get_ident',
'stack_size',
'force_yield', 'consider_yield',
'forceYield', 'considerYield',
'TIMEOUT_MAX'
]
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
# you may be running in Panda's SIMPLE_THREADS compilation mode.
forceYield = core.Thread.forceYield
considerYield = core.Thread.considerYield
force_yield = core.Thread.force_yield
consider_yield = core.Thread.consider_yield
class error(Exception):
pass
forceYield = force_yield
considerYield = consider_yield
if sys.version_info >= (3, 3):
error = RuntimeError
else:
class error(Exception):
pass
class LockType:
""" Implements a mutex lock. Instead of directly subclassing
@ -36,13 +50,18 @@ class LockType:
self.__cvar = core.ConditionVar(self.__lock)
self.__locked = False
def acquire(self, waitflag = 1):
def acquire(self, waitflag = 1, timeout = -1):
self.__lock.acquire()
try:
if self.__locked and not waitflag:
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
return True

View File

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

View File

@ -15,7 +15,7 @@ implementation. """
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 panda3d import core
_sleep = core.Thread.sleep
@ -23,25 +23,19 @@ _sleep = core.Thread.sleep
from time import time as _time
from traceback import format_exc as _format_exc
# Rename some stuff so "from threading import *" is safe
__all__ = [
'enumerate', 'active_count', 'activeCount',
'Condition',
'current_thread', 'currentThread',
'Event',
'Lock', 'RLock',
'Semaphore', 'BoundedSemaphore',
'Thread',
'Timer',
'local',
'setprofile', 'settrace', 'stack_size'
]
__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
'enumerate', 'main_thread', 'TIMEOUT_MAX',
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
'Timer', 'ThreadError',
'setprofile', 'settrace', 'local', 'stack_size']
_start_new_thread = thread.start_new_thread
_allocate_lock = thread.allocate_lock
_get_ident = thread.get_ident
ThreadError = thread.error
del thread
# Rename some stuff so "from threading import *" is safe
_start_new_thread = _thread.start_new_thread
_allocate_lock = _thread.allocate_lock
get_ident = _thread.get_ident
ThreadError = _thread.error
TIMEOUT_MAX = _thread.TIMEOUT_MAX
del _thread
# Debug support (adapted from ihooks.py).
@ -455,7 +449,7 @@ class Thread(_Verbose):
try:
self.__started = True
_active_limbo_lock.acquire()
_active[_get_ident()] = self
_active[get_ident()] = self
del _limbo[self]
_active_limbo_lock.release()
if __debug__:
@ -546,7 +540,7 @@ class Thread(_Verbose):
_active_limbo_lock.acquire()
try:
try:
del _active[_get_ident()]
del _active[get_ident()]
except KeyError:
if 'dummy_threading' not in _sys.modules:
raise
@ -645,7 +639,7 @@ class _MainThread(Thread):
Thread.__init__(self, name="MainThread")
self._Thread__started = True
_active_limbo_lock.acquire()
_active[_get_ident()] = self
_active[get_ident()] = self
_active_limbo_lock.release()
def _set_daemon(self):
@ -664,12 +658,6 @@ class _MainThread(Thread):
self._note("%s: exiting", self)
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.
# 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
_active_limbo_lock.acquire()
_active[_get_ident()] = self
_active[get_ident()] = self
_active_limbo_lock.release()
def _set_daemon(self):
@ -705,9 +693,9 @@ class _DummyThread(Thread):
def current_thread():
try:
return _active[_get_ident()]
return _active[get_ident()]
except KeyError:
##print "current_thread(): no current thread for", _get_ident()
##print "current_thread(): no current thread for", get_ident()
return _DummyThread()
currentThread = current_thread
@ -732,7 +720,21 @@ def enumerate():
# and make it available for the interpreter
# (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
# module, or from the python fallback