Beispiel #1
0
    def __init__(self,
                 group=None,
                 target=None,
                 name=None,
                 args=(),
                 kwargs={},
                 daemon=None):
        ThreadBase.__init__(self)

        assert group is None
        self.__target = target
        self.__args = args
        self.__kwargs = kwargs

        if not name:
            name = _newname()

        current = current_thread()
        if daemon is not None:
            self.__dict__['daemon'] = daemon
        else:
            self.__dict__['daemon'] = current.daemon
        self.__dict__['name'] = name

        def call_run():
            # As soon as the thread is done, break the circular reference.
            try:
                self.run()
            finally:
                self.__thread = None
                _thread._remove_thread_id(self.ident)

        self.__thread = core.PythonThread(call_run, None, name, name)
        threadId = _thread._add_thread(self.__thread, weakref.proxy(self))
        self.__dict__['ident'] = threadId
Beispiel #2
0
def start_new_thread(function, args, kwargs={}, name=None):
    def threadFunc(threadId, function=function, args=args, kwargs=kwargs):
        try:
            try:
                function(*args, **kwargs)
            except SystemExit:
                pass

        finally:
            _remove_thread_id(threadId)

    global _nextThreadId
    _threadsLock.acquire()
    try:
        threadId = _nextThreadId
        _nextThreadId += 1

        if name is None:
            name = 'PythonThread-%s' % (threadId)

        thread = core.PythonThread(threadFunc, [threadId], name, name)
        thread.setPythonIndex(threadId)
        _threads[threadId] = (thread, {}, None)

        thread.start(core.TPNormal, False)
        return threadId

    finally:
        _threadsLock.release()
Beispiel #3
0
def test_cvar_notify_thread(num_threads):
    # Tests notify() with some number of threads waiting.
    m = Mutex()
    cv = ConditionVarFull(m)

    # We prematurely notify, so that we can test that it's not doing anything.
    m.acquire()
    cv.notify()

    state = {'waiting': 0}

    def wait_thread():
        m.acquire()
        state['waiting'] += 1
        cv.wait()
        state['waiting'] -= 1
        m.release()

    # Start the threads, and yield to it, giving it a chance to mess up.
    threads = []
    for i in range(num_threads):
        thread = core.PythonThread(wait_thread, (), "", "")
        thread.start(core.TP_high, True)

    # Yield until all of the threads are waiting for the condition variable.
    for i in range(1000):
        m.release()
        yield_thread()
        m.acquire()
        if state['waiting'] == num_threads:
            break

    assert state['waiting'] == num_threads

    # OK, now signal it, and yield.  One thread must be unblocked per notify.
    for i in range(num_threads):
        cv.notify()
        expected_waiters = num_threads - i - 1

        for j in range(1000):
            m.release()
            yield_thread()
            m.acquire()
            if state['waiting'] == expected_waiters:
                break

        assert state['waiting'] == expected_waiters

    m.release()
    for thread in threads:
        thread.join()
    cv = None
 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
Beispiel #5
0
def test_mutex_contention():
    # As a smoke test for mutexes, we just spawn a bunch of threads that do a
    # lot of mutexing and hope that we can catch any obvious issues with the
    # mutex implementation, especially when compiling with DEBUG_THREADS.
    m1 = Mutex()
    m2 = Mutex()
    m3 = Mutex()
    m4 = Mutex()

    def thread_acq_rel(m):
        for i in range(5000):
            m.acquire()
            m.release()

    def thread_nested():
        for i in range(5000):
            m1.acquire()
            m4.acquire()
            m4.release()
            m1.release()

    def thread_hand_over_hand():
        m1.acquire()
        for i in range(5000):
            m2.acquire()
            m1.release()
            m3.acquire()
            m2.release()
            m1.acquire()
            m3.release()

        m1.release()

    def thread_sleep(m):
        for i in range(250):
            m.acquire()
            core.Thread.sleep(random() * 0.003)
            m.release()

    threads = [
        core.PythonThread(thread_acq_rel, (m1, ), "", ""),
        core.PythonThread(thread_acq_rel, (m2, ), "", ""),
        core.PythonThread(thread_acq_rel, (m3, ), "", ""),
        core.PythonThread(thread_acq_rel, (m4, ), "", ""),
        core.PythonThread(thread_nested, (), "", ""),
        core.PythonThread(thread_nested, (), "", ""),
        core.PythonThread(thread_nested, (), "", ""),
        core.PythonThread(thread_hand_over_hand, (), "", ""),
        core.PythonThread(thread_hand_over_hand, (), "", ""),
        core.PythonThread(thread_sleep, (m1, ), "", ""),
        core.PythonThread(thread_sleep, (m2, ), "", ""),
        core.PythonThread(thread_sleep, (m3, ), "", ""),
        core.PythonThread(thread_sleep, (m4, ), "", ""),
    ]
    for thread in threads:
        thread.start(core.TP_normal, True)

    for thread in threads:
        thread.join()