Go.rb 0.2.0 → 0.3.0

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: e83d89e95b8c04124d749769213f95cde616d2b4
4
- data.tar.gz: d13d4a943eb24c3bba7f0b6e6c32bde158c7c955
3
+ metadata.gz: fe407598b9d20a6bf74c4fd07429f179139f209a
4
+ data.tar.gz: e05d845eb41f6d5fdd2548316a6b9e500abaa02c
5
5
  SHA512:
6
- metadata.gz: f5f2b7ab6fb493a82ad4f2d82fcb47b16f3c7d8adb81506a520246ab46392271c078048008960829b0c3c07642e01017eb3510aafdc7ea5554c98def82b0356e
7
- data.tar.gz: ea7b1925d8f6365b442f79d929b690527c5b2ffd52b748e8eaaface9552ee040b276a67a68401e660c9c2e5488b441228ce9085201ce1b9eea5edef01794da6b
6
+ metadata.gz: 24bb125a575124cb190dd4c68ad10a352176cc50bda80ff42e26c0b69cd38709117b150cfad9e79e2e43816e1d14ca7db31782d6b570ac6a757b1eabdf347e60
7
+ data.tar.gz: f76a12972ce937eaaf27b2c9fe1a915a9e5deef66ca2f50fabe67769571cce6c64830f09558543e28e533e39f717b2794cf4afb8e830566aec35552c29674666
data/Rakefile CHANGED
@@ -1,5 +1,3 @@
1
- require "bundler/gem_tasks"
2
-
3
1
  require 'rake/testtask'
4
2
 
5
3
  task default: %w[test]
data/lib/go.rb CHANGED
@@ -1,7 +1,12 @@
1
1
  require "go/version"
2
- require "go/concurrency/pool"
3
- require "go/goconfig"
4
2
  require "go/kernel"
5
- require "go/engine"
6
- require "go/process/goprocess"
7
- require "go/process/channel"
3
+ require "go/pipe"
4
+ require "go/process"
5
+
6
+ module Go
7
+ class Channel < Queue
8
+ def get nonblock=false
9
+ pop nonblock
10
+ end
11
+ end
12
+ end
@@ -4,12 +4,16 @@
4
4
  # Author:: Jaci Brunning
5
5
  module ::Kernel
6
6
 
7
- def go(&block)
8
- Go::Config.get().thread_pool().execute_proc(block)
7
+ def go(*channels, &block)
8
+ Thread.new *channels, &block
9
9
  end
10
10
 
11
- def gofork(*channels, &block)
12
- Go::Process.new *channels, &block
11
+ def gofork(*pipes, &block)
12
+ Go::Process.new *pipes, &block
13
+ end
14
+
15
+ def gopipe
16
+ Go::Pipe.new
13
17
  end
14
18
 
15
19
  def gochan
@@ -1,12 +1,12 @@
1
1
  module Go
2
- class Channel
2
+ class Pipe
3
3
 
4
4
  def initialize
5
5
  @reader, @writer = IO.pipe
6
6
  end
7
7
 
8
- def puts data
9
- @writer.puts data
8
+ def puts data=""
9
+ writer.puts data
10
10
  end
11
11
 
12
12
  def << data
@@ -14,19 +14,15 @@ module Go
14
14
  end
15
15
 
16
16
  def gets
17
- @reader.gets
17
+ reader.gets
18
18
  end
19
19
 
20
20
  def write data
21
- @writer.write data
21
+ writer.write data
22
22
  end
23
23
 
24
- def read length=-1
25
- if length == -1
26
- @reader.read
27
- else
28
- @reader.read length
29
- end
24
+ def read length
25
+ reader.read length
30
26
  end
31
27
 
32
28
  def reader
@@ -42,8 +38,8 @@ module Go
42
38
  end
43
39
 
44
40
  def destroy
45
- @reader.close
46
- @writer.close
41
+ reader.close
42
+ writer.close
47
43
  @reader = nil
48
44
  @writer = nil
49
45
  end
@@ -0,0 +1,21 @@
1
+ module Go
2
+ class Process
3
+
4
+ def initialize *pipes, &block
5
+ @pid = fork { block.call *pipes }
6
+ end
7
+
8
+ def wait
9
+ ::Process.wait pid
10
+ end
11
+
12
+ def pid
13
+ @pid
14
+ end
15
+
16
+ def kill
17
+ ::Process.kill 9, pid
18
+ end
19
+
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Go
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Go.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JacisNonsense
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2015-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,13 +81,9 @@ files:
81
81
  - Rakefile
82
82
  - goruby.gemspec
83
83
  - lib/go.rb
84
- - lib/go/concurrency/future.rb
85
- - lib/go/concurrency/pool.rb
86
- - lib/go/engine.rb
87
- - lib/go/goconfig.rb
88
84
  - lib/go/kernel.rb
89
- - lib/go/process/channel.rb
90
- - lib/go/process/goprocess.rb
85
+ - lib/go/pipe.rb
86
+ - lib/go/process.rb
91
87
  - lib/go/version.rb
