particle_pool 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 36506753891f74213850afdb986921011c69aa5c0b860eae87c95cf4f50673e3
4
- data.tar.gz: ee10f2b65bc8419085d329ebdefeef99cb5e7a2e9e09ffe3c1174e0456d59358
3
+ metadata.gz: 5d7d654c37d60427a15c1124ea54a033238802156368e9c663d37ef43eff26cf
4
+ data.tar.gz: a61d02e0285ddd57c913a84adbb06bdcee3ea5921b8bea434ebb0380421f654e
5
5
  SHA512:
6
- metadata.gz: 36cddff2a62c774a5b0e844cdb582e294bbecdda79ef9d9154882c8d82bcb717ae7235d7f38e4cff2b80e1373c69b135977106439b7317eca9a4727f62124318
7
- data.tar.gz: ed5e0a5d08c9b35641460dbe6b846c8179fc9bb1be400e3e94a8ef0f06ae5d14ca4e93669378f4e529544238870ceeba8726921a0f172eec77ca487f7c6f6c02
6
+ metadata.gz: 8ccc3eec762cad7a76e05bd3f3836210d714f0bcaf0559298d6a3aabff2384d1f56f2821b507208b004c376263351ac1b101b155acc0064ed4b3cee477539808
7
+ data.tar.gz: add0d9f2773a637132d2f64560021b50fb534cc1e67ed7ffa0be444eb0dfc85ceba1434df28b643d0d934955304ed790566fbd0cdd43bbe62777384c5b685fe9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- particle_pool (0.1.1)
4
+ particle_pool (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -18,51 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- ```ruby
22
- # Require the library
23
- require 'particle_pool'
24
-
25
- # Create a new pool
26
- p = ParticlePool::Pool.new
27
-
28
- # Start it
29
- p.start
30
-
31
- (1024 * Etc.nprocessors).times do |i|
32
- # Create some tasks
33
- t = ParticlePool::Task.new do |n|
34
- raise 'fib not defined for negative numbers' if n < 0
35
- nv, ov = 1, 0
36
- n.times do
37
- nv, ov = nv + ov, nv
38
- Fiber.yield
39
- end
40
- $arr << ov
41
- $sz += 1
42
- ov
43
- end
44
-
45
- # And push them into the pool.
46
- p.push(t, i)
47
- end
48
-
49
- # You can wait for tasks to finish!
50
- t = ParticlePool::Task.new do
51
- sleep 16
52
- 'Hello, World!'
53
- end
54
- p.push(t)
55
-
56
- puts t.await_sync
57
-
58
- # Wait for all results to appear
59
- while $sz != (1024 * Etc.nprocessors) do
60
- sleep 1
61
- end
62
-
63
- # Sum results and print the final result
64
- puts $arr.reduce(:+)
65
- ```
21
+ You can find usage examples in the bin/ directory.
66
22
 
67
23
  ## Development
68
24
 
@@ -6,12 +6,18 @@ require 'particle_pool'
6
6
  load File.dirname(__FILE__) + '/task.rb'
7
7
 
8
8
  p = ParticlePool::Pool.new
9
- p.start
9
+ p.start 16
10
10
 
11
- $sz = 0
12
- $arr = []
11
+ Result = Struct.new(:sz, :arr)
12
+ Sum = Struct.new(:sz, :sum)
13
13
 
14
- (1024 * Etc.nprocessors).times do |i|
14
+ res = Result.new
15
+ res.sz = 0
16
+ res.arr = []
17
+
18
+ resbox = ParticlePool::Box.new res
19
+
20
+ 2048.times do |i|
15
21
  t = ParticlePool::Task.new do |n|
16
22
  raise 'fib not defined for negative numbers' if n < 0
17
23
  nv, ov = 1, 0
@@ -19,8 +25,8 @@ $arr = []
19
25
  nv, ov = nv + ov, nv
20
26
  Fiber.yield
21
27
  end
22
- $arr << ov
23
- $sz += 1
28
+ res.arr << ov
29
+ res.sz += 1
24
30
  ov
25
31
  end
26
32
 
@@ -33,10 +39,50 @@ t = ParticlePool::Task.new do
33
39
  end
34
40
  p.push(t)
35
41
 
36
- puts t.await_sync
42
+ t2 = ParticlePool::Task.new do
43
+ puts t.await
44
+ end
45
+ p.push(t2)
37
46
 
38
- while $sz != (1024 * Etc.nprocessors) do
39
- sleep 1
47
+ abox = ParticlePool::Box.new
48
+ t3 = ParticlePool::Task.new do
49
+ sleep 20
50
+ abox.value = 'asdf'
51
+ end
52
+ p.push(t3)
53
+
54
+ t4 = ParticlePool::Task.new do
55
+ puts abox.await
56
+ end
57
+ p.push(t4)
58
+
59
+ sum = Sum.new
60
+ sum.sz = 0
61
+ sum.sum = 0
62
+ sumbox = ParticlePool::Box.new sum
63
+
64
+ tres = ParticlePool::Task.new do
65
+ resbox.await_block do
66
+ resbox.value.sz == 2048
67
+ end
68
+
69
+ resbox.value.arr.each do |i|
70
+ t = ParticlePool::Task.new do
71
+ sumbox.value.sz += 1
72
+ sumbox.value.sum += i
73
+ end
74
+ p.push(t)
75
+ end
76
+
77
+ sumbox.await_block do
78
+ sumbox.value.sz == 2048
79
+ end
80
+
81
+ puts sumbox.value.sum
40
82
  end
83
+ p.push(tres)
41
84
 
42
- puts $arr.reduce(:+)
85
+ while sumbox.value.sz != 2048 do
86
+ sleep 1
87
+ end
88
+ sleep 2
@@ -8,4 +8,5 @@ require 'thread'
8
8
  require 'particle_pool/version'
9
9
  require 'particle_pool/task'
10
10
  require 'particle_pool/pool_thread'
11
- require 'particle_pool/pool'
11
+ require 'particle_pool/pool'
12
+ require 'particle_pool/box'
@@ -0,0 +1,47 @@
1
+ class ParticlePool::Box
2
+ attr_accessor :value, :extra
3
+
4
+ def initialize(value = nil)
5
+ @value = value
6
+ @extra = nil
7
+ end
8
+
9
+ def await(s=0.01)
10
+ until @value
11
+ Fiber.yield
12
+ sleep(s)
13
+ end
14
+ @value
15
+ end
16
+
17
+ def await_sync(s=0.01)
18
+ sleep(s) until @value
19
+ @value
20
+ end
21
+
22
+ def await_for(v, s=0.01)
23
+ until @value == v
24
+ Fiber.yield
25
+ sleep(s)
26
+ end
27
+ @value
28
+ end
29
+
30
+ def await_for_sync(v, s=0.01)
31
+ sleep(s) until @value == v
32
+ @value
33
+ end
34
+
35
+ def await_block(s=0.01, &b)
36
+ until b.()
37
+ Fiber.yield
38
+ sleep(s)
39
+ end
40
+ @value
41
+ end
42
+
43
+ def await_block_sync(s=0.01, &b)
44
+ sleep(s) until b.()
45
+ @value
46
+ end
47
+ end
@@ -1,6 +1,9 @@
1
1
  class ParticlePool::Pool
2
2
  def initialize
3
- @threads = []
3
+ @mtx = Mutex.new
4
+ @threads = []
5
+ @round_robin = false
6
+ @iter = @threads.each
4
7
  end
5
8
 
6
9
  def <<(t, *args, **kwargs)
@@ -8,9 +11,11 @@ class ParticlePool::Pool
8
11
  end
9
12
 
10
13
  def push(t, *args, **kwargs)
11
- thr = @threads.min
12
- raise 'no threads available' unless t
13
- thr.push(t, *args, **kwargs)
14
+ @mtx.synchronize do
15
+ thr = @threads.min
16
+ raise 'no threads available' unless t
17
+ thr.push(t, *args, **kwargs)
18
+ end
14
19
  end
15
20
 
16
21
  def start(size = Etc.nprocessors)
@@ -5,6 +5,7 @@ class ParticlePool::PoolThread
5
5
 
6
6
  def initialize
7
7
  @queue = Queue.new
8
+ @mtx = Mutex.new
8
9
  @tasks = 0
9
10
  end
10
11
 
@@ -13,12 +14,14 @@ class ParticlePool::PoolThread
13
14
  end
14
15
 
15
16
  def push(t, *args, **kwargs)
16
- @tasks += 1
17
- @queue << {
18
- task: t,
19
- args: args,
20
- kwargs: kwargs
21
- }
17
+ @mtx.synchronize do
18
+ @tasks += 1
19
+ @queue << {
20
+ task: t,
21
+ args: args,
22
+ kwargs: kwargs
23
+ }
24
+ end
22
25
  end
23
26
 
24
27
  def <=>(other)
@@ -1,3 +1,3 @@
1
1
  module ParticlePool
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: particle_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nickolay Ilyushin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-26 00:00:00.000000000 Z
11
+ date: 2018-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,6 +58,7 @@ files:
58
58
  - bin/test-task.rb
59
59
  - bin/test-thread.rb
60
60
  - lib/particle_pool.rb
61
+ - lib/particle_pool/box.rb
61
62
  - lib/particle_pool/pool.rb
62
63
  - lib/particle_pool/pool_thread.rb
63
64
  - lib/particle_pool/task.rb