<reponame>theclashingfritz/Cog-Invasion-Online-Dump
# uncompyle6 version 3.2.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
# Embedded file name: direct.stdpy.threading
import direct
from panda3d import core
from direct.stdpy import thread as _thread
import sys as _sys, weakref
__all__ = [
 'Thread',
 'Lock', 'RLock',
 'Condition',
 'Semaphore', 'BoundedSemaphore',
 'Event',
 'Timer',
 'local',
 'current_thread', 'currentThread',
 'enumerate', 'active_count', 'activeCount',
 'settrace', 'setprofile', 'stack_size']
local = _thread._local

class ThreadBase:

    def __init__(self):
        pass

    def getName(self):
        return self.name

    def is_alive(self):
        return self.__thread.isStarted()

    def isAlive(self):
        return self.__thread.isStarted()

    def isDaemon(self):
        return self.daemon

    def setDaemon(self, daemon):
        if self.is_alive():
            raise RuntimeError
        self.__dict__['daemon'] = daemon

    def __setattr__(self, key, value):
        if key == 'name':
            self.setName(value)
        else:
            if key == 'ident':
                raise AttributeError
            else:
                if key == 'daemon':
                    self.setDaemon(value)
                else:
                    self.__dict__[key] = value


ThreadBase.forceYield = core.Thread.forceYield
ThreadBase.considerYield = core.Thread.considerYield

class Thread(ThreadBase):

    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        ThreadBase.__init__(self)
        self.__target = target
        self.__args = args
        self.__kwargs = kwargs
        if not name:
            import threading2
            name = threading2._newname()
        current = current_thread()
        self.__dict__['daemon'] = current.daemon
        self.__dict__['name'] = name
        self.__thread = core.PythonThread(self.run, None, name, name)
        threadId = _thread._add_thread(self.__thread, weakref.proxy(self))
        self.__dict__['ident'] = threadId
        return

    def __del__(self):
        if _thread and _thread._remove_thread_id:
            _thread._remove_thread_id(self.ident)

    def start(self):
        if self.__thread.isStarted():
            raise RuntimeError
        if not self.__thread.start(core.TPNormal, True):
            raise RuntimeError

    def run(self):
        global _setprofile_func
        global _settrace_func
        if _settrace_func:
            _sys.settrace(_settrace_func)
        if _setprofile_func:
            _sys.setprofile(_setprofile_func)
        self.__target(*self.__args, **self.__kwargs)

    def join(self, timeout=None):
        self.__thread.join()
        self.__thread = None
        return

    def setName(self, name):
        self.__dict__['name'] = name
        self.__thread.setName(name)


class ExternalThread(ThreadBase):

    def __init__(self, extThread, threadId):
        ThreadBase.__init__(self)
        self.__thread = extThread
        self.__dict__['daemon'] = True
        self.__dict__['name'] = self.__thread.getName()
        self.__dict__['ident'] = threadId

    def start(self):
        raise RuntimeError

    def run(self):
        raise RuntimeError

    def join(self, timeout=None):
        raise RuntimeError

    def setDaemon(self, daemon):
        raise RuntimeError


class MainThread(ExternalThread):

    def __init__(self, extThread, threadId):
        ExternalThread.__init__(self, extThread, threadId)
        self.__dict__['daemon'] = False


class Lock(core.Mutex):

    def __init__(self, name='PythonLock'):
        core.Mutex.__init__(self, name)

    def acquire(self, blocking=True):
        if blocking:
            core.Mutex.acquire(self)
            return True
        return core.Mutex.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class RLock(core.ReMutex):

    def __init__(self, name='PythonRLock'):
        core.ReMutex.__init__(self, name)

    def acquire(self, blocking=True):
        if blocking:
            core.ReMutex.acquire(self)
            return True
        return core.ReMutex.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class Condition(core.ConditionVarFull):

    def __init__(self, lock=None):
        if not lock:
            lock = Lock()
        self.__lock = lock
        core.ConditionVarFull.__init__(self, self.__lock)

    def acquire(self, *args, **kw):
        return self.__lock.acquire(*args, **kw)

    def release(self):
        self.__lock.release()

    def wait(self, timeout=None):
        if timeout is None:
            core.ConditionVarFull.wait(self)
        else:
            core.ConditionVarFull.wait(self, timeout)
        return

    def notifyAll(self):
        core.ConditionVarFull.notifyAll(self)

    notify_all = notifyAll
    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class Semaphore(core.Semaphore):

    def __init__(self, value=1):
        core.Semaphore.__init__(self, value)

    def acquire(self, blocking=True):
        if blocking:
            core.Semaphore.acquire(self)
            return True
        return core.Semaphore.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class BoundedSemaphore(Semaphore):

    def __init__(self, value=1):
        self.__max = value
        Semaphore.__init__(value)

    def release(self):
        if self.getCount() > value:
            raise ValueError
        Semaphore.release(self)


