simple-pool 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/simple-pool.rb +59 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2cd8eee4c0260f6edaa113f29e59c9103b95ef49
4
+ data.tar.gz: e3d0e51c9d2ac8ece48b60f1e80dd0515f74304d
5
+ SHA512:
6
+ metadata.gz: edbb49f8f4c29efb40416f194165f2793c9656d3f8136b763c94d432cfd7af9de98062728552b0d1f4dbc4281c2031cb7dd610183e099a25a2d6a698d8966f71
7
+ data.tar.gz: 512fdd0c8941ce7df066f3b4cf996126a4d09b28368b5b89276fa3194b5d242e3ebe6e1717010b36912ef120163c06a889c7858e7691eeb54d6644f131afb055
@@ -0,0 +1,59 @@
1
+ require 'thread'
2
+
3
+ class SimplePool
4
+ def initialize(thread_num)
5
+ @kill = false
6
+ @stop = false
7
+ @q = Queue.new
8
+ @m = Mutex.new
9
+ @cv = ConditionVariable.new
10
+ @threads = []
11
+ thread_num.times do |i|
12
+ @threads << Thread.new{round_n_round}
13
+ end
14
+ end
15
+
16
+ def invoke(&callback)
17
+ @q << callback
18
+ @m.synchronize{ @cv.broadcast }
19
+ end
20
+
21
+ def kill_right_now
22
+ @kill = true
23
+ @m.synchronize{ @cv.broadcast }
24
+ @threads.each{|t| t.join}
25
+ end
26
+
27
+ def wait_until_finish
28
+ @stop = true
29
+ @m.synchronize{ @cv.broadcast }
30
+ @threads.each{|t| t.join}
31
+ end
32
+
33
+ private
34
+ def do_it
35
+ begin
36
+ callback = @q.pop
37
+ callback.call
38
+ rescue
39
+
40
+ end
41
+ end
42
+
43
+ def round_n_round
44
+ until @kill do
45
+ unless @q.empty?
46
+ do_it
47
+ next
48
+ end
49
+
50
+ if @stop
51
+ @kill = true
52
+ next
53
+ end
54
+
55
+ @m.synchronize{ @cv.wait(@m, 1) }
56
+ end
57
+ end
58
+ end
59
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-pool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - ireullin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple thread pool.
14
+ email:
15
+ - ireullin@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/simple-pool.rb
21
+ homepage: https://github.com/ireullin/simple-pool
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A simple thread pool.
45
+ test_files: []