multi_op_queue 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 495fd7b92b83ff144c091ac3e9f9124a6830714e
4
+ data.tar.gz: 307b88bd5f82abbfdaf30345db0310704bf55595
5
+ SHA512:
6
+ metadata.gz: 867d4a9e939310882713f7ad0d1dff2750513e177a484fd096c4f8beccaa7a9f1f5edfc736a052e480031c56e7d3711c6c669a46279e0b0c3c5fa9cc108d9ea9
7
+ data.tar.gz: 119f36efa5cbcd455e5fceeafbd9e5abcbd421f1cc7d9a729ad0329052818280d038f7ec40430750be4495f8526398471e0c3a8e07165638a4cbef0e936a8190
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .ruby-*
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi_op_queue.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # MultiOpQueue
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/multi_op_queue`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'multi_op_queue'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install multi_op_queue
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/multi_op_queue.
36
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << "spec"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multi_op_queue"
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
data/bin/setup ADDED
@@ -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,3 @@
1
+ module MultiOpQueue
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,169 @@
1
+ require "multi_op_queue/version"
2
+
3
+ module MultiOpQueue
4
+ # Original Queue implementation from Ruby-2.0.0
5
+ # https://github.com/ruby/ruby/blob/ruby_2_0_0/lib/thread.rb
6
+ #
7
+ # This class provides a way to synchronize communication between threads.
8
+ #
9
+ # Example:
10
+ #
11
+ # require 'thread'
12
+ #
13
+ # queue = Queue.new
14
+ #
15
+ # producer = Thread.new do
16
+ # 5.times do |i|
17
+ # sleep rand(i) # simulate expense
18
+ # queue << i
19
+ # puts "#{i} produced"
20
+ # end
21
+ # end
22
+ #
23
+ # consumer = Thread.new do
24
+ # 5.times do |i|
25
+ # value = queue.pop
26
+ # sleep rand(i/2) # simulate expense
27
+ # puts "consumed #{value}"
28
+ # end
29
+ # end
30
+ #
31
+ # consumer.join
32
+ #
33
+ class Queue
34
+ #
35
+ # Creates a new queue.
36
+ #
37
+ def initialize
38
+ @que = []
39
+ @que.taint # enable tainted communication
40
+ @num_waiting = 0
41
+ self.taint
42
+ @mutex = Mutex.new
43
+ @cond = ConditionVariable.new
44
+ end
45
+
46
+ #
47
+ # Pushes +obj+ to the queue.
48
+ #
49
+ def push(obj)
50
+ Thread.handle_interrupt(StandardError => :on_blocking) do
51
+ @mutex.synchronize do
52
+ @que.push obj
53
+ @cond.signal
54
+ end
55
+ end
56
+ end
57
+
58
+ #
59
+ # Alias of push
60
+ #
61
+ alias << push
62
+
63
+ #
64
+ # Alias of push
65
+ #
66
+ alias enq push
67
+
68
+ #
69
+ # Retrieves data from the queue. If the queue is empty, the calling thread is
70
+ # suspended until data is pushed onto the queue. If +non_block+ is true, the
71
+ # thread isn't suspended, and an exception is raised.
72
+ #
73
+ def pop(non_block=false)
74
+ Thread.handle_interrupt(StandardError => :on_blocking) do
75
+ @mutex.synchronize do
76
+ while true
77
+ if @que.empty?
78
+ if non_block
79
+ raise ThreadError, "queue empty"
80
+ else
81
+ begin
82
+ @num_waiting += 1
83
+ @cond.wait @mutex
84
+ ensure
85
+ @num_waiting -= 1
86
+ end
87
+ end
88
+ else
89
+ return @que.shift
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ #
97
+ # Alias of pop
98
+ #
99
+ alias shift pop
100
+
101
+ #
102
+ # Alias of pop
103
+ #
104
+ alias deq pop
105
+
106
+ #
107
+ # Retrieves data from the queue and returns array of contents.
108
+ # If +num_to_pop+ are available in the queue then multiple elements are returned in array response
109
+ # If the queue is empty, the calling thread is
110
+ # suspended until data is pushed onto the queue. If +non_block+ is true, the
111
+ # thread isn't suspended, and an exception is raised.
112
+ #
113
+ def pop_up_to(num_to_pop = 1, non_block=false)
114
+ Thread.handle_interrupt(StandardError => :on_blocking) do
115
+ @mutex.synchronize do
116
+ while true
117
+ if @que.empty?
118
+ if non_block
119
+ raise ThreadError, "queue empty"
120
+ else
121
+ begin
122
+ @num_waiting += 1
123
+ @cond.wait @mutex
124
+ ensure
125
+ @num_waiting -= 1
126
+ end
127
+ end
128
+ else
129
+ return @que.shift(num_to_pop)
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ #
137
+ # Returns +true+ if the queue is empty.
138
+ #
139
+ def empty?
140
+ @que.empty?
141
+ end
142
+
143
+ #
144
+ # Removes all objects from the queue.
145
+ #
146
+ def clear
147
+ @que.clear
148
+ end
149
+
150
+ #
151
+ # Returns the length of the queue.
152
+ #
153
+ def length
154
+ @que.length
155
+ end
156
+
157
+ #
158
+ # Alias of length.
159
+ #
160
+ alias size length
161
+
162
+ #
163
+ # Returns the number of threads waiting on the queue.
164
+ #
165
+ def num_waiting
166
+ @num_waiting
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_op_queue/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multi_op_queue"
8
+ spec.version = MultiOpQueue::VERSION
9
+ spec.authors = ["Brandon Dewitt"]
10
+ spec.email = ["brandonsdewitt@gmail.com"]
11
+
12
+ spec.summary = %q{ allow multi-op calls on Queue while holding lock }
13
+ spec.description = %q{ take default Queue from Ruby and allow multiple operations while holding lock }
14
+ spec.homepage = "https://github.com/abrandoned/multi_op_queue"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ spec.add_development_dependency "mocha"
36
+ spec.add_development_dependency "pry"
37
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_op_queue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: " take default Queue from Ruby and allow multiple operations while holding
84
+ lock "
85
+ email:
86
+ - brandonsdewitt@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/multi_op_queue.rb
99
+ - lib/multi_op_queue/version.rb
100
+ - multi_op_queue.gemspec
101
+ homepage: https://github.com/abrandoned/multi_op_queue
102
+ licenses: []
103
+ metadata:
104
+ allowed_push_host: https://rubygems.org
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.5.1
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: allow multi-op calls on Queue while holding lock
125
+ test_files: []