class Event:

    def __init__(self):
        self.__lock = core.Lock('Python Event')
        self.__cvar = core.ConditionVarFull(self.__lock)
        self.__flag = False

    def is_set(self):
        return self.__flag

    isSet = is_set

    def set(self):
        self.__lock.acquire()
        try:
            self.__flag = True
            self.__cvar.signalAll()
        finally:
            self.__lock.release()

    def clear(self):
        self.__lock.acquire()
        try:
            self.__flag = False
        finally:
            self.__lock.release()

    def wait(self, timeout=None):
        self.__lock.acquire()
        try:
            if timeout is None:
                while not self.__flag:
                    self.__cvar.wait()

            else:
                clock = core.TrueClock.getGlobalPtr()
                expires = clock.getShortTime() + timeout
                while not self.__flag:
                    wait = expires - clock.getShortTime()
                    if wait < 0:
                        return
                    self.__cvar.wait(wait)

        finally:
            self.__lock.release()

        return


class Timer(Thread):

    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()

    def cancel(self):
        self.finished.set()

    def run(self):
        self.finished.wait(self.interval)
        if not self.finished.isSet():
            self.function(*self.args, **self.kwargs)
        self.finished.set()


def _create_thread_wrapper(t, threadId):
    if isinstance(t, core.MainThread):
        pyt = MainThread(t, threadId)
    else:
        pyt = ExternalThread(t, threadId)
    return pyt


def current_thread():
    t = core.Thread.getCurrentThread()
    return _thread._get_thread_wrapper(t, _create_thread_wrapper)


currentThread = current_thread

def enumerate():
    tlist = []
    _thread._threadsLock.acquire()
    try:
        for thread, locals, wrapper in _thread._threads.values():
            if wrapper and thread.isStarted():
                tlist.append(wrapper)

        return tlist
    finally:
        _thread._threadsLock.release()


def active_count():
    return len(enumerate())


activeCount = active_count
_settrace_func = None

def settrace(func):
    global _settrace_func
    _settrace_func = func


_setprofile_func = None

def setprofile(func):
    global _setprofile_func
    _setprofile_func = func


def stack_size(size=None):
    raise ThreadError


def _test():
    from collections import deque
    _sleep = core.Thread.sleep
    _VERBOSE = False

    class _Verbose(object):

        def __init__(self, verbose=None):
            if verbose is None:
                verbose = _VERBOSE
            self.__verbose = verbose
            return

        def _note(self, format, *args):
            if self.__verbose:
                format = format % args
                format = '%s: %s\n' % (
                 currentThread().getName(), format)
                _sys.stderr.write(format)

    class BoundedQueue(_Verbose):

        def __init__(self, limit):
            _Verbose.__init__(self)
            self.mon = Lock(name='BoundedQueue.mon')
            self.rc = Condition(self.mon)
            self.wc = Condition(self.mon)
            self.limit = limit
            self.queue = deque()

        def put(self, item):
            self.mon.acquire()
            while len(self.queue) >= self.limit:
                self._note('put(%s): queue full', item)
                self.wc.wait()

            self.queue.append(item)
            self._note('put(%s): appended, length now %d', item, len(self.queue))
            self.rc.notify()
            self.mon.release()

        def get(self):
            self.mon.acquire()
            while not self.queue:
                self._note('get(): queue empty')
                self.rc.wait()

            item = self.queue.popleft()
            self._note('get(): got %s, %d left', item, len(self.queue))
            self.wc.notify()
            self.mon.release()
            return item

    class ProducerThread(Thread):

        def __init__(self, queue, quota):
            Thread.__init__(self, name='Producer')
            self.queue = queue
            self.quota = quota

        def run(self):
            from random import random
            counter = 0
            while counter < self.quota:
                counter = counter + 1
                self.queue.put('%s.%d' % (self.getName(), counter))
                _sleep(random() * 1e-05)

    class ConsumerThread(Thread):

        def __init__(self, queue, count):
            Thread.__init__(self, name='Consumer')
            self.queue = queue
            self.count = count

        def run(self):
            while self.count > 0:
                item = self.queue.get()
                print item
                self.count = self.count - 1

    NP = 3
    QL = 4
    NI = 5
    Q = BoundedQueue(QL)
    P = []
    for i in range(NP):
        t = ProducerThread(Q, NI)
        t.setName('Producer-%d' % (i + 1))
        P.append(t)

    C = ConsumerThread(Q, NI * NP)
    for t in P:
        t.start()
        _sleep(1e-06)

    C.start()
    for t in P:
        t.join()

    C.join()


if __name__ == '__main__':
    _test()
