backtor 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 36d7183f82af76c0f66866c460ba199ada9a2aa6f1e263b505bd7f31367a8980
4
+ data.tar.gz: 205efd80e76cd8d8128eb24fe29a72412a5850e70c0f36de92d71d92895bed9f
5
+ SHA512:
6
+ metadata.gz: 1a4ce39c94e9e640d5069e301288f138e35d2e124596ffa5a42b3eb36433d7a8f565fdb2ebafd51236299a12c60da32e96f507fd30a09fbb2485c78f14c5d851
7
+ data.tar.gz: fbbe8a9548a078db62b90abec42555fb10ddfb3c0f527ced5292fd150e38ed0748cac74f8ba555cac1b4356cadbd044d8115bc50d5ab33c1118281d034c003c6
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.0.dev
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in backtor.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ backtor (1.0.0)
5
+ concurrent-ruby
6
+ concurrent-ruby-edge
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ concurrent-ruby (1.1.7)
12
+ concurrent-ruby-edge (0.6.0)
13
+ concurrent-ruby (~> 1.1.6)
14
+ diff-lcs (1.4.4)
15
+ rake (13.0.1)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.2)
21
+ rspec-support (~> 3.9.3)
22
+ rspec-expectations (3.9.2)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ backtor!
35
+ rake (~> 13.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Aaron Christiansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,20 @@
1
+ # Backtor
2
+
3
+ Backtor is an experimental, thread-based backport of Ractor to earlier Ruby
4
+ versions.
5
+
6
+ Since it's based on Ruby's `Thread` mechanism, you don't get the true
7
+ concurrency of Ractors, but it does mean that you can write applications using
8
+ Ractor and still have them work at a reduced speed in Ruby 2.
9
+
10
+ This is still a work in progress! The basic `yield`/`take` and `send`/`recv`
11
+ mechanisms are implemented, but very little else.
12
+
13
+ ## Contributing
14
+
15
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AaronC81/backtor.
16
+
17
+
18
+ ## License
19
+
20
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "backtor"
5
+ spec.version = '0.1.0'
6
+ spec.authors = ["Aaron Christiansen"]
7
+ spec.email = ["aaronc20000@gmail.com"]
8
+
9
+ spec.summary = "Backport of Ractor using threads"
10
+ spec.homepage = "https://github.com/AaronC81/backtor"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'concurrent-ruby'
26
+ spec.add_dependency 'concurrent-ruby-edge'
27
+ end
@@ -0,0 +1,96 @@
1
+ require 'concurrent-ruby'
2
+ require 'concurrent-edge'
3
+
4
+ class Backtor
5
+ def self.new_id
6
+ @last_id ||= 0
7
+ @last_id += 1
8
+ @last_id
9
+ end
10
+
11
+ def initialize(*args, name: nil, __backtor_special_main: false, &block)
12
+ raise ArgumentError, 'must be called with a block' if block.nil?
13
+ raise TypeError, "no implicit conversion of #{name.class} into String" \
14
+ unless name.nil? || name.is_a?(String)
15
+
16
+ @name = name
17
+ @backtor_id = Backtor.new_id
18
+ @__backtor_special_main = __backtor_special_main
19
+
20
+ @thread = Thread.new do
21
+ unless __backtor_special_main
22
+ # TODO: send parameters the proper way, see spec
23
+ # TODO: exceptions
24
+ Backtor.yield(self.instance_exec(*args, &block))
25
+ end
26
+ end
27
+ @thread.abort_on_exception = true
28
+ @thread.instance_variable_set(:@backtor, self)
29
+
30
+ @incoming_queue = []
31
+ @incoming_queue_mutex = Mutex.new
32
+ @incoming_queue_cv = ConditionVariable.new
33
+ @outgoing_port = Concurrent::Channel.new
34
+ end
35
+
36
+ MAIN = Backtor.new(__backtor_special_main: true) {}
37
+
38
+ # TODO: proper clone behaviour like real ractors
39
+
40
+ def send(obj)
41
+ @incoming_queue_mutex.synchronize do
42
+ @incoming_queue << obj
43
+ @incoming_queue_cv.signal
44
+ end
45
+ end
46
+ alias << send
47
+
48
+ def self.recv
49
+ mutex = Thread.current.instance_variable_get(:@backtor).instance_variable_get(:@incoming_queue_mutex)
50
+
51
+ mutex.synchronize do
52
+ queue = Thread.current.instance_variable_get(:@backtor).instance_variable_get(:@incoming_queue)
53
+ cv = Thread.current.instance_variable_get(:@backtor).instance_variable_get(:@incoming_queue_cv)
54
+
55
+ cv.wait(mutex) if queue.empty?
56
+ queue.shift
57
+ end
58
+ end
59
+
60
+ def take
61
+ @outgoing_port.take
62
+ end
63
+
64
+ def self.yield(obj)
65
+ Thread.current.instance_variable_get(:@backtor).instance_variable_get(:@outgoing_port).put(obj)
66
+ end
67
+
68
+ def inspect
69
+ "#<Backtor:##{@backtor_id} #{status}>"
70
+ end
71
+
72
+ def self.current
73
+ if Thread.current == Thread.main
74
+ Backtor::MAIN
75
+ elsif !Thread.current.instance_variable_get(:@backtor).nil?
76
+ Thread.current.instance_variable_get(:@backtor)
77
+ else
78
+ raise 'current thread is not main nor a backtor'
79
+ end
80
+ end
81
+
82
+ def status
83
+ return "running" if @__backtor_special_main
84
+
85
+ case @thread.status
86
+ when "run"
87
+ "running"
88
+ when false
89
+ "terminated"
90
+ else
91
+ "unimplemented"
92
+ end
93
+ end
94
+
95
+ attr_reader :name
96
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backtor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Christiansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: concurrent-ruby-edge
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - aaronc20000@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/main.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - backtor.gemspec
57
+ - lib/backtor.rb
58
+ homepage: https://github.com/AaronC81/backtor
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/AaronC81/backtor
63
+ source_code_uri: https://github.com/AaronC81/backtor
64
+ changelog_uri: https://github.com/AaronC81/backtor/CHANGELOG.md
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.3.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Backport of Ractor using threads
84
+ test_files: []