コード例 #1
0
ファイル: test_mutex.py プロジェクト: yangmaoer/panda3d
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()
コード例 #2
0
def test_mutex_with():
    m = Mutex()

    rc = sys.getrefcount(m)
    with m:
        assert m.debug_is_locked()

    with m:
        assert m.debug_is_locked()

    assert rc == sys.getrefcount(m)
コード例 #3
0
ファイル: test_mutex.py プロジェクト: loblao/panda3d
def test_mutex_try_acquire():
    m = Mutex()

    # Trying to acquire the lock should succeed
    assert m.try_acquire()

    # Assert that the lock is truly held now
    assert m.debug_is_locked()

    # Clean up
    m.release()
コード例 #4
0
def test_cvar_notify_locked():
    # Tests the same thing, but with the lock held.
    m = Mutex()
    cv = ConditionVarFull(m)

    m.acquire()
    cv.notify()
    m.release()

    m.acquire()
    cv.notify_all()
    m.release()
    del cv
コード例 #5
0
ファイル: test_mutex.py プロジェクト: yangmaoer/panda3d
def test_mutex_acquire_release():
    m = Mutex()
    m.acquire()

    # Assert that the lock is truly held now
    assert m.debug_is_locked()

    # Release the lock
    m.release()

    # Make sure the lock is properly released
    assert m.try_acquire()

    # Clean up
    m.release()
コード例 #6
0
def test_cvar_notify():
    # Just tests that notifying without waiting does no harm.
    m = Mutex()
    cv = ConditionVarFull(m)

    cv.notify()
    cv.notify_all()
    del cv
コード例 #7
0
ファイル: test_mutex.py プロジェクト: loblao/panda3d
def test_mutex_acquire_release():
    m = Mutex()
    m.acquire()

    # Assert that the lock is truly held now
    assert m.debug_is_locked()

    # Release the lock
    m.release()

    # Make sure the lock is properly released
    assert m.try_acquire()

    # Clean up
    m.release()
コード例 #8
0
ファイル: world.py プロジェクト: viatoriche/suber
    def __init__(self, config, params):
        self.config = config
        self.params = params
        self.seed = random.randint(0, sys.maxint)
        self.root_node = NodePath('Root_World')
        self.change_params(params)

        self.mutex_repaint = Mutex('repaint')
        self.trees = []
        self.treeland = TreeLand(self.config, self)
        self.forest = None
コード例 #9
0
def test_cvar_notify_locked():
    # Tests the same thing, but with the lock held.
    m = Mutex()
    cv = ConditionVarFull(m)

    with m:
        cv.notify()

    with m:
        cv.notify_all()

    del cv
コード例 #10
0
ファイル: test_mutex.py プロジェクト: yangmaoer/panda3d
def test_mutex_try_acquire():
    m = Mutex()

    # Trying to acquire the lock should succeed
    assert m.try_acquire()

    # Assert that the lock is truly held now
    assert m.debug_is_locked()

    # Clean up
    m.release()
コード例 #11
0
def test_cvar_notify_all_threads(num_threads):
    # Tests notify_all() 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_all()

    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.  All threads must unblock.
    cv.notify_all()
    for i in range(1000):
        m.release()
        yield_thread()
        m.acquire()
        if state['waiting'] == 0:
            break

    assert state['waiting'] == 0
    m.release()

    for thread in threads:
        thread.join()
    cv = None