# uncompyle6 version 3.2.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
# Embedded file name: direct.stdpy.threading
import direct
from panda3d import core
from direct.stdpy import thread as _thread
import sys as _sys, weakref
__all__ = [
 'Thread',
 'Lock', 'RLock',
 'Condition',
 'Semaphore', 'BoundedSemaphore',
 'Event',
 'Timer',
 'local',
 'current_thread', 'currentThread',
 'enumerate', 'active_count', 'activeCount',
 'settrace', 'setprofile', 'stack_size']
local = _thread._local

class ThreadBase:

    def __init__(self):
        pass

    def getName(self):
        return self.name

    def is_alive(self):
        return self.__thread.isStarted()

    def isAlive(self):
        return self.__thread.isStarted()

    def isDaemon(self):
        return self.daemon

    def setDaemon(self, daemon):
        if self.is_alive():
            raise RuntimeError
        self.__dict__['daemon'] = daemon

    def __setattr__(self, key, value):
        if key == 'name':
            self.setName(value)
        else:
            if key == 'ident':
                raise AttributeError
            else:
                if key == 'daemon':
                    self.setDaemon(value)
                else:
                    self.__dict__[key] = value


ThreadBase.forceYield = core.Thread.forceYield
ThreadBase.considerYield = core.Thread.considerYield

class Thread(ThreadBase):

    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
        ThreadBase.__init__(self)
        self.__target = target
        self.__args = args
        self.__kwargs = kwargs
        if not name:
            import threading2
            name = threading2._newname()
        current = current_thread()
        self.__dict__['daemon'] = current.daemon
        self.__dict__['name'] = name
        self.__thread = core.PythonThread(self.run, None, name, name)
        threadId = _thread._add_thread(self.__thread, weakref.proxy(self))
        self.__dict__['ident'] = threadId
        return

    def __del__(self):
        if _thread and _thread._remove_thread_id:
            _thread._remove_thread_id(self.ident)

    def start(self):
        if self.__thread.isStarted():
            raise RuntimeError
        if not self.__thread.start(core.TPNormal, True):
            raise RuntimeError

    def run(self):
        global _setprofile_func
        global _settrace_func
        if _settrace_func:
            _sys.settrace(_settrace_func)
        if _setprofile_func:
            _sys.setprofile(_setprofile_func)
        self.__target(*self.__args, **self.__kwargs)

    def join(self, timeout=None):
        self.__thread.join()
        self.__thread = None
        return

    def setName(self, name):
        self.__dict__['name'] = name
        self.__thread.setName(name)


class ExternalThread(ThreadBase):

    def __init__(self, extThread, threadId):
        ThreadBase.__init__(self)
        self.__thread = extThread
        self.__dict__['daemon'] = True
        self.__dict__['name'] = self.__thread.getName()
        self.__dict__['ident'] = threadId

    def start(self):
        raise RuntimeError

    def run(self):
        raise RuntimeError

    def join(self, timeout=None):
        raise RuntimeError

    def setDaemon(self, daemon):
        raise RuntimeError


class MainThread(ExternalThread):

    def __init__(self, extThread, threadId):
        ExternalThread.__init__(self, extThread, threadId)
        self.__dict__['daemon'] = False


class Lock(core.Mutex):

    def __init__(self, name='PythonLock'):
        core.Mutex.__init__(self, name)

    def acquire(self, blocking=True):
        if blocking:
            core.Mutex.acquire(self)
            return True
        return core.Mutex.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class RLock(core.ReMutex):

    def __init__(self, name='PythonRLock'):
        core.ReMutex.__init__(self, name)

    def acquire(self, blocking=True):
        if blocking:
            core.ReMutex.acquire(self)
            return True
        return core.ReMutex.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class Condition(core.ConditionVarFull):

    def __init__(self, lock=None):
        if not lock:
            lock = Lock()
        self.__lock = lock
        core.ConditionVarFull.__init__(self, self.__lock)

    def acquire(self, *args, **kw):
        return self.__lock.acquire(*args, **kw)

    def release(self):
        self.__lock.release()

    def wait(self, timeout=None):
        if timeout is None:
            core.ConditionVarFull.wait(self)
        else:
            core.ConditionVarFull.wait(self, timeout)
        return

    def notifyAll(self):
        core.ConditionVarFull.notifyAll(self)

    notify_all = notifyAll
    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class Semaphore(core.Semaphore):

    def __init__(self, value=1):
        core.Semaphore.__init__(self, value)

    def acquire(self, blocking=True):
        if blocking:
            core.Semaphore.acquire(self)
            return True
        return core.Semaphore.tryAcquire(self)

    __enter__ = acquire

    def __exit__(self, t, v, tb):
        self.release()


class BoundedSemaphore(Semaphore):

    def __init__(self, value=1):
        self.__max = value
        Semaphore.__init__(value)

    def release(self):
        if self.getCount() > value:
            raise ValueError
        Semaphore.release(self)


