Class: HQ::Tools::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/tools/future.rb

Instance Method Summary collapse

Constructor Details

#initialize(thread_pool, *args, &block) ⇒ Future

Returns a new instance of Future.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hq/tools/future.rb', line 6

def initialize thread_pool, *args, &block

	@mutex =
		Mutex.new

	@resource =
		ConditionVariable.new

	@result =
		nil

	thread_pool.schedule do

		begin

			@result =
				block.call *args

			@result_type =
				:return

		rescue => exception

			@result =
				exception

			@result_type =
				:exception

		ensure

			@mutex.synchronize do
				@resource.broadcast
			end

		end

	end

end

Instance Method Details

#getObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hq/tools/future.rb', line 47

def get

	@mutex.synchronize do

		loop do

			case @result_type

				when :return

					return @result

				when :exception

					raise @result

				else

					@resource.wait \
						@mutex

			end

		end

	end

end