hyper_thread 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ff934f8b1cf5c637b8c2b8ec3e7cc3cc2d02a4a
4
+ data.tar.gz: 93e6e09be667cb2590b2f89de8649c6bffff26e2
5
+ SHA512:
6
+ metadata.gz: 326de2d3a0c88fc6d45997beb6d03bcb9f485df8544a4a6947fe21967dc8bf353191c48fb298ebad8bafc56c9be90849340cf4911d560878dc47515684970ff8
7
+ data.tar.gz: 2093b5a862a2e5a6efc5e3640266bb9e508319ef1f6d9d9dd6e5ef7a2479c1967ade03511ae061fcf8f22c255a87d7612c2dba788c76a3ccaf4181740d3525af
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.15.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kgruber1@emich.edu. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hyper_thread.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Kent Gruber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,55 @@
1
+ ![Hyper Thread Header](https://i.imgur.com/di45BuN.png)
2
+
3
+ # Hyper Thread
4
+
5
+ Hyper Thread is simple, flexible thread pool library for Ruby.
6
+
7
+ ## Installation
8
+
9
+ $ gem install hyper_thread
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require 'hyper_thread'
15
+
16
+ # make a pool of maximum 7 threads
17
+ pool = HyperThread.pool.new(max: 10)
18
+
19
+ # spawn 7 threads, and 3 jobs will be added to the queue
20
+ pool.async(count: 10) do
21
+ puts "some work to do"
22
+ end
23
+
24
+ # spool off queued jobs outside of thread pool
25
+ pool.todo(count: pool.queue.size) do |block|
26
+ block.call # "some work to do"
27
+ end
28
+
29
+ # kill off any dead threads
30
+ pool.reap
31
+
32
+ # queue up 300 jobs
33
+ counter = 0
34
+
35
+ pool.qsync(count: 300) do
36
+ puts "some work to do #{counter += 1}"
37
+ end
38
+
39
+ # spawn 2 threads to manage the queue forever
40
+ pool.async(forever: true, count: 2) do
41
+ pool.todo do |block|
42
+ block.call
43
+ end
44
+ end
45
+
46
+ # let them manage that queue for roughly 2 seconds
47
+ sleep 2
48
+
49
+ # shutdown the thread pool
50
+ pool.shutdown
51
+ ```
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hyper_thread"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'hyper_thread'
4
+ require 'pry'
5
+
6
+ binding.pry
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'hyper_thread'
4
+
5
+ # spawn thread pool
6
+ pool = HyperThread.pool.new(max: 7)
7
+
8
+ # ask to spawn more threads than max value
9
+ pool.async(count: 9) do
10
+ "this is some work"
11
+ end
12
+
13
+ # the extra jobs are in a queue
14
+ pool.queue.size
15
+ # => 2
16
+
17
+ # spool off any queue'd jobs outside of pool
18
+ pool.todo(count: pool.queue.size) do |block|
19
+ puts block.call
20
+ end
21
+
22
+ # shutdown pool
23
+ pool.shutdown
24
+
25
+
26
+
@@ -0,0 +1,44 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'hyper_thread'
4
+
5
+ # make a pool of maximum 7 threads
6
+ pool = HyperThread.pool.new(max: 10)
7
+
8
+ # spawn 7 threads, and 3 jobs will be added to the queue
9
+ pool.async(count: 10) do
10
+ puts "some work to do"
11
+ end
12
+
13
+ # spool off queued jobs
14
+ pool.todo(count: pool.queue.size) do |block|
15
+ block.call # "some work to do"
16
+ end
17
+
18
+ # kill off any dead threads
19
+ pool.reap
20
+
21
+ # queue up 300 jobs
22
+ counter = 0
23
+
24
+ pool.qsync(count: 300) do
25
+ puts "some work to do #{counter += 1}"
26
+ end
27
+
28
+ # spawn 2 threads to manage the queue forever
29
+ pool.async(forever: true, count: 2) do
30
+ pool.todo do |block|
31
+ block.call
32
+ end
33
+ end
34
+
35
+ pool.qsync(count: 300) do
36
+ "some work to do #{counter += 1}"
37
+ end
38
+
39
+ # let them manage that queue for roughly 2 seconds
40
+ sleep 2
41
+
42
+ # shutdown the thread pool
43
+ puts "Shutting down #{pool.threads.count} threads."
44
+ pool.shutdown
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'hyper_thread'
4
+ require 'pcaprub'
5
+
6
+ cap_pool = HyperThread.pool.new(max: 1)
7
+
8
+ packets = Queue.new
9
+
10
+ puts "starting capture"
11
+
12
+ cap_pool.async do
13
+ cap = PCAPRUB::Pcap.open_live(Pcap.lookupdev, 65535, true, 0)
14
+ loop do
15
+ next unless packet = cap.next
16
+ packets << packet
17
+ end
18
+ end
19
+
20
+ puts "starting parse pool"
21
+
22
+ parse_pool = HyperThread.pool.new(max: 5)
23
+
24
+ parse_pool.async(forever: true, count: parse_pool.max) do
25
+ next unless packet = packets.pop
26
+ puts packet.size
27
+ end
28
+
29
+ sleep 2 # let it run for roughly 2 seconds
30
+
31
+ puts "shutting down capture and parsing pools"
32
+ [ cap_pool, parse_pool ].each do |pool|
33
+ pool.shutdown
34
+ end
35
+
36
+ exit 0
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hyper_thread/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hyper_thread"
8
+ spec.version = HyperThread::VERSION
9
+ spec.authors = ["Kent 'picat' Gruber"]
10
+ spec.email = ["kgruber1@emich.edu"]
11
+
12
+ spec.summary = %q{Hyper Thread is simple, flexible thread pool library for Ruby.}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/picatz/hyper_thread"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "lolize"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'thread'
2
+ require "hyper_thread/version"
3
+ require "hyper_thread/pool"
4
+ require "hyper_thread/thread"
5
+
6
+ # Some code copyright (c) 2005, Zed Shaw
7
+ # Some code Copyright (c) 2011, Evan Phoenix
8
+ # Some code Copyright (c) 2015, Simone Margaritelli
9
+
10
+ # I basically was refactoring the ThreadPool in BetterCap and made this.
11
+
12
+ module HyperThread
13
+
14
+ def self.easter_egg
15
+ require 'lolize/auto'
16
+ rescue
17
+ raise "Please install lolize!\n$ gem install lolize"
18
+ end
19
+
20
+ def self.pool
21
+ Pool
22
+ end
23
+
24
+ def self.thread
25
+ HThread
26
+ end
27
+
28
+ end
@@ -0,0 +1,127 @@
1
+ module HyperThread
2
+ class Pool
3
+ attr_reader :queue
4
+
5
+ def initialize(max: 2)
6
+ @threads = []
7
+ @mutex = Mutex.new
8
+ @queue = Queue.new
9
+ @max = max.to_i
10
+ end
11
+
12
+ def max=(value)
13
+ nsync do
14
+ if value > @max
15
+ dead = @threads.sample(value).map(&:exit)
16
+ @threads -= dead
17
+ end
18
+ @max = value
19
+ return true
20
+ end
21
+ end
22
+
23
+ def max
24
+ @max
25
+ end
26
+
27
+ def threads
28
+ return @threads unless block_given?
29
+ @threads.each do |thread|
30
+ yield thread
31
+ end
32
+ end
33
+
34
+ def shutdown
35
+ @shutdown = true
36
+ dead = @threads.map(&:exit)
37
+ @threads -= dead
38
+ end
39
+
40
+ def reap
41
+ dead = @threads.reject(&:alive?)
42
+ dead.map(&:kill)
43
+ @threads -= dead
44
+ return true
45
+ end
46
+
47
+ def nsync(&block)
48
+ @mutex.synchronize do
49
+ break if @shutdown
50
+ block.call
51
+ end
52
+ return if @shutdown
53
+ end
54
+
55
+ def async(forever: false, count: 1, &block)
56
+ raise "Required block syntax" unless block_given?
57
+ count.times do
58
+ if @threads.count == max
59
+ @queue << block
60
+ next
61
+ end
62
+ self << Thread.new do
63
+ if forever
64
+ loop do
65
+ nsync do
66
+ block.call
67
+ end
68
+ end
69
+ else
70
+ nsync do
71
+ block.call
72
+ end
73
+ end
74
+ end
75
+ end
76
+ return @queue if @threads.count == max
77
+ @threads
78
+ end
79
+
80
+ def todo(forever: false, count: false)
81
+ raise "Required block syntax" unless block_given?
82
+ if count
83
+ count.times do
84
+ yield @queue.pop
85
+ end
86
+ else
87
+ if forever
88
+ loop do
89
+ while @queue.size > 0
90
+ yield @queue.pop
91
+ end
92
+ end
93
+ else
94
+ yield @queue.pop
95
+ end
96
+ end
97
+ end
98
+
99
+ def qsync(count: 1, &block)
100
+ nsync do
101
+ raise "Required block syntax" unless block_given?
102
+ Thread.new do
103
+ count.times do
104
+ @queue << block
105
+ end
106
+ end
107
+ true
108
+ end
109
+ end
110
+
111
+ def todo?
112
+ nsync do
113
+ return true if @queue.size > 0
114
+ false
115
+ end
116
+ end
117
+
118
+ def <<(thread)
119
+ nsync do
120
+ return false if @threads.count == max
121
+ @threads << thread
122
+ end
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -0,0 +1,6 @@
1
+ module HyperThread
2
+ class HThread
3
+ # @TODO
4
+ end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module HyperThread
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hyper_thread
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kent 'picat' Gruber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lolize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - kgruber1@emich.edu
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - examples/debug.rb
87
+ - examples/pool_example.rb
88
+ - examples/readme_example.rb
89
+ - examples/with_pcap.rb
90
+ - hyper_thread.gemspec
91
+ - lib/hyper_thread.rb
92
+ - lib/hyper_thread/pool.rb
93
+ - lib/hyper_thread/thread.rb
94
+ - lib/hyper_thread/version.rb
95
+ homepage: https://github.com/picatz/hyper_thread
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Hyper Thread is simple, flexible thread pool library for Ruby.
119
+ test_files: []