splib 1.4 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 1.4.1
2
+ * Updates in Monitor to attempt to force
3
+ threads to stay put
4
+ * Fix Monitor's locking/unlocking
1
5
  1.4
2
6
  * Clean up exclusive usage in Monitor
3
7
  * Add a timeout on Monitor#wait
data/README.rdoc CHANGED
@@ -104,12 +104,17 @@ Find constants easily, especially within loaded modules
104
104
  end"
105
105
  )
106
106
  p Splib.find_const('String')
107
- p Splib.find_const('Fu::Bar', [mod])
107
+ fb = Splib.find_const('Fu::Bar', [mod]).new
108
+ p fb
109
+ p Splib.type_of?(fb, 'Fu::Bar')
110
+
108
111
 
109
112
  Results:
110
113
 
111
114
  String
112
- #<Module:0x95f02a4>::Fu::Bar
115
+ #<#<Module:0x95f2fd0>::Fu::Bar:0x95f287c>
116
+ true
117
+
113
118
 
114
119
  ==== PriorityQueue
115
120
 
@@ -135,6 +140,51 @@ Add some logic to item queueing
135
140
  fubar
136
141
  last
137
142
 
143
+ ==== Float
144
+
145
+ Need to know if your float is within a delta?
146
+
147
+ require 'splib'
148
+ Splib.load :Float
149
+
150
+ p 4.21.within_delta?(:expected => 4.30, :delta => 0.1)
151
+
152
+ => true
153
+
154
+ ==== Monitor
155
+
156
+ Annoyed that your monitor is generating thread owner errors? This simple
157
+ monitor does its best to ensure your threads stay where they are supposed.
158
+
159
+ require 'splib'
160
+ Splib.load :Monitor
161
+
162
+ output = []
163
+ go = false
164
+ monitor = Splib::Monitor.new
165
+ t = Thread.new{ monitor.wait_until{go}; output << :foo}
166
+ Thread.new{ monitor.wait_while{!go}; output << :bar}
167
+ Thread.new{ monitor.wait; output << :foobar }
168
+ Thread.pass
169
+ monitor.broadcast
170
+ t.wakeup
171
+ Thread.pass
172
+ p output
173
+ go = true
174
+ monitor.signal
175
+ Thread.pass
176
+ p output
177
+ Thread.pass
178
+ monitor.broadcast
179
+ Thread.pass
180
+ p output
181
+
182
+ Results:
183
+
184
+ [:foobar]
185
+ [:foobar, :foo]
186
+ [:foobar, :foo, :bar]
187
+
138
188
  ==== TODO: Write examples for CodeReloader and RandomIterator
139
189
 
