threadpuddle 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/threadpuddle.rb +83 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 88a3409088076ee968a4af551e72c1c5d769ce0f
|
4
|
+
data.tar.gz: dab7980497489aeeed8f5d022974e31150e2ddc7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7475f7a25dfaf771bb2af4b3f47ef3168f927b368f2b580103bd7f09e63831f630cd4d2974a3f06e778fca9cd532a4b1353bc952aaed2e1df0109c8baf8a40a
|
7
|
+
data.tar.gz: 95a9d46bd9aa611b5f4533243a248b68dba7ee853d0d03167cf72d09ca399933f06e1e2e26d656f5fcdf0e23a3a01ba80be2779d5849881ddeb2fbc9fcb91135
|
data/lib/threadpuddle.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
#
|
2
|
+
# Like a smaller, lamer thread pool.
|
3
|
+
#
|
4
|
+
class ThreadPuddle
|
5
|
+
def initialize capacity=10
|
6
|
+
@capacity = capacity
|
7
|
+
@threads = []
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :capacity
|
11
|
+
|
12
|
+
#
|
13
|
+
# Blocks execution of the calling thread until there's a free
|
14
|
+
# slot in the puddle.
|
15
|
+
#
|
16
|
+
# WARNING: there is no guarantee this will ever return.
|
17
|
+
#
|
18
|
+
def block
|
19
|
+
loop {
|
20
|
+
return self if @threads.length < @capacity
|
21
|
+
if @threads.any?{|t| t.join(0) }
|
22
|
+
sweep
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
#
|
28
|
+
# Current size of the puddle.
|
29
|
+
#
|
30
|
+
def size
|
31
|
+
sweep
|
32
|
+
@threads.length
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Spawns a new thread in the puddle.
|
37
|
+
#
|
38
|
+
# If the puddle is full, this call blocks.
|
39
|
+
#
|
40
|
+
# @see ThreadPuddle#block
|
41
|
+
# @return the new Thread object
|
42
|
+
#
|
43
|
+
def spawn *args, &blk
|
44
|
+
# wait for a slot to open
|
45
|
+
block
|
46
|
+
# add the new thread
|
47
|
+
@threads << (t = Thread.new(*args, &blk))
|
48
|
+
t
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Waits for all threads in the puddle to join.
|
53
|
+
#
|
54
|
+
# @return this ThreadPuddle object
|
55
|
+
#
|
56
|
+
def join
|
57
|
+
while @threads.any?
|
58
|
+
sweep
|
59
|
+
@threads.each{|t| t.join rescue nil }
|
60
|
+
end
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Kills all threads in the puddle.
|
66
|
+
#
|
67
|
+
# @return the number of threads killed
|
68
|
+
#
|
69
|
+
def kill
|
70
|
+
l = @threads.each{|t| t.kill rescue nil }.length
|
71
|
+
sweep
|
72
|
+
l
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
# sweep the puddle, delete dead threads
|
78
|
+
def sweep #:nodoc:
|
79
|
+
@threads.delete_if {|t| ! t.alive? }
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: threadpuddle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Kerwin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Create threads, with an upper bound on how many can exist at the same
|
14
|
+
time.
|
15
|
+
email:
|
16
|
+
- matthew@kerwin.net.au
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/threadpuddle.rb
|
22
|
+
homepage: http://rubygems.org/gems/threadpuddle
|
23
|
+
licenses:
|
24
|
+
- Simplified BSD License
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.2
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Like a smaller, lamer thread pool.
|
46
|
+
test_files: []
|