示例#1
0
    def main():
        for i in range(n):
            go(test, i)

        for i in range(n):
            for i in range(n):
                running.recv()
            if i == n -1:
                m.lock()
                exit = True
                m.unlock()

            ret = select(awake.if_recv(), default)
            assert ret != awake.if_recv(), "coroutine not asleep"

            m.lock()
            c.broadcast()
            m.unlock()

            seen = {}
            for i in range(n):
                g = awake.recv()
                assert g not in seen, "coroutine woke up twice"
                seen[g] = True

        ret = select(running.if_recv(), default)
        assert ret != running.if_recv(), "coroutine did not exist"
        c.broadcast()
示例#2
0
 def main():
     go(test)
     for i in range(n):
         rlocked.recv()
         rl.unlock()
         ret = select(wlocked.if_recv(), default)
         assert ret != wlocked.if_recv(), "RLocker didn't read-lock it"
         rl.unlock()
         wlocked.recv()
         ret = select(rlocked.if_recv(), default)
         assert ret != rlocked.if_recv(), "RLocker didn't respect the write lock"
         wl.unlock()
示例#3
0
    def test(c):
        while True:
            ret = select(c.if_recv())
            if ret == c.if_recv():

                if ret.value == "QUIT":
                    break
                rlist.append(ret.value)
示例#4
0
 def test(c, quit):
     x = 0
     while True:
         ret = select(c.if_send(x), quit.if_recv())
         if ret == c.if_send(x):
             x = x + 1
         elif ret == quit.if_recv():
             return
示例#5
0
 def fibonacci(c, quit):
     x, y = 0, 1
     while True:
         ret = select(c.if_send(x), quit.if_recv())
         if ret == c.if_send(x):
             x, y = y, x+y
         elif ret == quit.if_recv():
             return
示例#6
0
    def main():
        m = Mutex()
        c = Cond(m)
        n = 2
        running = makechan(n)
        awake = makechan(n)

        for i in range(n):
            go(test, m, c, running, awake)

        for i in range(n):
            running.recv()

        while n > 0:
            ret = select(awake.if_recv(), default)
            assert ret != awake.if_recv(), "coroutine not asleep"

            m.lock()
            c.signal()
            awake.recv()
            ret = select(awake.if_recv(), default)
            assert ret != awake.if_recv(), "too many coroutines awakes"
            n -= 1
        c.signal()
示例#7
0
    def test_waitgroup(wg1, wg2):
        n = 16
        wg1.add(n)
        wg2.add(n)
        exited = makechan(n)

        def f(i):
            wg1.done()
            wg2.wait()
            exited.send(True)

        for i in range(n):
            go(f, i)

        wg1.wait()

        for i in range(n):
            ret = select(exited.if_recv(), default)
            assert ret != exited.if_recv(), "WaitGroup released group too soon"
            wg2.done()

            for i in range(16):
                exited.recv()