class Event:

    def __init__(self):
        self.__lock = core.Lock('Python Event')
        self.__cvar = core.ConditionVarFull(self.__lock)
        self.__flag = False

    def is_set(self):
        return self.__flag

    isSet = is_set

    def set(self):
        self.__lock.acquire()
        try:
            self.__flag = True
            self.__cvar.signalAll()
        finally:
            self.__lock.release()

    def clear(self):
        self.__lock.acquire()
        try:
            self.__flag = False
        finally:
            self.__lock.release()

    def wait(self, timeout=None):
        self.__lock.acquire()
        try:
            if timeout is None:
                while not self.__flag:
                    self.__cvar.wait()

            else:
                clock = core.TrueClock.getGlobalPtr()
                expires = clock.getShortTime() + timeout
                while not self.__flag:
                    wait = expires - clock.getShortTime()
                    if wait < 0:
                        return
                    self.__cvar.wait(wait)

        finally:
            self.__lock.release()

        return


class Timer(Thread):

    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()

    def cancel(self):
        self.finished.set()

    def run(self):
        self.finished.wait(self.interval)
        if not self.finished.isSet():
            self.function(*self.args, **self.kwargs)
        self.finished.set()


def _create_thread_wrapper(t, threadId):
    if isinstance(t, core.MainThread):
        pyt = MainThread(t, threadId)
    else:
        pyt = ExternalThread(t, threadId)
    return pyt


def current_thread():
    t = core.Thread.getCurrentThread()
    return _thread._get_thread_wrapper(t, _create_thread_wrapper)


currentThread = current_thread

def enumerate():
    tlist = []
    _thread._threadsLock.acquire()
    try:
        for thread, locals, wrapper in _thread._threads.values():
            if wrapper and thread.isStarted():
                tlist.append(wrapper)

        return tlist
    finally:
        _thread._threadsLock.release()


def active_count():
    return len(enumerate())


activeCount = active_count
_settrace_func = None

def settrace(func):
    global _settrace_func
    _settrace_func = func


_setprofile_func = None

def setprofile(func):
    global _setprofile_func
    _setprofile_func = func


def stack_size(size=None):
    raise ThreadError


def _test():
    from collections import deque
    _sleep = core.Thread.sleep
    _VERBOSE = False

    class _Verbose(object):

        def __init__(self, verbose=None):
            if verbose is None:
                verbose = _VERBOSE
            self.__verbose = verbose
            return

        def _note(self, format, *args):
            if self.__verbose:
                format = format % args
                format = '%s: %s\n' % (
                 currentThread().getName(), format)
                _sys.stderr.write(format)

    class BoundedQueue(_Verbose):

        def __init__(self, limit):
            _Verbose.__init__(self)
            self.mon = Lock(name='BoundedQueue.mon')
            self.rc = Condition(self.mon)
            self.wc = Condition(self.mon)
            self.limit = limit
            self.queue = deque()

        def put(self, item):
            self.mon.acquire()
            while len(self.queue) >= self.limit:
                self._note('put(%s): queue full', item)
                self.wc.wait()

            self.queue.append(item)
            self._note('put(%s): appended, length now %d', item, len(self.queue))
            self.rc.notify()
            self.mon.release()

        def get(self):
            self.mon.acquire()
            while not self.queue:
                self._note('get(): queue empty')
                self.rc.wait()

            item = self.queue.popleft()
            self._note('get(): got %s, %d left', item, len(self.queue))
            self.wc.notify()
            self.mon.release()
            return item

    class ProducerThread(Thread):

        def __init__(self, queue, quota):
            Thread.__init__(self, name='Producer')
            self.queue = queue
            self.quota = quota

        def run(self):
            from random import random
            counter = 0
            while counter < self.quota:
                counter = counter + 1
                self.queue.put('%s.%d' % (self.getName(), counter))
                _sleep(random() * 1e-05)

    class ConsumerThread(Thread):

        def __init__(self, queue, count):
            Thread.__init__(self, name='Consumer')
            self.queue = queue
            self.count = count

        def run(self):
            while self.count > 0:
                item = self.queue.get()
                print item
                self.count = self.count - 1

    NP = 3
    QL = 4
    NI = 5
    Q = BoundedQueue(QL)
    P = []
    for i in range(NP):
        t = ProducerThread(Q, NI)
        t.setName('Producer-%d' % (i + 1))
        P.append(t)

    C = ConsumerThread(Q, NI * NP)
    for t in P:
        t.start()
        _sleep(1e-06)

    C.start()
    for t in P:
        t.join()

    C.join()


if __name__ == '__main__':
    _test()
# uncompyle6 version 3.2.4 # Python bytecode 2.7 (62211) # Decompiled from: Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] # Embedded file name: direct.stdpy.threading