thread 0.0.8.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/thread/pool.rb +52 -2
  3. data/thread.gemspec +1 -1
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d257856d0bf60a4515f3512f44e1ca7f72ac22c
4
- data.tar.gz: 99094a0eff5d5e031230d872a67f30730d025730
3
+ metadata.gz: 136683911f76264b8d9c47d66607cd3d5b2d9d34
4
+ data.tar.gz: 8e669b7e1cad8f10467f45c56b0e7887b51ab9d8
5
5
  SHA512:
6
- metadata.gz: dbe9b316f2bdd6b3c141180de8be77bd8a62ffa2ddfee531a6f63bc39bcc3b0ca263688037549c6b3c479a55f4ab710112871da4ed3f4e7070fd8eab6e8ee622
7
- data.tar.gz: ac27c137c50fdc797ca56a0ebf6d2578634dcc86d007959fc3f55bf2ad0ba5f2d4715c04e1bb9de3f3134cab47efb18091e7571bdb661c049cf90ecd934d1521
6
+ metadata.gz: 657218c2a64a1ef1e5593a6b73d3f29abe376d6c31757220aed94bb742abcd2aa0c743c9dfe1aa168839c0d9d547d01361ddea351db206c6b8a1d140fc53a316
7
+ data.tar.gz: b2546abceef43fd444a6acfb950abae2c5c164cab5bd4733b1296fa836ee7dde616fa80af86856f643a4d485363428b61dbaebecfb53e7acdfc9b7dadf03d4ee
data/lib/thread/pool.rb CHANGED
@@ -117,6 +117,9 @@ class Thread::Pool
117
117
  @cond = ConditionVariable.new
118
118
  @mutex = Mutex.new
119
119
 
120
+ @done = ConditionVariable.new
121
+ @done_mutex = Mutex.new
122
+
120
123
  @todo = []
121
124
  @workers = []
122
125
  @timeouts = {}
@@ -126,6 +129,7 @@ class Thread::Pool
126
129
  @shutdown = false
127
130
  @trim_requests = 0
128
131
  @auto_trim = false
132
+ @idle_trim = nil
129
133
 
130
134
  @mutex.synchronize {
131
135
  min.times {
@@ -153,6 +157,22 @@ class Thread::Pool
153
157
  @auto_trim = false
154
158
  end
155
159
 
160
+ # Check if idle trimming is enabled.
161
+ def idle_trim?
162
+ !@idle_trim.nil?
163
+ end
164
+
165
+ # Enable idle trimming. Unneeded threads will be deleted after the given number of seconds of inactivity.
166
+ # The minimum number of threads is respeced.
167
+ def idle_trim!(timeout)
168
+ @idle_trim = timeout
169
+ end
170
+
171
+ # Turn of idle trimming.
172
+ def no_idle_trim!
173
+ @idle_trim = nil
174
+ end
175
+
156
176
  # Resize the pool with the passed arguments.
157
177
  def resize (min, max = nil)
158
178
  @min = min
@@ -168,6 +188,19 @@ class Thread::Pool
168
188
  }
169
189
  end
170
190
 
191
+ # Are all tasks consumed ?
192
+ def done?
193
+ @todo.empty? and @waiting == @spawned
194
+ end
195
+
196
+ # Wait until all tasks are consumed. The caller will be blocked until then.
197
+ def wait_done
198
+ @done_mutex.synchronize {
199
+ return if done?
200
+ @done.wait @done_mutex
201
+ }
202
+ end
203
+
171
204
  # Add a task to the pool which will execute the block with the given
172
205
  # argument.
173
206
  #
@@ -202,7 +235,7 @@ class Thread::Pool
202
235
  def trim (force = false)
203
236
  @mutex.synchronize {
204
237
  if (force || @waiting > 0) && @spawned - @trim_requests > @min
205
- @trim_requests -= 1
238
+ @trim_requests += 1
206
239
  @cond.signal
207
240
  end
208
241
  }
@@ -306,7 +339,17 @@ private
306
339
  break if shutdown?
307
340
 
308
341
  @waiting += 1
309
- @cond.wait @mutex
342
+
343
+ report_done
344
+
345
+ if @idle_trim and @spawned > @min
346
+ check_time = Time.now + @idle_trim
347
+ @cond.wait @mutex, @idle_trim
348
+ @trim_requests += 1 if Time.now >= check_time && @spawned - @trim_requests > @min
349
+ else
350
+ @cond.wait @mutex
351
+ end
352
+
310
353
  @waiting -= 1
311
354
  end
312
355
 
@@ -368,6 +411,13 @@ private
368
411
  end
369
412
  }
370
413
  end
414
+
415
+ def report_done
416
+ @done_mutex.synchronize {
417
+ @done.broadcast if done?
418
+ }
419
+ end
420
+
371
421
  end
372
422
 
373
423
  class Thread
data/thread.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new {|s|
2
2
  s.name = 'thread'
3
- s.version = '0.0.8.1'
3
+ s.version = '0.1.0'
4
4
  s.author = 'meh.'
5
5
  s.email = 'meh@schizofreni.co'
6
6
  s.homepage = 'http://github.com/meh/ruby-thread'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - meh.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-27 00:00:00.000000000 Z
11
+ date: 2013-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubyforge_project:
86
- rubygems_version: 2.0.0
86
+ rubygems_version: 2.0.3
87
87
  signing_key:
88
88
  specification_version: 4
89
89
  summary: Various extensions to the base thread library.