Esempio n. 1
0
class TimeoutGuard(Guard):
    """
    Timeout spawns a timer thread, when posted. If removed
    before timeout, then the timer thread is cancelled.
    """
    def __init__(self, seconds, action=None):
        self.seconds = seconds

        self.posted = (None, None)
        self.s = Scheduler()
        self.p = None

        self.g = (self, action)

    # Timer expired, offer an active Channel Request
    def expire(self):
        op, req = self.posted
        if op == READ:
            ChannelReq(self.p, msg=None).offer(req)
        elif op == WRITE:
            req.offer(ChannelReq(self.p))

    def post_read(self, reader):
        self.posted = (READ, reader)

        # Start process
        self.p = Process(self.expire)
        self.p.start()
        self.p.setstate(ACTIVE)

        # Put process on the scheduler timer queue
        self.s.timer_wait(self.p, self.seconds)

    def post_write(self, writer):
        self.posted = (WRITE, writer)

        # Start process
        self.p = Process(self.expire)
        self.p.start()
        self.p.setstate(ACTIVE)

        # Put process on the scheduler timer queue
        self.s.timer_wait(self.p, self.seconds)
  
    def remove_read(self, req):
        self.s.timer_cancel(self.p)

    def remove_write(self, req):
        self.s.timer_cancel(self.p)
Esempio n. 2
0
 def process(self):
     p = Process(self.empty)
     p.start()
     p.setstate(ACTIVE)
     return p