92
88
  homepage: http://www.github.com/JacisNonsense/go.rb
93
89
  licenses: []
@@ -1,64 +0,0 @@
1
- module Go
2
- module CC
3
- class Future
4
-
5
- def initialize proc
6
- @mutex = Mutex.new
7
- @cv = ConditionVariable.new
8
- @complete = false
9
- @work = proc
10
- end
11
-
12
- def work; @work end
13
-
14
- def put val
15
- @val = val
16
- put_complete
17
- end
18
-
19
- def ex e
20
- @ex = e
21
- put_complete
22
- end
23
-
24
- def put_complete
25
- @complete = true
26
- @cv.broadcast
27
- end
28
-
29
- def complete?
30
- @complete
31
- end
32
-
33
- def errored?
34
- !@ex.nil?
35
- end
36
-
37
- def get
38
- unless @complete
39
- @mutex.synchronize do
40
- unless @complete
41
- @cv.wait(@mutex)
42
- end
43
- end
44
- end
45
- if @ex
46
- raise @ex
47
- end
48
-
49
- @val
50
- end
51
-
52
- def wait
53
- unless @complete
54
- @mutex.synchronize do
55
- unless @complete
56
- @cv.wait(@mutex)
57
- end
58
- end
59
- end
60
- end
61
-
62
- end
63
- end
64
- end
@@ -1,83 +0,0 @@
1
- require 'thread'
2
- require 'go/concurrency/future'
3
- module Go
4
- module CC
5
- class ThreadPool
6
-
7
- def initialize(max_size = 10, growing = true)
8
- @max = max_size
9
- @threadCount = 0
10
- @threads = []
11
- @growing = growing
12
- @workQueue = Queue.new
13
- @running = true
14
-
15
- initStatic() unless @growing
16
- end
17
-
18
- def initStatic
19
- @max.times do
20
- pushThread
21
- end
22
- end
23
-
24
- def pushThread
25
- @threads << Thread.new do
26
- while @running
27
- future = @workQueue.pop
28
- begin
29
- future.put future.work().call()
30
- rescue Exception => e
31
- future.ex e
32
- end
33
- end
34
- end
35
- @threadCount+=1
36
- end
37
-
38
- def execute(&block)
39
- return execute_proc(block)
40
- end
41
-
42
- def execute_proc(proc)
43
- future = Future.new proc
44
- @workQueue << future
45
-
46
- if @threadCount < @max && @growing
47
- createNew = false
48
- @threads.each do |t|
49
- if t.status() != "sleep"
50
- createNew = true
51
- end
52
- end
53
- if (@threadCount == 0)
54
- createNew = true
55
- end
56
- pushThread() if createNew
57
- end
58
-
59
- return future
60
- end
61
-
62
- def shutdown
63
- @running = false
64
- @threads.each { |t| t.kill }
65
- end
66
-
67
- def max; @max end
68
- def growing?; @growing end
69
- def running?; @running end
70
- def workers; @threadCount end
71
- def queued; @workQueue.size() end
72
-
73
- def status
74
- status = "Thread Pool: \n"
75
- status << "\tMax Size: #{max}, Worker Count: #{workers}\n"
76
- status << "\tGrowing?: #{growing? ? 'true' : 'false'}, Queued Jobs: #{queued}\n"
77
- status << "\tRunning?: #{running? ? 'true' : 'false'}\n"
78
- return status
79
- end
80
-
81
- end
82
- end
83
- end
@@ -1,17 +0,0 @@
1
- module Go
2
- class Engine
3
-
4
- def initialize(max_size=10, growing=true)
5
- @config = Go::Config.new(max_size, growing)
6
- end
7
-
8
- def go &block
9
- @config.thread_pool.execute_proc block
10
- end
11
-
12
- def config
13
- @config
14
- end
15
-
16
- end
17
- end
@@ -1,31 +0,0 @@
1
- module Go
2
- class Config
3
-
4
- def initialize(max_threads, growing = true)
5
- @threads = max_threads
6
- @pool = Go::CC::ThreadPool.new(max_threads, growing)
7
- end
8
-
9
- def max_thread_count
10
- @threads
11
- end
12
-
13
- def thread_pool
14
- @pool
15
- end
16
-
17
- def self.create(max_threads = 10, growing = true)
18
- @@config = Config.new(max_threads, growing)
19
- end
20
-
21
- def self.maybe_create(max_threads = 10)
22
- @@config ||= Config.new(max_threads)
23
- end
24
-
25
- def self.get
26
- maybe_create
27
- @@config
28
- end
29
-
30
- end
31
- end
@@ -1,28 +0,0 @@
1
- module Go
2
- class Process
3
-
4
- def initialize *channels, &block
5
- @channels = channels
6
- @pid = fork {
7
- block.call *channels
8
- }
9
- end
10
-
11
- def wait
12
- ::Process.wait @pid
13
- end
14
-
15
- def channels
16
- @channels
17
- end
18
-
19
- def pid
20
- @pid
21
- end
22
-
23
- def kill
24
- ::Process.kill 9, @pid
25
- end
26
-
27
- end
28
- end