140
190
  == Last remarks
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+ require 'spec/rake/spectask'
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = 'splib'
16
+ s.author = 'spox'
17
+ s.email = 'spox@modspox.com'
18
+ s.version = '1.4.1'
19
+ s.summary = 'Spox Library'
20
+ s.platform = Gem::Platform::RUBY
21
+ s.files = %w(LICENSE README.rdoc CHANGELOG Rakefile) + Dir.glob("{bin,lib,spec,test}/**/*")
22
+ s.rdoc_options = %w(--title splib --main README.rdoc --line-numbers)
23
+ s.extra_rdoc_files = %w(README.rdoc CHANGELOG)
24
+ s.require_paths = %w(lib)
25
+ s.required_ruby_version = '>= 1.8.6'
26
+ s.homepage = %q(http://github.com/spox/splib)
27
+ s.description = "The spox library contains various useful tools to help you in your day to day life. Like a trusty pocket knife, only more computery."
28
+ end
29
+
30
+ Rake::GemPackageTask.new(spec) do |p|
31
+ p.gem_spec = spec
32
+ p.need_tar = true
33
+ p.need_zip = true
34
+ end
35
+
36
+ Rake::RDocTask.new do |rdoc|
37
+ files =['README', 'LICENSE', 'lib/**/*.rb']
38
+ rdoc.rdoc_files.add(files)
39
+ rdoc.main = "README" # page to start on
40
+ rdoc.title = "splib Docs"
41
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
42
+ rdoc.options << '--line-numbers'
43
+ end
44
+
45
+ Rake::TestTask.new do |t|
46
+ t.test_files = FileList['test/**/*.rb']
47
+ end
48
+
49
+ Spec::Rake::SpecTask.new do |t|
50
+ t.spec_files = FileList['spec/**/*.rb']
51
+ t.libs << Dir["lib"]
52
+ end
data/lib/splib/Monitor.rb CHANGED
@@ -15,7 +15,12 @@ module Splib
15
15
  if(timeout)
16
16
  timout = timeout.to_f
17
17
  @timers[Thread.current] = Thread.new(Thread.current) do |t|
18
- sleep(timeout)
18
+ time = 0.0
19
+ until(time >= timeout) do
20
+ s = Time.now.to_f
21
+ sleep(timeout - time)
22
+ time += Time.now.to_f - s
23
+ end
19
24
  t.wakeup
20
25
  end
21
26
  end
@@ -64,7 +69,11 @@ module Splib
64
69
  end
65
70
  # Lock the monitor
66
71
  def lock
67
- Thread.stop if Thread.exclusive{ do_lock }
72
+ if(Thread.exclusive{do_lock})
73
+ until(owner?(Thread.current)) do
74
+ Thread.stop
75
+ end
76
+ end
68
77
  end
69
78
  # Unlock the monitor
70
79
  def unlock
@@ -87,9 +96,13 @@ module Splib
87
96
 
88
97
  private
89
98
 
99
+ def owner?(t)
100
+ @lock_owner == t
101
+ end
102
+
90
103
  def do_lock
91
104
  stop = false
92
- unless(@locks.empty?)
105
+ if(@lock_owner)
93
106
  @locks << Thread.current
94
107
  stop = true
95
108
  else
@@ -99,11 +112,11 @@ module Splib
99
112
  end
100
113
 
101
114
  def do_unlock
102
- unless(@lock_owner == Thread.current)
115
+ unless(owner?(Thread.current))
103
116
  raise ThreadError.new("Thread #{Thread.current} is not the current owner: #{@lock_owner}")
104
117
  end
105
118
  unless(@locks.empty?)
106
- @locks.delete{|t|!t.alive?}
119
+ @locks.delete_if{|t|!t.alive?}
107
120
  @lock_owner = @locks.shift
108
121
  @lock_owner.wakeup
109
122
  else
data/spec/dummy.rb ADDED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -21,10 +21,11 @@ class MonitorTest < Test::Unit::TestCase
21
21
  t = []
22
22
  o = []
23
23
  5.times{|i| t << Thread.new{ @monitor.wait((i+1)/100.0); o << 1 } }
24
- sleep(0.015)
24
+ sleep(0.011)
25
+ Thread.pass
25
26
  assert(!t.shift.alive?)
26
27
  assert_equal(1, o.size)
27
- sleep(0.11)
28
+ sleep(0.1)
28
29
  assert_equal(5, o.size)
29
30
  t.each do |th|
30
31
  assert(!th.alive?)
@@ -120,18 +121,28 @@ class MonitorTest < Test::Unit::TestCase
120
121
  def test_lock_unlock
121
122
  t = []
122
123
  output = []
123
- 5.times{|i| t << Thread.new{ @monitor.lock; sleep((i+1)/100.0); output << i; @monitor.unlock;}}
124
- sleep(0.011)
125
- assert_equal(5, t.size)
126
- assert_equal(0, output.pop)
127
- sleep(0.011)
128
- assert_equal(1, output.pop)
124
+ 3.times{|i| t << Thread.new{ @monitor.lock; sleep((i+1)/100.0); output << i; @monitor.unlock;}}
129
125
  sleep(0.011)
130
- assert_equal(2, output.pop)
131
- sleep(0.011)
132
- assert_equal(3, output.pop)
133
- sleep(0.011)
134
- assert_equal(4, output.pop)
126
+ assert_equal(1, output.size)
127
+ sleep(0.021)
128
+ assert_equal(2, output.size)
129
+ sleep(0.031)
130
+ assert_equal(3, output.size)
131
+ assert(!t.any?{|th|th.alive?})
132
+ 3.times{|i|assert_equal(i, output.shift)}
133
+ end
134
+
135
+ def test_lock_unlock_wakeup
136
+ complete = false
137
+ t1 = Thread.new{@monitor.lock; sleep(0.1); @monitor.unlock}
138
+ t2 = Thread.new{@monitor.lock; complete = true; @monitor.unlock}
139
+ assert(!complete)
140
+ assert(t1.alive?)
141
+ t2.wakeup
142
+ Thread.pass
143
+ assert(!complete)
144
+ sleep(0.11)
145
+ assert(complete)
135
146
  end
136
147
 
137
148
  def synchronize
File without changes
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: splib
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.4"
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - spox
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-13 00:00:00 -08:00
12
+ date: 2010-01-19 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,19 +23,10 @@ extra_rdoc_files:
23
23
  - README.rdoc
24
24
  - CHANGELOG
25
25
  files:
26
- - tests/cases/Array.rb
27
- - tests/cases/PriorityQueue.rb
28
- - tests/cases/UrlShorteners.rb
29
- - tests/cases/Exec.rb
30
- - tests/cases/Constants.rb
31
- - tests/cases/Float.rb
32
- - tests/cases/CodeReloader.rb
33
- - tests/cases/HumanIdealRandomIterator.rb
34
- - tests/cases/Monitor.rb
35
- - tests/cases/Conversions.rb
36
- - tests/run_tests.rb
37
- - tests/samplecode2.rb
38
- - tests/samplecode1.rb
26
+ - LICENSE
27
+ - README.rdoc
28
+ - CHANGELOG
29
+ - Rakefile
39
30
  - lib/splib.rb
40
31
  - lib/splib/Array.rb
41
32
  - lib/splib/PriorityQueue.rb
@@ -47,11 +38,20 @@ files:
47
38
  - lib/splib/HumanIdealRandomIterator.rb
48
39
  - lib/splib/Monitor.rb
49
40
  - lib/splib/Conversions.rb
50
- - CHANGELOG
51
- - LICENSE
52
- - splib.gemspec
53
- - README.rdoc
54
- - splib-1.4.gem
41
+ - spec/dummy.rb
42
+ - test/cases/Array.rb
43
+ - test/cases/PriorityQueue.rb
44
+ - test/cases/UrlShorteners.rb
45
+ - test/cases/Exec.rb
46
+ - test/cases/Constants.rb
47
+ - test/cases/Float.rb
48
+ - test/cases/CodeReloader.rb
49
+ - test/cases/HumanIdealRandomIterator.rb
50
+ - test/cases/Monitor.rb
51
+ - test/cases/Conversions.rb
52
+ - test/run_tests.rb
53
+ - test/samplecode2.rb
54
+ - test/samplecode1.rb
55
55
  has_rdoc: true
56
56
  homepage: http://github.com/spox/splib
57
57
  licenses: []
data/splib.gemspec DELETED
@@ -1,15 +0,0 @@
1
- spec = Gem::Specification.new do |s|
2
- s.name = 'splib'
3
- s.author = 'spox'
4
- s.email = 'spox@modspox.com'
5
- s.version = '1.4'
6
- s.summary = 'Spox Library'
7
- s.platform = Gem::Platform::RUBY
8
- s.files = Dir['**/*']
9
- s.rdoc_options = %w(--title splib --main README.rdoc --line-numbers)
10
- s.extra_rdoc_files = %w(README.rdoc CHANGELOG)
11
- s.require_paths = %w(lib)
12
- s.required_ruby_version = '>= 1.8.6'
13
- s.homepage = %q(http://github.com/spox/splib)
14
- s.description = "The spox library contains various useful tools to help you in your day to day life. Like a trusty pocket knife, only more computery."
15
- end