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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 915dd79486e7e628d223363cc58d22072872ba2e
4
- data.tar.gz: 2a4c8d5cf1c04be29d42a29378eac723aed48907
3
+ metadata.gz: 2345d704e37f5dd5742e8e8c69f765a70eb061aa
4
+ data.tar.gz: cccad52eb3d6cb3da85dd087c43c4681ddf7c4d2
5
5
  SHA512:
6
- metadata.gz: 4b4049d70035accffa58a8640564716f169f48f65a18e5eb7e4eb7b3c353f71be3610339e6179edaa56ac76abae3e6d94ac214df59fbcbaadb22cf2d8152c215
7
- data.tar.gz: 1fe65f80b58a88d04c3b9de5cecd1f2d5bcf0a569b78f3dc5d6c8f5ee2c533b4ab70a8d367c089029ec8fb68d8a6a337fb953f36d1cc1f4c44b772dad7c1f6e6
6
+ metadata.gz: e3007f09957036eb9e3fa180df9beeb69a429fc8d899fe36fb4a5a1d409bf0baaa1a1104b5b59585362b1d8fe95c26ca25f3aec1ee674b2b76205e2353993d30
7
+ data.tar.gz: 1ca741fd5f9a71f2a3c5a023c6e443b346e89d8186f4f854b03cf350eab352f2ee4bc0b50a341b1665aa3b0619e7b10768a2dffb1f32807da64ac6c7ec2d68a7
@@ -1,7 +1,13 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
4
  - "1.9.2"
4
5
  - "1.9.3"
5
6
  - "2.0.0"
6
- - jruby-19mode # JRuby in 1.9 mode
7
- - rbx-19mode
7
+ - jruby
8
+ - rbx
9
+
10
+ before_script:
11
+ - gem install rspec
12
+ - gem build *.gemspec
13
+ - gem install *.gem
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 => [:install, :test]
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
@@ -156,3 +156,9 @@ module Kernel
156
156
  Thread::Future.new(pool, &block)
157
157
  end
158
158
  end
159
+
160
+ class Thread::Pool
161
+ def future (&block)
162
+ Thread.future self, &block
163
+ end
164
+ end
@@ -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 incapsulates a part of the pipe.
17
+ # A task encapsulates a part of the pipe.
18
18
  class Task
19
19
  attr_accessor :input, :output
20
20
 
@@ -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
@@ -10,9 +10,8 @@
10
10
 
11
11
  require 'thread/channel'
12
12
 
13
- # A process should only interact with the outside the outside through messages,
14
- # it still uses a thread, but it should make it safer to use than sharing and
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 ||= {}
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new {|s|
2
2
  s.name = 'thread'
3
- s.version = '0.1.3'
3
+ s.version = '0.1.4'
4
4
  s.author = 'meh.'
5
5
  s.email = 'meh@schizofreni.co'
6
6
  s.homepage = 'http://github.com/meh/ruby-thread'
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.3
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: 2013-11-19 00:00:00.000000000 Z
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.1.5
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: