Class: Pitchfork::SoftTimeout::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/pitchfork/soft_timeout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread, timeout, block) ⇒ Request

Returns a new instance of Request.



18
19
20
21
22
23
24
25
# File 'lib/pitchfork/soft_timeout.rb', line 18

def initialize(thread, timeout, block)
  @thread = thread
  @deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  @block = block

  @mutex = Mutex.new
  @done = false # protected by @mutex
end

Instance Attribute Details

#deadlineObject (readonly)

Returns the value of attribute deadline.



16
17
18
# File 'lib/pitchfork/soft_timeout.rb', line 16

def deadline
  @deadline
end

#threadObject (readonly)

Returns the value of attribute thread.



16
17
18
# File 'lib/pitchfork/soft_timeout.rb', line 16

def thread
  @thread
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/pitchfork/soft_timeout.rb', line 35

def done?
  @mutex.synchronize do
    @done
  end
end

#expired?(now) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/pitchfork/soft_timeout.rb', line 41

def expired?(now)
  now >= @deadline
end

#extend_deadline(timeout) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/pitchfork/soft_timeout.rb', line 27

def extend_deadline(timeout)
  @deadline += timeout
  QUEUE_MUTEX.synchronize do
    CONDVAR.signal
  end
  self
end

#finishedObject



57
58
59
60
61
# File 'lib/pitchfork/soft_timeout.rb', line 57

def finished
  @mutex.synchronize do
    @done = true
  end
end

#interruptObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pitchfork/soft_timeout.rb', line 45

def interrupt
  @mutex.synchronize do
    unless @done
      begin
        @block.call(@thread)
      ensure
        @done = true
      end
    end
  end
end