def f_pong(ping): while 1: print("pong" + str(self.x)) self.x = self.x - 1 coroutine.transfer(ping) if self.x == 0: break
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
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)
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
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)
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")
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")
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
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)
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
def generator(k): for i in range(1,k): print(i) coroutine.transfer(None)
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)