@@ -10,6 +10,7 @@ module Async
10
10
#
11
11
# @private
12
12
class WorkerPool
13
+ # Used to augment the scheduler to add support for blocking operations.
13
14
module BlockingOperationWait
14
15
# Wait for the given work to be executed.
15
16
#
@@ -23,7 +24,11 @@ def blocking_operation_wait(work)
23
24
end
24
25
end
25
26
27
+ # Execute the given work in a background thread.
26
28
class Promise
29
+ # Create a new promise.
30
+ #
31
+ # @parameter work [Proc] The work to be done.
27
32
def initialize ( work )
28
33
@work = work
29
34
@state = :pending
@@ -33,6 +38,7 @@ def initialize(work)
33
38
@thread = nil
34
39
end
35
40
41
+ # Execute the work and resolve the promise.
36
42
def call
37
43
work = nil
38
44
@@ -67,6 +73,7 @@ def call
67
73
end
68
74
end
69
75
76
+ # Cancel the work and raise an exception in the background thread.
70
77
def cancel
71
78
return unless @work
72
79
@@ -77,6 +84,9 @@ def cancel
77
84
end
78
85
end
79
86
87
+ # Wait for the work to be done.
88
+ #
89
+ # @returns [Object] The result of the work.
80
90
def wait
81
91
@guard . synchronize do
82
92
while @state == :pending
@@ -92,19 +102,22 @@ def wait
92
102
end
93
103
end
94
104
95
- # A handle to the work being done .
105
+ # A background worker thread .
96
106
class Worker
107
+ # Create a new worker.
97
108
def initialize
98
109
@work = ::Thread ::Queue . new
99
110
@thread = ::Thread . new ( &method ( :run ) )
100
111
end
101
112
113
+ # Execute work until the queue is closed.
102
114
def run
103
115
while work = @work . pop
104
116
work . call
105
117
end
106
118
end
107
119
120
+ # Close the worker thread.
108
121
def close
109
122
if thread = @thread
110
123
@thread = nil
0 commit comments