ractor-local_variable 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: 42c3bad37614b48bf00f4d991d3d286a2ee085bc6dc86ac2b786507a0d4e2a16
4
+ data.tar.gz: e83792f86254b33fc5ced0f9ab7153fe510a6a9145112d1fe992b4701924b0da
5
+ SHA512:
6
+ metadata.gz: 637e8e4b9289e5321b966435c3c15433128d5f9c600015ca9c7765bbb3fc19ffc4fd99e0197c9da67b2a80ff8301736c6dcc926bda1e16c4d9aaf10d73a5a214
7
+ data.tar.gz: 7ea5097e4e71b9bc0c20b78d14eea03854917a00ad0315476062253d0518f96b2950c6452cfa2248042a37e82e737407d92bbe28e1e454cd856172aee41c50a5
@@ -0,0 +1,49 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+
9
+ jobs:
10
+ tests:
11
+ name: >-
12
+ Specs | ${{ matrix.ruby }}
13
+ runs-on: ${{ matrix.os }}-latest
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ os: [ ubuntu ]
18
+ ruby: [ head ]
19
+ steps:
20
+ - name: checkout
21
+ uses: actions/checkout@v2
22
+ - name: set up Ruby
23
+ uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby }}
26
+ - name: install dependencies
27
+ run: bundle install --jobs 3 --retry 3
28
+ - name: spec
29
+ run: bundle exec rake
30
+ internal_investigation:
31
+ name: >-
32
+ Coding Style
33
+ runs-on: ${{ matrix.os }}-latest
34
+ strategy:
35
+ fail-fast: false
36
+ matrix:
37
+ os: [ ubuntu ]
38
+ ruby: [ head ]
39
+ steps:
40
+ - name: checkout
41
+ uses: actions/checkout@v2
42
+ - name: set up Ruby
43
+ uses: ruby/setup-ruby@v1
44
+ with:
45
+ ruby-version: ${{ matrix.ruby }}
46
+ - name: install dependencies
47
+ run: bundle install --jobs 3 --retry 3
48
+ - name: internal investigation
49
+ run: bundle exec rubocop
@@ -0,0 +1,14 @@
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
12
+
13
+ Gemfile.lock
14
+ .rubocop-https*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/ractor-tools/rubocop-ractor-tools/master/.rubocop.yml
3
+
4
+ Style/Alias:
5
+ EnforcedStyle: prefer_alias_method
@@ -0,0 +1 @@
1
+ 3
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at github@marc-andre.ca. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
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 ractor-local_variable.gemspec
6
+ gemspec
7
+
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ gem 'rubocop'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Marc-Andre Lafortune
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,66 @@
1
+ # Ractor::LocalVariable
2
+
3
+ Add Ractor-local storage.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ractor-local_variable'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ STORAGE = Ractor::LocalVariable.new { [] }
17
+
18
+ STORAGE.ractor_local_value << 1
19
+ Thread.new { STORAGE.ractor_local_value << 2 }.join
20
+ Ractor.new do
21
+ STORAGE.ractor_local_value << 3
22
+ Thread.new { STORAGE.ractor_local_value << 4 }.join
23
+ puts "In Ractor: ", STORAGE.ractor_local_value
24
+ end.take
25
+
26
+ puts "In Main: ", STORAGE.ractor_local_value
27
+ # => In Ractor: 3, 4; In Main: 1, 2
28
+ ```
29
+
30
+ You can also use a variant that delegates calls to the value:
31
+
32
+ ```ruby
33
+ STORAGE = Ractor::LocalVariable.new(delegate: true) { [] }
34
+
35
+ STORAGE << 1
36
+ Thread.new { STORAGE << 2 }.join
37
+ Ractor.new do
38
+ STORAGE << 3
39
+ Thread.new { STORAGE << 4 }.join
40
+ puts "In Ractor: ", STORAGE
41
+ end.take
42
+
43
+ puts "In Main: ", STORAGE
44
+ # => Same as above
45
+ ```
46
+
47
+ In this case, be careful to not do `ractor.send(STORAGE)` since no call is made to the object; you'll need to call `ractor_local_value` in a case like that.
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcandre/ractor-local_variable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/marcandre/ractor-local_variable/blob/master/CODE_OF_CONDUCT.md).
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Code of Conduct
65
+
66
+ Everyone interacting in the Ractor::LocalVariable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/marcandre/ractor-local_variable/blob/master/CODE_OF_CONDUCT.md).
@@ -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,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ractor/local_variable'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'delegate'
4
+ require_relative 'local_variable/version'
5
+
6
+ class Ractor
7
+ class LocalVariable
8
+ module Implementation
9
+ MUTEX = Ractor.new do
10
+ loop do
11
+ Ractor.yield :obtained
12
+ Ractor.receive
13
+ end
14
+ end
15
+ def MUTEX.synchronize
16
+ take # => :obtained
17
+ begin
18
+ yield
19
+ ensure
20
+ send :release
21
+ end
22
+ end
23
+ private_constant :MUTEX
24
+
25
+ def initialize(&block)
26
+ raise ArgumentError, 'No block given' unless block
27
+
28
+ @initializer = Ractor.make_shareable(block)
29
+ @hold_me = Object.new.freeze
30
+ @unique_id = :"Ractor::LocalVariable<#{@hold_me.object_id}>"
31
+ freeze
32
+ end
33
+
34
+ def ractor_local_value
35
+ t = Thread.main
36
+ t.thread_variable_get(@unique_id) ||
37
+ MUTEX.synchronize do
38
+ t.thread_variable_get(@unique_id) ||
39
+ t.thread_variable_set(@unique_id, @initializer.call ||
40
+ raise(NotImplementedError,
41
+ 'Initializer blocks returning `nil` of `false` not supported'))
42
+ end
43
+ end
44
+ end
45
+
46
+ include Implementation
47
+
48
+ def self.new(delegate: false, &block)
49
+ return Delegator.new(&block) if delegate
50
+
51
+ super()
52
+ end
53
+
54
+ class Delegator < ::Delegator
55
+ include Implementation
56
+ alias_method :__getobj__, :ractor_local_value
57
+ undef_method :marshal_load, :initialize_clone, :initialize_dup
58
+ define_method :freeze, Kernel.instance_method(:freeze) # do not delegate `freeze`
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Ractor
4
+ class LocalVariable
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ractor/local_variable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ractor-local_variable'
7
+ spec.version = Ractor::LocalVariable::VERSION
8
+ spec.authors = ['Marc-Andre Lafortune']
9
+ spec.email = ['github@marc-andre.ca']
10
+
11
+ spec.summary = 'Ractor-local storage'
12
+ spec.description = 'Ractor-local storage'
13
+ spec.homepage = 'https://github.com/marcandre/ractor-local_variable'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ractor-local_variable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc-Andre Lafortune
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ractor-local storage
14
+ email:
15
+ - github@marc-andre.ca
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/ractor-local_variable.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - ".ruby-version"
25
+ - ".travis.yml"
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/ractor/local_variable.rb
34
+ - lib/ractor/local_variable/version.rb
35
+ - ractor-local_variable.gemspec
36
+ homepage: https://github.com/marcandre/ractor-local_variable
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/marcandre/ractor-local_variable
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.0.rc.2
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Ractor-local storage
60
+ test_files: []