Go.rb 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fdae979af3a12078499ac3e78d0dbde65af3e9df
4
+ data.tar.gz: fd4e5b0672ae33b700720d22af4866186d0e4256
5
+ SHA512:
6
+ metadata.gz: e1c012781c4adbf2e59024b2d1df90af8515edea57c7631e32ab5c7a50e3939ec2de744be0383c2609160f83267fc3d2ad5f0f891594d95b8f811ff097d24c33
7
+ data.tar.gz: 1bc98c6736dbd2201fa0d3698e10c4e9cd3cb7d1afeff35d151c897ed9e38297567dcc6886f916a04afb51d7b62da1f3ff56a25e211d707f822f6c7fcd672cfb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in goruby.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/goruby.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'go/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "Go.rb"
8
+ spec.version = Go::VERSION
9
+ spec.authors = ["JacisNonsense"]
10
+ spec.email = ["jaci.brunning@gmail.com"]
11
+
12
+ spec.summary = %q{The features of the 'Go' language without the weird syntax}
13
+ spec.description = %q{'Go' is a language that specializes in concurrency and control flow, but some people (myself included) don't like the choice of Syntax. Not only that, but these features are something that could be useful in other projects, hence why I created 'GoRuby', bringing the Concurrency and Control Flow features of 'Go' into Ruby}
14
+ spec.homepage = "http://www.github.com/JacisNonsense/go.rb"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.bindir = "bin"
25
+ spec.files = Dir.glob("lib/**/*") + ['Rakefile', 'goruby.gemspec', 'Gemfile', 'Rakefile']
26
+ spec.executables = []
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.9"
30
+ end
data/lib/go.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "go/version"
2
+ require "go/concurrency/pool"
3
+ require "go/goconfig"
4
+ require "go/kernel"
5
+
6
+ module Go
7
+
8
+ end
@@ -0,0 +1,64 @@
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
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
@@ -0,0 +1,83 @@
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
+ @shutting_down = false
15
+
16
+ initStatic() unless @growing
17
+ end
18
+
19
+ def initStatic
20
+ @max.times do
21
+ pushThread
22
+ end
23
+ end
24
+
25
+ def pushThread
26
+ @threads << Thread.new do
27
+ while @running
28
+ future = @workQueue.pop
29
+ begin
30
+ future.put future.work().call()
31
+ rescue Exception => e
32
+ future.ex e
33
+ end
34
+ end
35
+ end
36
+ @threadCount+=1
37
+ end
38
+
39
+ def execute(&block)
40
+ execute_proc(args, block)
41
+ end
42
+
43
+ def execute_proc(proc)
44
+ future = Future.new proc
45
+ @workQueue << future
46
+
47
+ if @threadCount < @max && @growing
48
+ createNew = false
49
+ @threads.each do |t|
50
+ if t.status() != "sleep"
51
+ createNew = true
52
+ end
53
+ end
54
+ if (@threadCount == 0)
55
+ createNew = true
56
+ end
57
+ pushThread() if createNew
58
+ end
59
+
60
+ return future
61
+ end
62
+
63
+ def shutdown
64
+ @running = false
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
@@ -0,0 +1,27 @@
1
+ module Go
2
+ class Config
3
+
4
+ def initialize(max_threads)
5
+ @threads = max_threads
6
+ @pool = Go::CC::ThreadPool.new(max_threads)
7
+ end
8
+
9
+ def thread_count
10
+ @threads
11
+ end
12
+
13
+ def thread_pool
14
+ @pool
15
+ end
16
+
17
+ def self.create(max_threads = 10)
18
+ @@config ||= Config.new(max_threads)
19
+ end
20
+
21
+ def self.get
22
+ create
23
+ @@config
24
+ end
25
+
26
+ end
27
+ end
data/lib/go/kernel.rb ADDED
@@ -0,0 +1,11 @@
1
+ # The Kernel Module for GoRuby that is responsible for 'keywords' that can be
2
+ # used when 'require "go"' is present
3
+ #
4
+ # Author:: Jaci Brunning
5
+ module ::Kernel
6
+
7
+ def go(&block)
8
+ Go::Config.get().thread_pool().execute_proc(block)
9
+ end
10
+
11
+ end
data/lib/go/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Go
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Go.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - JacisNonsense
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ description: "'Go' is a language that specializes in concurrency and control flow,
28
+ but some people (myself included) don't like the choice of Syntax. Not only that,
29
+ but these features are something that could be useful in other projects, hence why
30
+ I created 'GoRuby', bringing the Concurrency and Control Flow features of 'Go' into
31
+ Ruby"
32
+ email:
33
+ - jaci.brunning@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - Gemfile
39
+ - Rakefile
40
+ - goruby.gemspec
41
+ - lib/go.rb
42
+ - lib/go/concurrency/future.rb
43
+ - lib/go/concurrency/pool.rb
44
+ - lib/go/goconfig.rb
45
+ - lib/go/kernel.rb
46
+ - lib/go/version.rb
47
+ homepage: http://www.github.com/JacisNonsense/go.rb
48
+ licenses: []
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.4.5
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: The features of the 'Go' language without the weird syntax
70
+ test_files: []