Esempio n. 1
0
 def f_pong(ping):
     while 1:
         print("pong" + str(self.x))
         self.x = self.x - 1
         coroutine.transfer(ping)
         if self.x == 0:
             break
Esempio n. 2
0
    def remove_all(self, elt):
        num_removed = 0
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                num_removed += 1
                if nn == self.tail:
                    self.tail = node
                    break

                nn = nn.next  # iterate nn
                # yield
                coroutine.transfer(label="remove_all_1")
                continue

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_all_2")
        return num_removed
Esempio n. 3
0
    def test_ping_pong(self):

        self.x = 10
        
        def f_ping(pong):
            while 1:
                print("ping" + str(self.x))
                self.x = self.x - 1
                coroutine.transfer(pong)
                if self.x == 0:
                    break


        def f_pong(ping):
            while 1:
                print("pong" + str(self.x))
                self.x = self.x - 1
                coroutine.transfer(ping)
                if self.x == 0:
                    break

        ping = coroutine(f_ping)
        pong = coroutine(f_pong)

        ping(pong)
        pong(ping)

        # returns back when both ping and pong terminates
        coroutine.transfer(ping)
Esempio n. 4
0
    def remove_all(self, elt):
        num_removed = 0
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                num_removed += 1
                if nn == self.tail:
                    self.tail = node
                    break

                nn = nn.next # iterate nn
                # yield
                coroutine.transfer(label="remove_all_1")
                continue

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_all_2")
        return num_removed
Esempio n. 5
0
 def foreach(self, f):
     node = self.head.next
     while node != None:
         if not node.marked:
             elt = node.elt
             f(elt)
             #yield
             coroutine.transfer(label="foreach")
         node = node.next
Esempio n. 6
0
 def foreach(self, f):
     node = self.head.next
     while node != None:
         if not node.marked:
             elt = node.elt
             f(elt)
             #yield
             coroutine.transfer(label="foreach")
         node = node.next
Esempio n. 7
0
    def test_generator(self):
        def generator(k):
            for i in range(1,k):
                print(i)
                coroutine.transfer(None)

        c = coroutine(generator)(100)

        for i in range(1, 50):
            coroutine.transfer(c)
Esempio n. 8
0
    def add(self, elt, sc=[]):
        # yield
        coroutine.transfer(label="add_1")

        if self.capacity > 0 and self.size() == self.capacity:
            raise RuntimeError("Capacity is reached!")

        node = concurrent_list.node(elt, None)
        self.tail.next = node
        self.tail = node

        # yield
        coroutine.transfer(label="add_2")
Esempio n. 9
0
    def add(self, elt, sc = []):
        # yield
        coroutine.transfer(label="add_1")

        if self.capacity > 0 and self.size() == self.capacity:
            raise RuntimeError("Capacity is reached!")

        node = concurrent_list.node(elt, None)
        self.tail.next = node
        self.tail = node

        # yield
        coroutine.transfer(label="add_2")
Esempio n. 10
0
    def take(self):
        # block until the list is non-empty
        while self.is_empty():
            # yield
            coroutine.transfer(label="take_1")
            pass

        assert self.head != self.tail
        assert self.head.next != None
        elt = self.head.next.elt
        self.head = self.head.next
        # yield
        coroutine.transfer(label="take_2")
        return elt
Esempio n. 11
0
    def take(self):
        # block until the list is non-empty
        while self.is_empty():
            # yield
            coroutine.transfer(label="take_1")
            pass

        assert self.head != self.tail
        assert self.head.next != None
        elt = self.head.next.elt
        self.head = self.head.next
        # yield
        coroutine.transfer(label="take_2")
        return elt
Esempio n. 12
0
    def test_positive_negative(self):

        def positive(k, neg):
            for i in range(1,k):
                print(i)
                coroutine.transfer(neg)

        def negative(k):
            for i in range(1,k):
                print(-i)
                coroutine.transfer(None)

        neg = coroutine(negative)
        pos = coroutine(positive)

        neg(100)
        pos(100, neg)

#        for i in range(1, 50): # this creates a deadlock since neg returns back to pos, not here!
        coroutine.transfer(pos)
Esempio n. 13
0
    def remove(self, elt):
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                if nn == self.tail:
                    self.tail = node

                # yield
                coroutine.transfer(label="remove_1")
                return True

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_2")
        return False
Esempio n. 14
0
    def remove(self, elt):
        assert elt != None
        node = self.head
        nn = node.next
        while nn != None:
            e = nn.elt
            if e == elt:
                nn.marked = True
                node.next = nn.next
                if nn == self.tail:
                    self.tail = node

                # yield
                coroutine.transfer(label="remove_1")
                return True

            node = nn
            nn = node.next

        #yield
        coroutine.transfer(label="remove_2")
        return False
Esempio n. 15
0
 def generator(k):
     for i in range(1,k):
         print(i)
         coroutine.transfer(None)
Esempio n. 16
0
 def positive(k, neg):
     for i in range(1,k):
         print(i)
         coroutine.transfer(neg)
Esempio n. 17
0
 def negative(k):
     for i in range(1,k):
         print(-i)
         coroutine.transfer(None)