Class: Thread::Pool

Inherits:
Object show all
Defined in:
lib/storyboard/thread-util.rb

Defined Under Namespace

Classes: Task

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, max = nil, &block) ⇒ Pool

Returns a new instance of Pool.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/storyboard/thread-util.rb', line 81

def initialize (min, max = nil, &block)
  @min   = min
  @max   = max || min
  @block = block

  @cond  = ConditionVariable.new
  @mutex = Mutex.new

  @todo     = []
  @workers  = []
  @timeouts = {}

  @spawned       = 0
  @waiting       = 0
  @shutdown      = false
  @trim_requests = 0
  @auto_trim     = false

  @mutex.synchronize {
    min.times {
      spawn_thread
    }
  }
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



79
80
81
# File 'lib/storyboard/thread-util.rb', line 79

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



79
80
81
# File 'lib/storyboard/thread-util.rb', line 79

def min
  @min
end

#spawnedObject (readonly)

Returns the value of attribute spawned.



79
80
81
# File 'lib/storyboard/thread-util.rb', line 79

def spawned
  @spawned
end

Instance Method Details

#auto_trim!Object



109
# File 'lib/storyboard/thread-util.rb', line 109

def auto_trim!;    @auto_trim = true;  end

#auto_trim?Boolean

Returns:

  • (Boolean)


108
# File 'lib/storyboard/thread-util.rb', line 108

def auto_trim?;    @auto_trim;         end

#backlogObject



119
120
121
122
123
# File 'lib/storyboard/thread-util.rb', line 119

def backlog
  @mutex.synchronize {
    @todo.length
  }
end

#joinObject



194
195
196
197
198
# File 'lib/storyboard/thread-util.rb', line 194

def join
  @workers.first.join until @workers.empty?

  self
end

#no_auto_trim!Object



110
# File 'lib/storyboard/thread-util.rb', line 110

def no_auto_trim!; @auto_trim = false; end

#process(*args, &block) ⇒ Object Also known as: <<



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/storyboard/thread-util.rb', line 125

def process (*args, &block)
  unless block || @block
    raise ArgumentError, 'you must pass a block'
  end

  task = Task.new(self, *args, &(block || @block))

  @mutex.synchronize {
    raise 'unable to add work while shutting down' if shutdown?

    @todo << task

    if @waiting == 0 && @spawned < @max
      spawn_thread
    end

    @cond.signal
  }

  task
end

#resize(min, max = nil) ⇒ Object



112
113
114
115
116
117
# File 'lib/storyboard/thread-util.rb', line 112

def resize (min, max = nil)
  @min = min
  @max = max || min

  trim!
end

#shutdownObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/storyboard/thread-util.rb', line 175

def shutdown
  @mutex.synchronize {
    @shutdown = :nicely
    @cond.broadcast
  }

  join

  if @timeout
    @shutdown = :now

    wake_up_timeout

    @timeout.join
  end

  self
end

#shutdown!Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/storyboard/thread-util.rb', line 164

def shutdown!
  @mutex.synchronize {
    @shutdown = :now
    @cond.broadcast
  }

  wake_up_timeout

  self
end

#shutdown?Boolean

Returns:

  • (Boolean)


106
# File 'lib/storyboard/thread-util.rb', line 106

def shutdown?; !!@shutdown; end

#shutdown_after(timeout) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/storyboard/thread-util.rb', line 212

def shutdown_after (timeout)
  Thread.new {
    sleep timeout

    shutdown
  }

  self
end

#timeout_for(task, timeout) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/storyboard/thread-util.rb', line 200

def timeout_for (task, timeout)
  unless @timeout
    spawn_timeout_thread
  end

  @mutex.synchronize {
    @timeouts[task] = timeout

    wake_up_timeout
  }
end

#trim(force = false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/storyboard/thread-util.rb', line 149

def trim (force = false)
  @mutex.synchronize {
    if (force || @waiting > 0) && @spawned - @trim_requests > @min
      @trim_requests -= 1
      @cond.signal
    end
  }

  self
end

#trim!Object



160
161
162
# File 'lib/storyboard/thread-util.rb', line 160

def trim!
  trim true
end

#wake_up_timeoutObject



222
223
224
225
226
# File 'lib/storyboard/thread-util.rb', line 222

def wake_up_timeout
  if @pipes
    @pipes.last.write_nonblock 'x' rescue nil
  end
end