thread 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -2
- data/README.md +34 -0
- data/Rakefile +1 -7
- data/lib/thread/future.rb +6 -0
- data/lib/thread/pipe.rb +1 -1
- data/lib/thread/pool.rb +27 -6
- data/lib/thread/process.rb +2 -3
- data/thread.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2345d704e37f5dd5742e8e8c69f765a70eb061aa
|
4
|
+
data.tar.gz: cccad52eb3d6cb3da85dd087c43c4681ddf7c4d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3007f09957036eb9e3fa180df9beeb69a429fc8d899fe36fb4a5a1d409bf0baaa1a1104b5b59585362b1d8fe95c26ca25f3aec1ee674b2b76205e2353993d30
|
7
|
+
data.tar.gz: 1ca741fd5f9a71f2a3c5a023c6e443b346e89d8186f4f854b03cf350eab352f2ee4bc0b50a341b1665aa3b0619e7b10768a2dffb1f32807da64ac6c7ec2d68a7
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -137,6 +137,13 @@ another thread.
|
|
137
137
|
|
138
138
|
The value returned by the block will be the value of the promise.
|
139
139
|
|
140
|
+
By default, `Thread.future` executes the block in a newly-created thread.
|
141
|
+
|
142
|
+
`Thread.future` accepts an optional argument of type `Thread.pool` if you want
|
143
|
+
the block executed in an existing thread-pool.
|
144
|
+
|
145
|
+
You can also use the `Thread::Pool` helper `#future`
|
146
|
+
|
140
147
|
Example
|
141
148
|
-------
|
142
149
|
|
@@ -152,6 +159,33 @@ f = Thread.future {
|
|
152
159
|
puts ~f # => 42
|
153
160
|
```
|
154
161
|
|
162
|
+
```ruby
|
163
|
+
require 'thread/pool'
|
164
|
+
require 'thread/future'
|
165
|
+
|
166
|
+
pool = Thread.pool 4
|
167
|
+
f = Thread.future pool do
|
168
|
+
sleep 5
|
169
|
+
42
|
170
|
+
end
|
171
|
+
|
172
|
+
puts ~f # => 42
|
173
|
+
```
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
require 'thread/pool'
|
177
|
+
require 'thread/future'
|
178
|
+
|
179
|
+
pool = Thread.pool 4
|
180
|
+
f = pool {
|
181
|
+
sleep 5
|
182
|
+
42
|
183
|
+
}
|
184
|
+
|
185
|
+
puts ~f # => 42
|
186
|
+
```
|
187
|
+
|
188
|
+
|
155
189
|
Delay
|
156
190
|
=====
|
157
191
|
A delay is kind of a promise, except the block is called when the value is
|
data/Rakefile
CHANGED
@@ -1,13 +1,7 @@
|
|
1
1
|
#! /usr/bin/env ruby
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
-
task :default =>
|
5
|
-
|
6
|
-
task :install do
|
7
|
-
sh 'gem install --no-force rspec'
|
8
|
-
sh 'gem build *.gemspec'
|
9
|
-
sh 'gem install *.gem'
|
10
|
-
end
|
4
|
+
task :default => :test
|
11
5
|
|
12
6
|
task :test do
|
13
7
|
FileUtils.cd 'tests' do
|
data/lib/thread/future.rb
CHANGED
data/lib/thread/pipe.rb
CHANGED
@@ -14,7 +14,7 @@ require 'thread'
|
|
14
14
|
# each datum inserted in the pipe is passed along through queues to the various
|
15
15
|
# functions composing the pipe, the final result is inserted in the final queue.
|
16
16
|
class Thread::Pipe
|
17
|
-
# A task
|
17
|
+
# A task encapsulates a part of the pipe.
|
18
18
|
class Task
|
19
19
|
attr_accessor :input, :output
|
20
20
|
|
data/lib/thread/pool.rb
CHANGED
@@ -100,7 +100,7 @@ class Thread::Pool
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
-
attr_reader :min, :max, :spawned
|
103
|
+
attr_reader :min, :max, :spawned, :waiting
|
104
104
|
|
105
105
|
# Create the pool with minimum and maximum threads.
|
106
106
|
#
|
@@ -119,7 +119,7 @@ class Thread::Pool
|
|
119
119
|
|
120
120
|
@done = ConditionVariable.new
|
121
121
|
@done_mutex = Mutex.new
|
122
|
-
|
122
|
+
|
123
123
|
@todo = []
|
124
124
|
@workers = []
|
125
125
|
@timeouts = {}
|
@@ -200,7 +200,29 @@ class Thread::Pool
|
|
200
200
|
@done.wait @done_mutex
|
201
201
|
}
|
202
202
|
end
|
203
|
-
|
203
|
+
|
204
|
+
# Check if there are idle workers.
|
205
|
+
def idle?
|
206
|
+
@todo.length < @waiting
|
207
|
+
end
|
208
|
+
|
209
|
+
# Process Block when there is a idle worker if not block its returns
|
210
|
+
def idle (&block)
|
211
|
+
while !idle?
|
212
|
+
@done_mutex.synchronize {
|
213
|
+
break if idle?
|
214
|
+
@done.wait @done_mutex
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
unless block
|
219
|
+
return
|
220
|
+
end
|
221
|
+
|
222
|
+
process &block
|
223
|
+
|
224
|
+
end
|
225
|
+
|
204
226
|
# Add a task to the pool which will execute the block with the given
|
205
227
|
# argument.
|
206
228
|
#
|
@@ -414,10 +436,9 @@ private
|
|
414
436
|
|
415
437
|
def report_done
|
416
438
|
@done_mutex.synchronize {
|
417
|
-
@done.broadcast if done?
|
439
|
+
@done.broadcast if done? or idle?
|
418
440
|
}
|
419
441
|
end
|
420
|
-
|
421
442
|
end
|
422
443
|
|
423
444
|
class Thread
|
@@ -425,4 +446,4 @@ class Thread
|
|
425
446
|
def self.pool (*args, &block)
|
426
447
|
Thread::Pool.new(*args, &block)
|
427
448
|
end
|
428
|
-
end
|
449
|
+
end
|
data/lib/thread/process.rb
CHANGED
@@ -10,9 +10,8 @@
|
|
10
10
|
|
11
11
|
require 'thread/channel'
|
12
12
|
|
13
|
-
# A process should only interact with the outside
|
14
|
-
#
|
15
|
-
# locks.
|
13
|
+
# A process should only interact with the outside through messages, it still
|
14
|
+
# uses a thread, but it should make it safer to use than sharing and locks.
|
16
15
|
class Thread::Process
|
17
16
|
def self.all
|
18
17
|
@@processes ||= {}
|
data/thread.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- meh.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -84,8 +84,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
version: '0'
|
85
85
|
requirements: []
|
86
86
|
rubyforge_project:
|
87
|
-
rubygems_version: 2.
|
87
|
+
rubygems_version: 2.2.2
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Various extensions to the base thread library.
|
91
91
|
test_files: []
|
92
|
+
has_rdoc:
|