Class: Thread::Pool::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/thread/pool.rb

Overview

A task incapsulates a block being ran by the pool and the arguments to pass to it.

Constant Summary collapse

Timeout =
Class.new(Exception)
Asked =
Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, *args, &block) ⇒ Task

Create a task in the given pool which will pass the arguments to the block.



29
30
31
32
33
34
35
36
37
38
# File 'lib/thread/pool.rb', line 29

def initialize (pool, *args, &block)
	@pool      = pool
	@arguments = args
	@block     = block

	@running    = false
	@finished   = false
	@timedout   = false
	@terminated = false
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



25
26
27
# File 'lib/thread/pool.rb', line 25

def exception
  @exception
end

#poolObject (readonly)

Returns the value of attribute pool.



25
26
27
# File 'lib/thread/pool.rb', line 25

def pool
  @pool
end

#started_atObject (readonly)

Returns the value of attribute started_at.



25
26
27
# File 'lib/thread/pool.rb', line 25

def started_at
  @started_at
end

#threadObject (readonly)

Returns the value of attribute thread.



25
26
27
# File 'lib/thread/pool.rb', line 25

def thread
  @thread
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



25
26
27
# File 'lib/thread/pool.rb', line 25

def timeout
  @timeout
end

Instance Method Details

#execute(thread) ⇒ Object

Execute the task in the given thread.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/thread/pool.rb', line 46

def execute (thread)
	return if terminated? || running? || finished?

	@thread     = thread
	@running    = true
	@started_at = Time.now

	pool.__send__ :wake_up_timeout

	begin
		@block.call(*@arguments)
	rescue Exception => reason
		if reason.is_a? Timeout
			@timedout = true
		elsif reason.is_a? Asked
			return
		else
			@exception = reason
		end
	end

	@running  = false
	@finished = true
	@thread   = nil
end

#finished?Boolean

Returns:

  • (Boolean)


41
# File 'lib/thread/pool.rb', line 41

def finished?;   @finished;   end

#raise(exception) ⇒ Object

Raise an exception in the thread used by the task.



73
74
75
# File 'lib/thread/pool.rb', line 73

def raise (exception)
	@thread.raise(exception)
end

#running?Boolean

Returns:

  • (Boolean)


40
# File 'lib/thread/pool.rb', line 40

def running?;    @running;   end

#terminate!(exception = Asked) ⇒ Object

Terminate the exception with an optionally given exception.



78
79
80
81
82
83
84
85
86
# File 'lib/thread/pool.rb', line 78

def terminate! (exception = Asked)
	return if terminated? || finished? || timeout?

	@terminated = true

	return unless running?

	self.raise exception
end

#terminated?Boolean

Returns:

  • (Boolean)


43
# File 'lib/thread/pool.rb', line 43

def terminated?; @terminated; end

#timeout!Object

Force the task to timeout.



89
90
91
# File 'lib/thread/pool.rb', line 89

def timeout!
	terminate! Timeout
end

#timeout?Boolean

Returns:

  • (Boolean)


42
# File 'lib/thread/pool.rb', line 42

def timeout?;    @timedout;   end

#timeout_after(time) ⇒ Object

Timeout the task after the given time.



94
95
96
97
98
99
100
# File 'lib/thread/pool.rb', line 94

def timeout_after (time)
	@timeout = time

	pool.timeout_for self, time

	self
end