stouset-threadpool 1.0.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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Stephen Touset
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = threadpool
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Stephen Touset. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "threadpool"
8
+ gem.summary = %Q{Parallelized, threaded enumeration, in Ruby}
9
+ gem.email = "stephen@touset.org"
10
+ gem.homepage = "http://github.com/stouset/threadpool"
11
+ gem.authors = ["Stephen Touset"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "threadpool #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/threadpool.rb ADDED
@@ -0,0 +1,74 @@
1
+ class ThreadPool
2
+ attr_reader :size
3
+ attr_reader :queue
4
+
5
+ #
6
+ # Creates a new ThreadPool with +size+ threads, waiting at your beck and
7
+ # call.
8
+ #
9
+ def initialize(size)
10
+ self.size = size
11
+ self.queue = Queue.new
12
+
13
+ # create a pool of threads the size requested
14
+ self.pool = Array.new(size) { thread }
15
+ end
16
+
17
+ #
18
+ # Schedules a +job+ to be run with the given +args+. Returns immediately.
19
+ #
20
+ def schedule(*args, &job)
21
+ self.queue.push [job, args]
22
+ end
23
+
24
+ #
25
+ # Schedules a +job+ to be run with the given +args+. Returns only once the
26
+ # queue of jobs to be performed is empty.
27
+ #
28
+ # This is primarily intended for preventing front-loading an arbitraril
29
+ # large number of jobs into the queue. However, it works simply by calling
30
+ # Thread.pass until the queue is empty. This has the disadvantage of
31
+ # spinning the processor the all the threads are mostly waiting on I/O or
32
+ # sleeping. It also means that if other threads are scheduling jobs, this
33
+ # may never return.
34
+ #
35
+ # Ideally, this method would pause the current thread until the job has
36
+ # begun execution, but this is surprisingly difficult to do with the Ruby
37
+ # built-in Queue class, which we use for the job queue.
38
+ #
39
+ def execute(*args, &job)
40
+ self.schedule(*args, &job)
41
+
42
+ Thread.pass until self.queue.empty?
43
+ end
44
+
45
+ #
46
+ # Cleans up after the ThreadPool, quitting and joining all remaining
47
+ # threads. No more jobs can be run in the pool once this method has been
48
+ # called.
49
+ #
50
+ def shutdown
51
+ self.size.times { self.schedule { Thread.exit } }
52
+ self.pool.each {|thread| thread.join }
53
+ end
54
+
55
+ protected
56
+
57
+ attr_writer :size
58
+ attr_writer :queue
59
+ attr_accessor :pool
60
+
61
+ #
62
+ # Instantiates a new thread. Each thread simply waits until there's a job
63
+ # on the queue, runs it, then returns back to the queue for more work. If
64
+ # the queue is empty, the thread sleeps until there is an available job.
65
+ #
66
+ def thread
67
+ Thread.new do
68
+ loop do
69
+ job, args = self.queue.pop
70
+ job[*args]
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ require 'threadpool'
8
+
9
+ Spec::Runner.configure do |config|
10
+
11
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ThreadPool do
4
+ subject { ThreadPool.new(4) }
5
+
6
+ it 'should report its size' do
7
+ subject.size.should == 4
8
+ end
9
+
10
+ it 'should have an empty queue of tasks' do
11
+ subject.queue.should be_empty
12
+ end
13
+
14
+ it 'should allow cleanup of its threads' do
15
+ subject.shutdown.each {|thread| thread.status.should be_false }
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stouset-threadpool
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Touset
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: stephen@touset.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/threadpool.rb
33
+ - spec/spec_helper.rb
34
+ - spec/threadpool_spec.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/stouset/threadpool
37
+ licenses:
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Parallelized, threaded enumeration, in Ruby
62
+ test_files:
63
+ - spec/spec_helper.rb
64
+ - spec/threadpool_spec.rb